Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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_export.h" | 11 #include "net/base/net_export.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 class NetworkChangeNotifierFactory; | 15 class NetworkChangeNotifierFactory; |
| 16 | 16 |
| 17 // NetworkChangeNotifier monitors the system for network changes, and notifies | 17 // NetworkChangeNotifier monitors the system for network changes, and notifies |
| 18 // registered observers of those events. Observers may register on any thread, | 18 // registered observers of those events. Observers may register on any thread, |
| 19 // and will be called back on the thread from which they registered. | 19 // and will be called back on the thread from which they registered. |
| 20 // NetworkChangeNotifiers are threadsafe, though they must be created and | 20 // NetworkChangeNotifiers are threadsafe, though they must be created and |
| 21 // destroyed on the same thread. | 21 // destroyed on the same thread. |
| 22 class NET_EXPORT NetworkChangeNotifier { | 22 class NET_EXPORT NetworkChangeNotifier { |
| 23 public: | 23 public: |
| 24 | |
| 25 // Using the terminology of the Netwotk Information API: | |
| 26 // http://www.w3.org/TR/netinfo-api. | |
| 27 enum ConnectionState { | |
|
wtc
2012/01/12 23:27:57
This enum type should be named ConnectionType? I
droger_google
2012/05/02 11:46:07
There is now another enum in that file which uses
| |
| 28 NONE, | |
| 29 UNKNOWN, | |
| 30 TWO_G, | |
| 31 THREE_G, | |
| 32 FOUR_G, | |
| 33 WIFI, | |
| 34 ETHERNET | |
| 35 }; | |
| 36 | |
| 24 class NET_EXPORT IPAddressObserver { | 37 class NET_EXPORT IPAddressObserver { |
| 25 public: | 38 public: |
| 26 virtual ~IPAddressObserver() {} | 39 virtual ~IPAddressObserver() {} |
| 27 | 40 |
| 28 // Will be called when the IP address of the primary interface changes. | 41 // Will be called when the IP address of the primary interface changes. |
| 29 // This includes when the primary interface itself changes. | 42 // This includes when the primary interface itself changes. |
| 30 virtual void OnIPAddressChanged() = 0; | 43 virtual void OnIPAddressChanged() = 0; |
| 31 | 44 |
| 32 protected: | 45 protected: |
| 33 IPAddressObserver() {} | 46 IPAddressObserver() {} |
| 34 | 47 |
| 35 private: | 48 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); | 49 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); |
| 37 }; | 50 }; |
| 38 | 51 |
| 39 class NET_EXPORT OnlineStateObserver { | 52 class NET_EXPORT ConnectionStateObserver { |
| 40 public: | 53 public: |
| 41 virtual ~OnlineStateObserver() {} | 54 virtual ~ConnectionStateObserver() {} |
| 42 | 55 |
| 43 // Will be called when the online state of the system may have changed. | 56 // Will be called when the connection state of the system may have changed. |
| 44 // See NetworkChangeNotifier::IsOffline() for important caveats about | 57 // See NetworkChangeNotifier::GetConnectionState() for important caveats |
| 45 // the unreliability of this signal. | 58 // about the unreliability of this signal. |
| 46 virtual void OnOnlineStateChanged(bool online) = 0; | 59 virtual void OnConnectionStateChanged(ConnectionState state) = 0; |
| 47 | 60 |
| 48 protected: | 61 protected: |
| 49 OnlineStateObserver() {} | 62 ConnectionStateObserver() {} |
| 50 | 63 |
| 51 private: | 64 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); | 65 DISALLOW_COPY_AND_ASSIGN(ConnectionStateObserver); |
| 53 }; | 66 }; |
| 54 | 67 |
| 55 class NET_EXPORT DNSObserver { | 68 class NET_EXPORT DNSObserver { |
| 56 public: | 69 public: |
| 57 virtual ~DNSObserver() {} | 70 virtual ~DNSObserver() {} |
| 58 | 71 |
| 59 // Will be called when the DNS resolver of the system may have changed. | 72 // Will be called when the DNS resolver of the system may have changed. |
| 60 // This is only used on Linux currently and watches /etc/resolv.conf | 73 // This is only used on Linux currently and watches /etc/resolv.conf |
| 61 // and /etc/hosts | 74 // and /etc/hosts |
| 62 virtual void OnDNSChanged() = 0; | 75 virtual void OnDNSChanged() = 0; |
| 63 | 76 |
| 64 protected: | 77 protected: |
| 65 DNSObserver() {} | 78 DNSObserver() {} |
| 66 | 79 |
| 67 private: | 80 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(DNSObserver); | 81 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
| 69 }; | 82 }; |
| 70 | 83 |
| 71 virtual ~NetworkChangeNotifier(); | 84 virtual ~NetworkChangeNotifier(); |
| 72 | 85 |
| 73 // See the description of NetworkChangeNotifier::IsOffline(). | 86 // See the description of NetworkChangeNotifier::GetConnectionState(). |
| 74 // Implementations must be thread-safe. Implementations must also be | 87 // Implementations must be thread-safe. Implementations must also be |
| 75 // cheap as this could be called (repeatedly) from the IO thread. | 88 // cheap as this could be called (repeatedly) from the IO thread. |
| 76 virtual bool IsCurrentlyOffline() const = 0; | 89 virtual ConnectionState GetCurrentConnectionState() const = 0; |
| 77 | 90 |
| 78 // Replaces the default class factory instance of NetworkChangeNotifier class. | 91 // Replaces the default class factory instance of NetworkChangeNotifier class. |
| 79 // The method will take over the ownership of |factory| object. | 92 // The method will take over the ownership of |factory| object. |
| 80 static void SetFactory(NetworkChangeNotifierFactory* factory); | 93 static void SetFactory(NetworkChangeNotifierFactory* factory); |
| 81 | 94 |
| 82 // Creates the process-wide, platform-specific NetworkChangeNotifier. The | 95 // Creates the process-wide, platform-specific NetworkChangeNotifier. The |
| 83 // caller owns the returned pointer. You may call this on any thread. You | 96 // caller owns the returned pointer. You may call this on any thread. You |
| 84 // may also avoid creating this entirely (in which case nothing will be | 97 // may also avoid creating this entirely (in which case nothing will be |
| 85 // monitored), but if you do create it, you must do so before any other | 98 // monitored), but if you do create it, you must do so before any other |
| 86 // threads try to access the API below, and it must outlive all other threads | 99 // threads try to access the API below, and it must outlive all other threads |
| 87 // which might try to use it. | 100 // which might try to use it. |
| 88 static NetworkChangeNotifier* Create(); | 101 static NetworkChangeNotifier* Create(); |
| 89 | 102 |
| 90 // Returns true if there is currently no internet connection. | 103 // A return value of |NONE| is a pretty strong indicator that the user |
| 91 // | 104 // won't be able to connect to remote sites. However, another return value is |
| 92 // A return value of |true| is a pretty strong indicator that the user | 105 // inconclusive; even if some link is up, it is uncertain whether a particular |
| 93 // won't be able to connect to remote sites. However, a return value of | 106 // connection attempt to a particular remote site will be successfully. |
| 94 // |false| is inconclusive; even if some link is up, it is uncertain | 107 static ConnectionState GetConnectionState(); |
| 95 // whether a particular connection attempt to a particular remote site | |
| 96 // will be successfully. | |
| 97 static bool IsOffline(); | |
| 98 | 108 |
| 99 // Like Create(), but for use in tests. The mock object doesn't monitor any | 109 // Like Create(), but for use in tests. The mock object doesn't monitor any |
| 100 // events, it merely rebroadcasts notifications when requested. | 110 // events, it merely rebroadcasts notifications when requested. |
| 101 static NetworkChangeNotifier* CreateMock(); | 111 static NetworkChangeNotifier* CreateMock(); |
| 102 | 112 |
| 103 // Registers |observer| to receive notifications of network changes. The | 113 // Registers |observer| to receive notifications of network changes. The |
| 104 // thread on which this is called is the thread on which |observer| will be | 114 // thread on which this is called is the thread on which |observer| will be |
| 105 // called back with notifications. This is safe to call if Create() has not | 115 // called back with notifications. This is safe to call if Create() has not |
| 106 // been called (as long as it doesn't race the Create() call on another | 116 // been called (as long as it doesn't race the Create() call on another |
| 107 // thread), in which case it will simply do nothing. | 117 // thread), in which case it will simply do nothing. |
| 108 static void AddIPAddressObserver(IPAddressObserver* observer); | 118 static void AddIPAddressObserver(IPAddressObserver* observer); |
| 109 static void AddOnlineStateObserver(OnlineStateObserver* observer); | 119 static void AddConnectionStateObserver(ConnectionStateObserver* observer); |
| 110 static void AddDNSObserver(DNSObserver* observer); | 120 static void AddDNSObserver(DNSObserver* observer); |
| 111 | 121 |
| 112 // Unregisters |observer| from receiving notifications. This must be called | 122 // Unregisters |observer| from receiving notifications. This must be called |
| 113 // on the same thread on which AddObserver() was called. Like AddObserver(), | 123 // on the same thread on which AddObserver() was called. Like AddObserver(), |
| 114 // this is safe to call if Create() has not been called (as long as it doesn't | 124 // this is safe to call if Create() has not been called (as long as it doesn't |
| 115 // race the Create() call on another thread), in which case it will simply do | 125 // race the Create() call on another thread), in which case it will simply do |
| 116 // nothing. Technically, it's also safe to call after the notifier object has | 126 // nothing. Technically, it's also safe to call after the notifier object has |
| 117 // been destroyed, if the call doesn't race the notifier's destruction, but | 127 // been destroyed, if the call doesn't race the notifier's destruction, but |
| 118 // there's no reason to use the API in this risky way, so don't do it. | 128 // there's no reason to use the API in this risky way, so don't do it. |
| 119 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 129 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
| 120 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); | 130 static void RemoveConnectionStateObserver(ConnectionStateObserver* observer); |
| 121 static void RemoveDNSObserver(DNSObserver* observer); | 131 static void RemoveDNSObserver(DNSObserver* observer); |
| 122 | 132 |
| 123 // Allow unit tests to trigger notifications. | 133 // Allow unit tests to trigger notifications. |
| 124 static void NotifyObserversOfIPAddressChangeForTests() { | 134 static void NotifyObserversOfIPAddressChangeForTests() { |
| 125 NotifyObserversOfIPAddressChange(); | 135 NotifyObserversOfIPAddressChange(); |
| 126 } | 136 } |
| 127 | 137 |
| 128 protected: | 138 protected: |
| 129 NetworkChangeNotifier(); | 139 NetworkChangeNotifier(); |
| 130 | 140 |
| 131 // Broadcasts a notification to all registered observers. Note that this | 141 // Broadcasts a notification to all registered observers. Note that this |
| 132 // happens asynchronously, even for observers on the current thread, even in | 142 // happens asynchronously, even for observers on the current thread, even in |
| 133 // tests. | 143 // tests. |
| 134 static void NotifyObserversOfIPAddressChange(); | 144 static void NotifyObserversOfIPAddressChange(); |
| 135 static void NotifyObserversOfOnlineStateChange(); | 145 static void NotifyObserversOfConnectionStateChange(); |
| 136 static void NotifyObserversOfDNSChange(); | 146 static void NotifyObserversOfDNSChange(); |
| 137 | 147 |
| 138 private: | 148 private: |
| 139 friend class NetworkChangeNotifierLinuxTest; | 149 friend class NetworkChangeNotifierLinuxTest; |
| 140 friend class NetworkChangeNotifierWinTest; | 150 friend class NetworkChangeNotifierWinTest; |
| 141 | 151 |
| 142 // Allows a second NetworkChangeNotifier to be created for unit testing, so | 152 // Allows a second NetworkChangeNotifier to be created for unit testing, so |
| 143 // the test suite can create a MockNetworkChangeNotifier, but platform | 153 // the test suite can create a MockNetworkChangeNotifier, but platform |
| 144 // specific NetworkChangeNotifiers can also be created for testing. To use, | 154 // specific NetworkChangeNotifiers can also be created for testing. To use, |
| 145 // create an DisableForTest object, and then create the new | 155 // create an DisableForTest object, and then create the new |
| 146 // NetworkChangeNotifier object. The NetworkChangeNotifier must be | 156 // NetworkChangeNotifier object. The NetworkChangeNotifier must be |
| 147 // destroyed before the DisableForTest object, as its destruction will restore | 157 // destroyed before the DisableForTest object, as its destruction will restore |
| 148 // the original NetworkChangeNotifier. | 158 // the original NetworkChangeNotifier. |
| 149 class NET_EXPORT_PRIVATE DisableForTest { | 159 class NET_EXPORT_PRIVATE DisableForTest { |
| 150 public: | 160 public: |
| 151 DisableForTest(); | 161 DisableForTest(); |
| 152 ~DisableForTest(); | 162 ~DisableForTest(); |
| 153 | 163 |
| 154 private: | 164 private: |
| 155 // The original NetworkChangeNotifier to be restored on destruction. | 165 // The original NetworkChangeNotifier to be restored on destruction. |
| 156 NetworkChangeNotifier* network_change_notifier_; | 166 NetworkChangeNotifier* network_change_notifier_; |
| 157 }; | 167 }; |
| 158 | 168 |
| 159 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 169 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
| 160 ip_address_observer_list_; | 170 ip_address_observer_list_; |
| 161 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > | 171 const scoped_refptr<ObserverListThreadSafe<ConnectionStateObserver> > |
| 162 online_state_observer_list_; | 172 connection_state_observer_list_; |
| 163 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 173 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
| 164 resolver_state_observer_list_; | 174 resolver_state_observer_list_; |
| 165 | 175 |
| 166 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 176 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
| 167 }; | 177 }; |
| 168 | 178 |
| 169 } // namespace net | 179 } // namespace net |
| 170 | 180 |
| 171 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 181 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| OLD | NEW |