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 #ifndef CONTENT_RENDERER_INPUT_INPUT_EVENT_FILTER_H_
6 #define CONTENT_RENDERER_INPUT_INPUT_EVENT_FILTER_H_
11 #include "base/callback_forward.h"
12 #include "base/synchronization/lock.h"
13 #include "content/common/content_export.h"
14 #include "content/common/input/input_event_ack_state.h"
15 #include "content/renderer/input/input_handler_manager_client.h"
16 #include "ipc/message_filter.h"
17 #include "third_party/WebKit/public/web/WebInputEvent.h"
20 class MessageLoopProxy;
21 class SingleThreadTaskRunner;
29 // This class can be used to intercept InputMsg_HandleInputEvent messages
30 // and have them be delivered to a target thread. Input events are filtered
31 // based on routing_id (see AddRoute and RemoveRoute).
33 // The user of this class provides an instance of InputEventFilter::Handler,
34 // which will be passed WebInputEvents on the target thread.
39 class CONTENT_EXPORT InputEventFilter : public InputHandlerManagerClient,
40 public IPC::MessageFilter {
43 IPC::Listener* main_listener,
44 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
45 const scoped_refptr<base::MessageLoopProxy>& target_loop);
47 // The |handler| is invoked on the thread associated with |target_loop| to
48 // handle input events matching the filtered routes.
50 // If INPUT_EVENT_ACK_STATE_NOT_CONSUMED is returned by the handler,
51 // the original InputMsg_HandleInputEvent message will be delivered to
52 // |main_listener| on the main thread. (The "main thread" in this context is
53 // the thread where the InputEventFilter was constructed.) The responsibility
54 // is left to the eventual handler to deliver the corresponding
55 // InputHostMsg_HandleInputEvent_ACK.
57 virtual void SetBoundHandler(const Handler& handler) OVERRIDE;
58 virtual void DidAddInputHandler(int routing_id,
59 cc::InputHandler* input_handler) OVERRIDE;
60 virtual void DidRemoveInputHandler(int routing_id) OVERRIDE;
61 virtual void DidOverscroll(int routing_id,
62 const DidOverscrollParams& params) OVERRIDE;
63 virtual void DidStopFlinging(int routing_id) OVERRIDE;
65 // IPC::MessageFilter methods:
66 virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
67 virtual void OnFilterRemoved() OVERRIDE;
68 virtual void OnChannelClosing() OVERRIDE;
69 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
72 virtual ~InputEventFilter();
74 void ForwardToMainListener(const IPC::Message& message);
75 void ForwardToHandler(const IPC::Message& message);
76 void SendMessage(scoped_ptr<IPC::Message> message);
77 void SendMessageOnIOThread(scoped_ptr<IPC::Message> message);
79 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
80 IPC::Listener* main_listener_;
82 // The sender_ only gets invoked on the thread corresponding to io_loop_.
83 scoped_refptr<base::MessageLoopProxy> io_loop_;
86 // The handler_ only gets Run on the thread corresponding to target_loop_.
87 scoped_refptr<base::MessageLoopProxy> target_loop_;
90 // Protects access to routes_.
91 base::Lock routes_lock_;
93 // Indicates the routing_ids for which input events should be filtered.
94 std::set<int> routes_;
96 // Specifies whether overscroll notifications are forwarded to the host.
97 bool overscroll_notifications_enabled_;
99 // Used to intercept overscroll notifications while an event is being
100 // dispatched. If the event causes overscroll, the overscroll metadata can be
101 // bundled in the event ack, saving an IPC. Note that we must continue
102 // supporting overscroll IPC notifications due to fling animation updates.
103 scoped_ptr<DidOverscrollParams>* current_overscroll_params_;
106 } // namespace content
108 #endif // CONTENT_RENDERER_INPUT_INPUT_EVENT_FILTER_H_