Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / net / base / network_change_notifier.h
blobbdd2d181a1f847c083e5ae323d3f2d83c3c6fadd
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"
13 class GURL;
15 namespace net {
17 struct DnsConfig;
18 class HistogramWatcher;
19 class NetworkChangeNotifierFactory;
20 class URLRequest;
22 #if defined(OS_LINUX)
23 namespace internal {
24 class AddressTrackerLinux;
26 #endif
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 {
34 public:
35 // This is a superset of the connection types in the NetInfo v3 specification:
36 // http://w3c.github.io/netinfo/.
37 enum ConnectionType {
38 CONNECTION_UNKNOWN = 0, // A connection exists, but its type is unknown.
39 // Also used as a default value.
40 CONNECTION_ETHERNET = 1,
41 CONNECTION_WIFI = 2,
42 CONNECTION_2G = 3,
43 CONNECTION_3G = 4,
44 CONNECTION_4G = 5,
45 CONNECTION_NONE = 6, // No connection.
46 CONNECTION_BLUETOOTH = 7,
47 CONNECTION_LAST = CONNECTION_BLUETOOTH
50 class NET_EXPORT IPAddressObserver {
51 public:
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;
56 protected:
57 IPAddressObserver() {}
58 virtual ~IPAddressObserver() {}
60 private:
61 DISALLOW_COPY_AND_ASSIGN(IPAddressObserver);
64 class NET_EXPORT ConnectionTypeObserver {
65 public:
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;
72 protected:
73 ConnectionTypeObserver() {}
74 virtual ~ConnectionTypeObserver() {}
76 private:
77 DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver);
80 class NET_EXPORT DNSObserver {
81 public:
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;
86 protected:
87 DNSObserver() {}
88 virtual ~DNSObserver() {}
90 private:
91 DISALLOW_COPY_AND_ASSIGN(DNSObserver);
94 class NET_EXPORT NetworkChangeObserver {
95 public:
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;
123 protected:
124 NetworkChangeObserver() {}
125 virtual ~NetworkChangeObserver() {}
127 private:
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 it is called often.
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 // Returns a theoretical upper limit on download bandwidth, potentially based
163 // on underlying connection type, signal strength, or some other signal. The
164 // default mapping of connection type to maximum bandwidth is provided in the
165 // NetInfo spec: http://w3c.github.io/netinfo/.
166 static double GetMaxBandwidth();
168 // Retrieve the last read DnsConfig. This could be expensive if the system has
169 // a large HOSTS file.
170 static void GetDnsConfig(DnsConfig* config);
172 #if defined(OS_LINUX)
173 // Returns the AddressTrackerLinux if present.
174 static const internal::AddressTrackerLinux* GetAddressTracker();
175 #endif
177 // Convenience method to determine if the user is offline.
178 // Returns true if there is currently no internet connection.
180 // A return value of |true| is a pretty strong indicator that the user
181 // won't be able to connect to remote sites. However, a return value of
182 // |false| is inconclusive; even if some link is up, it is uncertain
183 // whether a particular connection attempt to a particular remote site
184 // will be successfully.
185 static bool IsOffline();
187 // Returns true if |type| is a cellular connection.
188 // Returns false if |type| is CONNECTION_UNKNOWN, and thus, depending on the
189 // implementation of GetConnectionType(), it is possible that
190 // IsConnectionCellular(GetConnectionType()) returns false even if the
191 // current connection is cellular.
192 static bool IsConnectionCellular(ConnectionType type);
194 // Like Create(), but for use in tests. The mock object doesn't monitor any
195 // events, it merely rebroadcasts notifications when requested.
196 static NetworkChangeNotifier* CreateMock();
198 // Registers |observer| to receive notifications of network changes. The
199 // thread on which this is called is the thread on which |observer| will be
200 // called back with notifications. This is safe to call if Create() has not
201 // been called (as long as it doesn't race the Create() call on another
202 // thread), in which case it will simply do nothing.
203 static void AddIPAddressObserver(IPAddressObserver* observer);
204 static void AddConnectionTypeObserver(ConnectionTypeObserver* observer);
205 static void AddDNSObserver(DNSObserver* observer);
206 static void AddNetworkChangeObserver(NetworkChangeObserver* observer);
208 // Unregisters |observer| from receiving notifications. This must be called
209 // on the same thread on which AddObserver() was called. Like AddObserver(),
210 // this is safe to call if Create() has not been called (as long as it doesn't
211 // race the Create() call on another thread), in which case it will simply do
212 // nothing. Technically, it's also safe to call after the notifier object has
213 // been destroyed, if the call doesn't race the notifier's destruction, but
214 // there's no reason to use the API in this risky way, so don't do it.
215 static void RemoveIPAddressObserver(IPAddressObserver* observer);
216 static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer);
217 static void RemoveDNSObserver(DNSObserver* observer);
218 static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer);
220 // Allow unit tests to trigger notifications.
221 static void NotifyObserversOfIPAddressChangeForTests();
222 static void NotifyObserversOfConnectionTypeChangeForTests(
223 ConnectionType type);
224 static void NotifyObserversOfNetworkChangeForTests(ConnectionType type);
226 // Enable or disable notifications from the host. After setting to true, be
227 // sure to pump the RunLoop until idle to finish any preexisting
228 // notifications.
229 static void SetTestNotificationsOnly(bool test_only);
231 // Return a string equivalent to |type|.
232 static const char* ConnectionTypeToString(ConnectionType type);
234 // Let the NetworkChangeNotifier know we received some data.
235 // This is used for producing histogram data about the accuracy of
236 // the NetworkChangenotifier's online detection and rough network
237 // connection measurements.
238 static void NotifyDataReceived(const URLRequest& request, int bytes_read);
240 // Register the Observer callbacks for producing histogram data. This
241 // should be called from the network thread to avoid race conditions.
242 // ShutdownHistogramWatcher() must be called prior to NetworkChangeNotifier
243 // destruction.
244 static void InitHistogramWatcher();
246 // Unregister the Observer callbacks for producing histogram data. This
247 // should be called from the network thread to avoid race conditions.
248 static void ShutdownHistogramWatcher();
250 // Log the |NCN.NetworkOperatorMCCMNC| histogram.
251 static void LogOperatorCodeHistogram(ConnectionType type);
253 // Allows a second NetworkChangeNotifier to be created for unit testing, so
254 // the test suite can create a MockNetworkChangeNotifier, but platform
255 // specific NetworkChangeNotifiers can also be created for testing. To use,
256 // create an DisableForTest object, and then create the new
257 // NetworkChangeNotifier object. The NetworkChangeNotifier must be
258 // destroyed before the DisableForTest object, as its destruction will restore
259 // the original NetworkChangeNotifier.
260 class NET_EXPORT DisableForTest {
261 public:
262 DisableForTest();
263 ~DisableForTest();
265 private:
266 // The original NetworkChangeNotifier to be restored on destruction.
267 NetworkChangeNotifier* network_change_notifier_;
270 protected:
271 // NetworkChanged signal is calculated from the IPAddressChanged and
272 // ConnectionTypeChanged signals. Delay parameters control how long to delay
273 // producing NetworkChanged signal after particular input signals so as to
274 // combine duplicates. In other words if an input signal is repeated within
275 // the corresponding delay period, only one resulting NetworkChange signal is
276 // produced.
277 struct NET_EXPORT NetworkChangeCalculatorParams {
278 NetworkChangeCalculatorParams();
279 // Controls delay after OnIPAddressChanged when transitioning from an
280 // offline state.
281 base::TimeDelta ip_address_offline_delay_;
282 // Controls delay after OnIPAddressChanged when transitioning from an
283 // online state.
284 base::TimeDelta ip_address_online_delay_;
285 // Controls delay after OnConnectionTypeChanged when transitioning from an
286 // offline state.
287 base::TimeDelta connection_type_offline_delay_;
288 // Controls delay after OnConnectionTypeChanged when transitioning from an
289 // online state.
290 base::TimeDelta connection_type_online_delay_;
293 explicit NetworkChangeNotifier(
294 const NetworkChangeCalculatorParams& params =
295 NetworkChangeCalculatorParams());
297 #if defined(OS_LINUX)
298 // Returns the AddressTrackerLinux if present.
299 // TODO(szym): Retrieve AddressMap from NetworkState. http://crbug.com/144212
300 virtual const internal::AddressTrackerLinux*
301 GetAddressTrackerInternal() const;
302 #endif
304 // See the description of NetworkChangeNotifier::GetMaxBandwidth().
305 // Implementations must be thread-safe. Implementations must also be
306 // cheap as it is called often.
307 virtual double GetCurrentMaxBandwidth() const;
309 // Broadcasts a notification to all registered observers. Note that this
310 // happens asynchronously, even for observers on the current thread, even in
311 // tests.
312 static void NotifyObserversOfIPAddressChange();
313 static void NotifyObserversOfConnectionTypeChange();
314 static void NotifyObserversOfDNSChange();
315 static void NotifyObserversOfNetworkChange(ConnectionType type);
317 // Stores |config| in NetworkState and notifies observers.
318 static void SetDnsConfig(const DnsConfig& config);
320 private:
321 friend class HostResolverImplDnsTest;
322 friend class NetworkChangeNotifierAndroidTest;
323 friend class NetworkChangeNotifierLinuxTest;
324 friend class NetworkChangeNotifierWinTest;
326 class NetworkState;
327 class NetworkChangeCalculator;
329 void NotifyObserversOfIPAddressChangeImpl();
330 void NotifyObserversOfConnectionTypeChangeImpl(ConnectionType type);
331 void NotifyObserversOfDNSChangeImpl();
332 void NotifyObserversOfNetworkChangeImpl(ConnectionType type);
334 const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> >
335 ip_address_observer_list_;
336 const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> >
337 connection_type_observer_list_;
338 const scoped_refptr<ObserverListThreadSafe<DNSObserver> >
339 resolver_state_observer_list_;
340 const scoped_refptr<ObserverListThreadSafe<NetworkChangeObserver> >
341 network_change_observer_list_;
343 // The current network state. Hosts DnsConfig, exposed via GetDnsConfig.
344 scoped_ptr<NetworkState> network_state_;
346 // A little-piggy-back observer that simply logs UMA histogram data.
347 scoped_ptr<HistogramWatcher> histogram_watcher_;
349 // Computes NetworkChange signal from IPAddress and ConnectionType signals.
350 scoped_ptr<NetworkChangeCalculator> network_change_calculator_;
352 // Set true to disable non-test notifications (to prevent flakes in tests).
353 bool test_notifications_only_;
355 DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier);
358 } // namespace net
360 #endif // NET_BASE_NETWORK_CHANGE_NOTIFIER_H_