[GTK] Use GDK_EVENT_PROPAGATE and GDK_EVENT_STOP in WebKitWebViewBase.cpp
[WebKit-https.git] / Source / WebKit / UIProcess / API / gtk / WebKitWebViewBase.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  * Portions Copyright (c) 2010 Motorola Mobility, Inc.  All rights reserved.
4  * Copyright (C) 2011 Igalia S.L.
5  * Copyright (C) 2013 Gustavo Noronha Silva <gns@gnome.org>.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "WebKitWebViewBase.h"
31
32 #include "APIPageConfiguration.h"
33 #include "AcceleratedBackingStore.h"
34 #include "ActivityState.h"
35 #include "DrawingAreaProxyImpl.h"
36 #include "InputMethodFilter.h"
37 #include "KeyBindingTranslator.h"
38 #include "NativeWebKeyboardEvent.h"
39 #include "NativeWebMouseEvent.h"
40 #include "NativeWebWheelEvent.h"
41 #include "PageClientImpl.h"
42 #include "WebEventFactory.h"
43 #include "WebInspectorProxy.h"
44 #include "WebKit2Initialize.h"
45 #include "WebKitAuthenticationDialog.h"
46 #include "WebKitWebViewBaseAccessible.h"
47 #include "WebKitWebViewBasePrivate.h"
48 #include "WebPageGroup.h"
49 #include "WebPageProxy.h"
50 #include "WebPreferences.h"
51 #include "WebProcessPool.h"
52 #include "WebUserContentControllerProxy.h"
53 #include <WebCore/CairoUtilities.h>
54 #include <WebCore/GUniquePtrGtk.h>
55 #include <WebCore/GtkUtilities.h>
56 #include <WebCore/GtkVersioning.h>
57 #include <WebCore/NotImplemented.h>
58 #include <WebCore/PasteboardHelper.h>
59 #include <WebCore/PlatformDisplay.h>
60 #include <WebCore/RefPtrCairo.h>
61 #include <WebCore/Region.h>
62 #include <gdk/gdk.h>
63 #include <gdk/gdkkeysyms.h>
64 #include <glib/gi18n-lib.h>
65 #include <memory>
66 #include <pal/system/SleepDisabler.h>
67 #include <wtf/HashMap.h>
68 #include <wtf/glib/GRefPtr.h>
69 #include <wtf/glib/WTFGType.h>
70 #include <wtf/text/CString.h>
71
72 #if ENABLE(FULLSCREEN_API)
73 #include "WebFullScreenManagerProxy.h"
74 #endif
75
76 #if PLATFORM(X11)
77 #include <gdk/gdkx.h>
78 #endif
79
80 // gtk_widget_get_scale_factor() appeared in GTK 3.10, but we also need
81 // to make sure we have cairo new enough to support cairo_surface_set_device_scale
82 #define HAVE_GTK_SCALE_FACTOR HAVE_CAIRO_SURFACE_SET_DEVICE_SCALE && GTK_CHECK_VERSION(3, 10, 0)
83
84 using namespace WebKit;
85 using namespace WebCore;
86
87 struct ClickCounter {
88 public:
89     void reset()
90     {
91         currentClickCount = 0;
92         previousClickPoint = IntPoint();
93         previousClickTime = 0;
94         previousClickButton = 0;
95     }
96
97     int currentClickCountForGdkButtonEvent(GdkEventButton* buttonEvent)
98     {
99         GdkEvent* event = reinterpret_cast<GdkEvent*>(buttonEvent);
100         int doubleClickDistance = 250;
101         int doubleClickTime = 5;
102         g_object_get(gtk_settings_get_for_screen(gdk_event_get_screen(event)),
103             "gtk-double-click-distance", &doubleClickDistance, "gtk-double-click-time", &doubleClickTime, nullptr);
104
105         // GTK+ only counts up to triple clicks, but WebCore wants to know about
106         // quadruple clicks, quintuple clicks, ad infinitum. Here, we replicate the
107         // GDK logic for counting clicks.
108         guint32 eventTime = gdk_event_get_time(event);
109         if (!eventTime) {
110             // Real events always have a non-zero time, but events synthesized
111             // by the WTR do not and we must calculate a time manually. This time
112             // is not calculated in the WTR, because GTK+ does not work well with
113             // anything other than GDK_CURRENT_TIME on synthesized events.
114             GTimeVal timeValue;
115             g_get_current_time(&timeValue);
116             eventTime = (timeValue.tv_sec * 1000) + (timeValue.tv_usec / 1000);
117         }
118
119         if ((event->type == GDK_2BUTTON_PRESS || event->type == GDK_3BUTTON_PRESS)
120             || ((std::abs(buttonEvent->x - previousClickPoint.x()) < doubleClickDistance)
121                 && (std::abs(buttonEvent->y - previousClickPoint.y()) < doubleClickDistance)
122                 && (eventTime - previousClickTime < static_cast<unsigned>(doubleClickTime))
123                 && (buttonEvent->button == previousClickButton)))
124             currentClickCount++;
125         else
126             currentClickCount = 1;
127
128         double x, y;
129         gdk_event_get_coords(event, &x, &y);
130         previousClickPoint = IntPoint(x, y);
131         previousClickButton = buttonEvent->button;
132         previousClickTime = eventTime;
133
134         return currentClickCount;
135     }
136
137 private:
138     int currentClickCount;
139     IntPoint previousClickPoint;
140     unsigned previousClickButton;
141     int previousClickTime;
142 };
143
144 typedef HashMap<GtkWidget*, IntRect> WebKitWebViewChildrenMap;
145 typedef HashMap<uint32_t, GUniquePtr<GdkEvent>> TouchEventsMap;
146
147 struct _WebKitWebViewBasePrivate {
148     _WebKitWebViewBasePrivate()
149         : updateActivityStateTimer(RunLoop::main(), this, &_WebKitWebViewBasePrivate::updateActivityStateTimerFired)
150     {
151     }
152
153     void updateActivityStateTimerFired()
154     {
155         if (!pageProxy)
156             return;
157         pageProxy->activityStateDidChange(activityStateFlagsToUpdate);
158         activityStateFlagsToUpdate = ActivityState::NoFlags;
159     }
160
161     WebKitWebViewChildrenMap children;
162     std::unique_ptr<PageClientImpl> pageClient;
163     RefPtr<WebPageProxy> pageProxy;
164     bool shouldForwardNextKeyEvent { false };
165     bool shouldForwardNextWheelEvent { false };
166     ClickCounter clickCounter;
167     CString tooltipText;
168     IntRect tooltipArea;
169     GRefPtr<AtkObject> accessible;
170     GtkWidget* authenticationDialog { nullptr };
171     GtkWidget* inspectorView { nullptr };
172     AttachmentSide inspectorAttachmentSide { AttachmentSide::Bottom };
173     unsigned inspectorViewSize { 0 };
174     GUniquePtr<GdkEvent> contextMenuEvent;
175     WebContextMenuProxyGtk* activeContextMenuProxy { nullptr };
176     InputMethodFilter inputMethodFilter;
177     KeyBindingTranslator keyBindingTranslator;
178     TouchEventsMap touchEvents;
179     IntSize contentsSize;
180
181     GtkWindow* toplevelOnScreenWindow { nullptr };
182     unsigned long toplevelFocusInEventID { 0 };
183     unsigned long toplevelFocusOutEventID { 0 };
184     unsigned long toplevelWindowStateEventID { 0 };
185     unsigned long toplevelWindowRealizedID { 0 };
186
187     // View State.
188     ActivityState::Flags activityState { 0 };
189     ActivityState::Flags activityStateFlagsToUpdate { 0 };
190     RunLoop::Timer<WebKitWebViewBasePrivate> updateActivityStateTimer;
191
192 #if ENABLE(FULLSCREEN_API)
193     bool fullScreenModeActive { false };
194     std::unique_ptr<PAL::SleepDisabler> sleepDisabler;
195 #endif
196
197     std::unique_ptr<AcceleratedBackingStore> acceleratedBackingStore;
198
199 #if ENABLE(DRAG_SUPPORT)
200     std::unique_ptr<DragAndDropHandler> dragAndDropHandler;
201 #endif
202
203 #if HAVE(GTK_GESTURES)
204     std::unique_ptr<GestureController> gestureController;
205 #endif
206 };
207
208 WEBKIT_DEFINE_TYPE(WebKitWebViewBase, webkit_web_view_base, GTK_TYPE_CONTAINER)
209
210 static void webkitWebViewBaseScheduleUpdateActivityState(WebKitWebViewBase* webViewBase, ActivityState::Flags flagsToUpdate)
211 {
212     WebKitWebViewBasePrivate* priv = webViewBase->priv;
213     priv->activityStateFlagsToUpdate |= flagsToUpdate;
214     if (priv->updateActivityStateTimer.isActive())
215         return;
216
217     priv->updateActivityStateTimer.startOneShot(0_s);
218 }
219
220 static gboolean toplevelWindowFocusInEvent(GtkWidget* widget, GdkEventFocus*, WebKitWebViewBase* webViewBase)
221 {
222     // Spurious focus in events can occur when the window is hidden.
223     if (!gtk_widget_get_visible(widget))
224         return FALSE;
225
226     WebKitWebViewBasePrivate* priv = webViewBase->priv;
227     if (priv->activityState & ActivityState::WindowIsActive)
228         return FALSE;
229
230     priv->activityState |= ActivityState::WindowIsActive;
231     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, ActivityState::WindowIsActive);
232
233     return FALSE;
234 }
235
236 static gboolean toplevelWindowFocusOutEvent(GtkWidget*, GdkEventFocus*, WebKitWebViewBase* webViewBase)
237 {
238     WebKitWebViewBasePrivate* priv = webViewBase->priv;
239     if (!(priv->activityState & ActivityState::WindowIsActive))
240         return FALSE;
241
242     priv->activityState &= ~ActivityState::WindowIsActive;
243     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, ActivityState::WindowIsActive);
244
245     return FALSE;
246 }
247
248 static gboolean toplevelWindowStateEvent(GtkWidget*, GdkEventWindowState* event, WebKitWebViewBase* webViewBase)
249 {
250     WebKitWebViewBasePrivate* priv = webViewBase->priv;
251     if (!(event->changed_mask & GDK_WINDOW_STATE_ICONIFIED))
252         return FALSE;
253
254     bool visible = !(event->new_window_state & GDK_WINDOW_STATE_ICONIFIED);
255     if ((visible && priv->activityState & ActivityState::IsVisible) || (!visible && !(priv->activityState & ActivityState::IsVisible)))
256         return FALSE;
257
258     if (visible)
259         priv->activityState |= ActivityState::IsVisible;
260     else
261         priv->activityState &= ~ActivityState::IsVisible;
262     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, ActivityState::IsVisible);
263
264     return FALSE;
265 }
266
267 static void toplevelWindowRealized(WebKitWebViewBase* webViewBase)
268 {
269     gtk_widget_realize(GTK_WIDGET(webViewBase));
270
271     WebKitWebViewBasePrivate* priv = webViewBase->priv;
272     if (priv->toplevelWindowRealizedID) {
273         g_signal_handler_disconnect(priv->toplevelOnScreenWindow, priv->toplevelWindowRealizedID);
274         priv->toplevelWindowRealizedID = 0;
275     }
276 }
277
278 static void webkitWebViewBaseSetToplevelOnScreenWindow(WebKitWebViewBase* webViewBase, GtkWindow* window)
279 {
280     WebKitWebViewBasePrivate* priv = webViewBase->priv;
281     if (priv->toplevelOnScreenWindow == window)
282         return;
283
284     if (priv->toplevelFocusInEventID) {
285         g_signal_handler_disconnect(priv->toplevelOnScreenWindow, priv->toplevelFocusInEventID);
286         priv->toplevelFocusInEventID = 0;
287     }
288     if (priv->toplevelFocusOutEventID) {
289         g_signal_handler_disconnect(priv->toplevelOnScreenWindow, priv->toplevelFocusOutEventID);
290         priv->toplevelFocusOutEventID = 0;
291     }
292     if (priv->toplevelWindowStateEventID) {
293         g_signal_handler_disconnect(priv->toplevelOnScreenWindow, priv->toplevelWindowStateEventID);
294         priv->toplevelWindowStateEventID = 0;
295     }
296     if (priv->toplevelWindowRealizedID) {
297         g_signal_handler_disconnect(priv->toplevelOnScreenWindow, priv->toplevelWindowRealizedID);
298         priv->toplevelWindowRealizedID = 0;
299     }
300
301     priv->toplevelOnScreenWindow = window;
302
303     if (!priv->toplevelOnScreenWindow) {
304         ActivityState::Flags flagsToUpdate = 0;
305         if (priv->activityState & ActivityState::IsInWindow) {
306             priv->activityState &= ~ActivityState::IsInWindow;
307             flagsToUpdate |= ActivityState::IsInWindow;
308         }
309         if (priv->activityState & ActivityState::WindowIsActive) {
310             priv->activityState &= ~ActivityState::WindowIsActive;
311             flagsToUpdate |= ActivityState::IsInWindow;
312         }
313         if (flagsToUpdate)
314             webkitWebViewBaseScheduleUpdateActivityState(webViewBase, flagsToUpdate);
315
316         return;
317     }
318
319     priv->toplevelFocusInEventID =
320         g_signal_connect(priv->toplevelOnScreenWindow, "focus-in-event",
321                          G_CALLBACK(toplevelWindowFocusInEvent), webViewBase);
322     priv->toplevelFocusOutEventID =
323         g_signal_connect(priv->toplevelOnScreenWindow, "focus-out-event",
324                          G_CALLBACK(toplevelWindowFocusOutEvent), webViewBase);
325     priv->toplevelWindowStateEventID =
326         g_signal_connect(priv->toplevelOnScreenWindow, "window-state-event", G_CALLBACK(toplevelWindowStateEvent), webViewBase);
327
328     if (gtk_widget_get_realized(GTK_WIDGET(window)))
329         gtk_widget_realize(GTK_WIDGET(webViewBase));
330     else
331         priv->toplevelWindowRealizedID = g_signal_connect_swapped(window, "realize", G_CALLBACK(toplevelWindowRealized), webViewBase);
332 }
333
334 static void webkitWebViewBaseRealize(GtkWidget* widget)
335 {
336     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(widget);
337     WebKitWebViewBasePrivate* priv = webView->priv;
338
339     gtk_widget_set_realized(widget, TRUE);
340
341     GtkAllocation allocation;
342     gtk_widget_get_allocation(widget, &allocation);
343
344     GdkWindowAttr attributes;
345     attributes.window_type = GDK_WINDOW_CHILD;
346     attributes.x = allocation.x;
347     attributes.y = allocation.y;
348     attributes.width = allocation.width;
349     attributes.height = allocation.height;
350     attributes.wclass = GDK_INPUT_OUTPUT;
351     attributes.visual = gtk_widget_get_visual(widget);
352     attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK
353         | GDK_EXPOSURE_MASK
354         | GDK_BUTTON_PRESS_MASK
355         | GDK_BUTTON_RELEASE_MASK
356         | GDK_SCROLL_MASK
357         | GDK_SMOOTH_SCROLL_MASK
358         | GDK_POINTER_MOTION_MASK
359         | GDK_ENTER_NOTIFY_MASK
360         | GDK_LEAVE_NOTIFY_MASK
361         | GDK_KEY_PRESS_MASK
362         | GDK_KEY_RELEASE_MASK
363         | GDK_BUTTON_MOTION_MASK
364         | GDK_BUTTON1_MOTION_MASK
365         | GDK_BUTTON2_MOTION_MASK
366         | GDK_BUTTON3_MOTION_MASK
367         | GDK_TOUCH_MASK;
368
369     gint attributesMask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
370
371     GdkWindow* window = gdk_window_new(gtk_widget_get_parent_window(widget), &attributes, attributesMask);
372     gtk_widget_set_window(widget, window);
373     gdk_window_set_user_data(window, widget);
374
375 #if USE(TEXTURE_MAPPER_GL) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
376     if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11) {
377         if (DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(priv->pageProxy->drawingArea()))
378             drawingArea->setNativeSurfaceHandleForCompositing(GDK_WINDOW_XID(window));
379     }
380 #endif
381
382     gtk_style_context_set_background(gtk_widget_get_style_context(widget), window);
383
384     gtk_im_context_set_client_window(priv->inputMethodFilter.context(), window);
385 }
386
387 static void webkitWebViewBaseUnrealize(GtkWidget* widget)
388 {
389     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(widget);
390 #if USE(TEXTURE_MAPPER_GL) && PLATFORM(X11) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
391     if (PlatformDisplay::sharedDisplay().type() == PlatformDisplay::Type::X11) {
392         if (DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(webView->priv->pageProxy->drawingArea()))
393             drawingArea->destroyNativeSurfaceHandleForCompositing();
394     }
395 #endif
396     gtk_im_context_set_client_window(webView->priv->inputMethodFilter.context(), nullptr);
397
398     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->unrealize(widget);
399 }
400
401 static bool webkitWebViewChildIsInternalWidget(WebKitWebViewBase* webViewBase, GtkWidget* widget)
402 {
403     WebKitWebViewBasePrivate* priv = webViewBase->priv;
404     return widget == priv->inspectorView || widget == priv->authenticationDialog;
405 }
406
407 static void webkitWebViewBaseContainerAdd(GtkContainer* container, GtkWidget* widget)
408 {
409     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(container);
410     WebKitWebViewBasePrivate* priv = webView->priv;
411
412     // Internal widgets like the web inspector and authentication dialog have custom
413     // allocations so we don't need to add them to our list of children.
414     if (!webkitWebViewChildIsInternalWidget(webView, widget)) {
415         GtkAllocation childAllocation;
416         gtk_widget_get_allocation(widget, &childAllocation);
417         priv->children.set(widget, childAllocation);
418     }
419
420     gtk_widget_set_parent(widget, GTK_WIDGET(container));
421 }
422
423 void webkitWebViewBaseAddAuthenticationDialog(WebKitWebViewBase* webViewBase, GtkWidget* dialog)
424 {
425     WebKitWebViewBasePrivate* priv = webViewBase->priv;
426     priv->authenticationDialog = dialog;
427     gtk_container_add(GTK_CONTAINER(webViewBase), dialog);
428     gtk_widget_show(dialog);
429
430     // We need to draw the shadow over the widget.
431     gtk_widget_queue_draw(GTK_WIDGET(webViewBase));
432 }
433
434 void webkitWebViewBaseAddWebInspector(WebKitWebViewBase* webViewBase, GtkWidget* inspector, AttachmentSide attachmentSide)
435 {
436     if (webViewBase->priv->inspectorView == inspector && webViewBase->priv->inspectorAttachmentSide == attachmentSide)
437         return;
438
439     webViewBase->priv->inspectorAttachmentSide = attachmentSide;
440
441     if (webViewBase->priv->inspectorView == inspector) {
442         gtk_widget_queue_resize(GTK_WIDGET(webViewBase));
443         return;
444     }
445
446     webViewBase->priv->inspectorView = inspector;
447     gtk_container_add(GTK_CONTAINER(webViewBase), inspector);
448 }
449
450 static void webkitWebViewBaseContainerRemove(GtkContainer* container, GtkWidget* widget)
451 {
452     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(container);
453     WebKitWebViewBasePrivate* priv = webView->priv;
454     GtkWidget* widgetContainer = GTK_WIDGET(container);
455
456     gboolean wasVisible = gtk_widget_get_visible(widget);
457     gtk_widget_unparent(widget);
458
459     if (priv->inspectorView == widget) {
460         priv->inspectorView = 0;
461         priv->inspectorViewSize = 0;
462     } else if (priv->authenticationDialog == widget) {
463         priv->authenticationDialog = 0;
464     } else {
465         ASSERT(priv->children.contains(widget));
466         priv->children.remove(widget);
467     }
468     if (wasVisible && gtk_widget_get_visible(widgetContainer))
469         gtk_widget_queue_resize(widgetContainer);
470 }
471
472 static void webkitWebViewBaseContainerForall(GtkContainer* container, gboolean includeInternals, GtkCallback callback, gpointer callbackData)
473 {
474     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(container);
475     WebKitWebViewBasePrivate* priv = webView->priv;
476
477     for (const auto& child : copyToVector(priv->children.keys())) {
478         if (priv->children.contains(child))
479             (*callback)(child, callbackData);
480     }
481
482     if (includeInternals && priv->inspectorView)
483         (*callback)(priv->inspectorView, callbackData);
484
485     if (includeInternals && priv->authenticationDialog)
486         (*callback)(priv->authenticationDialog, callbackData);
487 }
488
489 void webkitWebViewBaseChildMoveResize(WebKitWebViewBase* webView, GtkWidget* child, const IntRect& childRect)
490 {
491     const IntRect& geometry = webView->priv->children.get(child);
492     if (geometry == childRect)
493         return;
494
495     webView->priv->children.set(child, childRect);
496     gtk_widget_queue_resize_no_redraw(GTK_WIDGET(webView));
497 }
498
499 static void webkitWebViewBaseDispose(GObject* gobject)
500 {
501     WebKitWebViewBase* webView = WEBKIT_WEB_VIEW_BASE(gobject);
502     webkitWebViewBaseSetToplevelOnScreenWindow(webView, nullptr);
503     webView->priv->pageProxy->close();
504     webView->priv->acceleratedBackingStore = nullptr;
505     webView->priv->sleepDisabler = nullptr;
506     G_OBJECT_CLASS(webkit_web_view_base_parent_class)->dispose(gobject);
507 }
508
509 static void webkitWebViewBaseConstructed(GObject* object)
510 {
511     G_OBJECT_CLASS(webkit_web_view_base_parent_class)->constructed(object);
512
513     GtkWidget* viewWidget = GTK_WIDGET(object);
514     gtk_widget_set_can_focus(viewWidget, TRUE);
515     gtk_drag_dest_set(viewWidget, static_cast<GtkDestDefaults>(0), nullptr, 0,
516         static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK | GDK_ACTION_PRIVATE));
517     gtk_drag_dest_set_target_list(viewWidget, PasteboardHelper::singleton().targetList());
518
519     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(object)->priv;
520     priv->pageClient = std::make_unique<PageClientImpl>(viewWidget);
521     priv->authenticationDialog = 0;
522 }
523
524 static gboolean webkitWebViewBaseDraw(GtkWidget* widget, cairo_t* cr)
525 {
526     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
527     DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(webViewBase->priv->pageProxy->drawingArea());
528     if (!drawingArea)
529         return FALSE;
530
531     GdkRectangle clipRect;
532     if (!gdk_cairo_get_clip_rectangle(cr, &clipRect))
533         return FALSE;
534
535     if (webViewBase->priv->acceleratedBackingStore && drawingArea->isInAcceleratedCompositingMode())
536         webViewBase->priv->acceleratedBackingStore->paint(cr, clipRect);
537     else {
538         WebCore::Region unpaintedRegion; // This is simply unused.
539         drawingArea->paint(cr, clipRect, unpaintedRegion);
540     }
541
542     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->draw(widget, cr);
543
544     return FALSE;
545 }
546
547 static void webkitWebViewBaseChildAllocate(GtkWidget* child, gpointer userData)
548 {
549     if (!gtk_widget_get_visible(child))
550         return;
551
552     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(userData);
553     WebKitWebViewBasePrivate* priv = webViewBase->priv;
554     const IntRect& geometry = priv->children.get(child);
555     if (geometry.isEmpty())
556         return;
557
558     GtkAllocation childAllocation = geometry;
559     gtk_widget_size_allocate(child, &childAllocation);
560     priv->children.set(child, IntRect());
561 }
562
563 static void webkitWebViewBaseSizeAllocate(GtkWidget* widget, GtkAllocation* allocation)
564 {
565     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->size_allocate(widget, allocation);
566
567     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
568     gtk_container_foreach(GTK_CONTAINER(webViewBase), webkitWebViewBaseChildAllocate, webViewBase);
569
570     IntRect viewRect(allocation->x, allocation->y, allocation->width, allocation->height);
571     WebKitWebViewBasePrivate* priv = webViewBase->priv;
572     if (priv->inspectorView) {
573         GtkAllocation childAllocation = viewRect;
574
575         if (priv->inspectorAttachmentSide == AttachmentSide::Bottom) {
576             int inspectorViewHeight = std::min(static_cast<int>(priv->inspectorViewSize), allocation->height);
577             childAllocation.x = 0;
578             childAllocation.y = allocation->height - inspectorViewHeight;
579             childAllocation.height = inspectorViewHeight;
580             viewRect.setHeight(std::max(allocation->height - inspectorViewHeight, 1));
581         } else {
582             int inspectorViewWidth = std::min(static_cast<int>(priv->inspectorViewSize), allocation->width);
583             childAllocation.y = 0;
584             childAllocation.x = allocation->width - inspectorViewWidth;
585             childAllocation.width = inspectorViewWidth;
586             viewRect.setWidth(std::max(allocation->width - inspectorViewWidth, 1));
587         }
588
589         gtk_widget_size_allocate(priv->inspectorView, &childAllocation);
590     }
591
592     // The authentication dialog is centered in the view rect, which means that it
593     // never overlaps the web inspector. Thus, we need to calculate the allocation here
594     // after calculating the inspector allocation.
595     if (priv->authenticationDialog) {
596         GtkRequisition minimumSize;
597         gtk_widget_get_preferred_size(priv->authenticationDialog, &minimumSize, nullptr);
598
599         GtkAllocation childAllocation = { 0, 0, std::max(minimumSize.width, viewRect.width()), std::max(minimumSize.height, viewRect.height()) };
600         gtk_widget_size_allocate(priv->authenticationDialog, &childAllocation);
601     }
602
603     if (DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(priv->pageProxy->drawingArea()))
604         drawingArea->setSize(viewRect.size(), IntSize(), IntSize());
605 }
606
607 static void webkitWebViewBaseGetPreferredWidth(GtkWidget* widget, gint* minimumSize, gint* naturalSize)
608 {
609     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
610     *minimumSize = 0;
611     *naturalSize = priv->contentsSize.width();
612 }
613
614 static void webkitWebViewBaseGetPreferredHeight(GtkWidget* widget, gint* minimumSize, gint* naturalSize)
615 {
616     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
617     *minimumSize = 0;
618     *naturalSize = priv->contentsSize.height();
619 }
620
621 static void webkitWebViewBaseMap(GtkWidget* widget)
622 {
623     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->map(widget);
624
625     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
626     WebKitWebViewBasePrivate* priv = webViewBase->priv;
627     ActivityState::Flags flagsToUpdate = 0;
628     if (!(priv->activityState & ActivityState::IsVisible))
629         flagsToUpdate |= ActivityState::IsVisible;
630     if (priv->toplevelOnScreenWindow) {
631         if (!(priv->activityState & ActivityState::IsInWindow))
632             flagsToUpdate |= ActivityState::IsInWindow;
633         if (gtk_window_is_active(GTK_WINDOW(priv->toplevelOnScreenWindow)) && !(priv->activityState & ActivityState::WindowIsActive))
634             flagsToUpdate |= ActivityState::WindowIsActive;
635     }
636     if (!flagsToUpdate)
637         return;
638
639     priv->activityState |= flagsToUpdate;
640     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, flagsToUpdate);
641 }
642
643 static void webkitWebViewBaseUnmap(GtkWidget* widget)
644 {
645     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->unmap(widget);
646
647     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
648     WebKitWebViewBasePrivate* priv = webViewBase->priv;
649     if (!(priv->activityState & ActivityState::IsVisible))
650         return;
651
652     priv->activityState &= ~ActivityState::IsVisible;
653     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, ActivityState::IsVisible);
654 }
655
656 static gboolean webkitWebViewBaseFocusInEvent(GtkWidget* widget, GdkEventFocus* event)
657 {
658     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
659     webkitWebViewBaseSetFocus(webViewBase, true);
660     webViewBase->priv->inputMethodFilter.notifyFocusedIn();
661
662     return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->focus_in_event(widget, event);
663 }
664
665 static gboolean webkitWebViewBaseFocusOutEvent(GtkWidget* widget, GdkEventFocus* event)
666 {
667     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
668     webkitWebViewBaseSetFocus(webViewBase, false);
669     webViewBase->priv->inputMethodFilter.notifyFocusedOut();
670
671     return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->focus_out_event(widget, event);
672 }
673
674 static gboolean webkitWebViewBaseKeyPressEvent(GtkWidget* widget, GdkEventKey* keyEvent)
675 {
676     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
677     WebKitWebViewBasePrivate* priv = webViewBase->priv;
678
679 #if ENABLE(DEVELOPER_MODE) && OS(LINUX)
680     if ((keyEvent->state & GDK_CONTROL_MASK) && (keyEvent->state & GDK_SHIFT_MASK) && keyEvent->keyval == GDK_KEY_G) {
681         auto& preferences = priv->pageProxy->preferences();
682         preferences.setResourceUsageOverlayVisible(!preferences.resourceUsageOverlayVisible());
683         priv->shouldForwardNextKeyEvent = FALSE;
684         return GDK_EVENT_STOP;
685     }
686 #endif
687
688     if (priv->authenticationDialog)
689         return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->key_press_event(widget, keyEvent);
690
691 #if ENABLE(FULLSCREEN_API)
692     if (priv->fullScreenModeActive) {
693         switch (keyEvent->keyval) {
694         case GDK_KEY_Escape:
695         case GDK_KEY_f:
696         case GDK_KEY_F:
697             priv->pageProxy->fullScreenManager()->requestExitFullScreen();
698             return GDK_EVENT_STOP;
699         default:
700             break;
701         }
702     }
703 #endif
704
705     // Since WebProcess key event handling is not synchronous, handle the event in two passes.
706     // When WebProcess processes the input event, it will call PageClientImpl::doneWithKeyEvent
707     // with event handled status which determines whether to pass the input event to parent or not
708     // using gtk_main_do_event().
709     if (priv->shouldForwardNextKeyEvent) {
710         priv->shouldForwardNextKeyEvent = FALSE;
711         return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->key_press_event(widget, keyEvent);
712     }
713
714     // We need to copy the event as otherwise it could be destroyed before we reach the lambda body.
715     GUniquePtr<GdkEvent> event(gdk_event_copy(reinterpret_cast<GdkEvent*>(keyEvent)));
716     priv->inputMethodFilter.filterKeyEvent(keyEvent, [priv, event = WTFMove(event)](const WebCore::CompositionResults& compositionResults, InputMethodFilter::EventFakedForComposition faked) {
717         priv->pageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(event.get(), compositionResults, faked,
718             !compositionResults.compositionUpdated() ? priv->keyBindingTranslator.commandsForKeyEvent(&event->key) : Vector<String>()));
719     });
720
721     return GDK_EVENT_STOP;
722 }
723
724 static gboolean webkitWebViewBaseKeyReleaseEvent(GtkWidget* widget, GdkEventKey* keyEvent)
725 {
726     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
727     WebKitWebViewBasePrivate* priv = webViewBase->priv;
728
729     if (priv->shouldForwardNextKeyEvent) {
730         priv->shouldForwardNextKeyEvent = FALSE;
731         return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->key_release_event(widget, keyEvent);
732     }
733
734     // We need to copy the event as otherwise it could be destroyed before we reach the lambda body.
735     GUniquePtr<GdkEvent> event(gdk_event_copy(reinterpret_cast<GdkEvent*>(keyEvent)));
736     priv->inputMethodFilter.filterKeyEvent(keyEvent, [priv, event = WTFMove(event)](const WebCore::CompositionResults& compositionResults, InputMethodFilter::EventFakedForComposition faked) {
737         priv->pageProxy->handleKeyboardEvent(NativeWebKeyboardEvent(event.get(), compositionResults, faked, { }));
738     });
739
740     return GDK_EVENT_STOP;
741 }
742
743 static gboolean webkitWebViewBaseButtonPressEvent(GtkWidget* widget, GdkEventButton* buttonEvent)
744 {
745     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
746     WebKitWebViewBasePrivate* priv = webViewBase->priv;
747
748     if (priv->authenticationDialog)
749         return GDK_EVENT_STOP;
750
751     gtk_widget_grab_focus(widget);
752
753     priv->inputMethodFilter.notifyMouseButtonPress();
754
755     // For double and triple clicks GDK sends both a normal button press event
756     // and a specific type (like GDK_2BUTTON_PRESS). If we detect a special press
757     // coming up, ignore this event as it certainly generated the double or triple
758     // click. The consequence of not eating this event is two DOM button press events
759     // are generated.
760     GUniquePtr<GdkEvent> nextEvent(gdk_event_peek());
761     if (nextEvent && (nextEvent->any.type == GDK_2BUTTON_PRESS || nextEvent->any.type == GDK_3BUTTON_PRESS))
762         return GDK_EVENT_STOP;
763
764     // If it's a right click event save it as a possible context menu event.
765     if (buttonEvent->button == 3)
766         priv->contextMenuEvent.reset(gdk_event_copy(reinterpret_cast<GdkEvent*>(buttonEvent)));
767
768     priv->pageProxy->handleMouseEvent(NativeWebMouseEvent(reinterpret_cast<GdkEvent*>(buttonEvent),
769         priv->clickCounter.currentClickCountForGdkButtonEvent(buttonEvent)));
770     return GDK_EVENT_STOP;
771 }
772
773 static gboolean webkitWebViewBaseButtonReleaseEvent(GtkWidget* widget, GdkEventButton* event)
774 {
775     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
776     WebKitWebViewBasePrivate* priv = webViewBase->priv;
777
778     if (priv->authenticationDialog)
779         return GDK_EVENT_STOP;
780
781     gtk_widget_grab_focus(widget);
782     priv->pageProxy->handleMouseEvent(NativeWebMouseEvent(reinterpret_cast<GdkEvent*>(event), 0 /* currentClickCount */));
783
784     return GDK_EVENT_STOP;
785 }
786
787 static gboolean webkitWebViewBaseScrollEvent(GtkWidget* widget, GdkEventScroll* event)
788 {
789     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
790     WebKitWebViewBasePrivate* priv = webViewBase->priv;
791
792     if (std::exchange(priv->shouldForwardNextWheelEvent, false))
793         return GDK_EVENT_PROPAGATE;
794
795     if (priv->authenticationDialog)
796         return GDK_EVENT_PROPAGATE;
797
798     priv->pageProxy->handleWheelEvent(NativeWebWheelEvent(reinterpret_cast<GdkEvent*>(event)));
799
800     return GDK_EVENT_STOP;
801 }
802
803 static gboolean webkitWebViewBasePopupMenu(GtkWidget* widget)
804 {
805     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
806     WebKitWebViewBasePrivate* priv = webViewBase->priv;
807
808     GdkEvent* currentEvent = gtk_get_current_event();
809     if (!currentEvent)
810         currentEvent = gdk_event_new(GDK_NOTHING);
811     priv->contextMenuEvent.reset(currentEvent);
812     priv->pageProxy->handleContextMenuKeyEvent();
813
814     return TRUE;
815 }
816
817 static gboolean webkitWebViewBaseMotionNotifyEvent(GtkWidget* widget, GdkEventMotion* event)
818 {
819     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
820     WebKitWebViewBasePrivate* priv = webViewBase->priv;
821
822     if (priv->authenticationDialog) {
823         auto* widgetClass = GTK_WIDGET_CLASS(webkit_web_view_base_parent_class);
824         return widgetClass->motion_notify_event ? widgetClass->motion_notify_event(widget, event) : GDK_EVENT_PROPAGATE;
825     }
826
827     priv->pageProxy->handleMouseEvent(NativeWebMouseEvent(reinterpret_cast<GdkEvent*>(event), 0 /* currentClickCount */));
828
829     return GDK_EVENT_PROPAGATE;
830 }
831
832 static gboolean webkitWebViewBaseCrossingNotifyEvent(GtkWidget* widget, GdkEventCrossing* crosssingEvent)
833 {
834     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
835     WebKitWebViewBasePrivate* priv = webViewBase->priv;
836
837     if (priv->authenticationDialog)
838         return GDK_EVENT_PROPAGATE;
839
840     // In the case of crossing events, it's very important the actual coordinates the WebProcess receives, because once the mouse leaves
841     // the web view, the WebProcess won't receive more events until the mouse enters again in the web view. So, if the coordinates of the leave
842     // event are not accurate, the WebProcess might not know the mouse left the view. This can happen because of double to integer conversion,
843     // if the coordinates of the leave event are for example (25.2, -0.9), the WebProcess will receive (25, 0) and any hit test will succeed
844     // because those coordinates are inside the web view.
845     GtkAllocation allocation;
846     gtk_widget_get_allocation(widget, &allocation);
847     double width = allocation.width;
848     double height = allocation.height;
849     double x = crosssingEvent->x;
850     double y = crosssingEvent->y;
851     if (x < 0 && x > -1)
852         x = -1;
853     else if (x >= width && x < width + 1)
854         x = width + 1;
855     if (y < 0 && y > -1)
856         y = -1;
857     else if (y >= height && y < height + 1)
858         y = height + 1;
859
860     GdkEvent* event = reinterpret_cast<GdkEvent*>(crosssingEvent);
861     GUniquePtr<GdkEvent> copiedEvent;
862     if (x != crosssingEvent->x || y != crosssingEvent->y) {
863         copiedEvent.reset(gdk_event_copy(event));
864         copiedEvent->crossing.x = x;
865         copiedEvent->crossing.y = y;
866     }
867
868     priv->pageProxy->handleMouseEvent(NativeWebMouseEvent(copiedEvent ? copiedEvent.get() : event, 0 /* currentClickCount */));
869
870     return GDK_EVENT_PROPAGATE;
871 }
872
873 #if ENABLE(TOUCH_EVENTS)
874 static void appendTouchEvent(Vector<WebPlatformTouchPoint>& touchPoints, const GdkEvent* event, WebPlatformTouchPoint::TouchPointState state)
875 {
876     gdouble x, y;
877     gdk_event_get_coords(event, &x, &y);
878
879     gdouble xRoot, yRoot;
880     gdk_event_get_root_coords(event, &xRoot, &yRoot);
881
882     uint32_t identifier = GPOINTER_TO_UINT(gdk_event_get_event_sequence(event));
883     touchPoints.uncheckedAppend(WebPlatformTouchPoint(identifier, state, IntPoint(xRoot, yRoot), IntPoint(x, y)));
884 }
885
886 static inline WebPlatformTouchPoint::TouchPointState touchPointStateForEvents(const GdkEvent* current, const GdkEvent* event)
887 {
888     if (gdk_event_get_event_sequence(current) != gdk_event_get_event_sequence(event))
889         return WebPlatformTouchPoint::TouchStationary;
890
891     switch (current->type) {
892     case GDK_TOUCH_UPDATE:
893         return WebPlatformTouchPoint::TouchMoved;
894     case GDK_TOUCH_BEGIN:
895         return WebPlatformTouchPoint::TouchPressed;
896     case GDK_TOUCH_END:
897         return WebPlatformTouchPoint::TouchReleased;
898     default:
899         return WebPlatformTouchPoint::TouchStationary;
900     }
901 }
902
903 static void webkitWebViewBaseGetTouchPointsForEvent(WebKitWebViewBase* webViewBase, GdkEvent* event, Vector<WebPlatformTouchPoint>& touchPoints)
904 {
905     WebKitWebViewBasePrivate* priv = webViewBase->priv;
906     touchPoints.reserveInitialCapacity(event->type == GDK_TOUCH_END ? priv->touchEvents.size() + 1 : priv->touchEvents.size());
907
908     for (const auto& it : priv->touchEvents)
909         appendTouchEvent(touchPoints, it.value.get(), touchPointStateForEvents(it.value.get(), event));
910
911     // Touch was already removed from the TouchEventsMap, add it here.
912     if (event->type == GDK_TOUCH_END)
913         appendTouchEvent(touchPoints, event, WebPlatformTouchPoint::TouchReleased);
914 }
915
916 static gboolean webkitWebViewBaseTouchEvent(GtkWidget* widget, GdkEventTouch* event)
917 {
918     WebKitWebViewBase* webViewBase = WEBKIT_WEB_VIEW_BASE(widget);
919     WebKitWebViewBasePrivate* priv = webViewBase->priv;
920
921     if (priv->authenticationDialog)
922         return GDK_EVENT_STOP;
923
924     GdkEvent* touchEvent = reinterpret_cast<GdkEvent*>(event);
925     uint32_t sequence = GPOINTER_TO_UINT(gdk_event_get_event_sequence(touchEvent));
926
927     switch (touchEvent->type) {
928     case GDK_TOUCH_BEGIN: {
929         ASSERT(!priv->touchEvents.contains(sequence));
930         GUniquePtr<GdkEvent> event(gdk_event_copy(touchEvent));
931         priv->touchEvents.add(sequence, WTFMove(event));
932         break;
933     }
934     case GDK_TOUCH_UPDATE: {
935         auto it = priv->touchEvents.find(sequence);
936         ASSERT(it != priv->touchEvents.end());
937         it->value.reset(gdk_event_copy(touchEvent));
938         break;
939     }
940     case GDK_TOUCH_END:
941         ASSERT(priv->touchEvents.contains(sequence));
942         priv->touchEvents.remove(sequence);
943         break;
944     default:
945         break;
946     }
947
948     Vector<WebPlatformTouchPoint> touchPoints;
949     webkitWebViewBaseGetTouchPointsForEvent(webViewBase, touchEvent, touchPoints);
950     priv->pageProxy->handleTouchEvent(NativeWebTouchEvent(reinterpret_cast<GdkEvent*>(event), WTFMove(touchPoints)));
951
952     return GDK_EVENT_STOP;
953 }
954 #endif // ENABLE(TOUCH_EVENTS)
955
956 #if HAVE(GTK_GESTURES)
957 GestureController& webkitWebViewBaseGestureController(WebKitWebViewBase* webViewBase)
958 {
959     WebKitWebViewBasePrivate* priv = webViewBase->priv;
960     if (!priv->gestureController)
961         priv->gestureController = std::make_unique<GestureController>(*priv->pageProxy);
962     return *priv->gestureController;
963 }
964 #endif
965
966 static gboolean webkitWebViewBaseQueryTooltip(GtkWidget* widget, gint /* x */, gint /* y */, gboolean keyboardMode, GtkTooltip* tooltip)
967 {
968     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
969
970     if (keyboardMode) {
971         // TODO: https://bugs.webkit.org/show_bug.cgi?id=61732.
972         notImplemented();
973         return FALSE;
974     }
975
976     if (priv->tooltipText.length() <= 0)
977         return FALSE;
978
979     if (!priv->tooltipArea.isEmpty()) {
980         GdkRectangle area = priv->tooltipArea;
981         gtk_tooltip_set_tip_area(tooltip, &area);
982     } else
983         gtk_tooltip_set_tip_area(tooltip, 0);
984     gtk_tooltip_set_text(tooltip, priv->tooltipText.data());
985
986     return TRUE;
987 }
988
989 #if ENABLE(DRAG_SUPPORT)
990 static void webkitWebViewBaseDragDataGet(GtkWidget* widget, GdkDragContext* context, GtkSelectionData* selectionData, guint info, guint /* time */)
991 {
992     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
993     ASSERT(priv->dragAndDropHandler);
994     priv->dragAndDropHandler->fillDragData(context, selectionData, info);
995 }
996
997 static void webkitWebViewBaseDragEnd(GtkWidget* widget, GdkDragContext* context)
998 {
999     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1000     ASSERT(priv->dragAndDropHandler);
1001     priv->dragAndDropHandler->finishDrag(context);
1002 }
1003
1004 static void webkitWebViewBaseDragDataReceived(GtkWidget* widget, GdkDragContext* context, gint /* x */, gint /* y */, GtkSelectionData* selectionData, guint info, guint time)
1005 {
1006     webkitWebViewBaseDragAndDropHandler(WEBKIT_WEB_VIEW_BASE(widget)).dragEntered(context, selectionData, info, time);
1007 }
1008 #endif // ENABLE(DRAG_SUPPORT)
1009
1010 static AtkObject* webkitWebViewBaseGetAccessible(GtkWidget* widget)
1011 {
1012     // If the socket has already been created and embedded a plug ID, return it.
1013     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1014     if (priv->accessible && atk_socket_is_occupied(ATK_SOCKET(priv->accessible.get())))
1015         return priv->accessible.get();
1016
1017     // Create the accessible object and associate it to the widget.
1018     if (!priv->accessible) {
1019         priv->accessible = adoptGRef(ATK_OBJECT(webkitWebViewBaseAccessibleNew(widget)));
1020
1021         // Set the parent not to break bottom-up navigation.
1022         GtkWidget* parentWidget = gtk_widget_get_parent(widget);
1023         AtkObject* axParent = parentWidget ? gtk_widget_get_accessible(parentWidget) : 0;
1024         if (axParent)
1025             atk_object_set_parent(priv->accessible.get(), axParent);
1026     }
1027
1028     // Try to embed the plug in the socket, if posssible.
1029     String plugID = priv->pageProxy->accessibilityPlugID();
1030     if (plugID.isNull())
1031         return priv->accessible.get();
1032
1033     atk_socket_embed(ATK_SOCKET(priv->accessible.get()), const_cast<gchar*>(plugID.utf8().data()));
1034
1035     return priv->accessible.get();
1036 }
1037
1038 #if ENABLE(DRAG_SUPPORT)
1039 static gboolean webkitWebViewBaseDragMotion(GtkWidget* widget, GdkDragContext* context, gint x, gint y, guint time)
1040 {
1041     webkitWebViewBaseDragAndDropHandler(WEBKIT_WEB_VIEW_BASE(widget)).dragMotion(context, IntPoint(x, y), time);
1042     return TRUE;
1043 }
1044
1045 static void webkitWebViewBaseDragLeave(GtkWidget* widget, GdkDragContext* context, guint /* time */)
1046 {
1047     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1048     ASSERT(priv->dragAndDropHandler);
1049     priv->dragAndDropHandler->dragLeave(context);
1050 }
1051
1052 static gboolean webkitWebViewBaseDragDrop(GtkWidget* widget, GdkDragContext* context, gint x, gint y, guint time)
1053 {
1054     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1055     ASSERT(priv->dragAndDropHandler);
1056     return priv->dragAndDropHandler->drop(context, IntPoint(x, y), time);
1057 }
1058 #endif // ENABLE(DRAG_SUPPORT)
1059
1060 static void webkitWebViewBaseHierarchyChanged(GtkWidget* widget, GtkWidget* oldToplevel)
1061 {
1062     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1063     if (widgetIsOnscreenToplevelWindow(oldToplevel) && GTK_WINDOW(oldToplevel) == priv->toplevelOnScreenWindow) {
1064         webkitWebViewBaseSetToplevelOnScreenWindow(WEBKIT_WEB_VIEW_BASE(widget), nullptr);
1065         return;
1066     }
1067
1068     if (!oldToplevel) {
1069         GtkWidget* toplevel = gtk_widget_get_toplevel(widget);
1070         if (widgetIsOnscreenToplevelWindow(toplevel))
1071             webkitWebViewBaseSetToplevelOnScreenWindow(WEBKIT_WEB_VIEW_BASE(widget), GTK_WINDOW(toplevel));
1072     }
1073 }
1074
1075 static gboolean webkitWebViewBaseFocus(GtkWidget* widget, GtkDirectionType direction)
1076 {
1077     // If the authentication dialog is active, we need to forward focus events there. This
1078     // ensures that you can tab between elements in the box.
1079     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1080     if (priv->authenticationDialog) {
1081         gboolean returnValue;
1082         g_signal_emit_by_name(priv->authenticationDialog, "focus", direction, &returnValue);
1083         return returnValue;
1084     }
1085
1086     return GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->focus(widget, direction);
1087 }
1088
1089 static void webkitWebViewBaseDestroy(GtkWidget* widget)
1090 {
1091     WebKitWebViewBasePrivate* priv = WEBKIT_WEB_VIEW_BASE(widget)->priv;
1092     if (priv->authenticationDialog)
1093         gtk_widget_destroy(priv->authenticationDialog);
1094
1095     GTK_WIDGET_CLASS(webkit_web_view_base_parent_class)->destroy(widget);
1096 }
1097
1098 static void webkit_web_view_base_class_init(WebKitWebViewBaseClass* webkitWebViewBaseClass)
1099 {
1100     GtkWidgetClass* widgetClass = GTK_WIDGET_CLASS(webkitWebViewBaseClass);
1101     widgetClass->realize = webkitWebViewBaseRealize;
1102     widgetClass->unrealize = webkitWebViewBaseUnrealize;
1103     widgetClass->draw = webkitWebViewBaseDraw;
1104     widgetClass->size_allocate = webkitWebViewBaseSizeAllocate;
1105     widgetClass->get_preferred_width = webkitWebViewBaseGetPreferredWidth;
1106     widgetClass->get_preferred_height = webkitWebViewBaseGetPreferredHeight;
1107     widgetClass->map = webkitWebViewBaseMap;
1108     widgetClass->unmap = webkitWebViewBaseUnmap;
1109     widgetClass->focus = webkitWebViewBaseFocus;
1110     widgetClass->focus_in_event = webkitWebViewBaseFocusInEvent;
1111     widgetClass->focus_out_event = webkitWebViewBaseFocusOutEvent;
1112     widgetClass->key_press_event = webkitWebViewBaseKeyPressEvent;
1113     widgetClass->key_release_event = webkitWebViewBaseKeyReleaseEvent;
1114     widgetClass->button_press_event = webkitWebViewBaseButtonPressEvent;
1115     widgetClass->button_release_event = webkitWebViewBaseButtonReleaseEvent;
1116     widgetClass->scroll_event = webkitWebViewBaseScrollEvent;
1117     widgetClass->popup_menu = webkitWebViewBasePopupMenu;
1118     widgetClass->motion_notify_event = webkitWebViewBaseMotionNotifyEvent;
1119     widgetClass->enter_notify_event = webkitWebViewBaseCrossingNotifyEvent;
1120     widgetClass->leave_notify_event = webkitWebViewBaseCrossingNotifyEvent;
1121 #if ENABLE(TOUCH_EVENTS)
1122     widgetClass->touch_event = webkitWebViewBaseTouchEvent;
1123 #endif
1124     widgetClass->query_tooltip = webkitWebViewBaseQueryTooltip;
1125 #if ENABLE(DRAG_SUPPORT)
1126     widgetClass->drag_end = webkitWebViewBaseDragEnd;
1127     widgetClass->drag_data_get = webkitWebViewBaseDragDataGet;
1128     widgetClass->drag_motion = webkitWebViewBaseDragMotion;
1129     widgetClass->drag_leave = webkitWebViewBaseDragLeave;
1130     widgetClass->drag_drop = webkitWebViewBaseDragDrop;
1131     widgetClass->drag_data_received = webkitWebViewBaseDragDataReceived;
1132 #endif // ENABLE(DRAG_SUPPORT)
1133     widgetClass->get_accessible = webkitWebViewBaseGetAccessible;
1134     widgetClass->hierarchy_changed = webkitWebViewBaseHierarchyChanged;
1135     widgetClass->destroy = webkitWebViewBaseDestroy;
1136
1137     GObjectClass* gobjectClass = G_OBJECT_CLASS(webkitWebViewBaseClass);
1138     gobjectClass->constructed = webkitWebViewBaseConstructed;
1139     gobjectClass->dispose = webkitWebViewBaseDispose;
1140
1141     GtkContainerClass* containerClass = GTK_CONTAINER_CLASS(webkitWebViewBaseClass);
1142     containerClass->add = webkitWebViewBaseContainerAdd;
1143     containerClass->remove = webkitWebViewBaseContainerRemove;
1144     containerClass->forall = webkitWebViewBaseContainerForall;
1145
1146     // Before creating a WebKitWebViewBasePriv we need to be sure that WebKit is started.
1147     // Usually starting a context triggers InitializeWebKit2, but in case
1148     // we create a view without asking before for a default_context we get a crash.
1149     WebKit::InitializeWebKit2();
1150 }
1151
1152 WebKitWebViewBase* webkitWebViewBaseCreate(const API::PageConfiguration& configuration)
1153 {
1154     WebKitWebViewBase* webkitWebViewBase = WEBKIT_WEB_VIEW_BASE(g_object_new(WEBKIT_TYPE_WEB_VIEW_BASE, nullptr));
1155     webkitWebViewBaseCreateWebPage(webkitWebViewBase, configuration.copy());
1156     return webkitWebViewBase;
1157 }
1158
1159 GtkIMContext* webkitWebViewBaseGetIMContext(WebKitWebViewBase* webkitWebViewBase)
1160 {
1161     return webkitWebViewBase->priv->inputMethodFilter.context();
1162 }
1163
1164 WebPageProxy* webkitWebViewBaseGetPage(WebKitWebViewBase* webkitWebViewBase)
1165 {
1166     return webkitWebViewBase->priv->pageProxy.get();
1167 }
1168
1169 #if HAVE(GTK_SCALE_FACTOR)
1170 static void deviceScaleFactorChanged(WebKitWebViewBase* webkitWebViewBase)
1171 {
1172     webkitWebViewBase->priv->pageProxy->setIntrinsicDeviceScaleFactor(gtk_widget_get_scale_factor(GTK_WIDGET(webkitWebViewBase)));
1173 }
1174 #endif // HAVE(GTK_SCALE_FACTOR)
1175
1176 void webkitWebViewBaseCreateWebPage(WebKitWebViewBase* webkitWebViewBase, Ref<API::PageConfiguration>&& configuration)
1177 {
1178     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1179     WebProcessPool* processPool = configuration->processPool();
1180     priv->pageProxy = processPool->createWebPage(*priv->pageClient, WTFMove(configuration));
1181     priv->pageProxy->initializeWebPage();
1182
1183     priv->acceleratedBackingStore = AcceleratedBackingStore::create(*priv->pageProxy);
1184
1185     priv->inputMethodFilter.setPage(priv->pageProxy.get());
1186
1187 #if HAVE(GTK_SCALE_FACTOR)
1188     // We attach this here, because changes in scale factor are passed directly to the page proxy.
1189     priv->pageProxy->setIntrinsicDeviceScaleFactor(gtk_widget_get_scale_factor(GTK_WIDGET(webkitWebViewBase)));
1190     g_signal_connect(webkitWebViewBase, "notify::scale-factor", G_CALLBACK(deviceScaleFactorChanged), nullptr);
1191 #endif
1192 }
1193
1194 void webkitWebViewBaseSetTooltipText(WebKitWebViewBase* webViewBase, const char* tooltip)
1195 {
1196     WebKitWebViewBasePrivate* priv = webViewBase->priv;
1197     if (tooltip && tooltip[0] != '\0') {
1198         priv->tooltipText = tooltip;
1199         gtk_widget_set_has_tooltip(GTK_WIDGET(webViewBase), TRUE);
1200     } else {
1201         priv->tooltipText = "";
1202         gtk_widget_set_has_tooltip(GTK_WIDGET(webViewBase), FALSE);
1203     }
1204
1205     gtk_widget_trigger_tooltip_query(GTK_WIDGET(webViewBase));
1206 }
1207
1208 void webkitWebViewBaseSetTooltipArea(WebKitWebViewBase* webViewBase, const IntRect& tooltipArea)
1209 {
1210     webViewBase->priv->tooltipArea = tooltipArea;
1211 }
1212
1213 #if ENABLE(DRAG_SUPPORT)
1214 DragAndDropHandler& webkitWebViewBaseDragAndDropHandler(WebKitWebViewBase* webViewBase)
1215 {
1216     WebKitWebViewBasePrivate* priv = webViewBase->priv;
1217     if (!priv->dragAndDropHandler)
1218         priv->dragAndDropHandler = std::make_unique<DragAndDropHandler>(*priv->pageProxy);
1219     return *priv->dragAndDropHandler;
1220 }
1221 #endif // ENABLE(DRAG_SUPPORT)
1222
1223 void webkitWebViewBaseForwardNextKeyEvent(WebKitWebViewBase* webkitWebViewBase)
1224 {
1225     webkitWebViewBase->priv->shouldForwardNextKeyEvent = TRUE;
1226 }
1227
1228 void webkitWebViewBaseForwardNextWheelEvent(WebKitWebViewBase* webkitWebViewBase)
1229 {
1230     webkitWebViewBase->priv->shouldForwardNextWheelEvent = true;
1231 }
1232
1233 void webkitWebViewBaseEnterFullScreen(WebKitWebViewBase* webkitWebViewBase)
1234 {
1235 #if ENABLE(FULLSCREEN_API)
1236     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1237     ASSERT(!priv->fullScreenModeActive);
1238
1239     WebFullScreenManagerProxy* fullScreenManagerProxy = priv->pageProxy->fullScreenManager();
1240     fullScreenManagerProxy->willEnterFullScreen();
1241
1242     GtkWidget* topLevelWindow = gtk_widget_get_toplevel(GTK_WIDGET(webkitWebViewBase));
1243     if (gtk_widget_is_toplevel(topLevelWindow))
1244         gtk_window_fullscreen(GTK_WINDOW(topLevelWindow));
1245     fullScreenManagerProxy->didEnterFullScreen();
1246     priv->fullScreenModeActive = true;
1247     priv->sleepDisabler = PAL::SleepDisabler::create(_("Website running in fullscreen mode"), PAL::SleepDisabler::Type::Display);
1248 #endif
1249 }
1250
1251 void webkitWebViewBaseExitFullScreen(WebKitWebViewBase* webkitWebViewBase)
1252 {
1253 #if ENABLE(FULLSCREEN_API)
1254     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1255     ASSERT(priv->fullScreenModeActive);
1256
1257     WebFullScreenManagerProxy* fullScreenManagerProxy = priv->pageProxy->fullScreenManager();
1258     fullScreenManagerProxy->willExitFullScreen();
1259
1260     GtkWidget* topLevelWindow = gtk_widget_get_toplevel(GTK_WIDGET(webkitWebViewBase));
1261     if (gtk_widget_is_toplevel(topLevelWindow))
1262         gtk_window_unfullscreen(GTK_WINDOW(topLevelWindow));
1263     fullScreenManagerProxy->didExitFullScreen();
1264     priv->fullScreenModeActive = false;
1265     priv->sleepDisabler = nullptr;
1266 #endif
1267 }
1268
1269 bool webkitWebViewBaseIsFullScreen(WebKitWebViewBase* webkitWebViewBase)
1270 {
1271 #if ENABLE(FULLSCREEN_API)
1272     return webkitWebViewBase->priv->fullScreenModeActive;
1273 #else
1274     return false;
1275 #endif
1276 }
1277
1278 void webkitWebViewBaseSetInspectorViewSize(WebKitWebViewBase* webkitWebViewBase, unsigned size)
1279 {
1280     if (webkitWebViewBase->priv->inspectorViewSize == size)
1281         return;
1282     webkitWebViewBase->priv->inspectorViewSize = size;
1283     if (webkitWebViewBase->priv->inspectorView)
1284         gtk_widget_queue_resize_no_redraw(GTK_WIDGET(webkitWebViewBase));
1285 }
1286
1287 static void activeContextMenuUnmapped(GtkMenu* menu, WebKitWebViewBase* webViewBase)
1288 {
1289     if (webViewBase->priv->activeContextMenuProxy && webViewBase->priv->activeContextMenuProxy->gtkMenu() == menu)
1290         webViewBase->priv->activeContextMenuProxy = nullptr;
1291 }
1292
1293 void webkitWebViewBaseSetActiveContextMenuProxy(WebKitWebViewBase* webkitWebViewBase, WebContextMenuProxyGtk* contextMenuProxy)
1294 {
1295     webkitWebViewBase->priv->activeContextMenuProxy = contextMenuProxy;
1296     g_signal_connect_object(contextMenuProxy->gtkMenu(), "unmap", G_CALLBACK(activeContextMenuUnmapped), webkitWebViewBase, static_cast<GConnectFlags>(0));
1297 }
1298
1299 WebContextMenuProxyGtk* webkitWebViewBaseGetActiveContextMenuProxy(WebKitWebViewBase* webkitWebViewBase)
1300 {
1301     return webkitWebViewBase->priv->activeContextMenuProxy;
1302 }
1303
1304 GdkEvent* webkitWebViewBaseTakeContextMenuEvent(WebKitWebViewBase* webkitWebViewBase)
1305 {
1306     return webkitWebViewBase->priv->contextMenuEvent.release();
1307 }
1308
1309 void webkitWebViewBaseSetFocus(WebKitWebViewBase* webViewBase, bool focused)
1310 {
1311     WebKitWebViewBasePrivate* priv = webViewBase->priv;
1312     if ((focused && priv->activityState & ActivityState::IsFocused) || (!focused && !(priv->activityState & ActivityState::IsFocused)))
1313         return;
1314
1315     ActivityState::Flags flagsToUpdate = ActivityState::IsFocused;
1316     if (focused) {
1317         priv->activityState |= ActivityState::IsFocused;
1318
1319         // If the view has received the focus and the window is not active
1320         // mark the current window as active now. This can happen if the
1321         // toplevel window is a GTK_WINDOW_POPUP and the focus has been
1322         // set programatically like WebKitTestRunner does, because POPUP
1323         // can't be focused.
1324         if (!(priv->activityState & ActivityState::WindowIsActive)) {
1325             priv->activityState |= ActivityState::WindowIsActive;
1326             flagsToUpdate |= ActivityState::WindowIsActive;
1327         }
1328     } else
1329         priv->activityState &= ~ActivityState::IsFocused;
1330
1331     webkitWebViewBaseScheduleUpdateActivityState(webViewBase, flagsToUpdate);
1332 }
1333
1334 bool webkitWebViewBaseIsInWindowActive(WebKitWebViewBase* webViewBase)
1335 {
1336     return webViewBase->priv->activityState & ActivityState::WindowIsActive;
1337 }
1338
1339 bool webkitWebViewBaseIsFocused(WebKitWebViewBase* webViewBase)
1340 {
1341     return webViewBase->priv->activityState & ActivityState::IsFocused;
1342 }
1343
1344 bool webkitWebViewBaseIsVisible(WebKitWebViewBase* webViewBase)
1345 {
1346     return webViewBase->priv->activityState & ActivityState::IsVisible;
1347 }
1348
1349 bool webkitWebViewBaseIsInWindow(WebKitWebViewBase* webViewBase)
1350 {
1351     return webViewBase->priv->activityState & ActivityState::IsInWindow;
1352 }
1353
1354 void webkitWebViewBaseSetInputMethodState(WebKitWebViewBase* webkitWebViewBase, bool enabled)
1355 {
1356     webkitWebViewBase->priv->inputMethodFilter.setEnabled(enabled);
1357 }
1358
1359 void webkitWebViewBaseUpdateTextInputState(WebKitWebViewBase* webkitWebViewBase)
1360 {
1361     const auto& editorState = webkitWebViewBase->priv->pageProxy->editorState();
1362     if (!editorState.isMissingPostLayoutData)
1363         webkitWebViewBase->priv->inputMethodFilter.setCursorRect(editorState.postLayoutData().caretRectAtStart);
1364 }
1365
1366 void webkitWebViewBaseSetContentsSize(WebKitWebViewBase* webkitWebViewBase, const IntSize& contentsSize)
1367 {
1368     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1369     if (priv->contentsSize == contentsSize)
1370         return;
1371     priv->contentsSize = contentsSize;
1372 }
1373
1374 void webkitWebViewBaseResetClickCounter(WebKitWebViewBase* webkitWebViewBase)
1375 {
1376     webkitWebViewBase->priv->clickCounter.reset();
1377 }
1378
1379 void webkitWebViewBaseEnterAcceleratedCompositingMode(WebKitWebViewBase* webkitWebViewBase, const LayerTreeContext& layerTreeContext)
1380 {
1381     if (webkitWebViewBase->priv->acceleratedBackingStore)
1382         webkitWebViewBase->priv->acceleratedBackingStore->update(layerTreeContext);
1383 }
1384
1385 void webkitWebViewBaseUpdateAcceleratedCompositingMode(WebKitWebViewBase* webkitWebViewBase, const LayerTreeContext& layerTreeContext)
1386 {
1387     if (webkitWebViewBase->priv->acceleratedBackingStore)
1388         webkitWebViewBase->priv->acceleratedBackingStore->update(layerTreeContext);
1389 }
1390
1391 void webkitWebViewBaseExitAcceleratedCompositingMode(WebKitWebViewBase* webkitWebViewBase)
1392 {
1393     if (webkitWebViewBase->priv->acceleratedBackingStore)
1394         webkitWebViewBase->priv->acceleratedBackingStore->update(LayerTreeContext());
1395 }
1396
1397 void webkitWebViewBaseDidRelaunchWebProcess(WebKitWebViewBase* webkitWebViewBase)
1398 {
1399     // Queue a resize to ensure the new DrawingAreaProxy is resized.
1400     gtk_widget_queue_resize_no_redraw(GTK_WIDGET(webkitWebViewBase));
1401
1402 #if PLATFORM(X11) && USE(TEXTURE_MAPPER_GL) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
1403     if (PlatformDisplay::sharedDisplay().type() != PlatformDisplay::Type::X11)
1404         return;
1405
1406     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1407     DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(priv->pageProxy->drawingArea());
1408     ASSERT(drawingArea);
1409
1410     if (!gtk_widget_get_realized(GTK_WIDGET(webkitWebViewBase)))
1411         return;
1412
1413     uint64_t windowID = GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(webkitWebViewBase)));
1414     drawingArea->setNativeSurfaceHandleForCompositing(windowID);
1415 #else
1416     UNUSED_PARAM(webkitWebViewBase);
1417 #endif
1418 }
1419
1420 void webkitWebViewBasePageClosed(WebKitWebViewBase* webkitWebViewBase)
1421 {
1422     if (webkitWebViewBase->priv->acceleratedBackingStore)
1423         webkitWebViewBase->priv->acceleratedBackingStore->update(LayerTreeContext());
1424 #if PLATFORM(X11) && USE(TEXTURE_MAPPER_GL) && !USE(REDIRECTED_XCOMPOSITE_WINDOW)
1425     if (PlatformDisplay::sharedDisplay().type() != PlatformDisplay::Type::X11)
1426         return;
1427
1428     if (!gtk_widget_get_realized(GTK_WIDGET(webkitWebViewBase)))
1429         return;
1430
1431     WebKitWebViewBasePrivate* priv = webkitWebViewBase->priv;
1432     DrawingAreaProxyImpl* drawingArea = static_cast<DrawingAreaProxyImpl*>(priv->pageProxy->drawingArea());
1433     ASSERT(drawingArea);
1434     drawingArea->destroyNativeSurfaceHandleForCompositing();
1435 #endif
1436 }