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"
13 class WebPlatformEventListener
;
18 // This class is used as a base class for PlatformEventObserver<ListenerType> to
19 // allow storing PlatformEventObserver<> with different typename in the same
21 class PlatformEventObserverBase
{
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
{
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),
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),
60 thread
->AddObserver(this);
63 // The observer will automatically stop observing when destroyed in case it
64 // did not stop before.
65 virtual ~PlatformEventObserver() {
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
{
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
);
87 // Stop observing. Will let the browser know that it doesn't need to observe
90 DCHECK(is_observing());
92 is_observing_
= false;
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() {
118 ListenerType
* listener_
;
120 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver
);
123 } // namespace content
125 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_