Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/values.h" | |
| 12 #include "chromecast/base/metrics/cast_metrics_helper.h" | |
| 13 #include "chromecast/base/serializers.h" | |
| 11 #include "chromecast/net/net_switches.h" | 14 #include "chromecast/net/net_switches.h" |
| 12 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 13 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_response_info.h" | 17 #include "net/http/http_response_info.h" |
| 15 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
| 16 #include "net/proxy/proxy_config.h" | 19 #include "net/proxy/proxy_config.h" |
| 17 #include "net/proxy/proxy_config_service_fixed.h" | 20 #include "net/proxy/proxy_config_service_fixed.h" |
| 18 #include "net/socket/ssl_client_socket.h" | 21 #include "net/socket/ssl_client_socket.h" |
| 19 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_context_builder.h" | 23 #include "net/url_request/url_request_context_builder.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 35 | 38 |
| 36 // Default url for connectivity checking. | 39 // Default url for connectivity checking. |
| 37 const char kDefaultConnectivityCheckUrl[] = | 40 const char kDefaultConnectivityCheckUrl[] = |
| 38 "https://connectivitycheck.gstatic.com/generate_204"; | 41 "https://connectivitycheck.gstatic.com/generate_204"; |
| 39 | 42 |
| 40 // Delay notification of network change events to smooth out rapid flipping. | 43 // Delay notification of network change events to smooth out rapid flipping. |
| 41 // Histogram "Cast.Network.Down.Duration.In.Seconds" shows 40% of network | 44 // Histogram "Cast.Network.Down.Duration.In.Seconds" shows 40% of network |
| 42 // downtime is less than 3 seconds. | 45 // downtime is less than 3 seconds. |
| 43 const char kNetworkChangedDelayInSeconds = 3; | 46 const char kNetworkChangedDelayInSeconds = 3; |
| 44 | 47 |
| 48 const char kMetricNameNetworkConnectivityErrorType[] = | |
| 49 "Network.Connectivity.ErrorType"; | |
|
wzhong
2016/05/12 22:48:52
Network.ConnectivityChecking.ErrorType
bcf
2016/05/13 03:05:20
Done.
| |
| 50 | |
| 45 } // namespace | 51 } // namespace |
| 46 | 52 |
| 47 ConnectivityCheckerImpl::ConnectivityCheckerImpl( | 53 ConnectivityCheckerImpl::ConnectivityCheckerImpl( |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 54 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 49 : ConnectivityChecker(), | 55 : ConnectivityChecker(), |
| 50 task_runner_(task_runner), | 56 task_runner_(task_runner), |
| 51 connected_(false), | 57 connected_(false), |
| 52 connection_type_(net::NetworkChangeNotifier::CONNECTION_NONE), | 58 connection_type_(net::NetworkChangeNotifier::CONNECTION_NONE), |
| 53 check_errors_(0), | 59 check_errors_(0), |
| 54 network_changed_pending_(false) { | 60 network_changed_pending_(false) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. | 173 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. |
| 168 | 174 |
| 169 if (http_response_code < 400) { | 175 if (http_response_code < 400) { |
| 170 VLOG(1) << "Connectivity check succeeded"; | 176 VLOG(1) << "Connectivity check succeeded"; |
| 171 check_errors_ = 0; | 177 check_errors_ = 0; |
| 172 SetConnected(true); | 178 SetConnected(true); |
| 173 timeout_.Cancel(); | 179 timeout_.Cancel(); |
| 174 return; | 180 return; |
| 175 } | 181 } |
| 176 VLOG(1) << "Connectivity check failed: " << http_response_code; | 182 VLOG(1) << "Connectivity check failed: " << http_response_code; |
| 177 OnUrlRequestError(); | 183 OnUrlRequestError(ErrorType::BAD_HTTP_STATUS); |
| 178 timeout_.Cancel(); | 184 timeout_.Cancel(); |
| 179 } | 185 } |
| 180 | 186 |
| 181 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request, | 187 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request, |
| 182 int bytes_read) { | 188 int bytes_read) { |
| 183 NOTREACHED(); | 189 NOTREACHED(); |
| 184 } | 190 } |
| 185 | 191 |
| 186 void ConnectivityCheckerImpl::OnSSLCertificateError( | 192 void ConnectivityCheckerImpl::OnSSLCertificateError( |
| 187 net::URLRequest* request, | 193 net::URLRequest* request, |
| 188 const net::SSLInfo& ssl_info, | 194 const net::SSLInfo& ssl_info, |
| 189 bool fatal) { | 195 bool fatal) { |
| 190 LOG(ERROR) << "OnSSLCertificateError: cert_status=" << ssl_info.cert_status; | 196 LOG(ERROR) << "OnSSLCertificateError: cert_status=" << ssl_info.cert_status; |
| 191 net::SSLClientSocket::ClearSessionCache(); | 197 net::SSLClientSocket::ClearSessionCache(); |
| 192 OnUrlRequestError(); | 198 OnUrlRequestError(ErrorType::SSL_CERTIFICATE_ERROR); |
| 193 timeout_.Cancel(); | 199 timeout_.Cancel(); |
| 194 } | 200 } |
| 195 | 201 |
| 196 void ConnectivityCheckerImpl::OnUrlRequestError() { | 202 void ConnectivityCheckerImpl::OnUrlRequestError(ErrorType type) { |
| 203 // Only record a metric for the initial disconnect event | |
|
wzhong
2016/05/12 22:48:52
I think we should record the event when we declare
bcf
2016/05/13 03:05:20
Done.
| |
| 204 if (check_errors_ == 0) { | |
| 205 metrics::CastMetricsHelper::GetInstance()->RecordEventWithValue( | |
| 206 kMetricNameNetworkConnectivityErrorType, static_cast<int>(type)); | |
| 207 } | |
| 208 | |
| 197 ++check_errors_; | 209 ++check_errors_; |
| 198 if (check_errors_ > kNumErrorsToNotifyOffline) { | 210 if (check_errors_ > kNumErrorsToNotifyOffline) { |
| 199 check_errors_ = kNumErrorsToNotifyOffline; | 211 check_errors_ = kNumErrorsToNotifyOffline; |
| 200 SetConnected(false); | 212 SetConnected(false); |
| 201 } | 213 } |
| 202 url_request_.reset(nullptr); | 214 url_request_.reset(nullptr); |
| 203 // Check again. | 215 // Check again. |
| 204 task_runner_->PostDelayedTask( | 216 task_runner_->PostDelayedTask( |
| 205 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), | 217 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), |
| 206 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); | 218 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); |
| 207 } | 219 } |
| 208 | 220 |
| 209 void ConnectivityCheckerImpl::OnUrlRequestTimeout() { | 221 void ConnectivityCheckerImpl::OnUrlRequestTimeout() { |
| 210 LOG(ERROR) << "time out"; | 222 LOG(ERROR) << "time out"; |
| 211 OnUrlRequestError(); | 223 OnUrlRequestError(ErrorType::REQUEST_TIMEOUT); |
| 212 } | 224 } |
| 213 | 225 |
| 214 void ConnectivityCheckerImpl::Cancel() { | 226 void ConnectivityCheckerImpl::Cancel() { |
| 215 if (!url_request_.get()) | 227 if (!url_request_.get()) |
| 216 return; | 228 return; |
| 217 VLOG(2) << "Cancel connectivity check in progress"; | 229 VLOG(2) << "Cancel connectivity check in progress"; |
| 218 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. | 230 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. |
| 219 timeout_.Cancel(); | 231 timeout_.Cancel(); |
| 220 } | 232 } |
| 221 | 233 |
| 222 } // namespace chromecast | 234 } // namespace chromecast |
| OLD | NEW |