diff options
author | droger@google.com <droger@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-24 11:30:05 +0000 |
---|---|---|
committer | droger@google.com <droger@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-24 11:30:05 +0000 |
commit | 8bbc7a79b07a8ea33b356492947ad8d455ab438a (patch) | |
tree | 2b7e728f0eb8467d51390846d1059595347a1d36 /net | |
parent | 9f230ed18793828c7f1776189b5b66a5b5f07771 (diff) | |
download | chromium_src-8bbc7a79b07a8ea33b356492947ad8d455ab438a.zip chromium_src-8bbc7a79b07a8ea33b356492947ad8d455ab438a.tar.gz chromium_src-8bbc7a79b07a8ea33b356492947ad8d455ab438a.tar.bz2 |
Support for navigator.connection API
This replaces the online state (a boolean) by a more complex state (UNKNOWN/NONE
/2G/3G/4G/WIFI/ETHERNET) inspired by the Network Information API
(http://www.w3.org/TR/netinfo-api/).
Breakdown of the modified files:
- network_change_notifier.h/.cc: actual API change
- network_change_notifier_*: platform specific implementations. This CL only provide a basic implementation where CONNECTION_UNKNOWN is returned when online and CONNECTION_NONE is returned when offline.
- other files: call sites for the static function and observer implementations. Most of the time this only changes the test IsOffline() by (GetConnectionType() == CONNECTION_NONE).
BUG=112937
TEST=NONE
Review URL: https://chromiumcodereview.appspot.com/9147026
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138780 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/android/network_change_notifier_android.cc | 12 | ||||
-rw-r--r-- | net/android/network_change_notifier_android.h | 3 | ||||
-rw-r--r-- | net/base/network_change_notifier.cc | 38 | ||||
-rw-r--r-- | net/base/network_change_notifier.h | 56 | ||||
-rw-r--r-- | net/base/network_change_notifier_linux.cc | 26 | ||||
-rw-r--r-- | net/base/network_change_notifier_linux.h | 2 | ||||
-rw-r--r-- | net/base/network_change_notifier_linux_unittest.cc | 54 | ||||
-rw-r--r-- | net/base/network_change_notifier_mac.cc | 49 | ||||
-rw-r--r-- | net/base/network_change_notifier_mac.h | 22 | ||||
-rw-r--r-- | net/base/network_change_notifier_win.cc | 25 | ||||
-rw-r--r-- | net/base/network_change_notifier_win.h | 6 | ||||
-rw-r--r-- | net/base/network_change_notifier_win_unittest.cc | 7 | ||||
-rw-r--r-- | net/url_request/url_request_throttler_manager.cc | 9 | ||||
-rw-r--r-- | net/url_request/url_request_throttler_manager.h | 7 | ||||
-rw-r--r-- | net/url_request/url_request_throttler_unittest.cc | 6 |
15 files changed, 192 insertions, 130 deletions
diff --git a/net/android/network_change_notifier_android.cc b/net/android/network_change_notifier_android.cc index e1cea06..d810a33 100644 --- a/net/android/network_change_notifier_android.cc +++ b/net/android/network_change_notifier_android.cc @@ -31,13 +31,17 @@ void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { } void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { - NotifyObserversOfOnlineStateChange(); + NotifyObserversOfConnectionTypeChange(); } -bool NetworkChangeNotifier::IsCurrentlyOffline() const { +net::NetworkChangeNotifier::ConnectionType + NetworkChangeNotifier::GetCurrentConnectionType() const { JNIEnv* env = base::android::AttachCurrentThread(); - return !Java_NetworkChangeNotifier_isConnected( - env, java_network_change_notifier_.obj()); + // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. + return Java_NetworkChangeNotifier_isConnected( + env, java_network_change_notifier_.obj()) ? + net::NetworkChangeNotifier::CONNECTION_UNKNOWN : + net::NetworkChangeNotifier::CONNECTION_NONE; } // static diff --git a/net/android/network_change_notifier_android.h b/net/android/network_change_notifier_android.h index f73168a..c16a7ff 100644 --- a/net/android/network_change_notifier_android.h +++ b/net/android/network_change_notifier_android.h @@ -27,7 +27,8 @@ class NetworkChangeNotifier : public net::NetworkChangeNotifier { void CreateJavaObject(JNIEnv* env); // NetworkChangeNotifier: - virtual bool IsCurrentlyOffline() const OVERRIDE; + virtual net::NetworkChangeNotifier::ConnectionType + GetCurrentConnectionType() const OVERRIDE; base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_; diff --git a/net/base/network_change_notifier.cc b/net/base/network_change_notifier.cc index 7a14d0e..7f5d901 100644 --- a/net/base/network_change_notifier.cc +++ b/net/base/network_change_notifier.cc @@ -28,7 +28,9 @@ NetworkChangeNotifierFactory* g_network_change_notifier_factory = NULL; class MockNetworkChangeNotifier : public NetworkChangeNotifier { public: - virtual bool IsCurrentlyOffline() const { return false; } + virtual ConnectionType GetCurrentConnectionType() const { + return CONNECTION_UNKNOWN; + } }; } // namespace @@ -75,9 +77,11 @@ NetworkChangeNotifier* NetworkChangeNotifier::Create() { } // static -bool NetworkChangeNotifier::IsOffline() { - return g_network_change_notifier && - g_network_change_notifier->IsCurrentlyOffline(); +NetworkChangeNotifier::ConnectionType +NetworkChangeNotifier::GetConnectionType() { + return g_network_change_notifier ? + g_network_change_notifier->GetCurrentConnectionType() : + CONNECTION_UNKNOWN; } // static @@ -98,10 +102,10 @@ void NetworkChangeNotifier::AddIPAddressObserver(IPAddressObserver* observer) { g_network_change_notifier->ip_address_observer_list_->AddObserver(observer); } -void NetworkChangeNotifier::AddOnlineStateObserver( - OnlineStateObserver* observer) { +void NetworkChangeNotifier::AddConnectionTypeObserver( + ConnectionTypeObserver* observer) { if (g_network_change_notifier) { - g_network_change_notifier->online_state_observer_list_->AddObserver( + g_network_change_notifier->connection_type_observer_list_->AddObserver( observer); } } @@ -121,10 +125,10 @@ void NetworkChangeNotifier::RemoveIPAddressObserver( } } -void NetworkChangeNotifier::RemoveOnlineStateObserver( - OnlineStateObserver* observer) { +void NetworkChangeNotifier::RemoveConnectionTypeObserver( + ConnectionTypeObserver* observer) { if (g_network_change_notifier) { - g_network_change_notifier->online_state_observer_list_->RemoveObserver( + g_network_change_notifier->connection_type_observer_list_->RemoveObserver( observer); } } @@ -140,9 +144,9 @@ NetworkChangeNotifier::NetworkChangeNotifier() : ip_address_observer_list_( new ObserverListThreadSafe<IPAddressObserver>( ObserverListBase<IPAddressObserver>::NOTIFY_EXISTING_ONLY)), - online_state_observer_list_( - new ObserverListThreadSafe<OnlineStateObserver>( - ObserverListBase<OnlineStateObserver>::NOTIFY_EXISTING_ONLY)), + connection_type_observer_list_( + new ObserverListThreadSafe<ConnectionTypeObserver>( + ObserverListBase<ConnectionTypeObserver>::NOTIFY_EXISTING_ONLY)), resolver_state_observer_list_( new ObserverListThreadSafe<DNSObserver>( ObserverListBase<DNSObserver>::NOTIFY_EXISTING_ONLY)), @@ -180,11 +184,11 @@ void NetworkChangeNotifier::NotifyObserversOfDNSChange(unsigned detail) { } } -// static -void NetworkChangeNotifier::NotifyObserversOfOnlineStateChange() { +void NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange() { if (g_network_change_notifier) { - g_network_change_notifier->online_state_observer_list_->Notify( - &OnlineStateObserver::OnOnlineStateChanged, !IsOffline()); + g_network_change_notifier->connection_type_observer_list_->Notify( + &ConnectionTypeObserver::OnConnectionTypeChanged, + GetConnectionType()); } } diff --git a/net/base/network_change_notifier.h b/net/base/network_change_notifier.h index 477d1f7..20ee6eb 100644 --- a/net/base/network_change_notifier.h +++ b/net/base/network_change_notifier.h @@ -38,6 +38,18 @@ class NET_EXPORT NetworkChangeNotifier { CHANGE_DNS_WATCH_FAILED = 1 << 3, }; + // Using the terminology of the Network Information API: + // http://www.w3.org/TR/netinfo-api. + enum ConnectionType { + CONNECTION_UNKNOWN, // A connection exists, but its type is unknown. + CONNECTION_ETHERNET, + CONNECTION_WIFI, + CONNECTION_2G, + CONNECTION_3G, + CONNECTION_4G, + CONNECTION_NONE // No connection. +}; + class NET_EXPORT IPAddressObserver { public: virtual ~IPAddressObserver() {} @@ -53,20 +65,21 @@ class NET_EXPORT NetworkChangeNotifier { DISALLOW_COPY_AND_ASSIGN(IPAddressObserver); }; - class NET_EXPORT OnlineStateObserver { + class NET_EXPORT ConnectionTypeObserver { public: - virtual ~OnlineStateObserver() {} + virtual ~ConnectionTypeObserver() {} - // Will be called when the online state of the system may have changed. - // See NetworkChangeNotifier::IsOffline() for important caveats about - // the unreliability of this signal. - virtual void OnOnlineStateChanged(bool online) = 0; + // Will be called when the connection type of the system has changed. + // See NetworkChangeNotifier::GetConnectionType() for important caveats + // about the unreliability of using this signal to infer the ability to + // reach remote sites. + virtual void OnConnectionTypeChanged(ConnectionType type) = 0; protected: - OnlineStateObserver() {} + ConnectionTypeObserver() {} private: - DISALLOW_COPY_AND_ASSIGN(OnlineStateObserver); + DISALLOW_COPY_AND_ASSIGN(ConnectionTypeObserver); }; class NET_EXPORT DNSObserver { @@ -86,10 +99,10 @@ class NET_EXPORT NetworkChangeNotifier { virtual ~NetworkChangeNotifier(); - // See the description of NetworkChangeNotifier::IsOffline(). + // See the description of NetworkChangeNotifier::GetConnectionType(). // Implementations must be thread-safe. Implementations must also be // cheap as this could be called (repeatedly) from the IO thread. - virtual bool IsCurrentlyOffline() const = 0; + virtual ConnectionType GetCurrentConnectionType() const = 0; // Replaces the default class factory instance of NetworkChangeNotifier class. // The method will take over the ownership of |factory| object. @@ -103,6 +116,15 @@ class NET_EXPORT NetworkChangeNotifier { // which might try to use it. static NetworkChangeNotifier* Create(); + // Returns the connection type. + // A return value of |CONNECTION_NONE| is a pretty strong indicator that the + // user won't be able to connect to remote sites. However, another return + // value doesn't imply that the user will be able to connect to remote sites; + // even if some link is up, it is uncertain whether a particular connection + // attempt to a particular remote site will be successful. + static ConnectionType GetConnectionType(); + + // Convenience method to determine if the user is offline. // Returns true if there is currently no internet connection. // // A return value of |true| is a pretty strong indicator that the user @@ -110,7 +132,9 @@ class NET_EXPORT NetworkChangeNotifier { // |false| is inconclusive; even if some link is up, it is uncertain // whether a particular connection attempt to a particular remote site // will be successfully. - static bool IsOffline(); + static bool IsOffline() { + return GetConnectionType() == CONNECTION_NONE; + } // Returns true if DNS watcher is operational. static bool IsWatchingDNS(); @@ -125,7 +149,7 @@ class NET_EXPORT NetworkChangeNotifier { // been called (as long as it doesn't race the Create() call on another // thread), in which case it will simply do nothing. static void AddIPAddressObserver(IPAddressObserver* observer); - static void AddOnlineStateObserver(OnlineStateObserver* observer); + static void AddConnectionTypeObserver(ConnectionTypeObserver* observer); static void AddDNSObserver(DNSObserver* observer); // Unregisters |observer| from receiving notifications. This must be called @@ -136,7 +160,7 @@ class NET_EXPORT NetworkChangeNotifier { // been destroyed, if the call doesn't race the notifier's destruction, but // there's no reason to use the API in this risky way, so don't do it. static void RemoveIPAddressObserver(IPAddressObserver* observer); - static void RemoveOnlineStateObserver(OnlineStateObserver* observer); + static void RemoveConnectionTypeObserver(ConnectionTypeObserver* observer); static void RemoveDNSObserver(DNSObserver* observer); // Allow unit tests to trigger notifications. @@ -153,7 +177,7 @@ class NET_EXPORT NetworkChangeNotifier { // happens asynchronously, even for observers on the current thread, even in // tests. static void NotifyObserversOfIPAddressChange(); - static void NotifyObserversOfOnlineStateChange(); + static void NotifyObserversOfConnectionTypeChange(); static void NotifyObserversOfDNSChange(unsigned detail); private: @@ -179,8 +203,8 @@ class NET_EXPORT NetworkChangeNotifier { const scoped_refptr<ObserverListThreadSafe<IPAddressObserver> > ip_address_observer_list_; - const scoped_refptr<ObserverListThreadSafe<OnlineStateObserver> > - online_state_observer_list_; + const scoped_refptr<ObserverListThreadSafe<ConnectionTypeObserver> > + connection_type_observer_list_; const scoped_refptr<ObserverListThreadSafe<DNSObserver> > resolver_state_observer_list_; diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc index 679593e..b7acd46 100644 --- a/net/base/network_change_notifier_linux.cc +++ b/net/base/network_change_notifier_linux.cc @@ -83,9 +83,9 @@ class NetworkManagerApi { // Must be called by the helper thread's CleanUp() method. void CleanUp(); - // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). + // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). // Safe to call from any thread, but will block until Init() has completed. - bool IsCurrentlyOffline(); + NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); private: // Callbacks for D-Bus API. @@ -232,12 +232,15 @@ bool NetworkManagerApi::StateIsOffline(uint32 state) { } } -bool NetworkManagerApi::IsCurrentlyOffline() { +NetworkChangeNotifier::ConnectionType +NetworkManagerApi::GetCurrentConnectionType() { // http://crbug.com/125097 base::ThreadRestrictions::ScopedAllowWait allow_wait; offline_state_initialized_.Wait(); base::AutoLock lock(is_offline_lock_); - return is_offline_; + // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. + return is_offline_ ? NetworkChangeNotifier::CONNECTION_NONE : + NetworkChangeNotifier::CONNECTION_UNKNOWN; } class NetworkChangeNotifierLinux::Thread @@ -250,10 +253,10 @@ class NetworkChangeNotifierLinux::Thread virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; virtual void OnFileCanWriteWithoutBlocking(int /* fd */) OVERRIDE; - // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline. + // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. // Safe to call from any thread. - bool IsCurrentlyOffline() { - return network_manager_api_.IsCurrentlyOffline(); + NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { + return network_manager_api_.GetCurrentConnectionType(); } protected: @@ -287,8 +290,8 @@ NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) : base::Thread("NetworkChangeNotifier"), netlink_fd_(kInvalidSocket), network_manager_api_( - base::Bind(&NetworkChangeNotifier - ::NotifyObserversOfOnlineStateChange), + base::Bind(&NetworkChangeNotifier:: + NotifyObserversOfConnectionTypeChange), bus) { } @@ -392,8 +395,9 @@ NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { notifier_thread_->Stop(); } -bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { - return notifier_thread_->IsCurrentlyOffline(); +NetworkChangeNotifier::ConnectionType +NetworkChangeNotifierLinux::GetCurrentConnectionType() const { + return notifier_thread_->GetCurrentConnectionType(); } } // namespace net diff --git a/net/base/network_change_notifier_linux.h b/net/base/network_change_notifier_linux.h index e9426d0..cc5ceff 100644 --- a/net/base/network_change_notifier_linux.h +++ b/net/base/network_change_notifier_linux.h @@ -33,7 +33,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierLinux virtual ~NetworkChangeNotifierLinux(); // NetworkChangeNotifier: - virtual bool IsCurrentlyOffline() const OVERRIDE; + virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; // The thread used to listen for notifications. This relays the notification // to the registered observers without posting back to the thread the object diff --git a/net/base/network_change_notifier_linux_unittest.cc b/net/base/network_change_notifier_linux_unittest.cc index d8513ce..10dda96 100644 --- a/net/base/network_change_notifier_linux_unittest.cc +++ b/net/base/network_change_notifier_linux_unittest.cc @@ -119,21 +119,22 @@ class NetworkChangeNotifierLinuxTest : public testing::Test { namespace { -class OfflineObserver : public NetworkChangeNotifier::OnlineStateObserver { +class OfflineObserver : public NetworkChangeNotifier::ConnectionTypeObserver { public: OfflineObserver() : notification_count(0), last_online_value(true) { - NetworkChangeNotifier::AddOnlineStateObserver(this); + NetworkChangeNotifier::AddConnectionTypeObserver(this); } ~OfflineObserver() { - NetworkChangeNotifier::RemoveOnlineStateObserver(this); + NetworkChangeNotifier::RemoveConnectionTypeObserver(this); } - virtual void OnOnlineStateChanged(bool online) OVERRIDE { + virtual void OnConnectionTypeChanged( + NetworkChangeNotifier::ConnectionType type) OVERRIDE { notification_count++; - last_online_value = online; + last_online_value = type != NetworkChangeNotifier::CONNECTION_NONE; } int notification_count; @@ -142,23 +143,26 @@ class OfflineObserver : public NetworkChangeNotifier::OnlineStateObserver { TEST_F(NetworkChangeNotifierLinuxTest, Offline) { SendResponse(NM_STATE_DISCONNECTED); - EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); } TEST_F(NetworkChangeNotifierLinuxTest, Online) { SendResponse(NM_STATE_CONNECTED_GLOBAL); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); -} + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType());} TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { OfflineObserver observer; SendResponse(NM_STATE_DISCONNECTED); - EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); SendSignal(NM_STATE_CONNECTED_GLOBAL); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(1, observer.notification_count); EXPECT_TRUE(observer.last_online_value); } @@ -167,16 +171,19 @@ TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { OfflineObserver observer; SendResponse(NM_STATE_CONNECTED_GLOBAL); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); SendSignal(NM_STATE_DISCONNECTED); - EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(1, observer.notification_count); EXPECT_FALSE(observer.last_online_value); SendSignal(NM_STATE_CONNECTED_GLOBAL); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(2, observer.notification_count); EXPECT_TRUE(observer.last_online_value); } @@ -185,11 +192,13 @@ TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { OfflineObserver observer; SendResponse(NM_STATE_CONNECTED_SITE); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); SendSignal(NM_STATE_CONNECTED_GLOBAL); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); } @@ -197,24 +206,28 @@ TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { OfflineObserver observer; SendResponse(NM_STATE_DISCONNECTING); - EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); SendSignal(NM_STATE_DISCONNECTED); - EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); + EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); EXPECT_EQ(0, observer.notification_count); } TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { RunOnNotifierThread(base::Bind( response_callback_, static_cast<dbus::Response*>(NULL))); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); } TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); RunOnNotifierThread(base::Bind(response_callback_, response.get())); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); } TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { @@ -222,7 +235,8 @@ TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { dbus::MessageWriter writer(response.get()); writer.AppendUint16(20); // Uint16 instead of the expected Uint32 RunOnNotifierThread(base::Bind(response_callback_, response.get())); - EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); + EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE, + NetworkChangeNotifier::GetConnectionType()); } } // namespace diff --git a/net/base/network_change_notifier_mac.cc b/net/base/network_change_notifier_mac.cc index 0265ef1..85e95a2 100644 --- a/net/base/network_change_notifier_mac.cc +++ b/net/base/network_change_notifier_mac.cc @@ -46,12 +46,13 @@ class NetworkChangeNotifierMac::DnsWatcherThread : public base::Thread { }; NetworkChangeNotifierMac::NetworkChangeNotifierMac() - : online_state_(UNINITIALIZED), - initial_state_cv_(&online_state_lock_), + : connection_type_(CONNECTION_UNKNOWN), + connection_type_initialized_(false), + initial_connection_type_cv_(&connection_type_lock_), forwarder_(this), dns_watcher_thread_(new DnsWatcherThread()) { // Must be initialized after the rest of this object, as it may call back into - // SetInitialState(). + // SetInitialConnectionType(). config_watcher_.reset(new NetworkConfigWatcherMac(&forwarder_)); dns_watcher_thread_->StartWithOptions( base::Thread::Options(MessageLoop::TYPE_IO, 0)); @@ -71,16 +72,17 @@ NetworkChangeNotifierMac::~NetworkChangeNotifierMac() { } } -bool NetworkChangeNotifierMac::IsCurrentlyOffline() const { - base::AutoLock lock(online_state_lock_); - // Make sure the initial state is set before returning. - while (online_state_ == UNINITIALIZED) { - initial_state_cv_.Wait(); +NetworkChangeNotifier::ConnectionType +NetworkChangeNotifierMac::GetCurrentConnectionType() const { + base::AutoLock lock(connection_type_lock_); + // Make sure the initial connection type is set before returning. + while (!connection_type_initialized_) { + initial_connection_type_cv_.Wait(); } - return online_state_ == OFFLINE; + return connection_type_; } -void NetworkChangeNotifierMac::SetInitialState() { +void NetworkChangeNotifierMac::SetInitialConnectionType() { // Called on notifier thread. // Try to reach 0.0.0.0. This is the approach taken by Firefox: @@ -100,11 +102,14 @@ void NetworkChangeNotifierMac::SetInitialState() { if (SCNetworkReachabilityGetFlags(reachability_, &flags)) reachable = CalculateReachability(flags); else - LOG(ERROR) << "Could not get initial network state, assuming online."; + LOG(ERROR) << "Could not get initial network connection type," + << "assuming online."; { - base::AutoLock lock(online_state_lock_); - online_state_ = reachable ? ONLINE : OFFLINE; - initial_state_cv_.Signal(); + base::AutoLock lock(connection_type_lock_); + // TODO(droger): Get something more detailed than CONNECTION_UNKNOWN. + connection_type_ = reachable ? CONNECTION_UNKNOWN : CONNECTION_NONE; + connection_type_initialized_ = true; + initial_connection_type_cv_.Signal(); } } @@ -187,15 +192,17 @@ void NetworkChangeNotifierMac::ReachabilityCallback( DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent()); - OnlineState new_state = CalculateReachability(flags) ? ONLINE : OFFLINE; - OnlineState old_state; + // TODO(droger): Get something more detailed than CONNECTION_UNKNOWN. + ConnectionType new_type = CalculateReachability(flags) ? CONNECTION_UNKNOWN + : CONNECTION_NONE; + ConnectionType old_type; { - base::AutoLock lock(notifier_mac->online_state_lock_); - old_state = notifier_mac->online_state_; - notifier_mac->online_state_ = new_state; + base::AutoLock lock(notifier_mac->connection_type_lock_); + old_type = notifier_mac->connection_type_; + notifier_mac->connection_type_ = new_type; } - if (old_state != new_state) - NotifyObserversOfOnlineStateChange(); + if (old_type != new_type) + NotifyObserversOfConnectionTypeChange(); } } // namespace net diff --git a/net/base/network_change_notifier_mac.h b/net/base/network_change_notifier_mac.h index c95e952..43d02fb 100644 --- a/net/base/network_change_notifier_mac.h +++ b/net/base/network_change_notifier_mac.h @@ -24,15 +24,8 @@ class NetworkChangeNotifierMac: public NetworkChangeNotifier { NetworkChangeNotifierMac(); virtual ~NetworkChangeNotifierMac(); - // NetworkChangeNotifier: - virtual bool IsCurrentlyOffline() const OVERRIDE; - - private: - enum OnlineState { - UNINITIALIZED = -1, - OFFLINE = 0, - ONLINE = 1 - }; + // NetworkChangeNotifier implementation: + virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; class DnsWatcherThread; @@ -45,7 +38,7 @@ class NetworkChangeNotifierMac: public NetworkChangeNotifier { // NetworkConfigWatcherMac::Delegate implementation: virtual void Init() OVERRIDE { - net_config_watcher_->SetInitialState(); + net_config_watcher_->SetInitialConnectionType(); } virtual void StartReachabilityNotifications() OVERRIDE { net_config_watcher_->StartReachabilityNotifications(); @@ -68,7 +61,7 @@ class NetworkChangeNotifierMac: public NetworkChangeNotifier { void SetDynamicStoreNotificationKeys(SCDynamicStoreRef store); void OnNetworkConfigChange(CFArrayRef changed_keys); - void SetInitialState(); + void SetInitialConnectionType(); static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, @@ -76,9 +69,10 @@ class NetworkChangeNotifierMac: public NetworkChangeNotifier { // These must be constructed before config_watcher_ to ensure // the lock is in a valid state when Forwarder::Init is called. - OnlineState online_state_; - mutable base::Lock online_state_lock_; - mutable base::ConditionVariable initial_state_cv_; + ConnectionType connection_type_; + bool connection_type_initialized_; + mutable base::Lock connection_type_lock_; + mutable base::ConditionVariable initial_connection_type_cv_; base::mac::ScopedCFTypeRef<SCNetworkReachabilityRef> reachability_; base::mac::ScopedCFTypeRef<CFRunLoopRef> run_loop_; diff --git a/net/base/network_change_notifier_win.cc b/net/base/network_change_notifier_win.cc index 928a688..ccb033b 100644 --- a/net/base/network_change_notifier_win.cc +++ b/net/base/network_change_notifier_win.cc @@ -69,9 +69,11 @@ NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { WSACloseEvent(addr_overlapped_.hEvent); } -// Conceptually we would like to tell whether the user is "online" verus -// "offline". This is challenging since the only thing we can test with -// certainty is whether a *particular* host is reachable. +// This implementation does not return the actual connection type but merely +// determines if the user is "online" (in which case it returns +// CONNECTION_UNKNOWN) or "offline" (and then it returns CONNECTION_NONE). +// This is challenging since the only thing we can test with certainty is +// whether a *particular* host is reachable. // // While we can't conclusively determine when a user is "online", we can at // least reliably recognize some of the situtations when they are clearly @@ -115,7 +117,8 @@ NetworkChangeNotifierWin::~NetworkChangeNotifierWin() { // experiments I ran... However none of them correctly returned "offline" when // executing 'ipconfig /release'. // -bool NetworkChangeNotifierWin::IsCurrentlyOffline() const { +NetworkChangeNotifier::ConnectionType +NetworkChangeNotifierWin::GetCurrentConnectionType() const { // TODO(eroman): We could cache this value, and only re-calculate it on // network changes. For now we recompute it each time asked, @@ -138,7 +141,7 @@ bool NetworkChangeNotifierWin::IsCurrentlyOffline() const { if (0 != WSALookupServiceBegin(&query_set, LUP_RETURN_ALL, &ws_handle)) { LOG(ERROR) << "WSALookupServiceBegin failed with: " << WSAGetLastError(); - return false; + return NetworkChangeNotifier::CONNECTION_UNKNOWN; } bool found_connection = false; @@ -183,7 +186,9 @@ bool NetworkChangeNotifierWin::IsCurrentlyOffline() const { LOG_IF(ERROR, result != 0) << "WSALookupServiceEnd() failed with: " << result; - return !found_connection; + // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. + return found_connection ? NetworkChangeNotifier::CONNECTION_UNKNOWN : + NetworkChangeNotifier::CONNECTION_NONE; } void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object) { @@ -201,14 +206,14 @@ void NetworkChangeNotifierWin::NotifyObservers() { DCHECK(CalledOnValidThread()); NotifyObserversOfIPAddressChange(); - // Calling IsOffline() at this very moment is likely to give + // Calling GetConnectionType() at this very moment is likely to give // the wrong result, so we delay that until a little bit later. // // The one second delay chosen here was determined experimentally // by adamk on Windows 7. timer_.Stop(); // cancel any already waiting notification timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(1), this, - &NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange); + &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange); } void NetworkChangeNotifierWin::WatchForAddressChange() { @@ -264,8 +269,8 @@ bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() { return true; } -void NetworkChangeNotifierWin::NotifyParentOfOnlineStateChange() { - NotifyObserversOfOnlineStateChange(); +void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() { + NotifyObserversOfConnectionTypeChange(); } } // namespace net diff --git a/net/base/network_change_notifier_win.h b/net/base/network_change_notifier_win.h index d8c9a45..d37cb8f 100644 --- a/net/base/network_change_notifier_win.h +++ b/net/base/network_change_notifier_win.h @@ -52,7 +52,7 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierWin friend class NetworkChangeNotifierWinTest; // NetworkChangeNotifier methods: - virtual bool IsCurrentlyOffline() const OVERRIDE; + virtual ConnectionType GetCurrentConnectionType() const OVERRIDE; // ObjectWatcher::Delegate methods: // Must only be called on the thread |this| was created on. @@ -63,8 +63,8 @@ class NET_EXPORT_PRIVATE NetworkChangeNotifierWin // thread |this| was created on. void NotifyObservers(); - // Forwards online state notifications to parent class. - void NotifyParentOfOnlineStateChange(); + // Forwards connection type notifications to parent class. + void NotifyParentOfConnectionTypeChange(); // Tries to start listening for a single subsequent address change. Returns // false on failure. The caller is responsible for updating |is_watching_|. diff --git a/net/base/network_change_notifier_win_unittest.cc b/net/base/network_change_notifier_win_unittest.cc index 170755a..4ade0e0 100644 --- a/net/base/network_change_notifier_win_unittest.cc +++ b/net/base/network_change_notifier_win_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -31,8 +31,9 @@ class TestNetworkChangeNotifierWin : public NetworkChangeNotifierWin { } // From NetworkChangeNotifier. - virtual bool IsCurrentlyOffline() const OVERRIDE { - return false; + virtual NetworkChangeNotifier::ConnectionType + GetCurrentConnectionType() const OVERRIDE { + return NetworkChangeNotifier::CONNECTION_UNKNOWN; } // From NetworkChangeNotifierWin. diff --git a/net/url_request/url_request_throttler_manager.cc b/net/url_request/url_request_throttler_manager.cc index c2fa430..5c50558 100644 --- a/net/url_request/url_request_throttler_manager.cc +++ b/net/url_request/url_request_throttler_manager.cc @@ -28,12 +28,12 @@ URLRequestThrottlerManager::URLRequestThrottlerManager() url_id_replacements_.ClearRef(); NetworkChangeNotifier::AddIPAddressObserver(this); - NetworkChangeNotifier::AddOnlineStateObserver(this); + NetworkChangeNotifier::AddConnectionTypeObserver(this); } URLRequestThrottlerManager::~URLRequestThrottlerManager() { NetworkChangeNotifier::RemoveIPAddressObserver(this); - NetworkChangeNotifier::RemoveOnlineStateObserver(this); + NetworkChangeNotifier::RemoveConnectionTypeObserver(this); // Since, for now, the manager object might conceivably go away before // the entries, detach the entries' back-pointer to the manager. @@ -166,7 +166,8 @@ void URLRequestThrottlerManager::OnIPAddressChanged() { OnNetworkChange(); } -void URLRequestThrottlerManager::OnOnlineStateChanged(bool online) { +void URLRequestThrottlerManager::OnConnectionTypeChanged( + NetworkChangeNotifier::ConnectionType type) { OnNetworkChange(); } @@ -207,7 +208,7 @@ void URLRequestThrottlerManager::OnNetworkChange() { // Remove all entries. Any entries that in-flight requests have a reference // to will live until those requests end, and these entries may be // inconsistent with new entries for the same URLs, but since what we - // want is a clean slate for the new connection state, this is OK. + // want is a clean slate for the new connection type, this is OK. url_entries_.clear(); requests_since_last_gc_ = 0; } diff --git a/net/url_request/url_request_throttler_manager.h b/net/url_request/url_request_throttler_manager.h index 97016a5..427b42e 100644 --- a/net/url_request/url_request_throttler_manager.h +++ b/net/url_request/url_request_throttler_manager.h @@ -36,7 +36,7 @@ class NetLog; class NET_EXPORT URLRequestThrottlerManager : NON_EXPORTED_BASE(public base::NonThreadSafe), public NetworkChangeNotifier::IPAddressObserver, - public NetworkChangeNotifier::OnlineStateObserver { + public NetworkChangeNotifier::ConnectionTypeObserver { public: URLRequestThrottlerManager(); virtual ~URLRequestThrottlerManager(); @@ -81,8 +81,9 @@ class NET_EXPORT URLRequestThrottlerManager // IPAddressObserver interface. virtual void OnIPAddressChanged() OVERRIDE; - // OnlineStateObserver interface. - virtual void OnOnlineStateChanged(bool online) OVERRIDE; + // ConnectionTypeObserver interface. + virtual void OnConnectionTypeChanged( + NetworkChangeNotifier::ConnectionType type) OVERRIDE; // Method that allows us to transform a URL into an ID that can be used in our // map. Resulting IDs will be lowercase and consist of the scheme, host, port diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc index 11a1f76..0d018cd 100644 --- a/net/url_request/url_request_throttler_unittest.cc +++ b/net/url_request/url_request_throttler_unittest.cc @@ -511,10 +511,12 @@ TEST(URLRequestThrottlerManager, ClearOnNetworkChange) { manager.OnIPAddressChanged(); break; case 1: - manager.OnOnlineStateChanged(true); + manager.OnConnectionTypeChanged( + net::NetworkChangeNotifier::CONNECTION_UNKNOWN); break; case 2: - manager.OnOnlineStateChanged(false); + manager.OnConnectionTypeChanged( + net::NetworkChangeNotifier::CONNECTION_NONE); break; default: FAIL(); |