Index: net/base/network_change_notifier.h |
=================================================================== |
--- net/base/network_change_notifier.h (revision 50775) |
+++ net/base/network_change_notifier.h (working copy) |
@@ -6,11 +6,15 @@ |
#define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
#include "base/basictypes.h" |
+#include "base/observer_list_threadsafe.h" |
+class MessageLoop; |
eroman
2010/06/25 00:29:58
Delete.
Peter Kasting
2010/06/25 01:04:35
Thanks, fixed.
|
+ |
namespace net { |
// NetworkChangeNotifier monitors the system for network changes, and notifies |
-// observers on those events. |
+// registered observers of those events. Observers may register on any thread, |
+// and will be called back on the thread from which they registered. |
class NetworkChangeNotifier { |
public: |
class Observer { |
@@ -28,21 +32,58 @@ |
DISALLOW_COPY_AND_ASSIGN(Observer); |
}; |
- NetworkChangeNotifier() {} |
- virtual ~NetworkChangeNotifier() {} |
+ virtual ~NetworkChangeNotifier(); |
- // These functions add and remove observers to/from the NetworkChangeNotifier. |
- // Each call to AddObserver() must be matched with a corresponding call to |
- // RemoveObserver() with the same parameter. Observers must remove themselves |
- // before they get deleted, otherwise the NetworkChangeNotifier may try to |
- // notify the wrong object. |
- virtual void AddObserver(Observer* observer) = 0; |
- virtual void RemoveObserver(Observer* observer) = 0; |
+ // Creates the process-wide, platform-specific NetworkChangeNotifier. The |
+ // caller owns the returned pointer. You may call this on any thread. You |
+ // may also avoid creating this entirely (in which case nothing will be |
+ // monitored), but if you do create it, you must do so before any other |
+ // threads try to access the API below, and it must outlive all other threads |
+ // which might try to use it. |
+ static NetworkChangeNotifier* Create(); |
- // This will create the platform specific default NetworkChangeNotifier. |
- static NetworkChangeNotifier* CreateDefaultNetworkChangeNotifier(); |
+#ifdef UNIT_TEST |
+ // Like Create(), but for use in tests. The mock object doesn't monitor any |
+ // events, it merely rebroadcasts notifications when requested. |
+ static NetworkChangeNotifier* CreateMock() { |
+ return new NetworkChangeNotifier(); |
+ } |
+#endif |
+ // Registers |observer| to receive notifications of network changes. The |
+ // thread on which this is called is the thread on which |observer| will be |
+ // called back with notifications. This is safe to call if Create() has not |
+ // been called (as long as it doesn't race the Create() call on another |
+ // thread), in which case it will simply do nothing. |
+ static void AddObserver(Observer* observer); |
+ |
+ // Unregisters |observer| from receiving notifications. This must be called |
+ // on the same thread on which AddObserver() was called. Like AddObserver(), |
+ // this is safe to call if Create() has not been called (as long as it doesn't |
+ // race the Create() call on another thread), in which case it will simply do |
+ // nothing. Technically, it's also safe to call after the notifier object has |
+ // been destroyed, if the call doesn't race the notifier's destruction, but |
+ // there's no reason to use the API in this risky way, so don't do it. |
+ static void RemoveObserver(Observer* observer); |
+ |
+#ifdef UNIT_TEST |
+ // Allow unit tests to trigger notifications. |
+ static void NotifyObserversOfIPAddressChangeForTests() { |
+ NotifyObserversOfIPAddressChange(); |
+ } |
+#endif |
+ |
+ protected: |
+ NetworkChangeNotifier(); |
+ |
+ // Broadcasts a notification to all registered observers. Note that this |
+ // happens asynchronously, even for observers on the current thread, even in |
+ // tests. |
+ static void NotifyObserversOfIPAddressChange(); |
+ |
private: |
+ const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
+ |
DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
}; |