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"
31 class InputAckHandler
;
32 class InputRouterClient
;
33 class OverscrollController
;
34 struct DidOverscrollParams
;
37 // A default implementation for browser input event routing.
38 class CONTENT_EXPORT InputRouterImpl
39 : public NON_EXPORTED_BASE(InputRouter
),
40 public NON_EXPORTED_BASE(GestureEventQueueClient
),
41 public NON_EXPORTED_BASE(TouchEventQueueClient
),
42 public NON_EXPORTED_BASE(TouchpadTapSuppressionControllerClient
) {
44 struct CONTENT_EXPORT Config
{
46 GestureEventQueue::Config gesture_config
;
47 TouchEventQueue::Config touch_config
;
50 InputRouterImpl(IPC::Sender
* sender
,
51 InputRouterClient
* client
,
52 InputAckHandler
* ack_handler
,
54 const Config
& config
);
55 ~InputRouterImpl() override
;
58 bool SendInput(scoped_ptr
<IPC::Message
> message
) override
;
59 void SendMouseEvent(const MouseEventWithLatencyInfo
& mouse_event
) override
;
61 const MouseWheelEventWithLatencyInfo
& wheel_event
) override
;
62 void SendKeyboardEvent(const NativeWebKeyboardEvent
& key_event
,
63 const ui::LatencyInfo
& latency_info
,
64 bool is_keyboard_shortcut
) override
;
65 void SendGestureEvent(
66 const GestureEventWithLatencyInfo
& gesture_event
) override
;
67 void SendTouchEvent(const TouchEventWithLatencyInfo
& touch_event
) override
;
68 const NativeWebKeyboardEvent
* GetLastKeyboardEvent() const override
;
69 void NotifySiteIsMobileOptimized(bool is_mobile_optimized
) override
;
70 void RequestNotificationWhenFlushed() override
;
71 bool HasPendingEvents() const override
;
74 bool OnMessageReceived(const IPC::Message
& message
) override
;
77 friend class InputRouterImplTest
;
79 // TouchpadTapSuppressionControllerClient
80 void SendMouseEventImmediately(
81 const MouseEventWithLatencyInfo
& mouse_event
) override
;
83 // TouchEventQueueClient
84 void SendTouchEventImmediately(
85 const TouchEventWithLatencyInfo
& touch_event
) override
;
86 void OnTouchEventAck(const TouchEventWithLatencyInfo
& event
,
87 InputEventAckState ack_result
) override
;
89 // GetureEventFilterClient
90 void SendGestureEventImmediately(
91 const GestureEventWithLatencyInfo
& gesture_event
) override
;
92 void OnGestureEventAck(const GestureEventWithLatencyInfo
& event
,
93 InputEventAckState ack_result
) override
;
95 bool SendMoveCaret(scoped_ptr
<IPC::Message
> message
);
96 bool SendSelectMessage(scoped_ptr
<IPC::Message
> message
);
97 bool Send(IPC::Message
* message
);
99 // Filters and forwards |input_event| to the appropriate handler.
100 void FilterAndSendWebInputEvent(const blink::WebInputEvent
& input_event
,
101 const ui::LatencyInfo
& latency_info
,
102 bool is_keyboard_shortcut
);
104 // Utility routine for filtering and forwarding |input_event| to the
105 // appropriate handler. |input_event| will be offered to the overscroll
106 // controller, client and renderer, in that order.
107 void OfferToHandlers(const blink::WebInputEvent
& input_event
,
108 const ui::LatencyInfo
& latency_info
,
109 bool is_keyboard_shortcut
);
111 // Returns true if |input_event| was consumed by the overscroll controller.
112 bool OfferToOverscrollController(const blink::WebInputEvent
& input_event
,
113 const ui::LatencyInfo
& latency_info
);
115 // Returns true if |input_event| was consumed by the client.
116 bool OfferToClient(const blink::WebInputEvent
& input_event
,
117 const ui::LatencyInfo
& latency_info
);
119 // Returns true if |input_event| was successfully sent to the renderer
120 // as an async IPC Message.
121 bool OfferToRenderer(const blink::WebInputEvent
& input_event
,
122 const ui::LatencyInfo
& latency_info
,
123 bool is_keyboard_shortcut
);
125 // IPC message handlers
126 void OnInputEventAck(const InputEventAck
& ack
);
127 void OnDidOverscroll(const DidOverscrollParams
& params
);
128 void OnMsgMoveCaretAck();
129 void OnSelectMessageAck();
130 void OnHasTouchEventHandlers(bool has_handlers
);
131 void OnSetTouchAction(TouchAction touch_action
);
132 void OnDidStopFlinging();
134 // Indicates the source of an ack provided to |ProcessInputEventAck()|.
135 // The source is tracked by |current_ack_source_|, which aids in ack routing.
139 IGNORING_DISPOSITION
,
142 // Note: This function may result in |this| being deleted, and as such
143 // should be the last method called in any internal chain of event handling.
144 void ProcessInputEventAck(blink::WebInputEvent::Type event_type
,
145 InputEventAckState ack_result
,
146 const ui::LatencyInfo
& latency_info
,
147 uint32 unique_touch_event_id
,
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
,
173 uint32 unique_touch_event_id
);
175 // Called when a touch timeout-affecting bit has changed, in turn toggling the
176 // touch ack timeout feature of the |touch_event_queue_| as appropriate. Input
177 // to that determination includes current view properties and the allowed
178 // touch action. Note that this will only affect platforms that have a
179 // non-zero touch timeout configuration.
180 void UpdateTouchAckTimeoutEnabled();
182 // If a flush has been requested, signals a completed flush to the client if
183 // all events have been dispatched (i.e., |HasPendingEvents()| is false).
184 void SignalFlushedIfNecessary();
186 int routing_id() const { return routing_id_
; }
189 IPC::Sender
* sender_
;
190 InputRouterClient
* client_
;
191 InputAckHandler
* ack_handler_
;
194 // (Similar to |mouse_move_pending_|.) True while waiting for SelectRange_ACK
195 // or MoveRangeSelectionExtent_ACK.
196 bool select_message_pending_
;
198 // Queue of pending select messages to send after receving the next select
200 std::deque
<IPC::Message
*> pending_select_messages_
;
202 // (Similar to |mouse_move_pending_|.) True while waiting for MoveCaret_ACK.
203 bool move_caret_pending_
;
205 // (Similar to |next_mouse_move_|.) The next MoveCaret to send, if any.
206 scoped_ptr
<IPC::Message
> next_move_caret_
;
208 // True if a mouse move event was sent to the render view and we are waiting
209 // for a corresponding InputHostMsg_HandleInputEvent_ACK message.
210 bool mouse_move_pending_
;
212 // The next mouse move event to send (only non-null while mouse_move_pending_
214 scoped_ptr
<MouseEventWithLatencyInfo
> next_mouse_move_
;
216 // (Similar to |mouse_move_pending_|.) True if a mouse wheel event was sent
217 // and we are waiting for a corresponding ack.
218 bool mouse_wheel_pending_
;
219 MouseWheelEventWithLatencyInfo current_wheel_event_
;
221 // (Similar to |next_mouse_move_|.) The next mouse wheel events to send.
222 // Unlike mouse moves, mouse wheel events received while one is pending are
223 // coalesced (by accumulating deltas) if they match the previous event in
224 // modifiers. On the Mac, in particular, mouse wheel events are received at a
225 // high rate; not waiting for the ack results in jankiness, and using the same
226 // mechanism as for mouse moves (just dropping old events when multiple ones
227 // would be queued) results in very slow scrolling.
228 typedef std::deque
<MouseWheelEventWithLatencyInfo
> WheelEventQueue
;
229 WheelEventQueue coalesced_mouse_wheel_events_
;
231 // A queue of keyboard events. We can't trust data from the renderer so we
232 // stuff key events into a queue and pop them out on ACK, feeding our copy
233 // back to whatever unhandled handler instead of the returned version.
234 typedef std::deque
<NativeWebKeyboardEvent
> KeyQueue
;
237 // The time when an input event was sent to the client.
238 base::TimeTicks input_event_start_time_
;
240 // The source of the ack within the scope of |ProcessInputEventAck()|.
241 // Defaults to ACK_SOURCE_NONE.
242 AckSource current_ack_source_
;
244 // Whether a call to |Flush()| has yet been accompanied by a |DidFlush()| call
245 // to the client_ after all events have been dispatched/acked.
246 bool flush_requested_
;
248 // Whether there are any active flings in the renderer. As the fling
249 // end notification is asynchronous, we use a count rather than a boolean
250 // to avoid races in bookkeeping when starting a new fling.
251 int active_renderer_fling_count_
;
253 TouchEventQueue touch_event_queue_
;
254 GestureEventQueue gesture_event_queue_
;
255 TouchActionFilter touch_action_filter_
;
256 InputEventStreamValidator input_stream_validator_
;
257 InputEventStreamValidator output_stream_validator_
;
259 DISALLOW_COPY_AND_ASSIGN(InputRouterImpl
);
262 } // namespace content
264 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_