Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / public / renderer / platform_event_observer.h
blob083007cfc999d2f7adac65e0a1a7566800718da9
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 must automatically stop observing when destroyed in case it
64 // did not stop before. Implementations of PlatformEventObserver must do
65 // so by calling StopIfObserving() from their destructors.
66 ~PlatformEventObserver() override {
67 // If this assert fails, the derived destructor failed to invoke
68 // StopIfObserving().
69 DCHECK(!is_observing());
72 // Called when a new IPC message is received. Must be used to listen to the
73 // responses from the browser process if any expected.
74 bool OnControlMessageReceived(const IPC::Message& msg) override {
75 return false;
78 // Start observing. Will request the browser process to start listening to the
79 // events. |listener| will receive any response from the browser process.
80 // Note: should not be called if already observing.
81 void Start(blink::WebPlatformEventListener* listener) override {
82 DCHECK(!is_observing());
83 listener_ = static_cast<ListenerType*>(listener);
84 is_observing_ = true;
86 SendStartMessage();
89 // Stop observing. Will let the browser know that it doesn't need to observe
90 // anymore.
91 void Stop() override {
92 DCHECK(is_observing());
93 listener_ = 0;
94 is_observing_ = false;
96 SendStopMessage();
99 protected:
100 // This method is expected to send an IPC to the browser process to let it
101 // know that it should start observing.
102 // It is expected for subclasses to override it.
103 virtual void SendStartMessage() = 0;
105 // This method is expected to send an IPC to the browser process to let it
106 // know that it should start observing.
107 // It is expected for subclasses to override it.
108 virtual void SendStopMessage() = 0;
110 // Implementations of PlatformEventObserver must call StopIfObserving()
111 // from their destructor to shutdown in an orderly manner.
112 // (As Stop() calls a virtual method, it cannot be handled by
113 // ~PlatformEventObserver.)
114 void StopIfObserving() {
115 if (is_observing())
116 Stop();
119 bool is_observing() const {
120 return is_observing_;
123 ListenerType* listener() {
124 return listener_;
127 private:
128 bool is_observing_;
129 ListenerType* listener_;
131 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver);
134 } // namespace content
136 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_