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