1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_WIDGET_INPUT_WIDGET_BASE_INPUT_HANDLER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_WIDGET_INPUT_WIDGET_BASE_INPUT_HANDLER_H_
7 
8 #include <memory>
9 
10 #include "base/memory/weak_ptr.h"
11 #include "base/optional.h"
12 #include "third_party/blink/public/common/input/web_coalesced_input_event.h"
13 #include "third_party/blink/public/common/input/web_gesture_event.h"
14 #include "third_party/blink/public/mojom/input/input_event_result.mojom-blink.h"
15 #include "third_party/blink/public/platform/input/input_handler_proxy.h"
16 #include "third_party/blink/public/platform/web_input_event_result.h"
17 #include "third_party/blink/public/platform/web_touch_action.h"
18 #include "third_party/blink/renderer/platform/platform_export.h"
19 #include "ui/base/cursor/cursor.h"
20 #include "ui/events/types/scroll_types.h"
21 
22 namespace cc {
23 struct ElementId;
24 class EventMetrics;
25 struct OverscrollBehavior;
26 }  // namespace cc
27 
28 namespace ui {
29 class LatencyInfo;
30 }
31 
32 namespace viz {
33 class FrameSinkId;
34 }
35 
36 namespace blink {
37 
38 class WidgetBase;
39 
40 class PLATFORM_EXPORT WidgetBaseInputHandler {
41  public:
42   WidgetBaseInputHandler(WidgetBase* widget);
43   WidgetBaseInputHandler(const WidgetBaseInputHandler&) = delete;
44   WidgetBaseInputHandler& operator=(const WidgetBaseInputHandler&) = delete;
45 
46   // Hit test the given point to find out the frame underneath and
47   // returns the FrameSinkId for that frame. |local_point| returns the point
48   // in the coordinate space of the FrameSinkId that was hit.
49   viz::FrameSinkId GetFrameSinkIdAtPoint(const gfx::PointF& point,
50                                          gfx::PointF* local_point);
51 
52   using HandledEventCallback = base::OnceCallback<void(
53       mojom::InputEventResultState ack_state,
54       const ui::LatencyInfo& latency_info,
55       std::unique_ptr<InputHandlerProxy::DidOverscrollParams>,
56       base::Optional<WebTouchAction>)>;
57 
58   // Handle input events from the input event provider. `metrics` contains
59   // information used in reporting latency metrics in case the event causes
60   // any updates. `callback` will be called when the event is handled.
61   void HandleInputEvent(const blink::WebCoalescedInputEvent& coalesced_event,
62                         std::unique_ptr<cc::EventMetrics> metrics,
63                         HandledEventCallback callback);
64 
65   // Handle overscroll from Blink. Returns whether the should be sent to the
66   // browser. This will return false if an event is currently being processed
67   // and will be returned part of the input ack.
68   bool DidOverscrollFromBlink(const gfx::Vector2dF& overscrollDelta,
69                               const gfx::Vector2dF& accumulatedOverscroll,
70                               const gfx::PointF& position,
71                               const gfx::Vector2dF& velocity,
72                               const cc::OverscrollBehavior& behavior);
73 
74   void InjectGestureScrollEvent(blink::WebGestureDevice device,
75                                 const gfx::Vector2dF& delta,
76                                 ui::ScrollGranularity granularity,
77                                 cc::ElementId scrollable_area_element_id,
78                                 blink::WebInputEvent::Type injected_type);
79 
handling_input_event()80   bool handling_input_event() const { return handling_input_event_; }
set_handling_input_event(bool handling_input_event)81   void set_handling_input_event(bool handling_input_event) {
82     handling_input_event_ = handling_input_event;
83   }
84 
85   // Process the touch action, returning whether the action should be relayed
86   // to the browser.
87   bool ProcessTouchAction(WebTouchAction touch_action);
88 
89   // Process the new cursor and returns true if it has changed from the last
90   // cursor.
91   bool DidChangeCursor(const ui::Cursor& cursor);
92 
93  private:
94   class HandlingState;
95   struct InjectScrollGestureParams {
96     WebGestureDevice device;
97     gfx::Vector2dF scroll_delta;
98     ui::ScrollGranularity granularity;
99     cc::ElementId scrollable_area_element_id;
100     blink::WebInputEvent::Type type;
101   };
102 
103   WebInputEventResult HandleTouchEvent(
104       const WebCoalescedInputEvent& coalesced_event);
105 
106   // Creates and handles scroll gestures based on parameters from
107   // `injected_scroll_params`. `input_event`, `original_latency_info`, and
108   // `original_metrics_timestamp` are the original event causing gesture
109   // scrolls, its latency info, and its metrics timestamp, respectively, used in
110   // generating new gestures along with their latency and metrics info.
111   void HandleInjectedScrollGestures(
112       std::vector<InjectScrollGestureParams> injected_scroll_params,
113       const WebInputEvent& input_event,
114       const ui::LatencyInfo& original_latency_info,
115       base::Optional<base::TimeTicks> original_metrics_timestamp);
116 
117   WidgetBase* widget_;
118 
119   // Are we currently handling an input event?
120   bool handling_input_event_ = false;
121 
122   // Current state from HandleInputEvent. This variable is stack allocated
123   // and is not owned.
124   HandlingState* handling_input_state_ = nullptr;
125 
126   // We store the current cursor object so we can avoid spamming SetCursor
127   // messages.
128   base::Optional<ui::Cursor> current_cursor_;
129 
130   // Indicates if the next sequence of Char events should be suppressed or not.
131   bool suppress_next_char_events_ = false;
132 
133   // Whether the last injected scroll gesture was a GestureScrollBegin. Used to
134   // determine which GestureScrollUpdate is the first in a gesture sequence for
135   // latency classification.
136   bool last_injected_gesture_was_begin_ = false;
137 
138   const bool supports_buffered_touch_ = false;
139 
140   base::WeakPtrFactory<WidgetBaseInputHandler> weak_ptr_factory_{this};
141 };
142 
143 }  // namespace blink
144 
145 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_WIDGET_INPUT_WIDGET_BASE_INPUT_HANDLER_H_
146