1 // Copyright 2013 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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "content/browser/renderer_host/input/gesture_event_queue.h"
14 #include "content/browser/renderer_host/input/input_router.h"
15 #include "content/browser/renderer_host/input/touch_action_filter.h"
16 #include "content/browser/renderer_host/input/touch_event_queue.h"
17 #include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h"
18 #include "content/common/input/input_event_stream_validator.h"
19 #include "content/public/browser/native_web_keyboard_event.h"
21 struct InputHostMsg_HandleInputEvent_ACK_Params
;
33 class InputAckHandler
;
34 class InputRouterClient
;
35 class OverscrollController
;
36 struct DidOverscrollParams
;
38 // A default implementation for browser input event routing.
39 class CONTENT_EXPORT InputRouterImpl
40 : public NON_EXPORTED_BASE(InputRouter
),
41 public NON_EXPORTED_BASE(GestureEventQueueClient
),
42 public NON_EXPORTED_BASE(TouchEventQueueClient
),
43 public NON_EXPORTED_BASE(TouchpadTapSuppressionControllerClient
) {
45 struct CONTENT_EXPORT Config
{
47 GestureEventQueue::Config gesture_config
;
48 TouchEventQueue::Config touch_config
;
51 InputRouterImpl(IPC::Sender
* sender
,
52 InputRouterClient
* client
,
53 InputAckHandler
* ack_handler
,
55 const Config
& config
);
56 ~InputRouterImpl() override
;
59 bool SendInput(scoped_ptr
<IPC::Message
> message
) override
;
60 void SendMouseEvent(const MouseEventWithLatencyInfo
& mouse_event
) override
;
62 const MouseWheelEventWithLatencyInfo
& wheel_event
) override
;
63 void SendKeyboardEvent(const NativeWebKeyboardEvent
& key_event
,
64 const ui::LatencyInfo
& latency_info
,
65 bool is_keyboard_shortcut
) override
;
66 void SendGestureEvent(
67 const GestureEventWithLatencyInfo
& gesture_event
) override
;
68 void SendTouchEvent(const TouchEventWithLatencyInfo
& touch_event
) override
;
69 const NativeWebKeyboardEvent
* GetLastKeyboardEvent() const override
;
70 void OnViewUpdated(int view_flags
) override
;
71 void RequestNotificationWhenFlushed() override
;
72 bool HasPendingEvents() const override
;
75 bool OnMessageReceived(const IPC::Message
& message
) override
;
78 friend class InputRouterImplTest
;
80 // TouchpadTapSuppressionControllerClient
81 void SendMouseEventImmediately(
82 const MouseEventWithLatencyInfo
& mouse_event
) override
;
84 // TouchEventQueueClient
85 void SendTouchEventImmediately(
86 const TouchEventWithLatencyInfo
& touch_event
) override
;
87 void OnTouchEventAck(const TouchEventWithLatencyInfo
& event
,
88 InputEventAckState ack_result
) override
;
90 // GetureEventFilterClient
91 void SendGestureEventImmediately(
92 const GestureEventWithLatencyInfo
& gesture_event
) override
;
93 void OnGestureEventAck(const GestureEventWithLatencyInfo
& event
,
94 InputEventAckState ack_result
) override
;
96 bool SendMoveCaret(scoped_ptr
<IPC::Message
> message
);
97 bool SendSelectMessage(scoped_ptr
<IPC::Message
> message
);
98 bool Send(IPC::Message
* message
);
100 // Filters and forwards |input_event| to the appropriate handler.
101 void FilterAndSendWebInputEvent(const blink::WebInputEvent
& input_event
,
102 const ui::LatencyInfo
& latency_info
,
103 bool is_keyboard_shortcut
);
105 // Utility routine for filtering and forwarding |input_event| to the
106 // appropriate handler. |input_event| will be offered to the overscroll
107 // controller, client and renderer, in that order.
108 void OfferToHandlers(const blink::WebInputEvent
& input_event
,
109 const ui::LatencyInfo
& latency_info
,
110 bool is_keyboard_shortcut
);
112 // Returns true if |input_event| was consumed by the overscroll controller.
113 bool OfferToOverscrollController(const blink::WebInputEvent
& input_event
,
114 const ui::LatencyInfo
& latency_info
);
116 // Returns true if |input_event| was consumed by the client.
117 bool OfferToClient(const blink::WebInputEvent
& input_event
,
118 const ui::LatencyInfo
& latency_info
);
120 // Returns true if |input_event| was successfully sent to the renderer
121 // as an async IPC Message.
122 bool OfferToRenderer(const blink::WebInputEvent
& input_event
,
123 const ui::LatencyInfo
& latency_info
,
124 bool is_keyboard_shortcut
);
126 // IPC message handlers
127 void OnInputEventAck(const InputHostMsg_HandleInputEvent_ACK_Params
& ack
);
128 void OnDidOverscroll(const DidOverscrollParams
& params
);
129 void OnMsgMoveCaretAck();
130 void OnSelectMessageAck();
131 void OnHasTouchEventHandlers(bool has_handlers
);
132 void OnSetTouchAction(TouchAction touch_action
);
133 void OnDidStopFlinging();
135 // Indicates the source of an ack provided to |ProcessInputEventAck()|.
136 // The source is tracked by |current_ack_source_|, which aids in ack routing.
140 IGNORING_DISPOSITION
,
143 // Note: This function may result in |this| being deleted, and as such
144 // should be the last method called in any internal chain of event handling.
145 void ProcessInputEventAck(blink::WebInputEvent::Type event_type
,
146 InputEventAckState ack_result
,
147 const ui::LatencyInfo
& latency_info
,
148 AckSource ack_source
);
150 // Dispatches the ack'ed event to |ack_handler_|.
151 void ProcessKeyboardAck(blink::WebInputEvent::Type type
,
152 InputEventAckState ack_result
);
154 // Forwards a valid |next_mouse_move_| if |type| is MouseMove.
155 void ProcessMouseAck(blink::WebInputEvent::Type type
,
156 InputEventAckState ack_result
);
158 // Dispatches the ack'ed event to |ack_handler_|, forwarding queued events
159 // from |coalesced_mouse_wheel_events_|.
160 void ProcessWheelAck(InputEventAckState ack_result
,
161 const ui::LatencyInfo
& latency
);
163 // Forwards the event ack to |gesture_event_queue|, potentially triggering
164 // dispatch of queued gesture events.
165 void ProcessGestureAck(blink::WebInputEvent::Type type
,
166 InputEventAckState ack_result
,
167 const ui::LatencyInfo
& latency
);
169 // Forwards the event ack to |touch_event_queue_|, potentially triggering
170 // dispatch of queued touch events, or the creation of gesture events.
171 void ProcessTouchAck(InputEventAckState ack_result
,
172 const ui::LatencyInfo
& latency
);
174 // Called when a touch timeout-affecting bit has changed, in turn toggling the
175 // touch ack timeout feature of the |touch_event_queue_| as appropriate. Input
176 // to that determination includes current view properties and the allowed
177 // touch action. Note that this will only affect platforms that have a
178 // non-zero touch timeout configuration.
179 void UpdateTouchAckTimeoutEnabled();
181 // If a flush has been requested, signals a completed flush to the client if
182 // all events have been dispatched (i.e., |HasPendingEvents()| is false).
183 void SignalFlushedIfNecessary();
185 int routing_id() const { return routing_id_
; }
188 IPC::Sender
* sender_
;
189 InputRouterClient
* client_
;
190 InputAckHandler
* ack_handler_
;
193 // (Similar to |mouse_move_pending_|.) True while waiting for SelectRange_ACK
194 // or MoveRangeSelectionExtent_ACK.
195 bool select_message_pending_
;
197 // Queue of pending select messages to send after receving the next select
199 std::deque
<IPC::Message
*> pending_select_messages_
;
201 // (Similar to |mouse_move_pending_|.) True while waiting for MoveCaret_ACK.
202 bool move_caret_pending_
;
204 // (Similar to |next_mouse_move_|.) The next MoveCaret to send, if any.
205 scoped_ptr
<IPC::Message
> next_move_caret_
;
207 // True if a mouse move event was sent to the render view and we are waiting
208 // for a corresponding InputHostMsg_HandleInputEvent_ACK message.
209 bool mouse_move_pending_
;
211 // The next mouse move event to send (only non-null while mouse_move_pending_
213 scoped_ptr
<MouseEventWithLatencyInfo
> next_mouse_move_
;
215 // (Similar to |mouse_move_pending_|.) True if a mouse wheel event was sent
216 // and we are waiting for a corresponding ack.
217 bool mouse_wheel_pending_
;
218 MouseWheelEventWithLatencyInfo current_wheel_event_
;
220 // (Similar to |next_mouse_move_|.) The next mouse wheel events to send.
221 // Unlike mouse moves, mouse wheel events received while one is pending are
222 // coalesced (by accumulating deltas) if they match the previous event in
223 // modifiers. On the Mac, in particular, mouse wheel events are received at a
224 // high rate; not waiting for the ack results in jankiness, and using the same
225 // mechanism as for mouse moves (just dropping old events when multiple ones
226 // would be queued) results in very slow scrolling.
227 typedef std::deque
<MouseWheelEventWithLatencyInfo
> WheelEventQueue
;
228 WheelEventQueue coalesced_mouse_wheel_events_
;
230 // A queue of keyboard events. We can't trust data from the renderer so we
231 // stuff key events into a queue and pop them out on ACK, feeding our copy
232 // back to whatever unhandled handler instead of the returned version.
233 typedef std::deque
<NativeWebKeyboardEvent
> KeyQueue
;
236 // The time when an input event was sent to the client.
237 base::TimeTicks input_event_start_time_
;
239 // Cached flags from |OnViewUpdated()|, defaults to 0.
240 int current_view_flags_
;
242 // The source of the ack within the scope of |ProcessInputEventAck()|.
243 // Defaults to ACK_SOURCE_NONE.
244 AckSource current_ack_source_
;
246 // Whether a call to |Flush()| has yet been accompanied by a |DidFlush()| call
247 // to the client_ after all events have been dispatched/acked.
248 bool flush_requested_
;
250 TouchEventQueue touch_event_queue_
;
251 GestureEventQueue gesture_event_queue_
;
252 TouchActionFilter touch_action_filter_
;
253 InputEventStreamValidator input_stream_validator_
;
254 InputEventStreamValidator output_stream_validator_
;
256 DISALLOW_COPY_AND_ASSIGN(InputRouterImpl
);
259 } // namespace content
261 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_