diff options
Diffstat (limited to 'net/base/network_change_notifier.h')
-rw-r--r-- | net/base/network_change_notifier.h | 96 |
1 files changed, 89 insertions, 7 deletions
diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h index 143abdc..4290331 100644 --- a/net/base/network_change_notifier.h +++ b/net/base/network_change_notifier.h @@ -8,6 +8,7 @@ #include <vector> #include "base/basictypes.h" +#include "base/macros.h" #include "base/observer_list_threadsafe.h" #include "base/time/time.h" #include "net/base/net_export.h" @@ -202,12 +203,49 @@ class NET_EXPORT NetworkChangeNotifier { DISALLOW_COPY_AND_ASSIGN(MaxBandwidthObserver); }; - virtual ~NetworkChangeNotifier(); + // Opaque handle for device-wide connection to a particular network. For + // example an association with a particular WiFi network with a particular + // SSID or a connection to particular cellular network. + // The meaning of this handle is target-dependent. On Android NetworkHandles + // are equivalent to the framework's concept of NetIDs (e.g. Network.netId). + typedef int32_t NetworkHandle; + + // A list of networks. + typedef std::vector<NetworkHandle> NetworkList; + + // An interface that when implemented and added via AddNeworkObserver(), + // provides notifications when networks come and go. + // Only implemented for Android (Lollipop and newer), no callbacks issued when + // unimplemented. + class NET_EXPORT NetworkObserver { + public: + // Called when device connects to |network|. For example device associates + // with a WiFi access point. This does not imply the network has Internet + // access as it may well be behind a captive portal. + virtual void OnNetworkConnected(NetworkHandle network) = 0; + // Called when device disconnects from |network|. + virtual void OnNetworkDisconnected(NetworkHandle network) = 0; + // Called when device determines the connection to |network| is no longer + // preferred, for example when a device transitions from cellular to WiFi + // it might deem the cellular connection no longer preferred. The device + // will disconnect from |network| in a period of time (30s on Android), + // allowing network communications via |network| to wrap up. + virtual void OnNetworkSoonToDisconnect(NetworkHandle network) = 0; + // Called when |network| is made the default network for communication. + virtual void OnNetworkMadeDefault(NetworkHandle network) = 0; - // See the description of NetworkChangeNotifier::GetConnectionType(). - // Implementations must be thread-safe. Implementations must also be - // cheap as it is called often. - virtual ConnectionType GetCurrentConnectionType() const = 0; + protected: + NetworkObserver() {} + virtual ~NetworkObserver() {} + + private: + DISALLOW_COPY_AND_ASSIGN(NetworkObserver); + }; + + // An invalid NetworkHandle. + static const NetworkHandle kInvalidNetworkHandle; + + virtual ~NetworkChangeNotifier(); // Replaces the default class factory instance of NetworkChangeNotifier class. // The method will take over the ownership of |factory| object. @@ -253,6 +291,27 @@ class NET_EXPORT NetworkChangeNotifier { // TODO(jkarlin): Rename to GetMaxBandwidthMbpsForConnectionSubtype. static double GetMaxBandwidthForConnectionSubtype(ConnectionSubtype subtype); + // Sets |network_list| to a list of all networks that are currently connected. + // Only implemented for Android (Lollipop and newer), leaves |network_list| + // empty when unimplemented. + static void GetConnectedNetworks(NetworkList* network_list); + + // Returns the type of connection |network| uses. Note that this may vary + // slightly over time (e.g. CONNECTION_2G to CONNECTION_3G). If |network| + // is no longer connected, it will return CONNECTION_UNKNOWN. + // Only implemented for Android (Lollipop and newer), returns + // CONNECTION_UNKNOWN when unimplemented. + static ConnectionType GetNetworkConnectionType(NetworkHandle network); + + // Returns the device's current default network connection. This is the + // network used for newly created socket communication for sockets that are + // not explicitly bound to a particular network (e.g. via + // DatagramClientSocket.BindToNetwork). Returns |kInvalidNetworkHandle| if + // there is no default connected network. + // Only implemented for Android (Lollipop and newer), returns + // |kInvalidNetworkHandle| when unimplemented. + static NetworkHandle GetDefaultNetwork(); + // Retrieve the last read DnsConfig. This could be expensive if the system has // a large HOSTS file. static void GetDnsConfig(DnsConfig* config); @@ -300,6 +359,7 @@ class NET_EXPORT NetworkChangeNotifier { static void AddDNSObserver(DNSObserver* observer); static void AddNetworkChangeObserver(NetworkChangeObserver* observer); static void AddMaxBandwidthObserver(MaxBandwidthObserver* observer); + static void AddNetworkObserver(NetworkObserver* observer); // Unregisters |observer| from receiving notifications. This must be called // on the same thread on which AddObserver() was called. Like AddObserver(), @@ -313,6 +373,7 @@ class NET_EXPORT NetworkChangeNotifier { static void RemoveDNSObserver(DNSObserver* observer); static void RemoveNetworkChangeObserver(NetworkChangeObserver* observer); static void RemoveMaxBandwidthObserver(MaxBandwidthObserver* observer); + static void RemoveNetworkObserver(NetworkObserver* observer); // Allow unit tests to trigger notifications. static void NotifyObserversOfIPAddressChangeForTests(); @@ -370,6 +431,15 @@ class NET_EXPORT NetworkChangeNotifier { }; protected: + // Types of network changes specified to + // NotifyObserversOfSpecificNetworkChange. + enum NetworkChangeType { + CONNECTED, + DISCONNECTED, + SOON_TO_DISCONNECT, + MADE_DEFAULT + }; + // NetworkChanged signal is calculated from the IPAddressChanged and // ConnectionTypeChanged signals. Delay parameters control how long to delay // producing NetworkChanged signal after particular input signals so as to @@ -403,12 +473,18 @@ class NET_EXPORT NetworkChangeNotifier { GetAddressTrackerInternal() const; #endif - // See the description of NetworkChangeNotifier::GetMaxBandwidth(). + // These are the actual implementations of the static queryable APIs. + // See the description of the corresponding functions named without "Current". // Implementations must be thread-safe. Implementations must also be - // cheap as it is called often. + // cheap as they are called often. + virtual ConnectionType GetCurrentConnectionType() const = 0; virtual void GetCurrentMaxBandwidthAndConnectionType( double* max_bandwidth_mbps, ConnectionType* connection_type) const; + virtual void GetCurrentConnectedNetworks(NetworkList* network_list) const; + virtual ConnectionType GetCurrentNetworkConnectionType( + NetworkHandle network) const; + virtual NetworkHandle GetCurrentDefaultNetwork() const; // Broadcasts a notification to all registered observers. Note that this // happens asynchronously, even for observers on the current thread, even in @@ -420,6 +496,8 @@ class NET_EXPORT NetworkChangeNotifier { static void NotifyObserversOfNetworkChange(ConnectionType type); static void NotifyObserversOfMaxBandwidthChange(double max_bandwidth_mbps, ConnectionType type); + static void NotifyObserversOfSpecificNetworkChange(NetworkChangeType type, + NetworkHandle network); // Stores |config| in NetworkState and notifies OnDNSChanged observers. static void SetDnsConfig(const DnsConfig& config); @@ -443,6 +521,8 @@ class NET_EXPORT NetworkChangeNotifier { void NotifyObserversOfNetworkChangeImpl(ConnectionType type); void NotifyObserversOfMaxBandwidthChangeImpl(double max_bandwidth_mbps, ConnectionType type); + void NotifyObserversOfSpecificNetworkChangeImpl(NetworkChangeType type, + NetworkHandle network); const scoped_refptr<base::ObserverListThreadSafe<IPAddressObserver>> ip_address_observer_list_; @@ -454,6 +534,8 @@ class NET_EXPORT NetworkChangeNotifier { network_change_observer_list_; const scoped_refptr<base::ObserverListThreadSafe<MaxBandwidthObserver>> max_bandwidth_observer_list_; + const scoped_refptr<base::ObserverListThreadSafe<NetworkObserver>> + network_observer_list_; // The current network state. Hosts DnsConfig, exposed via GetDnsConfig. scoped_ptr<NetworkState> network_state_; |