Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / renderer / platform_event_observer.h
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.
4
5 #ifndef CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
6 #define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_
7
8 #include "base/logging.h"
9 #include "content/public/renderer/render_process_observer.h"
10 #include "content/public/renderer/render_thread.h"
11
12 namespace blink {
13 class WebPlatformEventListener;
14 }
15
16 namespace content {
17
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() { }
24
25   // Methods that need to be exposed in PlatformEventObserverBase. Their purpose
26   // is described in PlatformEventObserver<>.
27
28   virtual void Start(blink::WebPlatformEventListener* listener) = 0;
29   virtual void Stop() = 0;
30
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) { }
34 };
35
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) {
51   }
52
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);
61   }
62
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   virtual ~PlatformEventObserver() {
67     // If this assert fails, the derived destructor failed to invoke
68     // StopIfObserving().
69     DCHECK(!is_observing());
70   }
71
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   virtual bool OnControlMessageReceived(const IPC::Message& msg) override {
75     return false;
76   }
77
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   virtual void Start(blink::WebPlatformEventListener* listener) {
82     DCHECK(!is_observing());
83     listener_ = static_cast<ListenerType*>(listener);
84     is_observing_ = true;
85
86     SendStartMessage();
87   }
88
89   // Stop observing. Will let the browser know that it doesn't need to observe
90   // anymore.
91   virtual void Stop() {
92     DCHECK(is_observing());
93     listener_ = 0;
94     is_observing_ = false;
95
96     SendStopMessage();
97   }
98
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;
104
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;
109
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();
117   }
118
119   bool is_observing() const {
120     return is_observing_;
121   }
122
123   ListenerType* listener() {
124     return listener_;
125   }
126
127  private:
128   bool is_observing_;
129   ListenerType* listener_;
130
131   DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver);
132 };
133
134 } // namespace content
135
136 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_