Removing uses of X11 native key events.
[chromium-blink-merge.git] / content / public / renderer / platform_event_observer.h
bloba67cbec1a060e50220585be2cef13d9fae78552b
1 // Copyright 2014 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_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
6 #define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
8 #include "base/logging.h"
9 #include "content/public/renderer/render_process_observer.h"
10 #include "content/public/renderer/render_thread.h"
12 namespace blink {
13 class WebPlatformEventListener;
16 namespace content {
18 // This class is used as a base class for PlatformEventObserver<ListenerType> to
19 // allow storing PlatformEventObserver<> with different typename in the same
20 // place.
21 class PlatformEventObserverBase {
22 public:
23 virtual ~PlatformEventObserverBase() { }
25 // Methods that need to be exposed in PlatformEventObserverBase. Their purpose
26 // is described in PlatformEventObserver<>.
28 virtual void Start(blink::WebPlatformEventListener* listener) = 0;
29 virtual void Stop() = 0;
31 // Helper method that allows an sub-class to write its own test helper.
32 // The |data| type MUST be known from the caller.
33 virtual void SendFakeDataForTesting(void* data) { }
36 // PlatformEventObserver<> defines the basic skeleton for an object requesting
37 // the browser process to start/stop listening to some platform/hardware events
38 // and observe the result.
39 // The results are received via IPC, assuming that the object was correctly
40 // registered as an observer via the constructor taking a RenderThread.
41 template <typename ListenerType>
42 class PlatformEventObserver : public PlatformEventObserverBase,
43 public RenderProcessObserver {
44 public:
45 // Creates a PlatformEventObserver that doesn't listen to responses from the
46 // browser process. Can be used for testing purposes or for observers that
47 // have other means to get their results.
48 PlatformEventObserver()
49 : is_observing_(false),
50 listener_(0) {
53 // Creates a PlatformEventObserver that registers to the RenderThread in order
54 // to intercept the received IPC messages (via OnControlMessageReceived). If
55 // |thread| is null, it will not register.
56 explicit PlatformEventObserver(RenderThread* thread)
57 : is_observing_(false),
58 listener_(0) {
59 if (thread)
60 thread->AddObserver(this);
63 // The observer will automatically stop observing when destroyed in case it
64 // did not stop before.
65 virtual ~PlatformEventObserver() {
66 if (is_observing())
67 Stop();
70 // Called when a new IPC message is received. Must be used to listen to the
71 // responses from the browser process if any expected.
72 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE {
73 return false;
76 // Start observing. Will request the browser process to start listening to the
77 // events. |listener| will receive any response from the browser process.
78 // Note: should not be called if already observing.
79 virtual void Start(blink::WebPlatformEventListener* listener) {
80 DCHECK(!is_observing());
81 listener_ = static_cast<ListenerType*>(listener);
82 is_observing_ = true;
84 SendStartMessage();
87 // Stop observing. Will let the browser know that it doesn't need to observe
88 // anymore.
89 virtual void Stop() {
90 DCHECK(is_observing());
91 listener_ = 0;
92 is_observing_ = false;
94 SendStopMessage();
97 protected:
98 // This method is expected to send an IPC to the browser process to let it
99 // know that it should start observing.
100 // It is expected for subclasses to override it.
101 virtual void SendStartMessage() = 0;
103 // This method is expected to send an IPC to the browser process to let it
104 // know that it should start observing.
105 // It is expected for subclasses to override it.
106 virtual void SendStopMessage() = 0;
108 bool is_observing() const {
109 return is_observing_;
112 ListenerType* listener() {
113 return listener_;
116 private:
117 bool is_observing_;
118 ListenerType* listener_;
120 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver);
123 } // namespace content
125 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_