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 class RenderWidgetHostImpl;
36 // A default implementation for browser input event routing.
37 class CONTENT_EXPORT InputRouterImpl
38 : public NON_EXPORTED_BASE(InputRouter),
39 public NON_EXPORTED_BASE(GestureEventQueueClient),
40 public NON_EXPORTED_BASE(TouchEventQueueClient),
41 public NON_EXPORTED_BASE(TouchpadTapSuppressionControllerClient) {
43 InputRouterImpl(IPC::Sender* sender,
44 InputRouterClient* client,
45 InputAckHandler* ack_handler,
47 virtual ~InputRouterImpl();
50 virtual void Flush() OVERRIDE;
51 virtual bool SendInput(scoped_ptr<IPC::Message> message) OVERRIDE;
52 virtual void SendMouseEvent(
53 const MouseEventWithLatencyInfo& mouse_event) OVERRIDE;
54 virtual void SendWheelEvent(
55 const MouseWheelEventWithLatencyInfo& wheel_event) OVERRIDE;
56 virtual void SendKeyboardEvent(
57 const NativeWebKeyboardEvent& key_event,
58 const ui::LatencyInfo& latency_info,
59 bool is_keyboard_shortcut) OVERRIDE;
60 virtual void SendGestureEvent(
61 const GestureEventWithLatencyInfo& gesture_event) OVERRIDE;
62 virtual void SendTouchEvent(
63 const TouchEventWithLatencyInfo& touch_event) OVERRIDE;
64 virtual const NativeWebKeyboardEvent* GetLastKeyboardEvent() const OVERRIDE;
65 virtual bool ShouldForwardTouchEvent() const OVERRIDE;
66 virtual void OnViewUpdated(int view_flags) OVERRIDE;
69 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
72 friend class InputRouterImplTest;
73 friend class MockRenderWidgetHost;
75 // TouchpadTapSuppressionControllerClient
76 virtual void SendMouseEventImmediately(
77 const MouseEventWithLatencyInfo& mouse_event) OVERRIDE;
79 // TouchEventQueueClient
80 virtual void SendTouchEventImmediately(
81 const TouchEventWithLatencyInfo& touch_event) OVERRIDE;
82 virtual void OnTouchEventAck(const TouchEventWithLatencyInfo& event,
83 InputEventAckState ack_result) OVERRIDE;
85 // GetureEventFilterClient
86 virtual void SendGestureEventImmediately(
87 const GestureEventWithLatencyInfo& gesture_event) OVERRIDE;
88 virtual void OnGestureEventAck(const GestureEventWithLatencyInfo& event,
89 InputEventAckState ack_result) OVERRIDE;
91 bool SendMoveCaret(scoped_ptr<IPC::Message> message);
92 bool SendSelectRange(scoped_ptr<IPC::Message> message);
93 bool Send(IPC::Message* message);
95 // Filters and forwards |input_event| to the appropriate handler.
96 void FilterAndSendWebInputEvent(const blink::WebInputEvent& input_event,
97 const ui::LatencyInfo& latency_info,
98 bool is_keyboard_shortcut);
100 // Utility routine for filtering and forwarding |input_event| to the
101 // appropriate handler. |input_event| will be offered to the overscroll
102 // controller, client and renderer, in that order.
103 void OfferToHandlers(const blink::WebInputEvent& input_event,
104 const ui::LatencyInfo& latency_info,
105 bool is_keyboard_shortcut);
107 // Returns true if |input_event| was consumed by the overscroll controller.
108 bool OfferToOverscrollController(const blink::WebInputEvent& input_event,
109 const ui::LatencyInfo& latency_info);
111 // Returns true if |input_event| was consumed by the client.
112 bool OfferToClient(const blink::WebInputEvent& input_event,
113 const ui::LatencyInfo& latency_info);
115 // Returns true if |input_event| was successfully sent to the renderer
116 // as an async IPC Message.
117 bool OfferToRenderer(const blink::WebInputEvent& input_event,
118 const ui::LatencyInfo& latency_info,
119 bool is_keyboard_shortcut);
121 // IPC message handlers
122 void OnInputEventAck(blink::WebInputEvent::Type event_type,
123 InputEventAckState ack_result,
124 const ui::LatencyInfo& latency_info);
125 void OnMsgMoveCaretAck();
126 void OnSelectRangeAck();
127 void OnHasTouchEventHandlers(bool has_handlers);
128 void OnSetTouchAction(TouchAction touch_action);
130 // Indicates the source of an ack provided to |ProcessInputEventAck()|.
131 // The source is tracked by |current_ack_source_|, which aids in ack routing.
135 OVERSCROLL_CONTROLLER,
136 IGNORING_DISPOSITION,
139 // Note: This function may result in |this| being deleted, and as such
140 // should be the last method called in any internal chain of event handling.
141 void ProcessInputEventAck(blink::WebInputEvent::Type event_type,
142 InputEventAckState ack_result,
143 const ui::LatencyInfo& latency_info,
144 AckSource ack_source);
146 // Dispatches the ack'ed event to |ack_handler_|.
147 void ProcessKeyboardAck(blink::WebInputEvent::Type type,
148 InputEventAckState ack_result);
150 // Forwards a valid |next_mouse_move_| if |type| is MouseMove.
151 void ProcessMouseAck(blink::WebInputEvent::Type type,
152 InputEventAckState ack_result);
154 // Dispatches the ack'ed event to |ack_handler_|, forwarding queued events
155 // from |coalesced_mouse_wheel_events_|.
156 void ProcessWheelAck(InputEventAckState ack_result,
157 const ui::LatencyInfo& latency);
159 // Forwards the event ack to |gesture_event_queue|, potentially triggering
160 // dispatch of queued gesture events.
161 void ProcessGestureAck(blink::WebInputEvent::Type type,
162 InputEventAckState ack_result,
163 const ui::LatencyInfo& latency);
165 // Forwards the event ack to |touch_event_queue_|, potentially triggering
166 // dispatch of queued touch events, or the creation of gesture events.
167 void ProcessTouchAck(InputEventAckState ack_result,
168 const ui::LatencyInfo& latency);
170 // Forwards |ack_result| to the client's OverscrollController, if necessary.
171 void ProcessAckForOverscroll(const blink::WebInputEvent& event,
172 InputEventAckState ack_result);
174 void SimulateTouchGestureWithMouse(
175 const MouseEventWithLatencyInfo& mouse_event);
177 // Called when a touch timeout-affecting bit has changed, in turn toggling the
178 // touch ack timeout feature of the |touch_event_queue_| as appropriate. Input
179 // to that determination includes current view properties, the allowed touch
180 // action and the command-line configured |touch_ack_timeout_supported_|.
181 void UpdateTouchAckTimeoutEnabled();
183 bool IsInOverscrollGesture() const;
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 bool select_range_pending_;
196 // (Similar to |next_mouse_move_|.) The next SelectRange to send, if any.
197 scoped_ptr<IPC::Message> next_selection_range_;
199 // (Similar to |mouse_move_pending_|.) True while waiting for MoveCaret_ACK.
200 bool move_caret_pending_;
202 // (Similar to |next_mouse_move_|.) The next MoveCaret to send, if any.
203 scoped_ptr<IPC::Message> next_move_caret_;
205 // True if a mouse move event was sent to the render view and we are waiting
206 // for a corresponding InputHostMsg_HandleInputEvent_ACK message.
207 bool mouse_move_pending_;
209 // The next mouse move event to send (only non-null while mouse_move_pending_
211 scoped_ptr<MouseEventWithLatencyInfo> next_mouse_move_;
213 // (Similar to |mouse_move_pending_|.) True if a mouse wheel event was sent
214 // and we are waiting for a corresponding ack.
215 bool mouse_wheel_pending_;
216 MouseWheelEventWithLatencyInfo current_wheel_event_;
218 typedef std::deque<MouseWheelEventWithLatencyInfo> WheelEventQueue;
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 WheelEventQueue coalesced_mouse_wheel_events_;
229 // The time when an input event was sent to the RenderWidget.
230 base::TimeTicks input_event_start_time_;
232 // Queue of keyboard events that we need to track.
233 typedef std::deque<NativeWebKeyboardEvent> KeyQueue;
235 // A queue of keyboard events. We can't trust data from the renderer so we
236 // stuff key events into a queue and pop them out on ACK, feeding our copy
237 // back to whatever unhandled handler instead of the returned version.
240 // Whether touch ack timeout handling has been enabled via the command line.
241 bool touch_ack_timeout_supported_;
242 base::TimeDelta touch_ack_timeout_delay_;
244 // Cached flags from |OnViewUpdated()|, defaults to 0.
245 int current_view_flags_;
247 // The source of the ack within the scope of |ProcessInputEventAck()|.
248 // Defaults to ACK_SOURCE_NONE.
249 AckSource current_ack_source_;
251 scoped_ptr<TouchEventQueue> touch_event_queue_;
252 scoped_ptr<GestureEventQueue> gesture_event_queue_;
253 TouchActionFilter touch_action_filter_;
254 InputEventStreamValidator event_stream_validator_;
256 DISALLOW_COPY_AND_ASSIGN(InputRouterImpl);
259 } // namespace content
261 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_