Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/observer_list_threadsafe.h" | |
| 10 | |
| 11 class MessageLoop; | |
|
eroman
2010/06/25 00:29:58
Delete.
Peter Kasting
2010/06/25 01:04:35
Thanks, fixed.
| |
| 9 | 12 |
| 10 namespace net { | 13 namespace net { |
| 11 | 14 |
| 12 // NetworkChangeNotifier monitors the system for network changes, and notifies | 15 // NetworkChangeNotifier monitors the system for network changes, and notifies |
| 13 // observers on those events. | 16 // registered observers of those events. Observers may register on any thread, |
| 17 // and will be called back on the thread from which they registered. | |
| 14 class NetworkChangeNotifier { | 18 class NetworkChangeNotifier { |
| 15 public: | 19 public: |
| 16 class Observer { | 20 class Observer { |
| 17 public: | 21 public: |
| 18 virtual ~Observer() {} | 22 virtual ~Observer() {} |
| 19 | 23 |
| 20 // Will be called when the IP address of the primary interface changes. | 24 // Will be called when the IP address of the primary interface changes. |
| 21 // This includes when the primary interface itself changes. | 25 // This includes when the primary interface itself changes. |
| 22 virtual void OnIPAddressChanged() = 0; | 26 virtual void OnIPAddressChanged() = 0; |
| 23 | 27 |
| 24 protected: | 28 protected: |
| 25 Observer() {} | 29 Observer() {} |
| 26 | 30 |
| 27 private: | 31 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(Observer); | 32 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 29 }; | 33 }; |
| 30 | 34 |
| 31 NetworkChangeNotifier() {} | 35 virtual ~NetworkChangeNotifier(); |
| 32 virtual ~NetworkChangeNotifier() {} | |
| 33 | 36 |
| 34 // These functions add and remove observers to/from the NetworkChangeNotifier. | 37 // Creates the process-wide, platform-specific NetworkChangeNotifier. The |
| 35 // Each call to AddObserver() must be matched with a corresponding call to | 38 // caller owns the returned pointer. You may call this on any thread. You |
| 36 // RemoveObserver() with the same parameter. Observers must remove themselves | 39 // may also avoid creating this entirely (in which case nothing will be |
| 37 // before they get deleted, otherwise the NetworkChangeNotifier may try to | 40 // monitored), but if you do create it, you must do so before any other |
| 38 // notify the wrong object. | 41 // threads try to access the API below, and it must outlive all other threads |
| 39 virtual void AddObserver(Observer* observer) = 0; | 42 // which might try to use it. |
| 40 virtual void RemoveObserver(Observer* observer) = 0; | 43 static NetworkChangeNotifier* Create(); |
| 41 | 44 |
| 42 // This will create the platform specific default NetworkChangeNotifier. | 45 #ifdef UNIT_TEST |
| 43 static NetworkChangeNotifier* CreateDefaultNetworkChangeNotifier(); | 46 // Like Create(), but for use in tests. The mock object doesn't monitor any |
| 47 // events, it merely rebroadcasts notifications when requested. | |
| 48 static NetworkChangeNotifier* CreateMock() { | |
| 49 return new NetworkChangeNotifier(); | |
| 50 } | |
| 51 #endif | |
| 52 | |
| 53 // Registers |observer| to receive notifications of network changes. The | |
| 54 // thread on which this is called is the thread on which |observer| will be | |
| 55 // called back with notifications. This is safe to call if Create() has not | |
| 56 // been called (as long as it doesn't race the Create() call on another | |
| 57 // thread), in which case it will simply do nothing. | |
| 58 static void AddObserver(Observer* observer); | |
| 59 | |
| 60 // Unregisters |observer| from receiving notifications. This must be called | |
| 61 // on the same thread on which AddObserver() was called. Like AddObserver(), | |
| 62 // this is safe to call if Create() has not been called (as long as it doesn't | |
| 63 // race the Create() call on another thread), in which case it will simply do | |
| 64 // nothing. Technically, it's also safe to call after the notifier object has | |
| 65 // been destroyed, if the call doesn't race the notifier's destruction, but | |
| 66 // there's no reason to use the API in this risky way, so don't do it. | |
| 67 static void RemoveObserver(Observer* observer); | |
| 68 | |
| 69 #ifdef UNIT_TEST | |
| 70 // Allow unit tests to trigger notifications. | |
| 71 static void NotifyObserversOfIPAddressChangeForTests() { | |
| 72 NotifyObserversOfIPAddressChange(); | |
| 73 } | |
| 74 #endif | |
| 75 | |
| 76 protected: | |
| 77 NetworkChangeNotifier(); | |
| 78 | |
| 79 // Broadcasts a notification to all registered observers. Note that this | |
| 80 // happens asynchronously, even for observers on the current thread, even in | |
| 81 // tests. | |
| 82 static void NotifyObserversOfIPAddressChange(); | |
| 44 | 83 |
| 45 private: | 84 private: |
| 85 const scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | |
| 86 | |
| 46 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 87 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
| 47 }; | 88 }; |
| 48 | 89 |
| 49 } // namespace net | 90 } // namespace net |
| 50 | 91 |
| 51 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 92 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| OLD | NEW |