OLD | NEW |
---|---|
(Empty) | |
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 virtual void SendFakeDataForTesting(void*) = 0; | |
31 }; | |
32 | |
33 // PlatformEventObserver<> defines the basic skeleton for an object requesting | |
34 // the browser process to start/stop listening to some platform/hardware events | |
35 // and observe the result. | |
36 // The results are received via IPC, assuming that the object was correctly | |
37 // registered as an observer via the constructor taking a RenderThread. | |
38 template <typename ListenerType> | |
39 class PlatformEventObserver : public PlatformEventObserverBase, | |
40 public RenderProcessObserver { | |
41 public: | |
42 // Creates a PlatformEventObserver that doesn't listen to response from the | |
43 // browser process. Can be used for testing purposes or for observers that | |
44 // have other means to get their results. | |
45 PlatformEventObserver() | |
46 : listener_(0), | |
47 is_observing(false) { | |
48 } | |
49 | |
50 // Creates a PlatformEventObserver that registers to the RenderThread in order | |
51 // to intercept the received IPC messages (via OnControlMessageReceived). If | |
52 // |thread| is null, it will not register. | |
53 PlatformEventObserver(RenderThread* thread) | |
scottmg
2014/08/05 21:09:18
explicit
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
| |
54 : listener_(0), | |
55 is_observing(false) { | |
56 if (thread) | |
57 thread->AddObserver(this); | |
58 } | |
59 | |
60 // The observer will automatically stop observing when destroyed in case of it | |
61 // did not stop before. | |
62 ~PlatformEventObserver() { | |
scottmg
2014/08/05 21:09:18
virtual, and out of line
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Forgot the virtual... However, having it out of li
scottmg
2014/08/05 21:33:31
Sorry, template, duh.
| |
63 if (IsObserving()) | |
64 Stop(); | |
65 } | |
66 | |
67 // Called when a new IPC message is received. Must be used to listen to the | |
68 // responses from the browser process if any is expected. | |
69 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE = 0; | |
70 | |
71 // Start observing. Will request the browser process to start listening to the | |
72 // events. |listener| will receive any response from the browser process. | |
73 // Note: should not be called if already observing. | |
74 virtual void Start(blink::WebPlatformEventListener* listener) { | |
75 DCHECK(!IsObserving()); | |
76 listener_ = static_cast<ListenerType*>(listener); | |
77 is_observing = true; | |
78 | |
79 SendStartMessage(); | |
80 } | |
81 | |
82 // Stop observing. Will let the browser know that it doesn't need to observe | |
83 // anymore. | |
84 virtual void Stop() { | |
85 DCHECK(IsObserving()); | |
86 listener_ = 0; | |
87 is_observing = false; | |
88 | |
89 SendStopMessage(); | |
90 } | |
91 | |
92 protected: | |
93 // This method is expected to send an IPC to the browser process to let it | |
94 // know that it should start observing. | |
95 virtual void SendStartMessage() = 0; | |
96 | |
97 // This method is expected to send an IPC to the browser process to let it | |
98 /// know that it should stop observing. | |
99 virtual void SendStopMessage() = 0; | |
100 | |
101 bool IsObserving() const { | |
102 return is_observing; | |
103 } | |
104 | |
105 ListenerType* listener_; | |
scottmg
2014/08/05 21:09:18
Oh, I see now. I don't know is this is against our
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
| |
106 | |
107 private: | |
108 bool is_observing; | |
scottmg
2014/08/05 21:09:18
is_observing_
mlamouri (slow - plz ping)
2014/08/05 21:32:36
Done.
| |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver); | |
111 }; | |
112 | |
113 } // namespace content | |
114 | |
115 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ | |
OLD | NEW |