OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ | 5 #ifndef CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |
6 #define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ | 6 #define CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "content/public/renderer/render_process_observer.h" | 9 #include "content/public/renderer/render_process_observer.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 // Creates a PlatformEventObserver that registers to the RenderThread in order | 53 // Creates a PlatformEventObserver that registers to the RenderThread in order |
54 // to intercept the received IPC messages (via OnControlMessageReceived). If | 54 // to intercept the received IPC messages (via OnControlMessageReceived). If |
55 // |thread| is null, it will not register. | 55 // |thread| is null, it will not register. |
56 explicit PlatformEventObserver(RenderThread* thread) | 56 explicit PlatformEventObserver(RenderThread* thread) |
57 : is_observing_(false), | 57 : is_observing_(false), |
58 listener_(0) { | 58 listener_(0) { |
59 if (thread) | 59 if (thread) |
60 thread->AddObserver(this); | 60 thread->AddObserver(this); |
61 } | 61 } |
62 | 62 |
63 // The observer will automatically stop observing when destroyed in case it | 63 // The observer must automatically stop observing when destroyed in case it |
64 // did not stop before. | 64 // did not stop before. Implementations of PlatformEventObserver must do |
65 // so by calling StopIfObserving() from their destructors. | |
65 virtual ~PlatformEventObserver() { | 66 virtual ~PlatformEventObserver() { |
66 if (is_observing()) | 67 // If this assert fails, the derived destructor failed to invoke |
67 Stop(); | 68 // StopIfObserving(). |
69 DCHECK(!is_observing()); | |
Mike West
2014/09/30 11:54:14
This is a good change.
| |
68 } | 70 } |
69 | 71 |
70 // Called when a new IPC message is received. Must be used to listen to the | 72 // Called when a new IPC message is received. Must be used to listen to the |
71 // responses from the browser process if any expected. | 73 // responses from the browser process if any expected. |
72 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE { | 74 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE { |
73 return false; | 75 return false; |
74 } | 76 } |
75 | 77 |
76 // Start observing. Will request the browser process to start listening to the | 78 // Start observing. Will request the browser process to start listening to the |
77 // events. |listener| will receive any response from the browser process. | 79 // events. |listener| will receive any response from the browser process. |
(...skipping 20 matching lines...) Expand all Loading... | |
98 // This method is expected to send an IPC to the browser process to let it | 100 // This method is expected to send an IPC to the browser process to let it |
99 // know that it should start observing. | 101 // know that it should start observing. |
100 // It is expected for subclasses to override it. | 102 // It is expected for subclasses to override it. |
101 virtual void SendStartMessage() = 0; | 103 virtual void SendStartMessage() = 0; |
102 | 104 |
103 // This method is expected to send an IPC to the browser process to let it | 105 // This method is expected to send an IPC to the browser process to let it |
104 // know that it should start observing. | 106 // know that it should start observing. |
105 // It is expected for subclasses to override it. | 107 // It is expected for subclasses to override it. |
106 virtual void SendStopMessage() = 0; | 108 virtual void SendStopMessage() = 0; |
107 | 109 |
110 // Implementations of PlatformEventObserver must call StopIfObserving() | |
111 // from their destructor to shutdown in an orderly manner . | |
Mike West
2014/09/30 11:55:32
Nit: Kill the space before the '.'.
| |
112 // (As Stop() calls a virtual method, it cannot be done handled by | |
Mike West
2014/09/30 11:55:32
Nit: s/done //
sof
2014/09/30 12:03:40
Thanks for catching that, tidied. My comment proof
| |
113 // ~PlatformEventObserver.) | |
114 void StopIfObserving() { | |
115 if (is_observing()) | |
116 Stop(); | |
117 } | |
118 | |
108 bool is_observing() const { | 119 bool is_observing() const { |
109 return is_observing_; | 120 return is_observing_; |
110 } | 121 } |
111 | 122 |
112 ListenerType* listener() { | 123 ListenerType* listener() { |
113 return listener_; | 124 return listener_; |
114 } | 125 } |
115 | 126 |
116 private: | 127 private: |
117 bool is_observing_; | 128 bool is_observing_; |
118 ListenerType* listener_; | 129 ListenerType* listener_; |
119 | 130 |
120 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver); | 131 DISALLOW_COPY_AND_ASSIGN(PlatformEventObserver); |
121 }; | 132 }; |
122 | 133 |
123 } // namespace content | 134 } // namespace content |
124 | 135 |
125 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ | 136 #endif // CONTENT_PUBLIC_RENDERER_PLATFORM_EVENT_OBSERVER_H_ |
OLD | NEW |