blob: 63e661a4940723167ce8730bcfabc20778b60ff9 [file] [log] [blame]
[email protected]446df2952012-02-28 07:22:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]100d5fb92009-12-21 21:08:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
6#define NET_BASE_NETWORK_CHANGE_NOTIFIER_H_
7
Avi Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
danakj7f767e62016-04-16 23:20:2310#include <memory>
derekjchow5482d5e2015-01-31 01:04:5111#include <vector>
12
pauljensenbe5bc3232015-10-05 20:39:2713#include "base/macros.h"
[email protected]66761b952010-06-25 21:30:3814#include "base/observer_list_threadsafe.h"
[email protected]9da992db2013-06-28 05:40:4715#include "base/time/time.h"
[email protected]172da1b2011-08-12 15:52:2616#include "net/base/net_export.h"
[email protected]100d5fb92009-12-21 21:08:3517
18namespace net {
19
[email protected]bb0e34542012-08-31 19:52:4020struct DnsConfig;
[email protected]373badd2011-07-11 21:55:5821class NetworkChangeNotifierFactory;
derekjchow5482d5e2015-01-31 01:04:5122struct NetworkInterface;
23typedef std::vector<NetworkInterface> NetworkInterfaceList;
[email protected]373badd2011-07-11 21:55:5824
[email protected]67172802012-07-16 22:27:2425#if defined(OS_LINUX)
[email protected]bb0e34542012-08-31 19:52:4026namespace internal {
[email protected]67172802012-07-16 22:27:2427class AddressTrackerLinux;
[email protected]05aad32d2012-05-16 18:10:5328}
[email protected]bb0e34542012-08-31 19:52:4029#endif
[email protected]05aad32d2012-05-16 18:10:5330
[email protected]100d5fb92009-12-21 21:08:3531// NetworkChangeNotifier monitors the system for network changes, and notifies
[email protected]66761b952010-06-25 21:30:3832// registered observers of those events. Observers may register on any thread,
33// and will be called back on the thread from which they registered.
[email protected]aab66932011-10-05 17:08:1334// NetworkChangeNotifiers are threadsafe, though they must be created and
35// destroyed on the same thread.
[email protected]172da1b2011-08-12 15:52:2636class NET_EXPORT NetworkChangeNotifier {
[email protected]100d5fb92009-12-21 21:08:3537 public:
[email protected]078cab22014-06-04 14:08:4038 // This is a superset of the connection types in the NetInfo v3 specification:
39 // http://w3c.github.io/netinfo/.
jkarlin0818d5252014-12-02 21:06:0240 //
41 // A Java counterpart will be generated for this enum.
42 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
gayane4849a8072015-03-27 16:49:2843 //
44 // New enum values should only be added to the end of the enum and no values
45 // should be modified or reused, as this is reported via UMA.
[email protected]8bbc7a792012-05-24 11:30:0546 enum ConnectionType {
[email protected]d7ff5fb2014-05-29 19:50:2547 CONNECTION_UNKNOWN = 0, // A connection exists, but its type is unknown.
48 // Also used as a default value.
[email protected]e0535b02012-09-11 00:30:3749 CONNECTION_ETHERNET = 1,
50 CONNECTION_WIFI = 2,
51 CONNECTION_2G = 3,
52 CONNECTION_3G = 4,
53 CONNECTION_4G = 5,
[email protected]d7ff5fb2014-05-29 19:50:2554 CONNECTION_NONE = 6, // No connection.
[email protected]078cab22014-06-04 14:08:4055 CONNECTION_BLUETOOTH = 7,
56 CONNECTION_LAST = CONNECTION_BLUETOOTH
[email protected]a6b32f6e2012-06-20 23:46:2657 };
[email protected]8bbc7a792012-05-24 11:30:0558
jkarlin0818d5252014-12-02 21:06:0259 // This is the NetInfo v3 set of connection technologies as seen in
bmcquade3f55de792016-02-26 22:10:3760 // http://w3c.github.io/netinfo/. This enum is duplicated in histograms.xml
61 // so be sure to change both at once. Additionally, since this enum is used in
62 // a UMA histogram, it should not be re-ordered and any new values should be
63 // added to the end.
jkarlin0818d5252014-12-02 21:06:0264 //
65 // A Java counterpart will be generated for this enum.
66 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
67 enum ConnectionSubtype {
bmcquade25781b42016-02-26 05:41:1468 SUBTYPE_UNKNOWN = 0,
69 SUBTYPE_NONE,
70 SUBTYPE_OTHER,
71 SUBTYPE_GSM,
jkarlin0818d5252014-12-02 21:06:0272 SUBTYPE_IDEN,
73 SUBTYPE_CDMA,
74 SUBTYPE_1XRTT,
75 SUBTYPE_GPRS,
76 SUBTYPE_EDGE,
77 SUBTYPE_UMTS,
78 SUBTYPE_EVDO_REV_0,
79 SUBTYPE_EVDO_REV_A,
80 SUBTYPE_HSPA,
81 SUBTYPE_EVDO_REV_B,
82 SUBTYPE_HSDPA,
83 SUBTYPE_HSUPA,
84 SUBTYPE_EHRPD,
85 SUBTYPE_HSPAP,
86 SUBTYPE_LTE,
87 SUBTYPE_LTE_ADVANCED,
88 SUBTYPE_BLUETOOTH_1_2,
89 SUBTYPE_BLUETOOTH_2_1,
90 SUBTYPE_BLUETOOTH_3_0,
91 SUBTYPE_BLUETOOTH_4_0,
92 SUBTYPE_ETHERNET,
93 SUBTYPE_FAST_ETHERNET,
94 SUBTYPE_GIGABIT_ETHERNET,
95 SUBTYPE_10_GIGABIT_ETHERNET,
96 SUBTYPE_WIFI_B,
97 SUBTYPE_WIFI_G,
98 SUBTYPE_WIFI_N,
99 SUBTYPE_WIFI_AC,
100 SUBTYPE_WIFI_AD,
bmcquade25781b42016-02-26 05:41:14101 SUBTYPE_LAST = SUBTYPE_WIFI_AD
jkarlin0818d5252014-12-02 21:06:02102 };
103
Helen Lif234a7f2017-08-11 16:06:51104 // DEPRECATED. Please use NetworkChangeObserver instead. crbug.com/754695.
[email protected]172da1b2011-08-12 15:52:26105 class NET_EXPORT IPAddressObserver {
[email protected]100d5fb92009-12-21 21:08:35106 public:
[email protected]100d5fb92009-12-21 21:08:35107 // Will be called when the IP address of the primary interface changes.
108 // This includes when the primary interface itself changes.
109 virtual void OnIPAddressChanged() = 0;
110
111 protected:
[email protected]232a5812011-03-04 22:42:08112 IPAddressObserver() {}
[email protected]e0845d5f2012-05-29 00:11:41113 virtual ~IPAddressObserver() {}
[email protected]100d5fb92009-12-21 21:08:35114
115 private:
[email protected]232a5812011-03-04 22:42:08116 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver);
117 };
118
Helen Lif234a7f2017-08-11 16:06:51119 // DEPRECATED. Please use NetworkChangeObserver instead. crbug.com/754695.
[email protected]8bbc7a792012-05-24 11:30:05120 class NET_EXPORT ConnectionTypeObserver {
[email protected]232a5812011-03-04 22:42:08121 public:
[email protected]e0845d5f2012-05-29 00:11:41122 // Will be called when the connection type of the system has changed.
123 // See NetworkChangeNotifier::GetConnectionType() for important caveats
124 // about the unreliability of using this signal to infer the ability to
125 // reach remote sites.
[email protected]8bbc7a792012-05-24 11:30:05126 virtual void OnConnectionTypeChanged(ConnectionType type) = 0;
[email protected]232a5812011-03-04 22:42:08127
128 protected:
[email protected]8bbc7a792012-05-24 11:30:05129 ConnectionTypeObserver() {}
[email protected]e0845d5f2012-05-29 00:11:41130 virtual ~ConnectionTypeObserver() {}
[email protected]232a5812011-03-04 22:42:08131
132 private:
[email protected]8bbc7a792012-05-24 11:30:05133 DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver);
[email protected]100d5fb92009-12-21 21:08:35134 };
135
[email protected]46018c9d2011-09-06 03:42:34136 class NET_EXPORT DNSObserver {
137 public:
[email protected]446df2952012-02-28 07:22:51138 // Will be called when the DNS settings of the system may have changed.
[email protected]bb0e34542012-08-31 19:52:40139 // Use GetDnsConfig to obtain the current settings.
140 virtual void OnDNSChanged() = 0;
pauljensen101ed372015-04-17 00:11:42141 // Will be called when DNS settings of the system have been loaded.
142 // Use GetDnsConfig to obtain the current settings.
Paul Jensen1ef04a32018-05-04 15:12:15143 // NOTE(pauljensen): This will not be called if the initial DNS config
144 // has already been read before this observer is registered.
145 // Determining if a DNS config has already been read can be done by
146 // calling GetDnsConfig() after registering an observer, and seeing if
147 // the DnsConfig's IsValid() returns true.
pauljensen101ed372015-04-17 00:11:42148 virtual void OnInitialDNSConfigRead();
[email protected]46018c9d2011-09-06 03:42:34149
150 protected:
151 DNSObserver() {}
[email protected]e0845d5f2012-05-29 00:11:41152 virtual ~DNSObserver() {}
[email protected]46018c9d2011-09-06 03:42:34153
154 private:
155 DISALLOW_COPY_AND_ASSIGN(DNSObserver);
156 };
157
[email protected]03848872012-12-08 02:46:41158 class NET_EXPORT NetworkChangeObserver {
159 public:
160 // OnNetworkChanged will be called when a change occurs to the host
161 // computer's hardware or software that affects the route network packets
162 // take to any network server. Some examples:
163 // 1. A network connection becoming available or going away. For example
164 // plugging or unplugging an Ethernet cable, WiFi or cellular modem
165 // connecting or disconnecting from a network, or a VPN tunnel being
166 // established or taken down.
167 // 2. An active network connection's IP address changes.
168 // 3. A change to the local IP routing tables.
169 // The signal shall only be produced when the change is complete. For
170 // example if a new network connection has become available, only give the
171 // signal once we think the O/S has finished establishing the connection
172 // (i.e. DHCP is done) to the point where the new connection is usable.
173 // The signal shall not be produced spuriously as it will be triggering some
174 // expensive operations, like socket pools closing all connections and
175 // sockets and then re-establishing them.
176 // |type| indicates the type of the active primary network connection after
177 // the change. Observers performing "constructive" activities like trying
178 // to establish a connection to a server should only do so when
179 // |type != CONNECTION_NONE|. Observers performing "destructive" activities
180 // like resetting already established server connections should only do so
181 // when |type == CONNECTION_NONE|. OnNetworkChanged will always be called
182 // with CONNECTION_NONE immediately prior to being called with an online
183 // state; this is done to make sure that destructive actions take place
184 // prior to constructive actions.
185 virtual void OnNetworkChanged(ConnectionType type) = 0;
186
187 protected:
188 NetworkChangeObserver() {}
189 virtual ~NetworkChangeObserver() {}
190
191 private:
192 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserver);
193 };
194
jkarlin7b25e6c2014-12-16 19:59:47195 class NET_EXPORT MaxBandwidthObserver {
196 public:
jkarlinbce491862015-09-18 15:14:07197 // Called when a change occurs to the network's maximum bandwidth as
198 // defined in http://w3c.github.io/netinfo/. Also called on type change,
199 // even if the maximum bandwidth doesn't change. See the documentation of
200 // GetMaxBanwidthAndConnectionType for what to expect for the values of
201 // |max_bandwidth_mbps|.
202 virtual void OnMaxBandwidthChanged(double max_bandwidth_mbps,
203 ConnectionType type) = 0;
jkarlin7b25e6c2014-12-16 19:59:47204
205 protected:
206 MaxBandwidthObserver() {}
207 virtual ~MaxBandwidthObserver() {}
208
209 private:
210 DISALLOW_COPY_AND_ASSIGN(MaxBandwidthObserver);
211 };
212
pauljensenbe5bc3232015-10-05 20:39:27213 // Opaque handle for device-wide connection to a particular network. For
214 // example an association with a particular WiFi network with a particular
215 // SSID or a connection to particular cellular network.
216 // The meaning of this handle is target-dependent. On Android NetworkHandles
pauljensen479143c2016-07-22 12:35:09217 // are equivalent to:
218 // On Lollipop, the framework's concept of NetIDs (e.g. Network.netId), and
219 // On Marshmallow and newer releases, network handles
220 // (e.g. Network.getNetworkHandle()).
221 typedef int64_t NetworkHandle;
[email protected]100d5fb92009-12-21 21:08:35222
pauljensenbe5bc3232015-10-05 20:39:27223 // A list of networks.
224 typedef std::vector<NetworkHandle> NetworkList;
225
226 // An interface that when implemented and added via AddNeworkObserver(),
227 // provides notifications when networks come and go.
228 // Only implemented for Android (Lollipop and newer), no callbacks issued when
229 // unimplemented.
230 class NET_EXPORT NetworkObserver {
231 public:
232 // Called when device connects to |network|. For example device associates
233 // with a WiFi access point. This does not imply the network has Internet
234 // access as it may well be behind a captive portal.
235 virtual void OnNetworkConnected(NetworkHandle network) = 0;
236 // Called when device disconnects from |network|.
237 virtual void OnNetworkDisconnected(NetworkHandle network) = 0;
238 // Called when device determines the connection to |network| is no longer
239 // preferred, for example when a device transitions from cellular to WiFi
240 // it might deem the cellular connection no longer preferred. The device
241 // will disconnect from |network| in a period of time (30s on Android),
242 // allowing network communications via |network| to wrap up.
243 virtual void OnNetworkSoonToDisconnect(NetworkHandle network) = 0;
244 // Called when |network| is made the default network for communication.
245 virtual void OnNetworkMadeDefault(NetworkHandle network) = 0;
246
247 protected:
248 NetworkObserver() {}
249 virtual ~NetworkObserver() {}
250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(NetworkObserver);
253 };
254
255 // An invalid NetworkHandle.
256 static const NetworkHandle kInvalidNetworkHandle;
257
258 virtual ~NetworkChangeNotifier();
[email protected]2d3b7762010-10-09 00:35:47259
Pengchengeaf5ae22018-01-18 01:57:17260 // Returns the factory or nullptr if it is not set.
261 static NetworkChangeNotifierFactory* GetFactory();
262
[email protected]373badd2011-07-11 21:55:58263 // Replaces the default class factory instance of NetworkChangeNotifier class.
264 // The method will take over the ownership of |factory| object.
265 static void SetFactory(NetworkChangeNotifierFactory* factory);
266
[email protected]66761b952010-06-25 21:30:38267 // Creates the process-wide, platform-specific NetworkChangeNotifier. The
268 // caller owns the returned pointer. You may call this on any thread. You
269 // may also avoid creating this entirely (in which case nothing will be
270 // monitored), but if you do create it, you must do so before any other
271 // threads try to access the API below, and it must outlive all other threads
272 // which might try to use it.
273 static NetworkChangeNotifier* Create();
[email protected]100d5fb92009-12-21 21:08:35274
Helen Lid19d0f02017-10-23 13:49:57275 // Returns whether the process-wide, platform-specific NetworkChangeNotifier
276 // has been created.
277 static bool HasNetworkChangeNotifier();
278
[email protected]8bbc7a792012-05-24 11:30:05279 // Returns the connection type.
280 // A return value of |CONNECTION_NONE| is a pretty strong indicator that the
281 // user won't be able to connect to remote sites. However, another return
282 // value doesn't imply that the user will be able to connect to remote sites;
283 // even if some link is up, it is uncertain whether a particular connection
284 // attempt to a particular remote site will be successful.
tbansal880e49e72017-03-25 04:44:25285 // The returned value only describes the first-hop connection, for example if
286 // the device is connected via WiFi to a 4G hotspot, the returned value will
287 // be CONNECTION_WIFI, not CONNECTION_4G.
[email protected]8bbc7a792012-05-24 11:30:05288 static ConnectionType GetConnectionType();
289
tbansal880e49e72017-03-25 04:44:25290 // Returns the device's current default active network connection's subtype.
291 // The returned value only describes the first-hop connection, for example if
292 // the device is connected via WiFi to a 4G hotspot, the returned value will
293 // reflect WiFi, not 4G. This method may return SUBTYPE_UNKNOWN even if the
294 // connection type is known.
295 static ConnectionSubtype GetConnectionSubtype();
296
jkarlinbce491862015-09-18 15:14:07297 // Sets |max_bandwidth_mbps| to a theoretical upper limit on download
298 // bandwidth, potentially based on underlying connection type, signal
299 // strength, or some other signal. If the network subtype is unknown then
300 // |max_bandwidth_mbps| is set to +Infinity and if there is no network
301 // connection then it is set to 0.0. The circumstances in which a more
302 // specific value is given are: when an Android device is connected to a
303 // cellular or WiFi network, and when a ChromeOS device is connected to a
304 // cellular network. See the NetInfo spec for the mapping of
305 // specific subtypes to bandwidth values: http://w3c.github.io/netinfo/.
306 // |connection_type| is set to the current active default network's connection
307 // type.
308 static void GetMaxBandwidthAndConnectionType(double* max_bandwidth_mbps,
309 ConnectionType* connection_type);
jkarlin59a810932014-10-06 18:00:43310
jkarlin300a1d22015-09-18 19:32:52311 // Returns a theoretical upper limit (in Mbps) on download bandwidth given a
312 // connection subtype. The mapping of connection type to maximum bandwidth is
313 // provided in the NetInfo spec: http://w3c.github.io/netinfo/.
zhuoyu.qian4d651402017-10-30 13:03:33314 static double GetMaxBandwidthMbpsForConnectionSubtype(
315 ConnectionSubtype subtype);
jkarlin300a1d22015-09-18 19:32:52316
jric94943b2015-12-03 18:50:22317 // Returns true if the platform supports use of APIs based on NetworkHandles.
318 // Public methods that use NetworkHandles are GetNetworkConnectionType(),
319 // GetNetworkConnectionType(), GetDefaultNetwork(), AddNetworkObserver(),
320 // RemoveNetworkObserver(), and all public NetworkObserver methods.
321 static bool AreNetworkHandlesSupported();
322
pauljensenbe5bc3232015-10-05 20:39:27323 // Sets |network_list| to a list of all networks that are currently connected.
324 // Only implemented for Android (Lollipop and newer), leaves |network_list|
jric94943b2015-12-03 18:50:22325 // empty when unimplemented. Requires NetworkHandles support, see
326 // AreNetworkHandlesSupported().
pauljensenbe5bc3232015-10-05 20:39:27327 static void GetConnectedNetworks(NetworkList* network_list);
328
329 // Returns the type of connection |network| uses. Note that this may vary
330 // slightly over time (e.g. CONNECTION_2G to CONNECTION_3G). If |network|
331 // is no longer connected, it will return CONNECTION_UNKNOWN.
332 // Only implemented for Android (Lollipop and newer), returns
jric94943b2015-12-03 18:50:22333 // CONNECTION_UNKNOWN when unimplemented. Requires NetworkHandles support,
334 // see AreNetworkHandlesSupported().
pauljensenbe5bc3232015-10-05 20:39:27335 static ConnectionType GetNetworkConnectionType(NetworkHandle network);
336
337 // Returns the device's current default network connection. This is the
338 // network used for newly created socket communication for sockets that are
339 // not explicitly bound to a particular network (e.g. via
340 // DatagramClientSocket.BindToNetwork). Returns |kInvalidNetworkHandle| if
341 // there is no default connected network.
342 // Only implemented for Android (Lollipop and newer), returns
343 // |kInvalidNetworkHandle| when unimplemented.
jric94943b2015-12-03 18:50:22344 // Requires NetworkHandles support, see AreNetworkHandlesSupported().
pauljensenbe5bc3232015-10-05 20:39:27345 static NetworkHandle GetDefaultNetwork();
346
[email protected]bb0e34542012-08-31 19:52:40347 // Retrieve the last read DnsConfig. This could be expensive if the system has
348 // a large HOSTS file.
349 static void GetDnsConfig(DnsConfig* config);
350
[email protected]67172802012-07-16 22:27:24351#if defined(OS_LINUX)
352 // Returns the AddressTrackerLinux if present.
353 static const internal::AddressTrackerLinux* GetAddressTracker();
354#endif
355
[email protected]8bbc7a792012-05-24 11:30:05356 // Convenience method to determine if the user is offline.
[email protected]2d3b7762010-10-09 00:35:47357 // Returns true if there is currently no internet connection.
358 //
359 // A return value of |true| is a pretty strong indicator that the user
360 // won't be able to connect to remote sites. However, a return value of
361 // |false| is inconclusive; even if some link is up, it is uncertain
362 // whether a particular connection attempt to a particular remote site
363 // will be successfully.
[email protected]720f2afe2012-11-05 14:05:54364 static bool IsOffline();
365
366 // Returns true if |type| is a cellular connection.
367 // Returns false if |type| is CONNECTION_UNKNOWN, and thus, depending on the
368 // implementation of GetConnectionType(), it is possible that
369 // IsConnectionCellular(GetConnectionType()) returns false even if the
370 // current connection is cellular.
371 static bool IsConnectionCellular(ConnectionType type);
[email protected]2d3b7762010-10-09 00:35:47372
derekjchow5482d5e2015-01-31 01:04:51373 // Gets the current connection type based on |interfaces|. Returns
374 // CONNECTION_NONE if there are no interfaces, CONNECTION_UNKNOWN if two
375 // interfaces have different connection types or the connection type of all
376 // interfaces if they have the same interface type.
377 static ConnectionType ConnectionTypeFromInterfaceList(
378 const NetworkInterfaceList& interfaces);
379
[email protected]66761b952010-06-25 21:30:38380 // Like Create(), but for use in tests. The mock object doesn't monitor any
381 // events, it merely rebroadcasts notifications when requested.
[email protected]2d3b7762010-10-09 00:35:47382 static NetworkChangeNotifier* CreateMock();
[email protected]66761b952010-06-25 21:30:38383
384 // Registers |observer| to receive notifications of network changes. The
385 // thread on which this is called is the thread on which |observer| will be
386 // called back with notifications. This is safe to call if Create() has not
387 // been called (as long as it doesn't race the Create() call on another
388 // thread), in which case it will simply do nothing.
Helen Lif234a7f2017-08-11 16:06:51389
390 // DEPRECATED. IPAddressObserver is deprecated. Please use
391 // NetworkChangeObserver instead. crbug.com/754695.
[email protected]232a5812011-03-04 22:42:08392 static void AddIPAddressObserver(IPAddressObserver* observer);
Helen Lif234a7f2017-08-11 16:06:51393 // DEPRECATED. ConnectionTypeObserver is deprecated. Please use
394 // NetworkChangeObserver instead. crbug.com/754695.
[email protected]8bbc7a792012-05-24 11:30:05395 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
[email protected]46018c9d2011-09-06 03:42:34396 static void AddDNSObserver(DNSObserver* observer);
[email protected]03848872012-12-08 02:46:41397 static void AddNetworkChangeObserver(NetworkChangeObserver* observer);
jkarlin7b25e6c2014-12-16 19:59:47398 static void AddMaxBandwidthObserver(MaxBandwidthObserver* observer);
pauljensenbe5bc3232015-10-05 20:39:27399 static void AddNetworkObserver(NetworkObserver* observer);
[email protected]66761b952010-06-25 21:30:38400
401 // Unregisters |observer| from receiving notifications. This must be called
402 // on the same thread on which AddObserver() was called. Like AddObserver(),
403 // this is safe to call if Create() has not been called (as long as it doesn't
404 // race the Create() call on another thread), in which case it will simply do
405 // nothing. Technically, it's also safe to call after the notifier object has
406 // been destroyed, if the call doesn't race the notifier's destruction, but
407 // there's no reason to use the API in this risky way, so don't do it.
Helen Lif234a7f2017-08-11 16:06:51408
409 // DEPRECATED. IPAddressObserver is deprecated. Please use
410 // NetworkChangeObserver instead. crbug.com/754695.
[email protected]232a5812011-03-04 22:42:08411 static void RemoveIPAddressObserver(IPAddressObserver* observer);
Helen Lif234a7f2017-08-11 16:06:51412 // DEPRECATED. ConnectionTypeObserver is deprecated. Please use
413 // NetworkChangeObserver instead. crbug.com/754695.
[email protected]8bbc7a792012-05-24 11:30:05414 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer);
[email protected]46018c9d2011-09-06 03:42:34415 static void RemoveDNSObserver(DNSObserver* observer);
[email protected]03848872012-12-08 02:46:41416 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer);
jkarlin7b25e6c2014-12-16 19:59:47417 static void RemoveMaxBandwidthObserver(MaxBandwidthObserver* observer);
pauljensenbe5bc3232015-10-05 20:39:27418 static void RemoveNetworkObserver(NetworkObserver* observer);
[email protected]66761b952010-06-25 21:30:38419
[email protected]66761b952010-06-25 21:30:38420 // Allow unit tests to trigger notifications.
[email protected]404f1742014-06-23 13:30:11421 static void NotifyObserversOfIPAddressChangeForTests();
422 static void NotifyObserversOfConnectionTypeChangeForTests(
423 ConnectionType type);
mgersh92b1af12017-03-29 15:49:23424 static void NotifyObserversOfDNSChangeForTests();
jaekyun22b47692014-09-04 23:22:41425 static void NotifyObserversOfNetworkChangeForTests(ConnectionType type);
pauljensen101ed372015-04-17 00:11:42426 static void NotifyObserversOfInitialDNSConfigReadForTests();
jkarlinbce491862015-09-18 15:14:07427 static void NotifyObserversOfMaxBandwidthChangeForTests(
428 double max_bandwidth_mbps,
429 ConnectionType type);
[email protected]404f1742014-06-23 13:30:11430
431 // Enable or disable notifications from the host. After setting to true, be
432 // sure to pump the RunLoop until idle to finish any preexisting
jkarline160f6b2015-07-31 17:04:08433 // notifications. To use this, it must must be called before a
434 // NetworkChangeNotifier is created.
[email protected]404f1742014-06-23 13:30:11435 static void SetTestNotificationsOnly(bool test_only);
[email protected]66761b952010-06-25 21:30:38436
[email protected]3d5aaad2012-10-27 12:31:28437 // Return a string equivalent to |type|.
438 static const char* ConnectionTypeToString(ConnectionType type);
439
bmcquade3f55de792016-02-26 22:10:37440 // Invoked at the time a new user metrics log record is being finalized, on
441 // the main thread. NCN Histograms that want to be logged once per record
442 // should be logged in this method. Platform-specific histograms should be
443 // logged in an overridden implementaton of OnFinalizingMetricsLogRecord.
444 static void FinalizingMetricsLogRecord();
445
[email protected]2a321de32014-05-10 19:59:06446 // Log the |NCN.NetworkOperatorMCCMNC| histogram.
447 static void LogOperatorCodeHistogram(ConnectionType type);
448
[email protected]ca456402013-05-21 01:07:07449 // Allows a second NetworkChangeNotifier to be created for unit testing, so
450 // the test suite can create a MockNetworkChangeNotifier, but platform
451 // specific NetworkChangeNotifiers can also be created for testing. To use,
452 // create an DisableForTest object, and then create the new
453 // NetworkChangeNotifier object. The NetworkChangeNotifier must be
454 // destroyed before the DisableForTest object, as its destruction will restore
455 // the original NetworkChangeNotifier.
456 class NET_EXPORT DisableForTest {
457 public:
458 DisableForTest();
459 ~DisableForTest();
460
461 private:
462 // The original NetworkChangeNotifier to be restored on destruction.
463 NetworkChangeNotifier* network_change_notifier_;
464 };
465
[email protected]66761b952010-06-25 21:30:38466 protected:
pauljensenbe5bc3232015-10-05 20:39:27467 // Types of network changes specified to
468 // NotifyObserversOfSpecificNetworkChange.
469 enum NetworkChangeType {
470 CONNECTED,
471 DISCONNECTED,
472 SOON_TO_DISCONNECT,
473 MADE_DEFAULT
474 };
475
[email protected]03848872012-12-08 02:46:41476 // NetworkChanged signal is calculated from the IPAddressChanged and
477 // ConnectionTypeChanged signals. Delay parameters control how long to delay
478 // producing NetworkChanged signal after particular input signals so as to
479 // combine duplicates. In other words if an input signal is repeated within
480 // the corresponding delay period, only one resulting NetworkChange signal is
481 // produced.
482 struct NET_EXPORT NetworkChangeCalculatorParams {
483 NetworkChangeCalculatorParams();
484 // Controls delay after OnIPAddressChanged when transitioning from an
485 // offline state.
486 base::TimeDelta ip_address_offline_delay_;
487 // Controls delay after OnIPAddressChanged when transitioning from an
488 // online state.
489 base::TimeDelta ip_address_online_delay_;
490 // Controls delay after OnConnectionTypeChanged when transitioning from an
491 // offline state.
492 base::TimeDelta connection_type_offline_delay_;
493 // Controls delay after OnConnectionTypeChanged when transitioning from an
494 // online state.
495 base::TimeDelta connection_type_online_delay_;
496 };
497
498 explicit NetworkChangeNotifier(
499 const NetworkChangeCalculatorParams& params =
500 NetworkChangeCalculatorParams());
[email protected]66761b952010-06-25 21:30:38501
[email protected]67172802012-07-16 22:27:24502#if defined(OS_LINUX)
503 // Returns the AddressTrackerLinux if present.
[email protected]bb0e34542012-08-31 19:52:40504 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212
[email protected]67172802012-07-16 22:27:24505 virtual const internal::AddressTrackerLinux*
506 GetAddressTrackerInternal() const;
507#endif
508
pauljensenbe5bc3232015-10-05 20:39:27509 // These are the actual implementations of the static queryable APIs.
510 // See the description of the corresponding functions named without "Current".
jkarlin59a810932014-10-06 18:00:43511 // Implementations must be thread-safe. Implementations must also be
pauljensenbe5bc3232015-10-05 20:39:27512 // cheap as they are called often.
513 virtual ConnectionType GetCurrentConnectionType() const = 0;
tbansal880e49e72017-03-25 04:44:25514 virtual ConnectionSubtype GetCurrentConnectionSubtype() const;
jkarlinbce491862015-09-18 15:14:07515 virtual void GetCurrentMaxBandwidthAndConnectionType(
516 double* max_bandwidth_mbps,
517 ConnectionType* connection_type) const;
jric94943b2015-12-03 18:50:22518 virtual bool AreNetworkHandlesCurrentlySupported() const;
pauljensenbe5bc3232015-10-05 20:39:27519 virtual void GetCurrentConnectedNetworks(NetworkList* network_list) const;
520 virtual ConnectionType GetCurrentNetworkConnectionType(
521 NetworkHandle network) const;
522 virtual NetworkHandle GetCurrentDefaultNetwork() const;
jkarlin59a810932014-10-06 18:00:43523
bmcquade3f55de792016-02-26 22:10:37524 // Hook that allows derived implementations to log histograms at the time a
525 // new histogram record is being finalized.
526 virtual void OnFinalizingMetricsLogRecord() {}
527
[email protected]66761b952010-06-25 21:30:38528 // Broadcasts a notification to all registered observers. Note that this
529 // happens asynchronously, even for observers on the current thread, even in
530 // tests.
531 static void NotifyObserversOfIPAddressChange();
[email protected]8bbc7a792012-05-24 11:30:05532 static void NotifyObserversOfConnectionTypeChange();
[email protected]bb0e34542012-08-31 19:52:40533 static void NotifyObserversOfDNSChange();
pauljensen101ed372015-04-17 00:11:42534 static void NotifyObserversOfInitialDNSConfigRead();
[email protected]03848872012-12-08 02:46:41535 static void NotifyObserversOfNetworkChange(ConnectionType type);
jkarlinbce491862015-09-18 15:14:07536 static void NotifyObserversOfMaxBandwidthChange(double max_bandwidth_mbps,
537 ConnectionType type);
pauljensenbe5bc3232015-10-05 20:39:27538 static void NotifyObserversOfSpecificNetworkChange(NetworkChangeType type,
539 NetworkHandle network);
[email protected]bb0e34542012-08-31 19:52:40540
John Abd-El-Malek1842b8c2018-05-16 14:53:22541 // Stores |config| in NetworkState and notifies observers. The first
542 // notification will be OnInitialDNSConfigRead, and after that OnDNSChanged.
[email protected]bb0e34542012-08-31 19:52:40543 static void SetDnsConfig(const DnsConfig& config);
John Abd-El-Malek1842b8c2018-05-16 14:53:22544
545 // Clears previous DnsConfig, if any, to simulate the first one being set.
546 static void ClearDnsConfigForTesting();
[email protected]100d5fb92009-12-21 21:08:35547
jkarlin013bc2a062017-05-09 19:05:05548 // Infer connection type from |GetNetworkList|. If all network interfaces
549 // have the same type, return it, otherwise return CONNECTION_UNKNOWN.
550 static ConnectionType ConnectionTypeFromInterfaces();
551
[email protected]100d5fb92009-12-21 21:08:35552 private:
[email protected]bb0e34542012-08-31 19:52:40553 friend class HostResolverImplDnsTest;
[email protected]ab3309da2012-09-20 02:27:14554 friend class NetworkChangeNotifierAndroidTest;
[email protected]ae66a48d2011-11-16 22:28:06555 friend class NetworkChangeNotifierLinuxTest;
[email protected]aab66932011-10-05 17:08:13556 friend class NetworkChangeNotifierWinTest;
557
[email protected]bb0e34542012-08-31 19:52:40558 class NetworkState;
[email protected]03848872012-12-08 02:46:41559 class NetworkChangeCalculator;
[email protected]bb0e34542012-08-31 19:52:40560
[email protected]404f1742014-06-23 13:30:11561 void NotifyObserversOfIPAddressChangeImpl();
562 void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type);
563 void NotifyObserversOfDNSChangeImpl();
pauljensen101ed372015-04-17 00:11:42564 void NotifyObserversOfInitialDNSConfigReadImpl();
[email protected]404f1742014-06-23 13:30:11565 void NotifyObserversOfNetworkChangeImpl(ConnectionType type);
jkarlinbce491862015-09-18 15:14:07566 void NotifyObserversOfMaxBandwidthChangeImpl(double max_bandwidth_mbps,
567 ConnectionType type);
pauljensenbe5bc3232015-10-05 20:39:27568 void NotifyObserversOfSpecificNetworkChangeImpl(NetworkChangeType type,
569 NetworkHandle network);
[email protected]404f1742014-06-23 13:30:11570
brettw236d3172015-06-03 16:31:43571 const scoped_refptr<base::ObserverListThreadSafe<IPAddressObserver>>
[email protected]232a5812011-03-04 22:42:08572 ip_address_observer_list_;
brettw236d3172015-06-03 16:31:43573 const scoped_refptr<base::ObserverListThreadSafe<ConnectionTypeObserver>>
[email protected]8bbc7a792012-05-24 11:30:05574 connection_type_observer_list_;
brettw236d3172015-06-03 16:31:43575 const scoped_refptr<base::ObserverListThreadSafe<DNSObserver>>
[email protected]46018c9d2011-09-06 03:42:34576 resolver_state_observer_list_;
brettw236d3172015-06-03 16:31:43577 const scoped_refptr<base::ObserverListThreadSafe<NetworkChangeObserver>>
[email protected]03848872012-12-08 02:46:41578 network_change_observer_list_;
brettw236d3172015-06-03 16:31:43579 const scoped_refptr<base::ObserverListThreadSafe<MaxBandwidthObserver>>
jkarlin7b25e6c2014-12-16 19:59:47580 max_bandwidth_observer_list_;
pauljensenbe5bc3232015-10-05 20:39:27581 const scoped_refptr<base::ObserverListThreadSafe<NetworkObserver>>
582 network_observer_list_;
[email protected]66761b952010-06-25 21:30:38583
[email protected]bb0e34542012-08-31 19:52:40584 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig.
danakj7f767e62016-04-16 23:20:23585 std::unique_ptr<NetworkState> network_state_;
[email protected]05aad32d2012-05-16 18:10:53586
[email protected]03848872012-12-08 02:46:41587 // Computes NetworkChange signal from IPAddress and ConnectionType signals.
danakj7f767e62016-04-16 23:20:23588 std::unique_ptr<NetworkChangeCalculator> network_change_calculator_;
[email protected]03848872012-12-08 02:46:41589
[email protected]404f1742014-06-23 13:30:11590 // Set true to disable non-test notifications (to prevent flakes in tests).
jkarline160f6b2015-07-31 17:04:08591 static bool test_notifications_only_;
[email protected]404f1742014-06-23 13:30:11592
[email protected]100d5fb92009-12-21 21:08:35593 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier);
594};
595
596} // namespace net
597
598#endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_