Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: content/public/renderer/platform_event_observer.h

Issue 613203002: Have PlatformEventObserver implementations orderly stop on destruction. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clean up a comment. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/renderer/battery_status/battery_status_dispatcher.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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());
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
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.
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
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_
OLDNEW
« no previous file with comments | « no previous file | content/renderer/battery_status/battery_status_dispatcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698