Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / input_router_impl.h
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.
4
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_
7
8 #include <queue>
9
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"
20
21 namespace IPC {
22 class Sender;
23 }
24
25 namespace ui {
26 struct LatencyInfo;
27 }
28
29 namespace content {
30
31 class InputAckHandler;
32 class InputRouterClient;
33 class OverscrollController;
34 class RenderWidgetHostImpl;
35
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) {
42  public:
43   InputRouterImpl(IPC::Sender* sender,
44                   InputRouterClient* client,
45                   InputAckHandler* ack_handler,
46                   int routing_id);
47   virtual ~InputRouterImpl();
48
49   // InputRouter
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;
67
68   // IPC::Listener
69   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
70
71 private:
72   friend class InputRouterImplTest;
73   friend class MockRenderWidgetHost;
74
75   // TouchpadTapSuppressionControllerClient
76   virtual void SendMouseEventImmediately(
77       const MouseEventWithLatencyInfo& mouse_event) OVERRIDE;
78
79   // TouchEventQueueClient
80   virtual void SendTouchEventImmediately(
81       const TouchEventWithLatencyInfo& touch_event) OVERRIDE;
82   virtual void OnTouchEventAck(const TouchEventWithLatencyInfo& event,
83                                InputEventAckState ack_result) OVERRIDE;
84
85   // GetureEventFilterClient
86   virtual void SendGestureEventImmediately(
87       const GestureEventWithLatencyInfo& gesture_event) OVERRIDE;
88   virtual void OnGestureEventAck(const GestureEventWithLatencyInfo& event,
89                                  InputEventAckState ack_result) OVERRIDE;
90
91   bool SendMoveCaret(scoped_ptr<IPC::Message> message);
92   bool SendSelectRange(scoped_ptr<IPC::Message> message);
93   bool Send(IPC::Message* message);
94
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);
99
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);
106
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);
110
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);
114
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);
120
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);
129
130   // Indicates the source of an ack provided to |ProcessInputEventAck()|.
131   // The source is tracked by |current_ack_source_|, which aids in ack routing.
132   enum AckSource {
133     RENDERER,
134     CLIENT,
135     OVERSCROLL_CONTROLLER,
136     IGNORING_DISPOSITION,
137     ACK_SOURCE_NONE
138   };
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);
145
146   // Dispatches the ack'ed event to |ack_handler_|.
147   void ProcessKeyboardAck(blink::WebInputEvent::Type type,
148                           InputEventAckState ack_result);
149
150   // Forwards a valid |next_mouse_move_| if |type| is MouseMove.
151   void ProcessMouseAck(blink::WebInputEvent::Type type,
152                        InputEventAckState ack_result);
153
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);
158
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);
164
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);
169
170   // Forwards |ack_result| to the client's OverscrollController, if necessary.
171   void ProcessAckForOverscroll(const blink::WebInputEvent& event,
172                                InputEventAckState ack_result);
173
174   void SimulateTouchGestureWithMouse(
175       const MouseEventWithLatencyInfo& mouse_event);
176
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();
182
183   bool IsInOverscrollGesture() const;
184
185   int routing_id() const { return routing_id_; }
186
187
188   IPC::Sender* sender_;
189   InputRouterClient* client_;
190   InputAckHandler* ack_handler_;
191   int routing_id_;
192
193   // (Similar to |mouse_move_pending_|.) True while waiting for SelectRange_ACK.
194   bool select_range_pending_;
195
196   // (Similar to |next_mouse_move_|.) The next SelectRange to send, if any.
197   scoped_ptr<IPC::Message> next_selection_range_;
198
199   // (Similar to |mouse_move_pending_|.) True while waiting for MoveCaret_ACK.
200   bool move_caret_pending_;
201
202   // (Similar to |next_mouse_move_|.) The next MoveCaret to send, if any.
203   scoped_ptr<IPC::Message> next_move_caret_;
204
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_;
208
209   // The next mouse move event to send (only non-null while mouse_move_pending_
210   // is true).
211   scoped_ptr<MouseEventWithLatencyInfo> next_mouse_move_;
212
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_;
217
218   typedef std::deque<MouseWheelEventWithLatencyInfo> WheelEventQueue;
219
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_;
228
229   // The time when an input event was sent to the RenderWidget.
230   base::TimeTicks input_event_start_time_;
231
232   // Queue of keyboard events that we need to track.
233   typedef std::deque<NativeWebKeyboardEvent> KeyQueue;
234
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.
238   KeyQueue key_queue_;
239
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_;
243
244   // Cached flags from |OnViewUpdated()|, defaults to 0.
245   int current_view_flags_;
246
247   // The source of the ack within the scope of |ProcessInputEventAck()|.
248   // Defaults to ACK_SOURCE_NONE.
249   AckSource current_ack_source_;
250
251   scoped_ptr<TouchEventQueue> touch_event_queue_;
252   scoped_ptr<GestureEventQueue> gesture_event_queue_;
253   TouchActionFilter touch_action_filter_;
254   InputEventStreamValidator event_stream_validator_;
255
256   DISALLOW_COPY_AND_ASSIGN(InputRouterImpl);
257 };
258
259 }  // namespace content
260
261 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_