1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/input/input_handler_manager.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/message_loop/message_loop_proxy.h"
#include "cc/input/input_handler.h"
#include "content/renderer/input/input_event_filter.h"
#include "content/renderer/input/input_handler_manager_client.h"
#include "content/renderer/input/input_handler_wrapper.h"
#include "content/renderer/input/input_scroll_elasticity_controller.h"
#include "content/renderer/scheduler/renderer_scheduler.h"
using blink::WebInputEvent;
namespace content {
namespace {
InputEventAckState InputEventDispositionToAck(
InputHandlerProxy::EventDisposition disposition) {
switch (disposition) {
case InputHandlerProxy::DID_HANDLE:
return INPUT_EVENT_ACK_STATE_CONSUMED;
case InputHandlerProxy::DID_NOT_HANDLE:
return INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
case InputHandlerProxy::DROP_EVENT:
return INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
}
NOTREACHED();
return INPUT_EVENT_ACK_STATE_UNKNOWN;
}
} // namespace
InputHandlerManager::InputHandlerManager(
const scoped_refptr<base::MessageLoopProxy>& message_loop_proxy,
InputHandlerManagerClient* client,
RendererScheduler* renderer_scheduler)
: message_loop_proxy_(message_loop_proxy),
client_(client),
renderer_scheduler_(renderer_scheduler) {
DCHECK(client_);
client_->SetBoundHandler(base::Bind(&InputHandlerManager::HandleInputEvent,
base::Unretained(this)));
}
InputHandlerManager::~InputHandlerManager() {
client_->SetBoundHandler(InputHandlerManagerClient::Handler());
}
void InputHandlerManager::AddInputHandler(
int routing_id,
const base::WeakPtr<cc::InputHandler>& input_handler,
const base::WeakPtr<RenderViewImpl>& render_view_impl) {
if (message_loop_proxy_->BelongsToCurrentThread()) {
AddInputHandlerOnCompositorThread(routing_id,
base::MessageLoopProxy::current(),
input_handler,
render_view_impl);
} else {
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(&InputHandlerManager::AddInputHandlerOnCompositorThread,
base::Unretained(this),
routing_id,
base::MessageLoopProxy::current(),
input_handler,
render_view_impl));
}
}
void InputHandlerManager::AddInputHandlerOnCompositorThread(
int routing_id,
const scoped_refptr<base::MessageLoopProxy>& main_loop,
const base::WeakPtr<cc::InputHandler>& input_handler,
const base::WeakPtr<RenderViewImpl>& render_view_impl) {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
// The handler could be gone by this point if the compositor has shut down.
if (!input_handler)
return;
// The same handler may be registered for a route multiple times.
if (input_handlers_.count(routing_id) != 0)
return;
TRACE_EVENT1("input",
"InputHandlerManager::AddInputHandlerOnCompositorThread",
"result", "AddingRoute");
client_->DidAddInputHandler(routing_id, input_handler.get());
input_handlers_.add(routing_id,
make_scoped_ptr(new InputHandlerWrapper(this,
routing_id, main_loop, input_handler, render_view_impl)));
}
void InputHandlerManager::RemoveInputHandler(int routing_id) {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
DCHECK(input_handlers_.contains(routing_id));
TRACE_EVENT0("input", "InputHandlerManager::RemoveInputHandler");
client_->DidRemoveInputHandler(routing_id);
input_handlers_.erase(routing_id);
}
void InputHandlerManager::ObserveWheelEventAndResultOnMainThread(
int routing_id,
const blink::WebMouseWheelEvent& wheel_event,
const cc::InputHandlerScrollResult& scroll_result) {
message_loop_proxy_->PostTask(
FROM_HERE,
base::Bind(
&InputHandlerManager::ObserveWheelEventAndResultOnCompositorThread,
base::Unretained(this), routing_id, wheel_event, scroll_result));
}
void InputHandlerManager::ObserveWheelEventAndResultOnCompositorThread(
int routing_id,
const blink::WebMouseWheelEvent& wheel_event,
const cc::InputHandlerScrollResult& scroll_result) {
auto it = input_handlers_.find(routing_id);
if (it == input_handlers_.end())
return;
InputHandlerProxy* proxy = it->second->input_handler_proxy();
DCHECK(proxy->scroll_elasticity_controller());
proxy->scroll_elasticity_controller()->ObserveWheelEventAndResult(
wheel_event, scroll_result);
}
InputEventAckState InputHandlerManager::HandleInputEvent(
int routing_id,
const WebInputEvent* input_event,
ui::LatencyInfo* latency_info) {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
auto it = input_handlers_.find(routing_id);
if (it == input_handlers_.end()) {
TRACE_EVENT1("input", "InputHandlerManager::HandleInputEvent",
"result", "NoInputHandlerFound");
// Oops, we no longer have an interested input handler..
return INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
}
InputHandlerProxy* proxy = it->second->input_handler_proxy();
return InputEventDispositionToAck(
proxy->HandleInputEventWithLatencyInfo(*input_event, latency_info));
}
void InputHandlerManager::DidOverscroll(int routing_id,
const DidOverscrollParams& params) {
client_->DidOverscroll(routing_id, params);
}
void InputHandlerManager::DidStopFlinging(int routing_id) {
client_->DidStopFlinging(routing_id);
}
void InputHandlerManager::DidReceiveInputEvent(
blink::WebInputEvent::Type type) {
renderer_scheduler_->DidReceiveInputEventOnCompositorThread(type);
}
void InputHandlerManager::DidAnimateForInput() {
renderer_scheduler_->DidAnimateForInputOnCompositorThread();
}
} // namespace content
|