1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "third_party/blink/renderer/core/page/page_widget_delegate.h"
32 
33 #include "third_party/blink/public/common/input/web_input_event.h"
34 #include "third_party/blink/renderer/core/accessibility/ax_object_cache.h"
35 #include "third_party/blink/renderer/core/events/web_input_event_conversion.h"
36 #include "third_party/blink/renderer/core/frame/local_frame.h"
37 #include "third_party/blink/renderer/core/frame/local_frame_view.h"
38 #include "third_party/blink/renderer/core/input/event_handler.h"
39 #include "third_party/blink/renderer/core/layout/layout_shift_tracker.h"
40 #include "third_party/blink/renderer/core/layout/layout_view.h"
41 #include "third_party/blink/renderer/core/page/autoscroll_controller.h"
42 #include "third_party/blink/renderer/core/page/page.h"
43 #include "third_party/blink/renderer/core/page/validation_message_client.h"
44 #include "third_party/blink/renderer/platform/runtime_enabled_features.h"
45 
46 namespace blink {
47 
Animate(Page & page,base::TimeTicks monotonic_frame_begin_time)48 void PageWidgetDelegate::Animate(Page& page,
49                                  base::TimeTicks monotonic_frame_begin_time) {
50   page.GetAutoscrollController().Animate();
51   page.Animator().ServiceScriptedAnimations(monotonic_frame_begin_time);
52   // The ValidationMessage overlay manages its own internal Page that isn't
53   // hooked up the normal BeginMainFrame flow, so we manually tick its
54   // animations here.
55   page.GetValidationMessageClient().ServiceScriptedAnimations(
56       monotonic_frame_begin_time);
57 }
58 
PostAnimate(Page & page)59 void PageWidgetDelegate::PostAnimate(Page& page) {
60   page.Animator().PostAnimate();
61 }
62 
UpdateLifecycle(Page & page,LocalFrame & root,WebLifecycleUpdate requested_update,DocumentUpdateReason reason)63 void PageWidgetDelegate::UpdateLifecycle(Page& page,
64                                          LocalFrame& root,
65                                          WebLifecycleUpdate requested_update,
66                                          DocumentUpdateReason reason) {
67   if (requested_update == WebLifecycleUpdate::kLayout) {
68     page.Animator().UpdateLifecycleToLayoutClean(root, reason);
69   } else if (requested_update == WebLifecycleUpdate::kPrePaint) {
70     page.Animator().UpdateAllLifecyclePhasesExceptPaint(root, reason);
71   } else {
72     page.Animator().UpdateAllLifecyclePhases(root, reason);
73   }
74 }
75 
DidBeginFrame(LocalFrame & root)76 void PageWidgetDelegate::DidBeginFrame(LocalFrame& root) {
77   if (LocalFrameView* frame_view = root.View())
78     frame_view->RunPostLifecycleSteps();
79   if (Page* page = root.GetPage())
80     PostAnimate(*page);
81 }
82 
HandleInputEvent(PageWidgetEventHandler & handler,const WebCoalescedInputEvent & coalesced_event,LocalFrame * root)83 WebInputEventResult PageWidgetDelegate::HandleInputEvent(
84     PageWidgetEventHandler& handler,
85     const WebCoalescedInputEvent& coalesced_event,
86     LocalFrame* root) {
87   const WebInputEvent& event = coalesced_event.Event();
88   if (root) {
89     Document* document = root->GetDocument();
90     DCHECK(document);
91     if (LocalFrameView* view = document->View())
92       view->GetLayoutShiftTracker().NotifyInput(event);
93   }
94 
95   if (event.GetModifiers() & WebInputEvent::kIsTouchAccessibility &&
96       WebInputEvent::IsMouseEventType(event.GetType())) {
97     WebMouseEvent mouse_event = TransformWebMouseEvent(
98         root->View(), static_cast<const WebMouseEvent&>(event));
99 
100     HitTestLocation location(root->View()->ConvertFromRootFrame(
101         FlooredIntPoint(mouse_event.PositionInRootFrame())));
102     HitTestResult result = root->GetEventHandler().HitTestResultAtLocation(
103         location, HitTestRequest::kReadOnly | HitTestRequest::kActive);
104     result.SetToShadowHostIfInRestrictedShadowRoot();
105     if (result.InnerNodeFrame()) {
106       Document* document = result.InnerNodeFrame()->GetDocument();
107       if (document) {
108         AXObjectCache* cache = document->ExistingAXObjectCache();
109         if (cache) {
110           cache->OnTouchAccessibilityHover(
111               result.RoundedPointInInnerNodeFrame());
112         }
113       }
114     }
115   }
116 
117   switch (event.GetType()) {
118     // FIXME: WebKit seems to always return false on mouse events processing
119     // methods. For now we'll assume it has processed them (as we are only
120     // interested in whether keyboard events are processed).
121     // FIXME: Why do we return HandleSuppressed when there is no root or
122     // the root is detached?
123     case WebInputEvent::kMouseMove:
124       if (!root || !root->View())
125         return WebInputEventResult::kHandledSuppressed;
126       handler.HandleMouseMove(*root, static_cast<const WebMouseEvent&>(event),
127                               coalesced_event.GetCoalescedEventsPointers(),
128                               coalesced_event.GetPredictedEventsPointers());
129       return WebInputEventResult::kHandledSystem;
130     case WebInputEvent::kMouseLeave:
131       if (!root || !root->View())
132         return WebInputEventResult::kHandledSuppressed;
133       handler.HandleMouseLeave(*root, static_cast<const WebMouseEvent&>(event));
134       return WebInputEventResult::kHandledSystem;
135     case WebInputEvent::kMouseDown:
136       if (!root || !root->View())
137         return WebInputEventResult::kHandledSuppressed;
138       handler.HandleMouseDown(*root, static_cast<const WebMouseEvent&>(event));
139       return WebInputEventResult::kHandledSystem;
140     case WebInputEvent::kMouseUp:
141       if (!root || !root->View())
142         return WebInputEventResult::kHandledSuppressed;
143       return handler.HandleMouseUp(*root,
144                                    static_cast<const WebMouseEvent&>(event));
145     case WebInputEvent::kMouseWheel:
146       if (!root || !root->View())
147         return WebInputEventResult::kNotHandled;
148       return handler.HandleMouseWheel(
149           *root, static_cast<const WebMouseWheelEvent&>(event));
150 
151     case WebInputEvent::kRawKeyDown:
152     case WebInputEvent::kKeyDown:
153     case WebInputEvent::kKeyUp:
154       return handler.HandleKeyEvent(
155           static_cast<const WebKeyboardEvent&>(event));
156 
157     case WebInputEvent::kChar:
158       return handler.HandleCharEvent(
159           static_cast<const WebKeyboardEvent&>(event));
160     case WebInputEvent::kGestureScrollBegin:
161     case WebInputEvent::kGestureScrollEnd:
162     case WebInputEvent::kGestureScrollUpdate:
163     case WebInputEvent::kGestureFlingStart:
164     case WebInputEvent::kGestureFlingCancel:
165     case WebInputEvent::kGestureTap:
166     case WebInputEvent::kGestureTapUnconfirmed:
167     case WebInputEvent::kGestureTapDown:
168     case WebInputEvent::kGestureShowPress:
169     case WebInputEvent::kGestureTapCancel:
170     case WebInputEvent::kGestureDoubleTap:
171     case WebInputEvent::kGestureTwoFingerTap:
172     case WebInputEvent::kGestureLongPress:
173     case WebInputEvent::kGestureLongTap:
174       return handler.HandleGestureEvent(
175           static_cast<const WebGestureEvent&>(event));
176 
177     case WebInputEvent::kPointerDown:
178     case WebInputEvent::kPointerUp:
179     case WebInputEvent::kPointerMove:
180     case WebInputEvent::kPointerRawUpdate:
181     case WebInputEvent::kPointerCancel:
182     case WebInputEvent::kPointerCausedUaAction:
183       if (!root || !root->View())
184         return WebInputEventResult::kNotHandled;
185       return handler.HandlePointerEvent(
186           *root, static_cast<const WebPointerEvent&>(event),
187           coalesced_event.GetCoalescedEventsPointers(),
188           coalesced_event.GetPredictedEventsPointers());
189 
190     case WebInputEvent::kTouchStart:
191     case WebInputEvent::kTouchMove:
192     case WebInputEvent::kTouchEnd:
193     case WebInputEvent::kTouchCancel:
194     case WebInputEvent::kTouchScrollStarted:
195       NOTREACHED();
196       return WebInputEventResult::kNotHandled;
197 
198     case WebInputEvent::kGesturePinchBegin:
199       // Gesture pinch events are handled entirely on the compositor.
200       DLOG(INFO) << "Gesture pinch ignored by main thread.";
201       FALLTHROUGH;
202     case WebInputEvent::kGesturePinchEnd:
203     case WebInputEvent::kGesturePinchUpdate:
204       return WebInputEventResult::kNotHandled;
205     default:
206       return WebInputEventResult::kNotHandled;
207   }
208 }
209 
210 // ----------------------------------------------------------------
211 // Default handlers for PageWidgetEventHandler
212 
HandleMouseMove(LocalFrame & main_frame,const WebMouseEvent & event,const WebVector<const WebInputEvent * > & coalesced_events,const WebVector<const WebInputEvent * > & predicted_events)213 void PageWidgetEventHandler::HandleMouseMove(
214     LocalFrame& main_frame,
215     const WebMouseEvent& event,
216     const WebVector<const WebInputEvent*>& coalesced_events,
217     const WebVector<const WebInputEvent*>& predicted_events) {
218   WebMouseEvent transformed_event =
219       TransformWebMouseEvent(main_frame.View(), event);
220   main_frame.GetEventHandler().HandleMouseMoveEvent(
221       transformed_event,
222       TransformWebMouseEventVector(main_frame.View(), coalesced_events),
223       TransformWebMouseEventVector(main_frame.View(), predicted_events));
224 }
225 
HandleMouseLeave(LocalFrame & main_frame,const WebMouseEvent & event)226 void PageWidgetEventHandler::HandleMouseLeave(LocalFrame& main_frame,
227                                               const WebMouseEvent& event) {
228   WebMouseEvent transformed_event =
229       TransformWebMouseEvent(main_frame.View(), event);
230   main_frame.GetEventHandler().HandleMouseLeaveEvent(transformed_event);
231 }
232 
HandleMouseDown(LocalFrame & main_frame,const WebMouseEvent & event)233 void PageWidgetEventHandler::HandleMouseDown(LocalFrame& main_frame,
234                                              const WebMouseEvent& event) {
235   WebMouseEvent transformed_event =
236       TransformWebMouseEvent(main_frame.View(), event);
237   main_frame.GetEventHandler().HandleMousePressEvent(transformed_event);
238 }
239 
HandleMouseUp(LocalFrame & main_frame,const WebMouseEvent & event)240 WebInputEventResult PageWidgetEventHandler::HandleMouseUp(
241     LocalFrame& main_frame,
242     const WebMouseEvent& event) {
243   WebMouseEvent transformed_event =
244       TransformWebMouseEvent(main_frame.View(), event);
245   return main_frame.GetEventHandler().HandleMouseReleaseEvent(
246       transformed_event);
247 }
248 
HandleMouseWheel(LocalFrame & frame,const WebMouseWheelEvent & event)249 WebInputEventResult PageWidgetEventHandler::HandleMouseWheel(
250     LocalFrame& frame,
251     const WebMouseWheelEvent& event) {
252   WebMouseWheelEvent transformed_event =
253       TransformWebMouseWheelEvent(frame.View(), event);
254   return frame.GetEventHandler().HandleWheelEvent(transformed_event);
255 }
256 
HandlePointerEvent(LocalFrame & main_frame,const WebPointerEvent & event,const WebVector<const WebInputEvent * > & coalesced_events,const WebVector<const WebInputEvent * > & predicted_events)257 WebInputEventResult PageWidgetEventHandler::HandlePointerEvent(
258     LocalFrame& main_frame,
259     const WebPointerEvent& event,
260     const WebVector<const WebInputEvent*>& coalesced_events,
261     const WebVector<const WebInputEvent*>& predicted_events) {
262   WebPointerEvent transformed_event =
263       TransformWebPointerEvent(main_frame.View(), event);
264   return main_frame.GetEventHandler().HandlePointerEvent(
265       transformed_event,
266       TransformWebPointerEventVector(main_frame.View(), coalesced_events),
267       TransformWebPointerEventVector(main_frame.View(), predicted_events));
268 }
269 
270 }  // namespace blink
271