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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « chromecast/net/connectivity_checker_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chromecast/net/connectivity_checker_impl.h" 5 #include "chromecast/net/connectivity_checker_impl.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "chromecast/net/net_switches.h" 10 #include "chromecast/net/net_switches.h"
(...skipping 10 matching lines...) Expand all
21 21
22 namespace { 22 namespace {
23 23
24 // How often connectivity checks are performed in seconds. 24 // How often connectivity checks are performed in seconds.
25 const unsigned int kConnectivityPeriodSeconds = 1; 25 const unsigned int kConnectivityPeriodSeconds = 1;
26 26
27 // Number of consecutive connectivity check errors before status is changed 27 // Number of consecutive connectivity check errors before status is changed
28 // to offline. 28 // to offline.
29 const unsigned int kNumErrorsToNotifyOffline = 3; 29 const unsigned int kNumErrorsToNotifyOffline = 3;
30 30
31 // Timeout to get response from request.
32 const unsigned int kRequestTimeOutInSeconds = 3;
33
31 // Default url for connectivity checking. 34 // Default url for connectivity checking.
32 const char kDefaultConnectivityCheckUrl[] = 35 const char kDefaultConnectivityCheckUrl[] =
33 "https://clients3.google.com/generate_204"; 36 "https://clients3.google.com/generate_204";
34 37
35 } // namespace 38 } // namespace
36 39
37 ConnectivityCheckerImpl::ConnectivityCheckerImpl( 40 ConnectivityCheckerImpl::ConnectivityCheckerImpl(
38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) 41 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
39 : ConnectivityChecker(), 42 : ConnectivityChecker(),
40 task_runner_(task_runner), 43 task_runner_(task_runner),
41 connected_(false), 44 connected_(false),
42 check_errors_(0) { 45 check_errors_(0),
46 request_index_(0) {
43 DCHECK(task_runner_.get()); 47 DCHECK(task_runner_.get());
44 task_runner->PostTask(FROM_HERE, 48 task_runner->PostTask(FROM_HERE,
45 base::Bind(&ConnectivityCheckerImpl::Initialize, this)); 49 base::Bind(&ConnectivityCheckerImpl::Initialize, this));
46 } 50 }
47 51
48 void ConnectivityCheckerImpl::Initialize() { 52 void ConnectivityCheckerImpl::Initialize() {
49 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 53 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
50 base::CommandLine::StringType check_url_str = 54 base::CommandLine::StringType check_url_str =
51 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); 55 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl);
52 connectivity_check_url_.reset(new GURL( 56 connectivity_check_url_.reset(new GURL(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // Don't check connectivity if network is offline, because Internet could be 98 // Don't check connectivity if network is offline, because Internet could be
95 // accessible via netifs ignored. 99 // accessible via netifs ignored.
96 if (net::NetworkChangeNotifier::IsOffline()) 100 if (net::NetworkChangeNotifier::IsOffline())
97 return; 101 return;
98 102
99 // If url_request_ is non-null, there is already a check going on. Don't 103 // If url_request_ is non-null, there is already a check going on. Don't
100 // start another. 104 // start another.
101 if (url_request_.get()) 105 if (url_request_.get())
102 return; 106 return;
103 107
108 ++request_index_;
104 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_; 109 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_;
105 url_request_ = url_request_context_->CreateRequest( 110 url_request_ = url_request_context_->CreateRequest(
106 *connectivity_check_url_, net::MAXIMUM_PRIORITY, this); 111 *connectivity_check_url_, net::MAXIMUM_PRIORITY, this);
107 url_request_->set_method("HEAD"); 112 url_request_->set_method("HEAD");
108 url_request_->Start(); 113 url_request_->Start();
114
115 // Schedule time out callback.
116 task_runner_->PostDelayedTask(
117 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::OnUrlRequestTimeOut, this,
118 request_index_),
119 base::TimeDelta::FromSeconds(kRequestTimeOutInSeconds));
109 } 120 }
110 121
111 void ConnectivityCheckerImpl::OnNetworkChanged( 122 void ConnectivityCheckerImpl::OnNetworkChanged(
112 net::NetworkChangeNotifier::ConnectionType type) { 123 net::NetworkChangeNotifier::ConnectionType type) {
113 VLOG(2) << "OnNetworkChanged " << type; 124 VLOG(2) << "OnNetworkChanged " << type;
114 Cancel(); 125 Cancel();
115 126
116 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) { 127 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) {
117 SetConnected(false); 128 SetConnected(false);
118 return; 129 return;
119 } 130 }
120 131
121 Check(); 132 Check();
122 } 133 }
123 134
124 void ConnectivityCheckerImpl::OnResponseStarted(net::URLRequest* request) { 135 void ConnectivityCheckerImpl::OnResponseStarted(net::URLRequest* request) {
125 int http_response_code = 136 int http_response_code =
126 (request->status().is_success() && 137 (request->status().is_success() &&
127 request->response_info().headers.get() != NULL) 138 request->response_info().headers.get() != NULL)
128 ? request->response_info().headers->response_code() 139 ? request->response_info().headers->response_code()
129 : net::HTTP_BAD_REQUEST; 140 : net::HTTP_BAD_REQUEST;
130 141
131 // Clears resources. 142 // Clears resources.
132 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. 143 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor.
byungchul 2015/06/09 20:48:26 increase request_index_? then, you don't need line
wzhong 2015/06/09 20:51:45 Just realized that need to increment for all overr
133 144
134 if (http_response_code < 400) { 145 if (http_response_code < 400) {
135 VLOG(1) << "Connectivity check succeeded"; 146 VLOG(1) << "Connectivity check succeeded";
136 check_errors_ = 0; 147 check_errors_ = 0;
137 SetConnected(true); 148 SetConnected(true);
138 return; 149 return;
139 } 150 }
140 VLOG(1) << "Connectivity check failed: " << http_response_code; 151 VLOG(1) << "Connectivity check failed: " << http_response_code;
141 OnUrlRequestError(); 152 OnUrlRequestError();
142 } 153 }
143 154
144 void ConnectivityCheckerImpl::OnSSLCertificateError( 155 void ConnectivityCheckerImpl::OnSSLCertificateError(
145 net::URLRequest* request, 156 net::URLRequest* request,
146 const net::SSLInfo& ssl_info, 157 const net::SSLInfo& ssl_info,
147 bool fatal) { 158 bool fatal) {
148 LOG(ERROR) << "OnSSLCertificateError"; 159 LOG(ERROR) << "OnSSLCertificateError";
byungchul 2015/06/09 20:48:26 ditto
149 OnUrlRequestError(); 160 OnUrlRequestError();
150 } 161 }
151 162
152 void ConnectivityCheckerImpl::OnUrlRequestError() { 163 void ConnectivityCheckerImpl::OnUrlRequestError() {
153 ++check_errors_; 164 ++check_errors_;
154 if (check_errors_ > kNumErrorsToNotifyOffline) { 165 if (check_errors_ > kNumErrorsToNotifyOffline) {
155 check_errors_ = kNumErrorsToNotifyOffline; 166 check_errors_ = kNumErrorsToNotifyOffline;
156 SetConnected(false); 167 SetConnected(false);
157 } 168 }
158 url_request_.reset(NULL); 169 url_request_.reset(NULL);
159 // Check again. 170 // Check again.
160 task_runner_->PostDelayedTask( 171 task_runner_->PostDelayedTask(
161 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), 172 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this),
162 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); 173 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds));
163 } 174 }
164 175
176 void ConnectivityCheckerImpl::OnUrlRequestTimeOut(unsigned int request_index) {
177 if (request_index_ != request_index)
178 return;
179 if (!url_request_.get()) {
180 LOG(INFO) << "OnUrlRequestTimeOut, request has completed";
181 return;
182 }
183 VLOG(2) << "UrlRequest timed out";
184 OnUrlRequestError();
185 }
186
165 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request, 187 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request,
166 int bytes_read) { 188 int bytes_read) {
167 NOTREACHED(); 189 NOTREACHED();
168 } 190 }
169 191
170 void ConnectivityCheckerImpl::Cancel() { 192 void ConnectivityCheckerImpl::Cancel() {
171 if (url_request_.get()) { 193 if (url_request_.get()) {
172 VLOG(2) << "Cancel connectivity check in progress"; 194 VLOG(2) << "Cancel connectivity check in progress";
173 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. 195 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor.
174 } 196 }
175 } 197 }
176 198
177 } // namespace chromecast 199 } // namespace chromecast
OLDNEW
« 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