Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Unified Diff: chromecast/net/connectivity_checker_impl.cc

Issue 1169163002: Add timeout check for connectivity_checker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add timeout check for connectivity_checker. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chromecast/net/connectivity_checker_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chromecast/net/connectivity_checker_impl.cc
diff --git a/chromecast/net/connectivity_checker_impl.cc b/chromecast/net/connectivity_checker_impl.cc
index d10d06a13f40de64aba74f9586ab203f8b48febc..ac45f2b9313c29d0a7267a5dff4a8fb03db99178 100644
--- a/chromecast/net/connectivity_checker_impl.cc
+++ b/chromecast/net/connectivity_checker_impl.cc
@@ -28,6 +28,9 @@ const unsigned int kConnectivityPeriodSeconds = 1;
// to offline.
const unsigned int kNumErrorsToNotifyOffline = 3;
+// Timeout to get response from request.
+const unsigned int kRequestTimeOutInSeconds = 3;
+
// Default url for connectivity checking.
const char kDefaultConnectivityCheckUrl[] =
"https://clients3.google.com/generate_204";
@@ -39,7 +42,8 @@ ConnectivityCheckerImpl::ConnectivityCheckerImpl(
: ConnectivityChecker(),
task_runner_(task_runner),
connected_(false),
- check_errors_(0) {
+ check_errors_(0),
+ request_index_(0) {
DCHECK(task_runner_.get());
task_runner->PostTask(FROM_HERE,
base::Bind(&ConnectivityCheckerImpl::Initialize, this));
@@ -101,11 +105,18 @@ void ConnectivityCheckerImpl::Check() {
if (url_request_.get())
return;
+ ++request_index_;
VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_;
url_request_ = url_request_context_->CreateRequest(
*connectivity_check_url_, net::MAXIMUM_PRIORITY, this);
url_request_->set_method("HEAD");
url_request_->Start();
+
+ // Schedule time out callback.
+ task_runner_->PostDelayedTask(
+ FROM_HERE, base::Bind(&ConnectivityCheckerImpl::OnUrlRequestTimeOut, this,
+ request_index_),
+ base::TimeDelta::FromSeconds(kRequestTimeOutInSeconds));
}
void ConnectivityCheckerImpl::OnNetworkChanged(
@@ -162,6 +173,17 @@ void ConnectivityCheckerImpl::OnUrlRequestError() {
base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds));
}
+void ConnectivityCheckerImpl::OnUrlRequestTimeOut(unsigned int request_index) {
+ if (request_index_ != request_index)
+ return;
+ if (!url_request_.get()) {
+ LOG(INFO) << "OnUrlRequestTimeOut, request has completed";
+ return;
+ }
+ VLOG(2) << "UrlRequest timed out";
+ OnUrlRequestError();
+}
+
void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request,
int bytes_read) {
NOTREACHED();
« no previous file with comments | « chromecast/net/connectivity_checker_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698