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 #include "content/renderer/input/input_handler_manager.h"
8 #include "base/debug/trace_event.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "cc/input/input_handler.h"
11 #include "content/renderer/input/input_event_filter.h"
12 #include "content/renderer/input/input_handler_manager_client.h"
13 #include "content/renderer/input/input_handler_wrapper.h"
14 #include "content/renderer/scheduler/renderer_scheduler.h"
16 using blink::WebInputEvent
;
22 InputEventAckState
InputEventDispositionToAck(
23 InputHandlerProxy::EventDisposition disposition
) {
24 switch (disposition
) {
25 case InputHandlerProxy::DID_HANDLE
:
26 return INPUT_EVENT_ACK_STATE_CONSUMED
;
27 case InputHandlerProxy::DID_NOT_HANDLE
:
28 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED
;
29 case InputHandlerProxy::DROP_EVENT
:
30 return INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS
;
33 return INPUT_EVENT_ACK_STATE_UNKNOWN
;
38 InputHandlerManager::InputHandlerManager(
39 const scoped_refptr
<base::MessageLoopProxy
>& message_loop_proxy
,
40 InputHandlerManagerClient
* client
,
41 RendererScheduler
* renderer_scheduler
)
42 : message_loop_proxy_(message_loop_proxy
),
44 renderer_scheduler_(renderer_scheduler
) {
46 client_
->SetBoundHandler(base::Bind(&InputHandlerManager::HandleInputEvent
,
47 base::Unretained(this)));
50 InputHandlerManager::~InputHandlerManager() {
51 client_
->SetBoundHandler(InputHandlerManagerClient::Handler());
54 void InputHandlerManager::AddInputHandler(
56 const base::WeakPtr
<cc::InputHandler
>& input_handler
,
57 const base::WeakPtr
<RenderViewImpl
>& render_view_impl
) {
58 if (message_loop_proxy_
->BelongsToCurrentThread()) {
59 AddInputHandlerOnCompositorThread(routing_id
,
60 base::MessageLoopProxy::current(),
64 message_loop_proxy_
->PostTask(
66 base::Bind(&InputHandlerManager::AddInputHandlerOnCompositorThread
,
67 base::Unretained(this),
69 base::MessageLoopProxy::current(),
75 void InputHandlerManager::AddInputHandlerOnCompositorThread(
77 const scoped_refptr
<base::MessageLoopProxy
>& main_loop
,
78 const base::WeakPtr
<cc::InputHandler
>& input_handler
,
79 const base::WeakPtr
<RenderViewImpl
>& render_view_impl
) {
80 DCHECK(message_loop_proxy_
->BelongsToCurrentThread());
82 // The handler could be gone by this point if the compositor has shut down.
86 // The same handler may be registered for a route multiple times.
87 if (input_handlers_
.count(routing_id
) != 0)
91 "InputHandlerManager::AddInputHandlerOnCompositorThread",
92 "result", "AddingRoute");
93 client_
->DidAddInputHandler(routing_id
, input_handler
.get());
94 input_handlers_
.add(routing_id
,
95 make_scoped_ptr(new InputHandlerWrapper(this,
96 routing_id
, main_loop
, input_handler
, render_view_impl
)));
99 void InputHandlerManager::RemoveInputHandler(int routing_id
) {
100 DCHECK(message_loop_proxy_
->BelongsToCurrentThread());
101 DCHECK(input_handlers_
.contains(routing_id
));
103 TRACE_EVENT0("input", "InputHandlerManager::RemoveInputHandler");
105 client_
->DidRemoveInputHandler(routing_id
);
106 input_handlers_
.erase(routing_id
);
109 InputEventAckState
InputHandlerManager::HandleInputEvent(
111 const WebInputEvent
* input_event
,
112 ui::LatencyInfo
* latency_info
) {
113 DCHECK(message_loop_proxy_
->BelongsToCurrentThread());
115 InputHandlerMap::iterator it
= input_handlers_
.find(routing_id
);
116 if (it
== input_handlers_
.end()) {
117 TRACE_EVENT1("input", "InputHandlerManager::HandleInputEvent",
118 "result", "NoInputHandlerFound");
119 // Oops, we no longer have an interested input handler..
120 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED
;
123 InputHandlerProxy
* proxy
= it
->second
->input_handler_proxy();
124 return InputEventDispositionToAck(
125 proxy
->HandleInputEventWithLatencyInfo(*input_event
, latency_info
));
128 void InputHandlerManager::DidOverscroll(int routing_id
,
129 const DidOverscrollParams
& params
) {
130 client_
->DidOverscroll(routing_id
, params
);
133 void InputHandlerManager::DidStopFlinging(int routing_id
) {
134 client_
->DidStopFlinging(routing_id
);
137 void InputHandlerManager::DidReceiveInputEvent(
138 blink::WebInputEvent::Type type
) {
139 renderer_scheduler_
->DidReceiveInputEventOnCompositorThread(type
);
142 void InputHandlerManager::DidAnimateForInput() {
143 renderer_scheduler_
->DidAnimateForInputOnCompositorThread();
146 } // namespace content