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
3 // found in the LICENSE file.
5 #ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
6 #define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
8 #include "base/basictypes.h"
9 #include "base/observer_list_threadsafe.h"
10 #include "base/time/time.h"
11 #include "net/base/net_export.h"
18 class HistogramWatcher
;
19 class NetworkChangeNotifierFactory
;
24 class AddressTrackerLinux
;
28 // NetworkChangeNotifier monitors the system for network changes, and notifies
29 // registered observers of those events. Observers may register on any thread,
30 // and will be called back on the thread from which they registered.
31 // NetworkChangeNotifiers are threadsafe, though they must be created and
32 // destroyed on the same thread.
33 class NET_EXPORT NetworkChangeNotifier
{
35 // This is a superset of the connection types in the NetInfo v3 specification:
36 // http://w3c.github.io/netinfo/.
38 CONNECTION_UNKNOWN
= 0, // A connection exists, but its type is unknown.
39 // Also used as a default value.
40 CONNECTION_ETHERNET
= 1,
45 CONNECTION_NONE
= 6, // No connection.
46 CONNECTION_BLUETOOTH
= 7,
47 CONNECTION_LAST
= CONNECTION_BLUETOOTH
50 class NET_EXPORT IPAddressObserver
{
52 // Will be called when the IP address of the primary interface changes.
53 // This includes when the primary interface itself changes.
54 virtual void OnIPAddressChanged() = 0;
57 IPAddressObserver() {}
58 virtual ~IPAddressObserver() {}
61 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver
);
64 class NET_EXPORT ConnectionTypeObserver
{
66 // Will be called when the connection type of the system has changed.
67 // See NetworkChangeNotifier::GetConnectionType() for important caveats
68 // about the unreliability of using this signal to infer the ability to
69 // reach remote sites.
70 virtual void OnConnectionTypeChanged(ConnectionType type
) = 0;
73 ConnectionTypeObserver() {}
74 virtual ~ConnectionTypeObserver() {}
77 DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver
);
80 class NET_EXPORT DNSObserver
{
82 // Will be called when the DNS settings of the system may have changed.
83 // Use GetDnsConfig to obtain the current settings.
84 virtual void OnDNSChanged() = 0;
88 virtual ~DNSObserver() {}
91 DISALLOW_COPY_AND_ASSIGN(DNSObserver
);
94 class NET_EXPORT NetworkChangeObserver
{
96 // OnNetworkChanged will be called when a change occurs to the host
97 // computer's hardware or software that affects the route network packets
98 // take to any network server. Some examples:
99 // 1. A network connection becoming available or going away. For example
100 // plugging or unplugging an Ethernet cable, WiFi or cellular modem
101 // connecting or disconnecting from a network, or a VPN tunnel being
102 // established or taken down.
103 // 2. An active network connection's IP address changes.
104 // 3. A change to the local IP routing tables.
105 // The signal shall only be produced when the change is complete. For
106 // example if a new network connection has become available, only give the
107 // signal once we think the O/S has finished establishing the connection
108 // (i.e. DHCP is done) to the point where the new connection is usable.
109 // The signal shall not be produced spuriously as it will be triggering some
110 // expensive operations, like socket pools closing all connections and
111 // sockets and then re-establishing them.
112 // |type| indicates the type of the active primary network connection after
113 // the change. Observers performing "constructive" activities like trying
114 // to establish a connection to a server should only do so when
115 // |type != CONNECTION_NONE|. Observers performing "destructive" activities
116 // like resetting already established server connections should only do so
117 // when |type == CONNECTION_NONE|. OnNetworkChanged will always be called
118 // with CONNECTION_NONE immediately prior to being called with an online
119 // state; this is done to make sure that destructive actions take place
120 // prior to constructive actions.
121 virtual void OnNetworkChanged(ConnectionType type
) = 0;
124 NetworkChangeObserver() {}
125 virtual ~NetworkChangeObserver() {}
128 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver
);
131 virtual ~NetworkChangeNotifier();
133 // See the description of NetworkChangeNotifier::GetConnectionType().
134 // Implementations must be thread-safe. Implementations must also be
135 // cheap as this could be called (repeatedly) from the network thread.
136 virtual ConnectionType
GetCurrentConnectionType() const = 0;
138 // Replaces the default class factory instance of NetworkChangeNotifier class.
139 // The method will take over the ownership of |factory| object.
140 static void SetFactory(NetworkChangeNotifierFactory
* factory
);
142 // Creates the process-wide, platform-specific NetworkChangeNotifier. The
143 // caller owns the returned pointer. You may call this on any thread. You
144 // may also avoid creating this entirely (in which case nothing will be
145 // monitored), but if you do create it, you must do so before any other
146 // threads try to access the API below, and it must outlive all other threads
147 // which might try to use it.
148 static NetworkChangeNotifier
* Create();
150 // Returns the connection type.
151 // A return value of |CONNECTION_NONE| is a pretty strong indicator that the
152 // user won't be able to connect to remote sites. However, another return
153 // value doesn't imply that the user will be able to connect to remote sites;
154 // even if some link is up, it is uncertain whether a particular connection
155 // attempt to a particular remote site will be successful.
156 // The returned value only describes the connection currently used by the
157 // device, and does not take into account other machines on the network. For
158 // example, if the device is connected using Wifi to a 3G gateway to access
159 // the internet, the connection type is CONNECTION_WIFI.
160 static ConnectionType
GetConnectionType();
162 // Retrieve the last read DnsConfig. This could be expensive if the system has
163 // a large HOSTS file.
164 static void GetDnsConfig(DnsConfig
* config
);
166 #if defined(OS_LINUX)
167 // Returns the AddressTrackerLinux if present.
168 static const internal::AddressTrackerLinux
* GetAddressTracker();
171 // Convenience method to determine if the user is offline.
172 // Returns true if there is currently no internet connection.
174 // A return value of |true| is a pretty strong indicator that the user
175 // won't be able to connect to remote sites. However, a return value of
176 // |false| is inconclusive; even if some link is up, it is uncertain
177 // whether a particular connection attempt to a particular remote site
178 // will be successfully.
179 static bool IsOffline();
181 // Returns true if |type| is a cellular connection.
182 // Returns false if |type| is CONNECTION_UNKNOWN, and thus, depending on the
183 // implementation of GetConnectionType(), it is possible that
184 // IsConnectionCellular(GetConnectionType()) returns false even if the
185 // current connection is cellular.
186 static bool IsConnectionCellular(ConnectionType type
);
188 // Like Create(), but for use in tests. The mock object doesn't monitor any
189 // events, it merely rebroadcasts notifications when requested.
190 static NetworkChangeNotifier
* CreateMock();
192 // Registers |observer| to receive notifications of network changes. The
193 // thread on which this is called is the thread on which |observer| will be
194 // called back with notifications. This is safe to call if Create() has not
195 // been called (as long as it doesn't race the Create() call on another
196 // thread), in which case it will simply do nothing.
197 static void AddIPAddressObserver(IPAddressObserver
* observer
);
198 static void AddConnectionTypeObserver(ConnectionTypeObserver
* observer
);
199 static void AddDNSObserver(DNSObserver
* observer
);
200 static void AddNetworkChangeObserver(NetworkChangeObserver
* observer
);
202 // Unregisters |observer| from receiving notifications. This must be called
203 // on the same thread on which AddObserver() was called. Like AddObserver(),
204 // this is safe to call if Create() has not been called (as long as it doesn't
205 // race the Create() call on another thread), in which case it will simply do
206 // nothing. Technically, it's also safe to call after the notifier object has
207 // been destroyed, if the call doesn't race the notifier's destruction, but
208 // there's no reason to use the API in this risky way, so don't do it.
209 static void RemoveIPAddressObserver(IPAddressObserver
* observer
);
210 static void RemoveConnectionTypeObserver(ConnectionTypeObserver
* observer
);
211 static void RemoveDNSObserver(DNSObserver
* observer
);
212 static void RemoveNetworkChangeObserver(NetworkChangeObserver
* observer
);
214 // Allow unit tests to trigger notifications.
215 static void NotifyObserversOfIPAddressChangeForTests();
216 static void NotifyObserversOfConnectionTypeChangeForTests(
217 ConnectionType type
);
219 // Enable or disable notifications from the host. After setting to true, be
220 // sure to pump the RunLoop until idle to finish any preexisting
222 static void SetTestNotificationsOnly(bool test_only
);
224 // Return a string equivalent to |type|.
225 static const char* ConnectionTypeToString(ConnectionType type
);
227 // Let the NetworkChangeNotifier know we received some data.
228 // This is used for producing histogram data about the accuracy of
229 // the NetworkChangenotifier's online detection and rough network
230 // connection measurements.
231 static void NotifyDataReceived(const URLRequest
& request
, int bytes_read
);
233 // Register the Observer callbacks for producing histogram data. This
234 // should be called from the network thread to avoid race conditions.
235 // ShutdownHistogramWatcher() must be called prior to NetworkChangeNotifier
237 static void InitHistogramWatcher();
239 // Unregister the Observer callbacks for producing histogram data. This
240 // should be called from the network thread to avoid race conditions.
241 static void ShutdownHistogramWatcher();
243 // Log the |NCN.NetworkOperatorMCCMNC| histogram.
244 static void LogOperatorCodeHistogram(ConnectionType type
);
246 // Allows a second NetworkChangeNotifier to be created for unit testing, so
247 // the test suite can create a MockNetworkChangeNotifier, but platform
248 // specific NetworkChangeNotifiers can also be created for testing. To use,
249 // create an DisableForTest object, and then create the new
250 // NetworkChangeNotifier object. The NetworkChangeNotifier must be
251 // destroyed before the DisableForTest object, as its destruction will restore
252 // the original NetworkChangeNotifier.
253 class NET_EXPORT DisableForTest
{
259 // The original NetworkChangeNotifier to be restored on destruction.
260 NetworkChangeNotifier
* network_change_notifier_
;
264 // NetworkChanged signal is calculated from the IPAddressChanged and
265 // ConnectionTypeChanged signals. Delay parameters control how long to delay
266 // producing NetworkChanged signal after particular input signals so as to
267 // combine duplicates. In other words if an input signal is repeated within
268 // the corresponding delay period, only one resulting NetworkChange signal is
270 struct NET_EXPORT NetworkChangeCalculatorParams
{
271 NetworkChangeCalculatorParams();
272 // Controls delay after OnIPAddressChanged when transitioning from an
274 base::TimeDelta ip_address_offline_delay_
;
275 // Controls delay after OnIPAddressChanged when transitioning from an
277 base::TimeDelta ip_address_online_delay_
;
278 // Controls delay after OnConnectionTypeChanged when transitioning from an
280 base::TimeDelta connection_type_offline_delay_
;
281 // Controls delay after OnConnectionTypeChanged when transitioning from an
283 base::TimeDelta connection_type_online_delay_
;
286 explicit NetworkChangeNotifier(
287 const NetworkChangeCalculatorParams
& params
=
288 NetworkChangeCalculatorParams());
290 #if defined(OS_LINUX)
291 // Returns the AddressTrackerLinux if present.
292 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212
293 virtual const internal::AddressTrackerLinux
*
294 GetAddressTrackerInternal() const;
297 // Broadcasts a notification to all registered observers. Note that this
298 // happens asynchronously, even for observers on the current thread, even in
300 static void NotifyObserversOfIPAddressChange();
301 static void NotifyObserversOfConnectionTypeChange();
302 static void NotifyObserversOfDNSChange();
303 static void NotifyObserversOfNetworkChange(ConnectionType type
);
305 // Stores |config| in NetworkState and notifies observers.
306 static void SetDnsConfig(const DnsConfig
& config
);
309 friend class HostResolverImplDnsTest
;
310 friend class NetworkChangeNotifierAndroidTest
;
311 friend class NetworkChangeNotifierLinuxTest
;
312 friend class NetworkChangeNotifierWinTest
;
315 class NetworkChangeCalculator
;
317 void NotifyObserversOfIPAddressChangeImpl();
318 void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type
);
319 void NotifyObserversOfDNSChangeImpl();
320 void NotifyObserversOfNetworkChangeImpl(ConnectionType type
);
322 const scoped_refptr
<ObserverListThreadSafe
<IPAddressObserver
> >
323 ip_address_observer_list_
;
324 const scoped_refptr
<ObserverListThreadSafe
<ConnectionTypeObserver
> >
325 connection_type_observer_list_
;
326 const scoped_refptr
<ObserverListThreadSafe
<DNSObserver
> >
327 resolver_state_observer_list_
;
328 const scoped_refptr
<ObserverListThreadSafe
<NetworkChangeObserver
> >
329 network_change_observer_list_
;
331 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig.
332 scoped_ptr
<NetworkState
> network_state_
;
334 // A little-piggy-back observer that simply logs UMA histogram data.
335 scoped_ptr
<HistogramWatcher
> histogram_watcher_
;
337 // Computes NetworkChange signal from IPAddress and ConnectionType signals.
338 scoped_ptr
<NetworkChangeCalculator
> network_change_calculator_
;
340 // Set true to disable non-test notifications (to prevent flakes in tests).
341 bool test_notifications_only_
;
343 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier
);
348 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_