Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: net/base/network_change_notifier.h

Issue 11620007: Switch from OnIPAddressChanged and OnConnectionTypeChange to OnNetworkChanged Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/time.h"
11 #include "net/base/net_export.h" 11 #include "net/base/net_export.h"
12 #if defined(OS_WIN)
13 #include "net/dns/dns_config_service_win.h"
14 #endif
12 15
13 class GURL; 16 class GURL;
14 17
18 // Temporary layering violation to allow existing users of a deprecated
19 // interface.
20 class IOThreadLoggingNetworkChangeObserver;
21
22 class NetWatcher;
23
15 namespace net { 24 namespace net {
16 25
17 struct DnsConfig; 26 struct DnsConfig;
18 class HistogramWatcher;
19 class NetworkChangeNotifierFactory; 27 class NetworkChangeNotifierFactory;
20 28
21 #if defined(OS_LINUX) 29 #if defined(OS_LINUX)
22 namespace internal { 30 namespace internal {
23 class AddressTrackerLinux; 31 class AddressTrackerLinux;
24 } 32 }
25 #endif 33 #endif
26 34
27 // NetworkChangeNotifier monitors the system for network changes, and notifies 35 // NetworkChangeNotifier monitors the system for network changes, and notifies
28 // registered observers of those events. Observers may register on any thread, 36 // registered observers of those events. Observers may register on any thread,
29 // and will be called back on the thread from which they registered. 37 // and will be called back on the thread from which they registered.
30 // NetworkChangeNotifiers are threadsafe, though they must be created and 38 // NetworkChangeNotifiers are threadsafe, though they must be created and
31 // destroyed on the same thread. 39 // destroyed on the same thread.
32 class NET_EXPORT NetworkChangeNotifier { 40 class NET_EXPORT NetworkChangeNotifier {
33 public: 41 public:
34 // Using the terminology of the Network Information API: 42 // Using the terminology of the Network Information API:
35 // http://www.w3.org/TR/netinfo-api. 43 // http://www.w3.org/TR/netinfo-api.
36 enum ConnectionType { 44 enum ConnectionType {
37 CONNECTION_UNKNOWN = 0, // A connection exists, but its type is unknown. 45 CONNECTION_UNKNOWN = 0, // A connection exists, but its type is unknown.
38 CONNECTION_ETHERNET = 1, 46 CONNECTION_ETHERNET = 1,
39 CONNECTION_WIFI = 2, 47 CONNECTION_WIFI = 2,
40 CONNECTION_2G = 3, 48 CONNECTION_2G = 3,
41 CONNECTION_3G = 4, 49 CONNECTION_3G = 4,
42 CONNECTION_4G = 5, 50 CONNECTION_4G = 5,
43 CONNECTION_NONE = 6 // No connection. 51 CONNECTION_NONE = 6 // No connection.
44 }; 52 };
45 53
54 class HistogramWatcher;
55 class NetworkChangeCalculator;
56
46 class NET_EXPORT IPAddressObserver { 57 class NET_EXPORT IPAddressObserver {
47 public: 58 public:
48 // Will be called when the IP address of the primary interface changes. 59 // Will be called when the IP address of the primary interface changes.
49 // This includes when the primary interface itself changes. 60 // This includes when the primary interface itself changes.
50 virtual void OnIPAddressChanged() = 0; 61 virtual void OnIPAddressChanged() = 0;
51 62
52 protected: 63 protected:
53 IPAddressObserver() {} 64 IPAddressObserver() {}
54 virtual ~IPAddressObserver() {} 65 virtual ~IPAddressObserver() {}
55 66
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 194
184 // Like Create(), but for use in tests. The mock object doesn't monitor any 195 // Like Create(), but for use in tests. The mock object doesn't monitor any
185 // events, it merely rebroadcasts notifications when requested. 196 // events, it merely rebroadcasts notifications when requested.
186 static NetworkChangeNotifier* CreateMock(); 197 static NetworkChangeNotifier* CreateMock();
187 198
188 // Registers |observer| to receive notifications of network changes. The 199 // Registers |observer| to receive notifications of network changes. The
189 // thread on which this is called is the thread on which |observer| will be 200 // thread on which this is called is the thread on which |observer| will be
190 // called back with notifications. This is safe to call if Create() has not 201 // called back with notifications. This is safe to call if Create() has not
191 // been called (as long as it doesn't race the Create() call on another 202 // been called (as long as it doesn't race the Create() call on another
192 // thread), in which case it will simply do nothing. 203 // thread), in which case it will simply do nothing.
193 static void AddIPAddressObserver(IPAddressObserver* observer);
194 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
195 static void AddDNSObserver(DNSObserver* observer); 204 static void AddDNSObserver(DNSObserver* observer);
196 static void AddNetworkChangeObserver(NetworkChangeObserver* observer); 205 static void AddNetworkChangeObserver(NetworkChangeObserver* observer);
206 // IPAddress and ConnectionType notifications are now only used for logging
207 // and calculation of NetworkChanged signal. Users are explicitly friended.
208 class NET_EXPORT Deprecated {
209 private:
210 // TODO(pauljensen): Delete these 6-12 months after transition to
211 // NetworkChange signal when they are no longer needed.
212 friend class HistogramWatcher;
213 friend class ::IOThreadLoggingNetworkChangeObserver;
214 friend class ::NetWatcher;
215 // TODO(pauljensen): Change this to a direct call instead of Observer once
216 // it's the only remaining Deprecated user.
217 friend class NetworkChangeCalculator;
218 // TODO(pauljensen): This should either become a direct call from
219 // NetworkChangeNotifierWin or use NotifyAddrChange() itself.
szym 2013/01/20 06:52:08 I suggest you file a bug specifically for that. Yo
220 #if defined(OS_WIN)
221 friend class internal::DnsConfigServiceWin::Watcher;
222 #endif
223
224 static void AddIPAddressObserver(IPAddressObserver* observer);
225 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
226
227 DISALLOW_IMPLICIT_CONSTRUCTORS(Deprecated);
228 };
197 229
198 // Unregisters |observer| from receiving notifications. This must be called 230 // Unregisters |observer| from receiving notifications. This must be called
199 // on the same thread on which AddObserver() was called. Like AddObserver(), 231 // on the same thread on which AddObserver() was called. Like AddObserver(),
200 // this is safe to call if Create() has not been called (as long as it doesn't 232 // this is safe to call if Create() has not been called (as long as it doesn't
201 // race the Create() call on another thread), in which case it will simply do 233 // race the Create() call on another thread), in which case it will simply do
202 // nothing. Technically, it's also safe to call after the notifier object has 234 // nothing. Technically, it's also safe to call after the notifier object has
203 // been destroyed, if the call doesn't race the notifier's destruction, but 235 // been destroyed, if the call doesn't race the notifier's destruction, but
204 // there's no reason to use the API in this risky way, so don't do it. 236 // there's no reason to use the API in this risky way, so don't do it.
205 static void RemoveIPAddressObserver(IPAddressObserver* observer); 237 static void RemoveIPAddressObserver(IPAddressObserver* observer);
206 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); 238 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer);
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 static void SetDnsConfig(const DnsConfig& config); 302 static void SetDnsConfig(const DnsConfig& config);
271 303
272 private: 304 private:
273 friend class HostResolverImplDnsTest; 305 friend class HostResolverImplDnsTest;
274 friend class NetworkChangeNotifierAndroidTest; 306 friend class NetworkChangeNotifierAndroidTest;
275 friend class NetworkChangeNotifierLinuxTest; 307 friend class NetworkChangeNotifierLinuxTest;
276 friend class NetworkChangeNotifierWinTest; 308 friend class NetworkChangeNotifierWinTest;
277 friend class URLFetcherMockDnsTest; 309 friend class URLFetcherMockDnsTest;
278 310
279 class NetworkState; 311 class NetworkState;
280 class NetworkChangeCalculator;
281 312
282 // Allows a second NetworkChangeNotifier to be created for unit testing, so 313 // Allows a second NetworkChangeNotifier to be created for unit testing, so
283 // the test suite can create a MockNetworkChangeNotifier, but platform 314 // the test suite can create a MockNetworkChangeNotifier, but platform
284 // specific NetworkChangeNotifiers can also be created for testing. To use, 315 // specific NetworkChangeNotifiers can also be created for testing. To use,
285 // create an DisableForTest object, and then create the new 316 // create an DisableForTest object, and then create the new
286 // NetworkChangeNotifier object. The NetworkChangeNotifier must be 317 // NetworkChangeNotifier object. The NetworkChangeNotifier must be
287 // destroyed before the DisableForTest object, as its destruction will restore 318 // destroyed before the DisableForTest object, as its destruction will restore
288 // the original NetworkChangeNotifier. 319 // the original NetworkChangeNotifier.
289 class NET_EXPORT_PRIVATE DisableForTest { 320 class NET_EXPORT_PRIVATE DisableForTest {
290 public: 321 public:
291 DisableForTest(); 322 DisableForTest();
292 ~DisableForTest(); 323 ~DisableForTest();
293 324
294 private: 325 private:
295 // The original NetworkChangeNotifier to be restored on destruction. 326 // The original NetworkChangeNotifier to be restored on destruction.
296 NetworkChangeNotifier* network_change_notifier_; 327 NetworkChangeNotifier* network_change_notifier_;
297 }; 328 };
298 329
330 // Allow unit tests to reset parameters for NetworkChangeCalculator.
331 void ResetNetworkChangeCalculatorParamsForTest(
332 NetworkChangeCalculatorParams params);
333
299 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > 334 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> >
300 ip_address_observer_list_; 335 ip_address_observer_list_;
301 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > 336 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> >
302 connection_type_observer_list_; 337 connection_type_observer_list_;
303 const scoped_refptr<ObserverListThreadSafe<DNSObserver> > 338 const scoped_refptr<ObserverListThreadSafe<DNSObserver> >
304 resolver_state_observer_list_; 339 resolver_state_observer_list_;
305 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> > 340 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> >
306 network_change_observer_list_; 341 network_change_observer_list_;
307 342
308 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. 343 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig.
309 scoped_ptr<NetworkState> network_state_; 344 scoped_ptr<NetworkState> network_state_;
310 345
311 // A little-piggy-back observer that simply logs UMA histogram data. 346 // A little-piggy-back observer that simply logs UMA histogram data.
312 scoped_ptr<HistogramWatcher> histogram_watcher_; 347 scoped_ptr<HistogramWatcher> histogram_watcher_;
313 348
314 // Computes NetworkChange signal from IPAddress and ConnectionType signals. 349 // Computes NetworkChange signal from IPAddress and ConnectionType signals.
315 scoped_ptr<NetworkChangeCalculator> network_change_calculator_; 350 scoped_ptr<NetworkChangeCalculator> network_change_calculator_;
316 351
317 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier); 352 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier);
318 }; 353 };
319 354
320 } // namespace net 355 } // namespace net
321 356
322 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_ 357 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698