Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 // The DNS configuration (name servers, suffix search) has changed. | 31 // The DNS configuration (name servers, suffix search) has changed. |
| 32 CHANGE_DNS_SETTINGS = 1 << 0, | 32 CHANGE_DNS_SETTINGS = 1 << 0, |
| 33 // The HOSTS file has changed. | 33 // The HOSTS file has changed. |
| 34 CHANGE_DNS_HOSTS = 1 << 1, | 34 CHANGE_DNS_HOSTS = 1 << 1, |
| 35 // The watcher has started. | 35 // The watcher has started. |
| 36 CHANGE_DNS_WATCH_STARTED = 1 << 2, | 36 CHANGE_DNS_WATCH_STARTED = 1 << 2, |
| 37 // The watcher has failed and will not be available until further notice. | 37 // The watcher has failed and will not be available until further notice. |
| 38 CHANGE_DNS_WATCH_FAILED = 1 << 3, | 38 CHANGE_DNS_WATCH_FAILED = 1 << 3, |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 // Using the terminology of the Netwotk Information API: | |
|
Sergey Ulanov
2012/05/23 17:51:00
typo: Netwotk
| |
| 42 // http://www.w3.org/TR/netinfo-api. | |
| 43 enum ConnectionType { | |
| 44 CONNECTION_UNKNOWN, // A connection exists, but its type is unknown. | |
| 45 CONNECTION_ETHERNET, | |
| 46 CONNECTION_WIFI, | |
| 47 CONNECTION_2G, | |
| 48 CONNECTION_3G, | |
| 49 CONNECTION_4G, | |
| 50 CONNECTION_NONE // No connection. | |
| 51 }; | |
| 52 | |
| 41 class NET_EXPORT IPAddressObserver { | 53 class NET_EXPORT IPAddressObserver { |
| 42 public: | 54 public: |
| 43 virtual ~IPAddressObserver() {} | 55 virtual ~IPAddressObserver() {} |
| 44 | 56 |
| 45 // Will be called when the IP address of the primary interface changes. | 57 // Will be called when the IP address of the primary interface changes. |
| 46 // This includes when the primary interface itself changes. | 58 // This includes when the primary interface itself changes. |
| 47 virtual void OnIPAddressChanged() = 0; | 59 virtual void OnIPAddressChanged() = 0; |
| 48 | 60 |
| 49 protected: | 61 protected: |
| 50 IPAddressObserver() {} | 62 IPAddressObserver() {} |
| 51 | 63 |
| 52 private: | 64 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); | 65 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); |
| 54 }; | 66 }; |
| 55 | 67 |
| 56 class NET_EXPORT OnlineStateObserver { | 68 class NET_EXPORT ConnectionTypeObserver { |
| 57 public: | 69 public: |
| 58 virtual ~OnlineStateObserver() {} | 70 virtual ~ConnectionTypeObserver() {} |
| 59 | 71 |
| 60 // Will be called when the online state of the system may have changed. | 72 // Will be called when the connection type of the system has changed. |
| 61 // See NetworkChangeNotifier::IsOffline() for important caveats about | 73 // See NetworkChangeNotifier::GetConnectionType() for important caveats |
| 62 // the unreliability of this signal. | 74 // about the unreliability of using this signal to infer the ability to |
| 63 virtual void OnOnlineStateChanged(bool online) = 0; | 75 // reach remote sites. |
| 76 virtual void OnConnectionTypeChanged(ConnectionType type) = 0; | |
| 64 | 77 |
| 65 protected: | 78 protected: |
| 66 OnlineStateObserver() {} | 79 ConnectionTypeObserver() {} |
| 67 | 80 |
| 68 private: | 81 private: |
| 69 DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); | 82 DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver); |
| 70 }; | 83 }; |
| 71 | 84 |
| 72 class NET_EXPORT DNSObserver { | 85 class NET_EXPORT DNSObserver { |
| 73 public: | 86 public: |
| 74 virtual ~DNSObserver() {} | 87 virtual ~DNSObserver() {} |
| 75 | 88 |
| 76 // Will be called when the DNS settings of the system may have changed. | 89 // Will be called when the DNS settings of the system may have changed. |
| 77 // The flags set in |detail| provide the specific set of changes. | 90 // The flags set in |detail| provide the specific set of changes. |
| 78 virtual void OnDNSChanged(unsigned detail) = 0; | 91 virtual void OnDNSChanged(unsigned detail) = 0; |
| 79 | 92 |
| 80 protected: | 93 protected: |
| 81 DNSObserver() {} | 94 DNSObserver() {} |
| 82 | 95 |
| 83 private: | 96 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(DNSObserver); | 97 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
| 85 }; | 98 }; |
| 86 | 99 |
| 87 virtual ~NetworkChangeNotifier(); | 100 virtual ~NetworkChangeNotifier(); |
| 88 | 101 |
| 89 // See the description of NetworkChangeNotifier::IsOffline(). | 102 // See the description of NetworkChangeNotifier::GetConnectionType(). |
| 90 // Implementations must be thread-safe. Implementations must also be | 103 // Implementations must be thread-safe. Implementations must also be |
| 91 // cheap as this could be called (repeatedly) from the IO thread. | 104 // cheap as this could be called (repeatedly) from the IO thread. |
| 92 virtual bool IsCurrentlyOffline() const = 0; | 105 virtual ConnectionType GetCurrentConnectionType() const = 0; |
| 93 | 106 |
| 94 // Replaces the default class factory instance of NetworkChangeNotifier class. | 107 // Replaces the default class factory instance of NetworkChangeNotifier class. |
| 95 // The method will take over the ownership of |factory| object. | 108 // The method will take over the ownership of |factory| object. |
| 96 static void SetFactory(NetworkChangeNotifierFactory* factory); | 109 static void SetFactory(NetworkChangeNotifierFactory* factory); |
| 97 | 110 |
| 98 // Creates the process-wide, platform-specific NetworkChangeNotifier. The | 111 // Creates the process-wide, platform-specific NetworkChangeNotifier. The |
| 99 // caller owns the returned pointer. You may call this on any thread. You | 112 // caller owns the returned pointer. You may call this on any thread. You |
| 100 // may also avoid creating this entirely (in which case nothing will be | 113 // may also avoid creating this entirely (in which case nothing will be |
| 101 // monitored), but if you do create it, you must do so before any other | 114 // monitored), but if you do create it, you must do so before any other |
| 102 // threads try to access the API below, and it must outlive all other threads | 115 // threads try to access the API below, and it must outlive all other threads |
| 103 // which might try to use it. | 116 // which might try to use it. |
| 104 static NetworkChangeNotifier* Create(); | 117 static NetworkChangeNotifier* Create(); |
| 105 | 118 |
| 119 // Returns the connection type. | |
| 120 // A return value of |CONNECTION_NONE| is a pretty strong indicator that the | |
| 121 // user won't be able to connect to remote sites. However, another return | |
| 122 // value doesn't imply that the user will be able to connect to remote sites; | |
| 123 // even if some link is up, it is uncertain whether a particular connection | |
| 124 // attempt to a particular remote site will be successful. | |
| 125 static ConnectionType GetConnectionType(); | |
| 126 | |
| 127 // Convenience method to determine if the user is offline. | |
| 106 // Returns true if there is currently no internet connection. | 128 // Returns true if there is currently no internet connection. |
| 107 // | 129 // |
| 108 // A return value of |true| is a pretty strong indicator that the user | 130 // A return value of |true| is a pretty strong indicator that the user |
| 109 // won't be able to connect to remote sites. However, a return value of | 131 // won't be able to connect to remote sites. However, a return value of |
| 110 // |false| is inconclusive; even if some link is up, it is uncertain | 132 // |false| is inconclusive; even if some link is up, it is uncertain |
| 111 // whether a particular connection attempt to a particular remote site | 133 // whether a particular connection attempt to a particular remote site |
| 112 // will be successfully. | 134 // will be successfully. |
| 113 static bool IsOffline(); | 135 static bool IsOffline() { |
| 136 return GetConnectionType() == CONNECTION_NONE; | |
| 137 } | |
| 114 | 138 |
| 115 // Returns true if DNS watcher is operational. | 139 // Returns true if DNS watcher is operational. |
| 116 static bool IsWatchingDNS(); | 140 static bool IsWatchingDNS(); |
| 117 | 141 |
| 118 // Like Create(), but for use in tests. The mock object doesn't monitor any | 142 // Like Create(), but for use in tests. The mock object doesn't monitor any |
| 119 // events, it merely rebroadcasts notifications when requested. | 143 // events, it merely rebroadcasts notifications when requested. |
| 120 static NetworkChangeNotifier* CreateMock(); | 144 static NetworkChangeNotifier* CreateMock(); |
| 121 | 145 |
| 122 // Registers |observer| to receive notifications of network changes. The | 146 // Registers |observer| to receive notifications of network changes. The |
| 123 // thread on which this is called is the thread on which |observer| will be | 147 // thread on which this is called is the thread on which |observer| will be |
| 124 // called back with notifications. This is safe to call if Create() has not | 148 // called back with notifications. This is safe to call if Create() has not |
| 125 // been called (as long as it doesn't race the Create() call on another | 149 // been called (as long as it doesn't race the Create() call on another |
| 126 // thread), in which case it will simply do nothing. | 150 // thread), in which case it will simply do nothing. |
| 127 static void AddIPAddressObserver(IPAddressObserver* observer); | 151 static void AddIPAddressObserver(IPAddressObserver* observer); |
| 128 static void AddOnlineStateObserver(OnlineStateObserver* observer); | 152 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); |
| 129 static void AddDNSObserver(DNSObserver* observer); | 153 static void AddDNSObserver(DNSObserver* observer); |
| 130 | 154 |
| 131 // Unregisters |observer| from receiving notifications. This must be called | 155 // Unregisters |observer| from receiving notifications. This must be called |
| 132 // on the same thread on which AddObserver() was called. Like AddObserver(), | 156 // on the same thread on which AddObserver() was called. Like AddObserver(), |
| 133 // this is safe to call if Create() has not been called (as long as it doesn't | 157 // this is safe to call if Create() has not been called (as long as it doesn't |
| 134 // race the Create() call on another thread), in which case it will simply do | 158 // race the Create() call on another thread), in which case it will simply do |
| 135 // nothing. Technically, it's also safe to call after the notifier object has | 159 // nothing. Technically, it's also safe to call after the notifier object has |
| 136 // been destroyed, if the call doesn't race the notifier's destruction, but | 160 // been destroyed, if the call doesn't race the notifier's destruction, but |
| 137 // there's no reason to use the API in this risky way, so don't do it. | 161 // there's no reason to use the API in this risky way, so don't do it. |
| 138 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 162 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
| 139 static void RemoveOnlineStateObserver(OnlineStateObserver* observer); | 163 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); |
| 140 static void RemoveDNSObserver(DNSObserver* observer); | 164 static void RemoveDNSObserver(DNSObserver* observer); |
| 141 | 165 |
| 142 // Allow unit tests to trigger notifications. | 166 // Allow unit tests to trigger notifications. |
| 143 static void NotifyObserversOfIPAddressChangeForTests() { | 167 static void NotifyObserversOfIPAddressChangeForTests() { |
| 144 NotifyObserversOfIPAddressChange(); | 168 NotifyObserversOfIPAddressChange(); |
| 145 } | 169 } |
| 146 | 170 |
| 147 protected: | 171 protected: |
| 148 friend class internal::DnsConfigWatcher; | 172 friend class internal::DnsConfigWatcher; |
| 149 | 173 |
| 150 NetworkChangeNotifier(); | 174 NetworkChangeNotifier(); |
| 151 | 175 |
| 152 // Broadcasts a notification to all registered observers. Note that this | 176 // Broadcasts a notification to all registered observers. Note that this |
| 153 // happens asynchronously, even for observers on the current thread, even in | 177 // happens asynchronously, even for observers on the current thread, even in |
| 154 // tests. | 178 // tests. |
| 155 static void NotifyObserversOfIPAddressChange(); | 179 static void NotifyObserversOfIPAddressChange(); |
| 156 static void NotifyObserversOfOnlineStateChange(); | 180 static void NotifyObserversOfConnectionTypeChange(); |
| 157 static void NotifyObserversOfDNSChange(unsigned detail); | 181 static void NotifyObserversOfDNSChange(unsigned detail); |
| 158 | 182 |
| 159 private: | 183 private: |
| 160 friend class NetworkChangeNotifierLinuxTest; | 184 friend class NetworkChangeNotifierLinuxTest; |
| 161 friend class NetworkChangeNotifierWinTest; | 185 friend class NetworkChangeNotifierWinTest; |
| 162 | 186 |
| 163 // Allows a second NetworkChangeNotifier to be created for unit testing, so | 187 // Allows a second NetworkChangeNotifier to be created for unit testing, so |
| 164 // the test suite can create a MockNetworkChangeNotifier, but platform | 188 // the test suite can create a MockNetworkChangeNotifier, but platform |
| 165 // specific NetworkChangeNotifiers can also be created for testing. To use, | 189 // specific NetworkChangeNotifiers can also be created for testing. To use, |
| 166 // create an DisableForTest object, and then create the new | 190 // create an DisableForTest object, and then create the new |
| 167 // NetworkChangeNotifier object. The NetworkChangeNotifier must be | 191 // NetworkChangeNotifier object. The NetworkChangeNotifier must be |
| 168 // destroyed before the DisableForTest object, as its destruction will restore | 192 // destroyed before the DisableForTest object, as its destruction will restore |
| 169 // the original NetworkChangeNotifier. | 193 // the original NetworkChangeNotifier. |
| 170 class NET_EXPORT_PRIVATE DisableForTest { | 194 class NET_EXPORT_PRIVATE DisableForTest { |
| 171 public: | 195 public: |
| 172 DisableForTest(); | 196 DisableForTest(); |
| 173 ~DisableForTest(); | 197 ~DisableForTest(); |
| 174 | 198 |
| 175 private: | 199 private: |
| 176 // The original NetworkChangeNotifier to be restored on destruction. | 200 // The original NetworkChangeNotifier to be restored on destruction. |
| 177 NetworkChangeNotifier* network_change_notifier_; | 201 NetworkChangeNotifier* network_change_notifier_; |
| 178 }; | 202 }; |
| 179 | 203 |
| 180 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 204 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
| 181 ip_address_observer_list_; | 205 ip_address_observer_list_; |
| 182 const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > | 206 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > |
| 183 online_state_observer_list_; | 207 connection_type_observer_list_; |
| 184 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 208 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
| 185 resolver_state_observer_list_; | 209 resolver_state_observer_list_; |
| 186 | 210 |
| 187 // True iff DNS watchers are operational. | 211 // True iff DNS watchers are operational. |
| 188 // Otherwise, OnDNSChanged might not be issued for future changes. | 212 // Otherwise, OnDNSChanged might not be issued for future changes. |
| 189 // TODO(szym): This is a temporary interface, consider restarting them. | 213 // TODO(szym): This is a temporary interface, consider restarting them. |
| 190 // http://crbug.com/116139 | 214 // http://crbug.com/116139 |
| 191 base::Lock watching_dns_lock_; | 215 base::Lock watching_dns_lock_; |
| 192 bool watching_dns_; | 216 bool watching_dns_; |
| 193 | 217 |
| 194 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 218 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
| 195 }; | 219 }; |
| 196 | 220 |
| 197 } // namespace net | 221 } // namespace net |
| 198 | 222 |
| 199 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 223 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
| OLD | NEW |