| 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.h" | 5 #include "chromecast/net/connectivity_checker.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" |
| 11 #include "net/base/request_priority.h" | 11 #include "net/base/request_priority.h" |
| 12 #include "net/http/http_response_headers.h" | 12 #include "net/http/http_response_headers.h" |
| 13 #include "net/http/http_response_info.h" | 13 #include "net/http/http_response_info.h" |
| 14 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
| 15 #include "net/proxy/proxy_config.h" | 15 #include "net/proxy/proxy_config.h" |
| 16 #include "net/proxy/proxy_config_service_fixed.h" | 16 #include "net/proxy/proxy_config_service_fixed.h" |
| 17 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 18 #include "net/url_request/url_request_context_builder.h" | 18 #include "net/url_request/url_request_context_builder.h" |
| 19 | 19 |
| 20 namespace chromecast { | 20 namespace chromecast { |
| 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 bad responses received before connectivity status is | 27 // Number of consecutive connectivity check errors before status is changed |
| 28 // changed to offline | 28 // to offline. |
| 29 const unsigned int kNumBadResponses = 3; | 29 const unsigned int kNumErrorsToNotifyOffline = 3; |
| 30 | 30 |
| 31 // Default url for connectivity checking. | 31 // Default url for connectivity checking. |
| 32 const char kDefaultConnectivityCheckUrl[] = | 32 const char kDefaultConnectivityCheckUrl[] = |
| 33 "https://clients3.google.com/generate_204"; | 33 "https://clients3.google.com/generate_204"; |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 ConnectivityChecker::ConnectivityChecker( | 37 ConnectivityChecker::ConnectivityChecker( |
| 38 const scoped_refptr<base::MessageLoopProxy>& loop_proxy) | 38 const scoped_refptr<base::MessageLoopProxy>& loop_proxy) |
| 39 : connectivity_observer_list_( | 39 : connectivity_observer_list_( |
| 40 new ObserverListThreadSafe<ConnectivityObserver>()), | 40 new ObserverListThreadSafe<ConnectivityObserver>()), |
| 41 loop_proxy_(loop_proxy), | 41 loop_proxy_(loop_proxy), |
| 42 connected_(false), | 42 connected_(false), |
| 43 bad_responses_(0) { | 43 check_errors_(0) { |
| 44 DCHECK(loop_proxy_.get()); | 44 DCHECK(loop_proxy_.get()); |
| 45 loop_proxy->PostTask(FROM_HERE, | 45 loop_proxy->PostTask(FROM_HERE, |
| 46 base::Bind(&ConnectivityChecker::Initialize, this)); | 46 base::Bind(&ConnectivityChecker::Initialize, this)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void ConnectivityChecker::Initialize() { | 49 void ConnectivityChecker::Initialize() { |
| 50 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 50 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 51 base::CommandLine::StringType check_url_str = | 51 base::CommandLine::StringType check_url_str = |
| 52 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); | 52 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); |
| 53 connectivity_check_url_.reset(new GURL( | 53 connectivity_check_url_.reset(new GURL( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 } | 98 } |
| 99 | 99 |
| 100 void ConnectivityChecker::Check() { | 100 void ConnectivityChecker::Check() { |
| 101 if (!loop_proxy_->BelongsToCurrentThread()) { | 101 if (!loop_proxy_->BelongsToCurrentThread()) { |
| 102 loop_proxy_->PostTask(FROM_HERE, | 102 loop_proxy_->PostTask(FROM_HERE, |
| 103 base::Bind(&ConnectivityChecker::Check, this)); | 103 base::Bind(&ConnectivityChecker::Check, this)); |
| 104 return; | 104 return; |
| 105 } | 105 } |
| 106 DCHECK(url_request_context_.get()); | 106 DCHECK(url_request_context_.get()); |
| 107 | 107 |
| 108 // Don't check connectivity if network is offline, because internet could be | 108 // Don't check connectivity if network is offline, because Internet could be |
| 109 // accessible via netifs ignored. | 109 // accessible via netifs ignored. |
| 110 if (net::NetworkChangeNotifier::IsOffline()) | 110 if (net::NetworkChangeNotifier::IsOffline()) |
| 111 return; | 111 return; |
| 112 | 112 |
| 113 // If url_request_ is non-null, there is already a check going on. Don't | 113 // If url_request_ is non-null, there is already a check going on. Don't |
| 114 // start another. | 114 // start another. |
| 115 if (url_request_.get()) | 115 if (url_request_.get()) |
| 116 return; | 116 return; |
| 117 | 117 |
| 118 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_; | 118 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 144 (request->status().is_success() && | 144 (request->status().is_success() && |
| 145 request->response_info().headers.get() != NULL) | 145 request->response_info().headers.get() != NULL) |
| 146 ? request->response_info().headers->response_code() | 146 ? request->response_info().headers->response_code() |
| 147 : net::HTTP_BAD_REQUEST; | 147 : net::HTTP_BAD_REQUEST; |
| 148 | 148 |
| 149 // Clears resources. | 149 // Clears resources. |
| 150 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. | 150 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. |
| 151 | 151 |
| 152 if (http_response_code < 400) { | 152 if (http_response_code < 400) { |
| 153 VLOG(1) << "Connectivity check succeeded"; | 153 VLOG(1) << "Connectivity check succeeded"; |
| 154 bad_responses_ = 0; | 154 check_errors_ = 0; |
| 155 SetConnectivity(true); | 155 SetConnectivity(true); |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 VLOG(1) << "Connectivity check failed: " << http_response_code; |
| 159 OnUrlRequestError(); |
| 160 } |
| 158 | 161 |
| 159 VLOG(1) << "Connectivity check failed: " << http_response_code; | 162 void ConnectivityChecker::OnSSLCertificateError(net::URLRequest* request, |
| 160 ++bad_responses_; | 163 const net::SSLInfo& ssl_info, |
| 161 if (bad_responses_ > kNumBadResponses) { | 164 bool fatal) { |
| 162 bad_responses_ = kNumBadResponses; | 165 LOG(ERROR) << "OnSSLCertificateError"; |
| 166 OnUrlRequestError(); |
| 167 } |
| 168 |
| 169 void ConnectivityChecker::OnUrlRequestError() { |
| 170 ++check_errors_; |
| 171 if (check_errors_ > kNumErrorsToNotifyOffline) { |
| 172 check_errors_ = kNumErrorsToNotifyOffline; |
| 163 SetConnectivity(false); | 173 SetConnectivity(false); |
| 164 } | 174 } |
| 165 | 175 url_request_.reset(NULL); |
| 166 // Check again | 176 // Check again. |
| 167 loop_proxy_->PostDelayedTask( | 177 loop_proxy_->PostDelayedTask( |
| 168 FROM_HERE, base::Bind(&ConnectivityChecker::Check, this), | 178 FROM_HERE, base::Bind(&ConnectivityChecker::Check, this), |
| 169 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); | 179 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); |
| 170 } | 180 } |
| 171 | 181 |
| 172 void ConnectivityChecker::OnReadCompleted(net::URLRequest* request, | 182 void ConnectivityChecker::OnReadCompleted(net::URLRequest* request, |
| 173 int bytes_read) { | 183 int bytes_read) { |
| 174 NOTREACHED(); | 184 NOTREACHED(); |
| 175 } | 185 } |
| 176 | 186 |
| 177 void ConnectivityChecker::Cancel() { | 187 void ConnectivityChecker::Cancel() { |
| 178 if (url_request_.get()) { | 188 if (url_request_.get()) { |
| 179 VLOG(2) << "Cancel connectivity check in progress"; | 189 VLOG(2) << "Cancel connectivity check in progress"; |
| 180 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. | 190 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. |
| 181 } | 191 } |
| 182 } | 192 } |
| 183 | 193 |
| 184 } // namespace chromecast | 194 } // namespace chromecast |
| OLD | NEW |