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

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

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

Powered by Google App Engine
This is Rietveld 408576698