Upstream version 9.38.198.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 struct InputHostMsg_HandleInputEvent_ACK_Params;
22
23 namespace IPC {
24 class Sender;
25 }
26
27 namespace ui {
28 struct LatencyInfo;
29 }
30
31 namespace content {
32
33 class InputAckHandler;
34 class InputRouterClient;
35 class OverscrollController;
36 struct DidOverscrollParams;
37
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) {
44  public:
45   struct CONTENT_EXPORT Config {
46     Config();
47     GestureEventQueue::Config gesture_config;
48     TouchEventQueue::Config touch_config;
49   };
50
51   InputRouterImpl(IPC::Sender* sender,
52                   InputRouterClient* client,
53                   InputAckHandler* ack_handler,
54                   int routing_id,
55                   const Config& config);
56   virtual ~InputRouterImpl();
57
58   // InputRouter
59   virtual void Flush() OVERRIDE;
60   virtual bool SendInput(scoped_ptr<IPC::Message> message) OVERRIDE;
61   virtual void SendMouseEvent(
62       const MouseEventWithLatencyInfo& mouse_event) OVERRIDE;
63   virtual void SendWheelEvent(
64       const MouseWheelEventWithLatencyInfo& wheel_event) OVERRIDE;
65   virtual void SendKeyboardEvent(
66       const NativeWebKeyboardEvent& key_event,
67       const ui::LatencyInfo& latency_info,
68       bool is_keyboard_shortcut) OVERRIDE;
69   virtual void SendGestureEvent(
70       const GestureEventWithLatencyInfo& gesture_event) OVERRIDE;
71   virtual void SendTouchEvent(
72       const TouchEventWithLatencyInfo& touch_event) OVERRIDE;
73   virtual const NativeWebKeyboardEvent* GetLastKeyboardEvent() const OVERRIDE;
74   virtual bool ShouldForwardTouchEvent() const OVERRIDE;
75   virtual void OnViewUpdated(int view_flags) OVERRIDE;
76   virtual bool HasPendingEvents() const OVERRIDE;
77
78   // IPC::Listener
79   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
80
81 private:
82   friend class InputRouterImplTest;
83
84   // TouchpadTapSuppressionControllerClient
85   virtual void SendMouseEventImmediately(
86       const MouseEventWithLatencyInfo& mouse_event) OVERRIDE;
87
88   // TouchEventQueueClient
89   virtual void SendTouchEventImmediately(
90       const TouchEventWithLatencyInfo& touch_event) OVERRIDE;
91   virtual void OnTouchEventAck(const TouchEventWithLatencyInfo& event,
92                                InputEventAckState ack_result) OVERRIDE;
93
94   // GetureEventFilterClient
95   virtual void SendGestureEventImmediately(
96       const GestureEventWithLatencyInfo& gesture_event) OVERRIDE;
97   virtual void OnGestureEventAck(const GestureEventWithLatencyInfo& event,
98                                  InputEventAckState ack_result) OVERRIDE;
99
100   bool SendMoveCaret(scoped_ptr<IPC::Message> message);
101   bool SendSelectRange(scoped_ptr<IPC::Message> message);
102   bool Send(IPC::Message* message);
103
104   // Filters and forwards |input_event| to the appropriate handler.
105   void FilterAndSendWebInputEvent(const blink::WebInputEvent& input_event,
106                                   const ui::LatencyInfo& latency_info,
107                                   bool is_keyboard_shortcut);
108
109   // Utility routine for filtering and forwarding |input_event| to the
110   // appropriate handler. |input_event| will be offered to the overscroll
111   // controller, client and renderer, in that order.
112   void OfferToHandlers(const blink::WebInputEvent& input_event,
113                        const ui::LatencyInfo& latency_info,
114                        bool is_keyboard_shortcut);
115
116   // Returns true if |input_event| was consumed by the overscroll controller.
117   bool OfferToOverscrollController(const blink::WebInputEvent& input_event,
118                                    const ui::LatencyInfo& latency_info);
119
120   // Returns true if |input_event| was consumed by the client.
121   bool OfferToClient(const blink::WebInputEvent& input_event,
122                      const ui::LatencyInfo& latency_info);
123
124   // Returns true if |input_event| was successfully sent to the renderer
125   // as an async IPC Message.
126   bool OfferToRenderer(const blink::WebInputEvent& input_event,
127                        const ui::LatencyInfo& latency_info,
128                        bool is_keyboard_shortcut);
129
130   // A data structure that attaches some metadata to a WebMouseWheelEvent
131   // and its latency info.
132   struct QueuedWheelEvent {
133     QueuedWheelEvent();
134     QueuedWheelEvent(const MouseWheelEventWithLatencyInfo& event,
135                      bool synthesized_from_pinch);
136     ~QueuedWheelEvent();
137
138     MouseWheelEventWithLatencyInfo event;
139     bool synthesized_from_pinch;
140   };
141
142   // Enqueue or send a mouse wheel event.
143   void SendWheelEvent(const QueuedWheelEvent& wheel_event);
144
145   // Given a Touchpad GesturePinchUpdate event, create and send a synthetic
146   // wheel event for it.
147   void SendSyntheticWheelEventForPinch(
148       const GestureEventWithLatencyInfo& pinch_event);
149
150   // IPC message handlers
151   void OnInputEventAck(const InputHostMsg_HandleInputEvent_ACK_Params& ack);
152   void OnDidOverscroll(const DidOverscrollParams& params);
153   void OnMsgMoveCaretAck();
154   void OnSelectRangeAck();
155   void OnHasTouchEventHandlers(bool has_handlers);
156   void OnSetTouchAction(TouchAction touch_action);
157
158   // Indicates the source of an ack provided to |ProcessInputEventAck()|.
159   // The source is tracked by |current_ack_source_|, which aids in ack routing.
160   enum AckSource {
161     RENDERER,
162     CLIENT,
163     IGNORING_DISPOSITION,
164     ACK_SOURCE_NONE
165   };
166   // Note: This function may result in |this| being deleted, and as such
167   // should be the last method called in any internal chain of event handling.
168   void ProcessInputEventAck(blink::WebInputEvent::Type event_type,
169                             InputEventAckState ack_result,
170                             const ui::LatencyInfo& latency_info,
171                             AckSource ack_source);
172
173   // Dispatches the ack'ed event to |ack_handler_|.
174   void ProcessKeyboardAck(blink::WebInputEvent::Type type,
175                           InputEventAckState ack_result);
176
177   // Forwards a valid |next_mouse_move_| if |type| is MouseMove.
178   void ProcessMouseAck(blink::WebInputEvent::Type type,
179                        InputEventAckState ack_result);
180
181   // Dispatches the ack'ed event to |ack_handler_|, forwarding queued events
182   // from |coalesced_mouse_wheel_events_|.
183   void ProcessWheelAck(InputEventAckState ack_result,
184                        const ui::LatencyInfo& latency);
185
186   // Forwards the event ack to |gesture_event_queue|, potentially triggering
187   // dispatch of queued gesture events.
188   void ProcessGestureAck(blink::WebInputEvent::Type type,
189                          InputEventAckState ack_result,
190                          const ui::LatencyInfo& latency);
191
192   // Forwards the event ack to |touch_event_queue_|, potentially triggering
193   // dispatch of queued touch events, or the creation of gesture events.
194   void ProcessTouchAck(InputEventAckState ack_result,
195                        const ui::LatencyInfo& latency);
196
197   // Called when a touch timeout-affecting bit has changed, in turn toggling the
198   // touch ack timeout feature of the |touch_event_queue_| as appropriate. Input
199   // to that determination includes current view properties and the allowed
200   // touch action. Note that this will only affect platforms that have a
201   // non-zero touch timeout configuration.
202   void UpdateTouchAckTimeoutEnabled();
203
204   // If a flush has been requested, signals a completed flush to the client if
205   // all events have been dispatched (i.e., |HasPendingEvents()| is false).
206   void SignalFlushedIfNecessary();
207
208   bool IsInOverscrollGesture() const;
209
210   int routing_id() const { return routing_id_; }
211
212
213   IPC::Sender* sender_;
214   InputRouterClient* client_;
215   InputAckHandler* ack_handler_;
216   int routing_id_;
217
218   // (Similar to |mouse_move_pending_|.) True while waiting for SelectRange_ACK.
219   bool select_range_pending_;
220
221   // (Similar to |next_mouse_move_|.) The next SelectRange to send, if any.
222   scoped_ptr<IPC::Message> next_selection_range_;
223
224   // (Similar to |mouse_move_pending_|.) True while waiting for MoveCaret_ACK.
225   bool move_caret_pending_;
226
227   // (Similar to |next_mouse_move_|.) The next MoveCaret to send, if any.
228   scoped_ptr<IPC::Message> next_move_caret_;
229
230   // True if a mouse move event was sent to the render view and we are waiting
231   // for a corresponding InputHostMsg_HandleInputEvent_ACK message.
232   bool mouse_move_pending_;
233
234   // The next mouse move event to send (only non-null while mouse_move_pending_
235   // is true).
236   scoped_ptr<MouseEventWithLatencyInfo> next_mouse_move_;
237
238   // (Similar to |mouse_move_pending_|.) True if a mouse wheel event was sent
239   // and we are waiting for a corresponding ack.
240   bool mouse_wheel_pending_;
241   QueuedWheelEvent current_wheel_event_;
242
243   // (Similar to |next_mouse_move_|.) The next mouse wheel events to send.
244   // Unlike mouse moves, mouse wheel events received while one is pending are
245   // coalesced (by accumulating deltas) if they match the previous event in
246   // modifiers. On the Mac, in particular, mouse wheel events are received at a
247   // high rate; not waiting for the ack results in jankiness, and using the same
248   // mechanism as for mouse moves (just dropping old events when multiple ones
249   // would be queued) results in very slow scrolling.
250   typedef std::deque<QueuedWheelEvent> WheelEventQueue;
251   WheelEventQueue coalesced_mouse_wheel_events_;
252
253   // A queue of keyboard events. We can't trust data from the renderer so we
254   // stuff key events into a queue and pop them out on ACK, feeding our copy
255   // back to whatever unhandled handler instead of the returned version.
256   typedef std::deque<NativeWebKeyboardEvent> KeyQueue;
257   KeyQueue key_queue_;
258
259   // The time when an input event was sent to the client.
260   base::TimeTicks input_event_start_time_;
261
262   // Cached flags from |OnViewUpdated()|, defaults to 0.
263   int current_view_flags_;
264
265   // The source of the ack within the scope of |ProcessInputEventAck()|.
266   // Defaults to ACK_SOURCE_NONE.
267   AckSource current_ack_source_;
268
269   // Whether a call to |Flush()| has yet been accompanied by a |DidFlush()| call
270   // to the client_ after all events have been dispatched/acked.
271   bool flush_requested_;
272
273   TouchEventQueue touch_event_queue_;
274   GestureEventQueue gesture_event_queue_;
275   TouchActionFilter touch_action_filter_;
276   InputEventStreamValidator input_stream_validator_;
277   InputEventStreamValidator output_stream_validator_;
278
279   DISALLOW_COPY_AND_ASSIGN(InputRouterImpl);
280 };
281
282 }  // namespace content
283
284 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_INPUT_ROUTER_IMPL_H_