Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/observer_list_threadsafe.h" | 10 #include "base/observer_list_threadsafe.h" |
| 11 #include "net/base/net_api.h" | |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 // NetworkChangeNotifier monitors the system for network changes, and notifies | 15 // NetworkChangeNotifier monitors the system for network changes, and notifies |
| 15 // registered observers of those events. Observers may register on any thread, | 16 // registered observers of those events. Observers may register on any thread, |
| 16 // and will be called back on the thread from which they registered. | 17 // and will be called back on the thread from which they registered. |
| 17 class NetworkChangeNotifier { | 18 class NET_API NetworkChangeNotifier { |
| 18 public: | 19 public: |
| 19 class IPAddressObserver { | 20 class NET_API IPAddressObserver { |
| 20 public: | 21 public: |
| 21 virtual ~IPAddressObserver() {} | 22 virtual ~IPAddressObserver() {} |
| 22 | 23 |
| 23 // 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. |
| 24 // This includes when the primary interface itself changes. | 25 // This includes when the primary interface itself changes. |
| 25 virtual void OnIPAddressChanged() = 0; | 26 virtual void OnIPAddressChanged() = 0; |
| 26 | 27 |
| 27 protected: | 28 protected: |
| 28 IPAddressObserver() {} | 29 IPAddressObserver() {} |
| 29 | 30 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 // Unregisters |observer| from receiving notifications. This must be called | 87 // Unregisters |observer| from receiving notifications. This must be called |
| 87 // on the same thread on which AddObserver() was called. Like AddObserver(), | 88 // on the same thread on which AddObserver() was called. Like AddObserver(), |
| 88 // this is safe to call if Create() has not been called (as long as it doesn't | 89 // this is safe to call if Create() has not been called (as long as it doesn't |
| 89 // race the Create() call on another thread), in which case it will simply do | 90 // race the Create() call on another thread), in which case it will simply do |
| 90 // nothing. Technically, it's also safe to call after the notifier object has | 91 // nothing. Technically, it's also safe to call after the notifier object has |
| 91 // been destroyed, if the call doesn't race the notifier's destruction, but | 92 // been destroyed, if the call doesn't race the notifier's destruction, but |
| 92 // there's no reason to use the API in this risky way, so don't do it. | 93 // there's no reason to use the API in this risky way, so don't do it. |
| 93 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 94 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
| 94 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); | 95 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); |
| 95 | 96 |
| 96 #ifdef UNIT_TEST | 97 #if defined(UNIT_TEST) || defined(NET_DLL) |
|
wtc
2011/05/16 20:55:17
Perhaps we should just delete this ifdef, given ou
| |
| 97 // Allow unit tests to trigger notifications. | 98 // Allow unit tests to trigger notifications. |
| 98 static void NotifyObserversOfIPAddressChangeForTests() { | 99 static void NotifyObserversOfIPAddressChangeForTests() { |
| 99 NotifyObserversOfIPAddressChange(); | 100 NotifyObserversOfIPAddressChange(); |
| 100 } | 101 } |
| 101 #endif | 102 #endif |
| 102 | 103 |
| 103 protected: | 104 protected: |
| 104 NetworkChangeNotifier(); | 105 NetworkChangeNotifier(); |
| 105 | 106 |
| 106 // Broadcasts a notification to all registered observers. Note that this | 107 // Broadcasts a notification to all registered observers. Note that this |
| 107 // happens asynchronously, even for observers on the current thread, even in | 108 // happens asynchronously, even for observers on the current thread, even in |
| 108 // tests. | 109 // tests. |
| 109 static void NotifyObserversOfIPAddressChange(); | 110 static void NotifyObserversOfIPAddressChange(); |
| 110 void NotifyObserversOfOnlineStateChange(); | 111 void NotifyObserversOfOnlineStateChange(); |
| 111 | 112 |
| 112 private: | 113 private: |
| 113 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 114 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
| 114 ip_address_observer_list_; | 115 ip_address_observer_list_; |
| 115 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > | 116 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > |
| 116 online_state_observer_list_; | 117 online_state_observer_list_; |
| 117 | 118 |
| 118 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 119 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 } // namespace net | 122 } // namespace net |
| 122 | 123 |
| 123 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 124 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| OLD | NEW |