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"; | |
| 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 | |
| 204 if (check_errors_ == 0) | |
| 205 RecordNetworkConnectivityErrorType(type); | |
| 206 | |
| 197 ++check_errors_; | 207 ++check_errors_; |
| 198 if (check_errors_ > kNumErrorsToNotifyOffline) { | 208 if (check_errors_ > kNumErrorsToNotifyOffline) { |
| 199 check_errors_ = kNumErrorsToNotifyOffline; | 209 check_errors_ = kNumErrorsToNotifyOffline; |
| 200 SetConnected(false); | 210 SetConnected(false); |
| 201 } | 211 } |
| 202 url_request_.reset(nullptr); | 212 url_request_.reset(nullptr); |
| 203 // Check again. | 213 // Check again. |
| 204 task_runner_->PostDelayedTask( | 214 task_runner_->PostDelayedTask( |
| 205 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), | 215 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), |
| 206 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); | 216 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); |
| 207 } | 217 } |
| 208 | 218 |
| 209 void ConnectivityCheckerImpl::OnUrlRequestTimeout() { | 219 void ConnectivityCheckerImpl::OnUrlRequestTimeout() { |
| 210 LOG(ERROR) << "time out"; | 220 LOG(ERROR) << "time out"; |
| 211 OnUrlRequestError(); | 221 OnUrlRequestError(ErrorType::REQUEST_TIMEOUT); |
| 212 } | 222 } |
| 213 | 223 |
| 214 void ConnectivityCheckerImpl::Cancel() { | 224 void ConnectivityCheckerImpl::Cancel() { |
| 215 if (!url_request_.get()) | 225 if (!url_request_.get()) |
| 216 return; | 226 return; |
| 217 VLOG(2) << "Cancel connectivity check in progress"; | 227 VLOG(2) << "Cancel connectivity check in progress"; |
| 218 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. | 228 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. |
| 219 timeout_.Cancel(); | 229 timeout_.Cancel(); |
| 220 } | 230 } |
| 221 | 231 |
| 232 void ConnectivityCheckerImpl::RecordNetworkConnectivityErrorType( | |
| 233 ConnectivityCheckerImpl::ErrorType type) { | |
| 234 std::unique_ptr<base::DictionaryValue> cast_event( | |
|
bcf
2016/05/12 07:35:56
Is there a better way of constructing metrics in c
halliwell
2016/05/12 13:46:19
CastMetricsHelper::RecordApplicationEventWIthValue
bcf
2016/05/12 17:04:10
I saw this, but I wasn't sure we wanted to add the
| |
| 235 new base::DictionaryValue()); | |
| 236 cast_event->SetString("name", kMetricNameNetworkConnectivityErrorType); | |
| 237 cast_event->SetDouble("time", base::TimeTicks::Now().ToInternalValue()); | |
| 238 cast_event->SetInteger("value", static_cast<int>(type)); | |
| 239 std::unique_ptr<std::string> message = SerializeToJson(*cast_event); | |
| 240 DCHECK(message != nullptr); | |
| 241 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction(*message); | |
| 242 } | |
| 243 | |
| 222 } // namespace chromecast | 244 } // namespace chromecast |
| OLD | NEW |