Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / input / input_handler_manager.cc
blob98df2661446b27a11bbcf80eb2cc4b11519d08b8
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"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/trace_event/trace_event.h"
12 #include "cc/input/input_handler.h"
13 #include "components/scheduler/renderer/renderer_scheduler.h"
14 #include "content/renderer/input/input_event_filter.h"
15 #include "content/renderer/input/input_handler_manager_client.h"
16 #include "content/renderer/input/input_handler_wrapper.h"
17 #include "content/renderer/input/input_scroll_elasticity_controller.h"
19 using blink::WebInputEvent;
20 using scheduler::RendererScheduler;
22 namespace content {
24 namespace {
26 InputEventAckState InputEventDispositionToAck(
27 InputHandlerProxy::EventDisposition disposition) {
28 switch (disposition) {
29 case InputHandlerProxy::DID_HANDLE:
30 return INPUT_EVENT_ACK_STATE_CONSUMED;
31 case InputHandlerProxy::DID_NOT_HANDLE:
32 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
33 case InputHandlerProxy::DROP_EVENT:
34 return INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
36 NOTREACHED();
37 return INPUT_EVENT_ACK_STATE_UNKNOWN;
40 } // namespace
42 InputHandlerManager::InputHandlerManager(
43 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
44 InputHandlerManagerClient* client,
45 scheduler::RendererScheduler* renderer_scheduler)
46 : task_runner_(task_runner),
47 client_(client),
48 renderer_scheduler_(renderer_scheduler) {
49 DCHECK(client_);
50 client_->SetBoundHandler(base::Bind(&InputHandlerManager::HandleInputEvent,
51 base::Unretained(this)));
54 InputHandlerManager::~InputHandlerManager() {
55 client_->SetBoundHandler(InputHandlerManagerClient::Handler());
58 void InputHandlerManager::AddInputHandler(
59 int routing_id,
60 const base::WeakPtr<cc::InputHandler>& input_handler,
61 const base::WeakPtr<RenderViewImpl>& render_view_impl) {
62 if (task_runner_->BelongsToCurrentThread()) {
63 AddInputHandlerOnCompositorThread(routing_id,
64 base::ThreadTaskRunnerHandle::Get(),
65 input_handler, render_view_impl);
66 } else {
67 task_runner_->PostTask(
68 FROM_HERE,
69 base::Bind(&InputHandlerManager::AddInputHandlerOnCompositorThread,
70 base::Unretained(this), routing_id,
71 base::ThreadTaskRunnerHandle::Get(), input_handler,
72 render_view_impl));
76 void InputHandlerManager::AddInputHandlerOnCompositorThread(
77 int routing_id,
78 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
79 const base::WeakPtr<cc::InputHandler>& input_handler,
80 const base::WeakPtr<RenderViewImpl>& render_view_impl) {
81 DCHECK(task_runner_->BelongsToCurrentThread());
83 // The handler could be gone by this point if the compositor has shut down.
84 if (!input_handler)
85 return;
87 // The same handler may be registered for a route multiple times.
88 if (input_handlers_.count(routing_id) != 0)
89 return;
91 TRACE_EVENT1("input",
92 "InputHandlerManager::AddInputHandlerOnCompositorThread",
93 "result", "AddingRoute");
94 client_->DidAddInputHandler(routing_id, input_handler.get());
95 input_handlers_.add(routing_id, make_scoped_ptr(new InputHandlerWrapper(
96 this, routing_id, main_task_runner,
97 input_handler, render_view_impl)));
100 void InputHandlerManager::RemoveInputHandler(int routing_id) {
101 DCHECK(task_runner_->BelongsToCurrentThread());
102 DCHECK(input_handlers_.contains(routing_id));
104 TRACE_EVENT0("input", "InputHandlerManager::RemoveInputHandler");
106 client_->DidRemoveInputHandler(routing_id);
107 input_handlers_.erase(routing_id);
110 void InputHandlerManager::ObserveWheelEventAndResultOnMainThread(
111 int routing_id,
112 const blink::WebMouseWheelEvent& wheel_event,
113 const cc::InputHandlerScrollResult& scroll_result) {
114 task_runner_->PostTask(
115 FROM_HERE,
116 base::Bind(
117 &InputHandlerManager::ObserveWheelEventAndResultOnCompositorThread,
118 base::Unretained(this), routing_id, wheel_event, scroll_result));
121 void InputHandlerManager::ObserveWheelEventAndResultOnCompositorThread(
122 int routing_id,
123 const blink::WebMouseWheelEvent& wheel_event,
124 const cc::InputHandlerScrollResult& scroll_result) {
125 auto it = input_handlers_.find(routing_id);
126 if (it == input_handlers_.end())
127 return;
129 InputHandlerProxy* proxy = it->second->input_handler_proxy();
130 DCHECK(proxy->scroll_elasticity_controller());
131 proxy->scroll_elasticity_controller()->ObserveWheelEventAndResult(
132 wheel_event, scroll_result);
135 InputEventAckState InputHandlerManager::HandleInputEvent(
136 int routing_id,
137 const WebInputEvent* input_event,
138 ui::LatencyInfo* latency_info) {
139 DCHECK(task_runner_->BelongsToCurrentThread());
141 auto it = input_handlers_.find(routing_id);
142 if (it == input_handlers_.end()) {
143 TRACE_EVENT1("input", "InputHandlerManager::HandleInputEvent",
144 "result", "NoInputHandlerFound");
145 // Oops, we no longer have an interested input handler..
146 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
149 InputHandlerProxy* proxy = it->second->input_handler_proxy();
150 InputEventAckState input_event_ack_state = InputEventDispositionToAck(
151 proxy->HandleInputEventWithLatencyInfo(*input_event, latency_info));
152 switch (input_event_ack_state) {
153 case INPUT_EVENT_ACK_STATE_CONSUMED:
154 renderer_scheduler_->DidHandleInputEventOnCompositorThread(
155 *input_event,
156 RendererScheduler::InputEventState::EVENT_CONSUMED_BY_COMPOSITOR);
157 break;
158 case INPUT_EVENT_ACK_STATE_NOT_CONSUMED:
159 renderer_scheduler_->DidHandleInputEventOnCompositorThread(
160 *input_event,
161 RendererScheduler::InputEventState::EVENT_FORWARDED_TO_MAIN_THREAD);
162 break;
163 default:
164 break;
166 return input_event_ack_state;
169 void InputHandlerManager::DidOverscroll(int routing_id,
170 const DidOverscrollParams& params) {
171 client_->DidOverscroll(routing_id, params);
174 void InputHandlerManager::DidStopFlinging(int routing_id) {
175 client_->DidStopFlinging(routing_id);
178 void InputHandlerManager::DidAnimateForInput() {
179 renderer_scheduler_->DidAnimateForInputOnCompositorThread();
182 } // namespace content