diff options
author | gfhuang <gfhuang@chromium.org> | 2016-01-08 16:43:52 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-09 00:44:54 +0000 |
commit | a2d008928dfdada4e56426ff391f47fda53bcb1f (patch) | |
tree | 9f75c25c0162bd37cbf26cac0483ec9dd331bc69 /chromecast | |
parent | 927f27305d77ff600a505aa33d586ca8246f1614 (diff) | |
download | chromium_src-a2d008928dfdada4e56426ff391f47fda53bcb1f.zip chromium_src-a2d008928dfdada4e56426ff391f47fda53bcb1f.tar.gz chromium_src-a2d008928dfdada4e56426ff391f47fda53bcb1f.tar.bz2 |
[chromecast] Smooth out OnNetworkChanged event to avoid rapid flipping
between setup state 50 and 60.
BUG=internal b/26017446
Review URL: https://codereview.chromium.org/1496323003
Cr-Commit-Position: refs/heads/master@{#368482}
Diffstat (limited to 'chromecast')
-rw-r--r-- | chromecast/net/connectivity_checker_impl.cc | 24 | ||||
-rw-r--r-- | chromecast/net/connectivity_checker_impl.h | 4 |
2 files changed, 26 insertions, 2 deletions
diff --git a/chromecast/net/connectivity_checker_impl.cc b/chromecast/net/connectivity_checker_impl.cc index 4bfc52f..a79571c 100644 --- a/chromecast/net/connectivity_checker_impl.cc +++ b/chromecast/net/connectivity_checker_impl.cc @@ -36,6 +36,11 @@ const unsigned int kRequestTimeoutInSeconds = 3; const char kDefaultConnectivityCheckUrl[] = "https://clients3.google.com/generate_204"; +// Delay notification of network change events to smooth out rapid flipping. +// Histogram "Cast.Network.Down.Duration.In.Seconds" shows 40% of network +// downtime is less than 3 seconds. +const char kNetworkChangedDelayInSeconds = 3; + } // namespace ConnectivityCheckerImpl::ConnectivityCheckerImpl( @@ -43,7 +48,9 @@ ConnectivityCheckerImpl::ConnectivityCheckerImpl( : ConnectivityChecker(), task_runner_(task_runner), connected_(false), - check_errors_(0) { + connection_type_(net::NetworkChangeNotifier::CONNECTION_NONE), + check_errors_(0), + network_changed_pending_(false) { DCHECK(task_runner_.get()); task_runner->PostTask(FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Initialize, this)); @@ -125,9 +132,22 @@ void ConnectivityCheckerImpl::Check() { void ConnectivityCheckerImpl::OnNetworkChanged( net::NetworkChangeNotifier::ConnectionType type) { VLOG(2) << "OnNetworkChanged " << type; + connection_type_ = type; + + if (network_changed_pending_) + return; + network_changed_pending_ = true; + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&ConnectivityCheckerImpl::OnNetworkChangedInternal, this), + base::TimeDelta::FromSeconds(kNetworkChangedDelayInSeconds)); +} + +void ConnectivityCheckerImpl::OnNetworkChangedInternal() { + network_changed_pending_ = false; Cancel(); - if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { + if (connection_type_ == net::NetworkChangeNotifier::CONNECTION_NONE) { SetConnected(false); return; } diff --git a/chromecast/net/connectivity_checker_impl.h b/chromecast/net/connectivity_checker_impl.h index 1db5dfb..0b40b51 100644 --- a/chromecast/net/connectivity_checker_impl.h +++ b/chromecast/net/connectivity_checker_impl.h @@ -57,6 +57,8 @@ class ConnectivityCheckerImpl void OnNetworkChanged( net::NetworkChangeNotifier::ConnectionType type) override; + void OnNetworkChangedInternal(); + // Cancels current connectivity checking in progress. void Cancel(); @@ -74,8 +76,10 @@ class ConnectivityCheckerImpl scoped_ptr<net::URLRequest> url_request_; const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; bool connected_; + net::NetworkChangeNotifier::ConnectionType connection_type_; // Number of connectivity check errors. unsigned int check_errors_; + bool network_changed_pending_; // Timeout handler for connectivity checks. // Note: Cancelling this timeout can cause the destructor for this class to be // to be called. |