diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-25 21:30:38 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-06-25 21:30:38 +0000 |
commit | 66761b95332549f825999e482c17c94675275f49 (patch) | |
tree | fc5307808a2c62f1eff2a9f37db3aff11c5455d9 /net/base/network_change_notifier.h | |
parent | e313f3b11360902a3da9b3b1cc0df2a4792d0867 (diff) | |
download | chromium_src-66761b95332549f825999e482c17c94675275f49.zip chromium_src-66761b95332549f825999e482c17c94675275f49.tar.gz chromium_src-66761b95332549f825999e482c17c94675275f49.tar.bz2 |
Massively simplify the NetworkChangeNotifier infrastructure:
* Use a process-wide object (singleton pattern)
* Create/destroy this object on the main thread, make it outlive all consumers
* Make observer-related functions threadsafe
As a result, the notifier can now be used by any thread (eliminating things like NetworkChangeObserverProxy and NetworkChangeNotifierProxy, and expanding its usefulness); its creation and inner workings are much simplified (eliminating implementation-specific classes); and it is simpler to access (eliminating things like NetworkChangeNotifierThread and a LOT of passing pointers around).
BUG=none
TEST=Unittests; network changes still trigger notifications
Review URL: http://codereview.chromium.org/2802015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50895 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/network_change_notifier.h')
-rw-r--r-- | net/base/network_change_notifier.h | 63 |
1 files changed, 51 insertions, 12 deletions
diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h index 70b195d..e0a8800 100644 --- a/net/base/network_change_notifier.h +++ b/net/base/network_change_notifier.h @@ -6,11 +6,13 @@ #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ #include "base/basictypes.h" +#include "base/observer_list_threadsafe.h" 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 +30,58 @@ class NetworkChangeNotifier { 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); }; |