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#include "content/browser/android/in_process/synchronous_input_event_filter.h"
6
7#include "base/callback.h"
8#include "cc/input/input_handler.h"
9#include "content/browser/android/in_process/synchronous_compositor_impl.h"
10#include "content/public/browser/browser_thread.h"
11#include "ui/events/latency_info.h"
12
13using blink::WebInputEvent;
14
15namespace content {
16
17SynchronousInputEventFilter::SynchronousInputEventFilter() {
18}
19
20SynchronousInputEventFilter::~SynchronousInputEventFilter() {
21}
22
23InputEventAckState SynchronousInputEventFilter::HandleInputEvent(
24    int routing_id,
25    const blink::WebInputEvent& input_event) {
26  // The handler will be empty both before renderer initialization and after
27  // renderer destruction. It's possible that this will be reached in such a
28  // state. While not good, it should also not be fatal.
29  if (handler_.is_null())
30    return INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS;
31  ui::LatencyInfo latency;
32  return handler_.Run(routing_id, &input_event, &latency);
33}
34
35void SynchronousInputEventFilter::SetBoundHandler(const Handler& handler) {
36  BrowserThread::PostTask(
37      BrowserThread::UI, FROM_HERE,
38      base::Bind(&SynchronousInputEventFilter::SetBoundHandlerOnUIThread,
39                 base::Unretained(this), handler));
40}
41
42void SynchronousInputEventFilter::SetBoundHandlerOnUIThread(
43    const Handler& handler) {
44  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
45  handler_ = handler;
46}
47
48void SynchronousInputEventFilter::DidAddInputHandler(
49    int routing_id,
50    cc::InputHandler* input_handler) {
51  // The SynchronusCompositorImpl can be NULL if the WebContents that it's
52  // bound to has already been deleted.
53  SynchronousCompositorImpl* compositor =
54      SynchronousCompositorImpl::FromRoutingID(routing_id);
55  if (compositor)
56    compositor->SetInputHandler(input_handler);
57}
58
59void SynchronousInputEventFilter::DidRemoveInputHandler(int routing_id) {
60  // The SynchronusCompositorImpl can be NULL if the WebContents that it's
61  // bound to has already been deleted.
62  SynchronousCompositorImpl* compositor =
63      SynchronousCompositorImpl::FromRoutingID(routing_id);
64  if (compositor)
65    compositor->SetInputHandler(NULL);
66}
67
68void SynchronousInputEventFilter::DidOverscroll(
69    int routing_id,
70    const DidOverscrollParams& params) {
71  // The SynchronusCompositorImpl can be NULL if the WebContents that it's
72  // bound to has already been deleted.
73  SynchronousCompositorImpl* compositor =
74      SynchronousCompositorImpl::FromRoutingID(routing_id);
75  if (compositor)
76    compositor->DidOverscroll(params);
77}
78
79void SynchronousInputEventFilter::DidStopFlinging(int routing_id) {
80  // The SynchronusCompositorImpl can be NULL if the WebContents that it's
81  // bound to has already been deleted.
82  SynchronousCompositorImpl* compositor =
83      SynchronousCompositorImpl::FromRoutingID(routing_id);
84  if (compositor)
85    compositor->DidStopFlinging();
86}
87
88}  // namespace content
89