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 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/observer_list_threadsafe.h" | 9 #include "base/observer_list_threadsafe.h" |
10 #include "base/time.h" | |
10 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
11 | 12 |
12 class GURL; | 13 class GURL; |
13 | 14 |
14 namespace net { | 15 namespace net { |
15 | 16 |
16 struct DnsConfig; | 17 struct DnsConfig; |
17 class HistogramWatcher; | 18 class HistogramWatcher; |
18 class NetworkChangeNotifierFactory; | 19 class NetworkChangeNotifierFactory; |
19 | 20 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 virtual void OnDNSChanged() = 0; | 80 virtual void OnDNSChanged() = 0; |
80 | 81 |
81 protected: | 82 protected: |
82 DNSObserver() {} | 83 DNSObserver() {} |
83 virtual ~DNSObserver() {} | 84 virtual ~DNSObserver() {} |
84 | 85 |
85 private: | 86 private: |
86 DISALLOW_COPY_AND_ASSIGN(DNSObserver); | 87 DISALLOW_COPY_AND_ASSIGN(DNSObserver); |
87 }; | 88 }; |
88 | 89 |
90 class NET_EXPORT NetworkChangeObserver { | |
91 public: | |
92 // OnNetworkChanged will be called when a change occurs to the host | |
93 // computer's hardware or software that affects the route network packets | |
94 // take to any network server. Some examples: | |
95 // 1. A network connection becoming available or going away. For example | |
96 // plugging or unplugging an Ethernet cable, WiFi or cellular modem | |
97 // connecting or disconnecting from a network, or a VPN tunnel being | |
98 // established or taken down. | |
99 // 2. An active network connection's IP address changes. | |
100 // 3. A change to the local IP routing tables. | |
101 // The signal shall only be produced when the change is complete. For | |
102 // example if a new network connection has become available, only give the | |
103 // signal once we think the O/S has finished establishing the connection | |
104 // (i.e. DHCP is done) to the point where the new connection is usable. | |
105 // The signal shall not be produced spuriously as it will be triggering some | |
106 // expensive operations, like socket pools closing all connections and | |
107 // sockets and then re-establishing them. | |
108 // |type| indicates the type of the active primary network connection after | |
109 // the change. Observers performing "constructive" activities like trying | |
110 // to establish a connection to a server should only do so when | |
111 // |type != CONNECTION_NONE|. Observers performing "destructive" activities | |
112 // like resetting already established server connections should only do so | |
113 // when |type == CONNECTION_NONE|. OnNetworkChanged will be called with | |
szym
2012/11/27 22:22:05
I suggest "will always be called ..." to make it
| |
114 // CONNECTION_NONE immediately prior to being called with an online state; | |
115 // this is done to make sure that destructive actions take place prior to | |
116 // constructive actions. | |
117 virtual void OnNetworkChanged(ConnectionType type) = 0; | |
118 | |
119 protected: | |
120 NetworkChangeObserver() {} | |
121 virtual ~NetworkChangeObserver() {} | |
122 | |
123 private: | |
124 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver); | |
125 }; | |
126 | |
89 virtual ~NetworkChangeNotifier(); | 127 virtual ~NetworkChangeNotifier(); |
90 | 128 |
91 // See the description of NetworkChangeNotifier::GetConnectionType(). | 129 // See the description of NetworkChangeNotifier::GetConnectionType(). |
92 // Implementations must be thread-safe. Implementations must also be | 130 // Implementations must be thread-safe. Implementations must also be |
93 // cheap as this could be called (repeatedly) from the network thread. | 131 // cheap as this could be called (repeatedly) from the network thread. |
94 virtual ConnectionType GetCurrentConnectionType() const = 0; | 132 virtual ConnectionType GetCurrentConnectionType() const = 0; |
95 | 133 |
96 // Replaces the default class factory instance of NetworkChangeNotifier class. | 134 // Replaces the default class factory instance of NetworkChangeNotifier class. |
97 // The method will take over the ownership of |factory| object. | 135 // The method will take over the ownership of |factory| object. |
98 static void SetFactory(NetworkChangeNotifierFactory* factory); | 136 static void SetFactory(NetworkChangeNotifierFactory* factory); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
148 static NetworkChangeNotifier* CreateMock(); | 186 static NetworkChangeNotifier* CreateMock(); |
149 | 187 |
150 // Registers |observer| to receive notifications of network changes. The | 188 // Registers |observer| to receive notifications of network changes. The |
151 // thread on which this is called is the thread on which |observer| will be | 189 // thread on which this is called is the thread on which |observer| will be |
152 // called back with notifications. This is safe to call if Create() has not | 190 // called back with notifications. This is safe to call if Create() has not |
153 // been called (as long as it doesn't race the Create() call on another | 191 // been called (as long as it doesn't race the Create() call on another |
154 // thread), in which case it will simply do nothing. | 192 // thread), in which case it will simply do nothing. |
155 static void AddIPAddressObserver(IPAddressObserver* observer); | 193 static void AddIPAddressObserver(IPAddressObserver* observer); |
156 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); | 194 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); |
157 static void AddDNSObserver(DNSObserver* observer); | 195 static void AddDNSObserver(DNSObserver* observer); |
196 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); | |
158 | 197 |
159 // Unregisters |observer| from receiving notifications. This must be called | 198 // Unregisters |observer| from receiving notifications. This must be called |
160 // on the same thread on which AddObserver() was called. Like AddObserver(), | 199 // on the same thread on which AddObserver() was called. Like AddObserver(), |
161 // this is safe to call if Create() has not been called (as long as it doesn't | 200 // this is safe to call if Create() has not been called (as long as it doesn't |
162 // race the Create() call on another thread), in which case it will simply do | 201 // race the Create() call on another thread), in which case it will simply do |
163 // nothing. Technically, it's also safe to call after the notifier object has | 202 // nothing. Technically, it's also safe to call after the notifier object has |
164 // been destroyed, if the call doesn't race the notifier's destruction, but | 203 // been destroyed, if the call doesn't race the notifier's destruction, but |
165 // there's no reason to use the API in this risky way, so don't do it. | 204 // there's no reason to use the API in this risky way, so don't do it. |
166 static void RemoveIPAddressObserver(IPAddressObserver* observer); | 205 static void RemoveIPAddressObserver(IPAddressObserver* observer); |
167 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); | 206 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); |
168 static void RemoveDNSObserver(DNSObserver* observer); | 207 static void RemoveDNSObserver(DNSObserver* observer); |
208 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); | |
169 | 209 |
170 // Allow unit tests to trigger notifications. | 210 // Allow unit tests to trigger notifications. |
171 static void NotifyObserversOfIPAddressChangeForTests() { | 211 static void NotifyObserversOfIPAddressChangeForTests() { |
172 NotifyObserversOfIPAddressChange(); | 212 NotifyObserversOfIPAddressChange(); |
173 } | 213 } |
174 | 214 |
175 // Return a string equivalent to |type|. | 215 // Return a string equivalent to |type|. |
176 static const char* ConnectionTypeToString(ConnectionType type); | 216 static const char* ConnectionTypeToString(ConnectionType type); |
177 | 217 |
178 // Let the NetworkChangeNotifier know we received some data. | 218 // Let the NetworkChangeNotifier know we received some data. |
179 // This is used strictly for producing histogram data about the accuracy of | 219 // This is used strictly for producing histogram data about the accuracy of |
180 // the NetworkChangenotifier's online detection. | 220 // the NetworkChangenotifier's online detection. |
181 static void NotifyDataReceived(const GURL& source); | 221 static void NotifyDataReceived(const GURL& source); |
182 | 222 |
183 // Register the Observer callbacks for producing histogram data. This | 223 // Register the Observer callbacks for producing histogram data. This |
184 // should be called from the network thread to avoid race conditions. | 224 // should be called from the network thread to avoid race conditions. |
185 static void InitHistogramWatcher(); | 225 static void InitHistogramWatcher(); |
186 | 226 |
187 protected: | 227 protected: |
188 NetworkChangeNotifier(); | 228 // NetworkChanged signal is calculated from the IPAddressChanged and |
229 // ConnectionTypeChanged signals. Delay parameters control how long to delay | |
230 // producing NetworkChanged signal after particular input signals so as to | |
231 // combine duplicates. In other words if an input signal is repeated within | |
232 // the corresponding delay period, only one resulting NetworkChange signal is | |
233 // produced. | |
234 struct NET_EXPORT NetworkChangeCalculatorParams { | |
235 NetworkChangeCalculatorParams(); | |
236 // Controls delay after OnIPAddressChanged when transitioning from an | |
237 // offline state. | |
238 base::TimeDelta ip_address_offline_delay_; | |
239 // Controls delay after OnIPAddressChanged when transitioning from an | |
240 // online state. | |
241 base::TimeDelta ip_address_online_delay_; | |
242 // Controls delay after OnConnectionTypeChanged when transitioning from an | |
243 // offline state. | |
244 base::TimeDelta connection_type_offline_delay_; | |
245 // Controls delay after OnConnectionTypeChanged when transitioning from an | |
246 // online state. | |
247 base::TimeDelta connection_type_online_delay_; | |
248 }; | |
249 | |
250 explicit NetworkChangeNotifier(const NetworkChangeCalculatorParams& params); | |
189 | 251 |
190 #if defined(OS_LINUX) | 252 #if defined(OS_LINUX) |
191 // Returns the AddressTrackerLinux if present. | 253 // Returns the AddressTrackerLinux if present. |
192 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 | 254 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212 |
193 virtual const internal::AddressTrackerLinux* | 255 virtual const internal::AddressTrackerLinux* |
194 GetAddressTrackerInternal() const; | 256 GetAddressTrackerInternal() const; |
195 #endif | 257 #endif |
196 | 258 |
197 // Broadcasts a notification to all registered observers. Note that this | 259 // Broadcasts a notification to all registered observers. Note that this |
198 // happens asynchronously, even for observers on the current thread, even in | 260 // happens asynchronously, even for observers on the current thread, even in |
199 // tests. | 261 // tests. |
200 static void NotifyObserversOfIPAddressChange(); | 262 static void NotifyObserversOfIPAddressChange(); |
201 static void NotifyObserversOfConnectionTypeChange(); | 263 static void NotifyObserversOfConnectionTypeChange(); |
202 static void NotifyObserversOfDNSChange(); | 264 static void NotifyObserversOfDNSChange(); |
265 static void NotifyObserversOfNetworkChange(ConnectionType type); | |
203 | 266 |
204 // Stores |config| in NetworkState and notifies observers. | 267 // Stores |config| in NetworkState and notifies observers. |
205 static void SetDnsConfig(const DnsConfig& config); | 268 static void SetDnsConfig(const DnsConfig& config); |
206 | 269 |
207 private: | 270 private: |
208 friend class HostResolverImplDnsTest; | 271 friend class HostResolverImplDnsTest; |
209 friend class NetworkChangeNotifierAndroidTest; | 272 friend class NetworkChangeNotifierAndroidTest; |
210 friend class NetworkChangeNotifierLinuxTest; | 273 friend class NetworkChangeNotifierLinuxTest; |
211 friend class NetworkChangeNotifierWinTest; | 274 friend class NetworkChangeNotifierWinTest; |
212 | 275 |
213 class NetworkState; | 276 class NetworkState; |
277 class NetworkChangeCalculator; | |
214 | 278 |
215 // Allows a second NetworkChangeNotifier to be created for unit testing, so | 279 // Allows a second NetworkChangeNotifier to be created for unit testing, so |
216 // the test suite can create a MockNetworkChangeNotifier, but platform | 280 // the test suite can create a MockNetworkChangeNotifier, but platform |
217 // specific NetworkChangeNotifiers can also be created for testing. To use, | 281 // specific NetworkChangeNotifiers can also be created for testing. To use, |
218 // create an DisableForTest object, and then create the new | 282 // create an DisableForTest object, and then create the new |
219 // NetworkChangeNotifier object. The NetworkChangeNotifier must be | 283 // NetworkChangeNotifier object. The NetworkChangeNotifier must be |
220 // destroyed before the DisableForTest object, as its destruction will restore | 284 // destroyed before the DisableForTest object, as its destruction will restore |
221 // the original NetworkChangeNotifier. | 285 // the original NetworkChangeNotifier. |
222 class NET_EXPORT_PRIVATE DisableForTest { | 286 class NET_EXPORT_PRIVATE DisableForTest { |
223 public: | 287 public: |
224 DisableForTest(); | 288 DisableForTest(); |
225 ~DisableForTest(); | 289 ~DisableForTest(); |
226 | 290 |
227 private: | 291 private: |
228 // The original NetworkChangeNotifier to be restored on destruction. | 292 // The original NetworkChangeNotifier to be restored on destruction. |
229 NetworkChangeNotifier* network_change_notifier_; | 293 NetworkChangeNotifier* network_change_notifier_; |
230 }; | 294 }; |
231 | 295 |
232 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > | 296 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > |
233 ip_address_observer_list_; | 297 ip_address_observer_list_; |
234 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > | 298 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > |
235 connection_type_observer_list_; | 299 connection_type_observer_list_; |
236 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > | 300 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > |
237 resolver_state_observer_list_; | 301 resolver_state_observer_list_; |
302 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> > | |
303 network_change_observer_list_; | |
238 | 304 |
239 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. | 305 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. |
240 scoped_ptr<NetworkState> network_state_; | 306 scoped_ptr<NetworkState> network_state_; |
241 | 307 |
242 // A little-piggy-back observer that simply logs UMA histogram data. | 308 // A little-piggy-back observer that simply logs UMA histogram data. |
243 scoped_ptr<HistogramWatcher> histogram_watcher_; | 309 scoped_ptr<HistogramWatcher> histogram_watcher_; |
244 | 310 |
311 // Computes NetworkChange signal from IPAddress and ConnectionType signals. | |
312 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; | |
313 | |
245 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); | 314 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); |
246 }; | 315 }; |
247 | 316 |
248 } // namespace net | 317 } // namespace net |
249 | 318 |
250 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ | 319 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ |
OLD | NEW |