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/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 Loading... | |
| 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 // Request timeout value in seconds. | |
| 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), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 58 builder.DisableHttpCache(); | 61 builder.DisableHttpCache(); |
| 59 url_request_context_.reset(builder.Build()); | 62 url_request_context_.reset(builder.Build()); |
| 60 | 63 |
| 61 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 64 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
| 62 task_runner_->PostTask(FROM_HERE, | 65 task_runner_->PostTask(FROM_HERE, |
| 63 base::Bind(&ConnectivityCheckerImpl::Check, this)); | 66 base::Bind(&ConnectivityCheckerImpl::Check, this)); |
| 64 } | 67 } |
| 65 | 68 |
| 66 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { | 69 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { |
| 67 DCHECK(task_runner_.get()); | 70 DCHECK(task_runner_.get()); |
| 71 timeout_.Cancel(); | |
| 68 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 72 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
| 69 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); | 73 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); |
| 70 task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); | 74 task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); |
| 71 } | 75 } |
| 72 | 76 |
| 73 bool ConnectivityCheckerImpl::Connected() const { | 77 bool ConnectivityCheckerImpl::Connected() const { |
| 74 return connected_; | 78 return connected_; |
| 75 } | 79 } |
| 76 | 80 |
| 77 void ConnectivityCheckerImpl::SetConnected(bool connected) { | 81 void ConnectivityCheckerImpl::SetConnected(bool connected) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 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 |
| 104 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_; | 108 VLOG(1) << "Connectivity check: url=" << *connectivity_check_url_; |
| 105 url_request_ = url_request_context_->CreateRequest( | 109 url_request_ = url_request_context_->CreateRequest( |
| 106 *connectivity_check_url_, net::MAXIMUM_PRIORITY, this); | 110 *connectivity_check_url_, net::MAXIMUM_PRIORITY, this); |
| 107 url_request_->set_method("HEAD"); | 111 url_request_->set_method("HEAD"); |
| 108 url_request_->Start(); | 112 url_request_->Start(); |
| 113 | |
| 114 timeout_.Reset(base::Bind(&ConnectivityCheckerImpl::OnUrlRequestTimeout, | |
| 115 this)); | |
| 116 base::MessageLoop::current()->PostDelayedTask( | |
| 117 FROM_HERE, | |
| 118 timeout_.callback(), | |
| 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) { |
| 136 timeout_.Cancel(); | |
| 125 int http_response_code = | 137 int http_response_code = |
| 126 (request->status().is_success() && | 138 (request->status().is_success() && |
| 127 request->response_info().headers.get() != NULL) | 139 request->response_info().headers.get() != NULL) |
| 128 ? request->response_info().headers->response_code() | 140 ? request->response_info().headers->response_code() |
| 129 : net::HTTP_BAD_REQUEST; | 141 : net::HTTP_BAD_REQUEST; |
| 130 | 142 |
| 131 // Clears resources. | 143 // Clears resources. |
| 132 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. | 144 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. |
| 133 | 145 |
| 134 if (http_response_code < 400) { | 146 if (http_response_code < 400) { |
| 135 VLOG(1) << "Connectivity check succeeded"; | 147 VLOG(1) << "Connectivity check succeeded"; |
| 136 check_errors_ = 0; | 148 check_errors_ = 0; |
| 137 SetConnected(true); | 149 SetConnected(true); |
| 138 return; | 150 return; |
| 139 } | 151 } |
| 140 VLOG(1) << "Connectivity check failed: " << http_response_code; | 152 VLOG(1) << "Connectivity check failed: " << http_response_code; |
| 141 OnUrlRequestError(); | 153 OnUrlRequestError(); |
| 142 } | 154 } |
| 143 | 155 |
| 156 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request, | |
| 157 int bytes_read) { | |
| 158 NOTREACHED(); | |
| 159 } | |
| 160 | |
| 144 void ConnectivityCheckerImpl::OnSSLCertificateError( | 161 void ConnectivityCheckerImpl::OnSSLCertificateError( |
| 145 net::URLRequest* request, | 162 net::URLRequest* request, |
| 146 const net::SSLInfo& ssl_info, | 163 const net::SSLInfo& ssl_info, |
| 147 bool fatal) { | 164 bool fatal) { |
| 148 LOG(ERROR) << "OnSSLCertificateError"; | 165 LOG(ERROR) << "OnSSLCertificateError"; |
| 166 timeout_.Cancel(); | |
| 149 OnUrlRequestError(); | 167 OnUrlRequestError(); |
| 150 } | 168 } |
| 151 | 169 |
| 152 void ConnectivityCheckerImpl::OnUrlRequestError() { | 170 void ConnectivityCheckerImpl::OnUrlRequestError() { |
| 153 ++check_errors_; | 171 ++check_errors_; |
|
gunsch
2015/06/09 22:19:11
Should this case also cancel the timeout?
wzhong
2015/06/09 22:34:57
Not override. Already called at callsite.
| |
| 154 if (check_errors_ > kNumErrorsToNotifyOffline) { | 172 if (check_errors_ > kNumErrorsToNotifyOffline) { |
| 155 check_errors_ = kNumErrorsToNotifyOffline; | 173 check_errors_ = kNumErrorsToNotifyOffline; |
| 156 SetConnected(false); | 174 SetConnected(false); |
| 157 } | 175 } |
| 158 url_request_.reset(NULL); | 176 url_request_.reset(NULL); |
| 159 // Check again. | 177 // Check again. |
| 160 task_runner_->PostDelayedTask( | 178 task_runner_->PostDelayedTask( |
| 161 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), | 179 FROM_HERE, base::Bind(&ConnectivityCheckerImpl::Check, this), |
| 162 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); | 180 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); |
| 163 } | 181 } |
| 164 | 182 |
| 165 void ConnectivityCheckerImpl::OnReadCompleted(net::URLRequest* request, | 183 void ConnectivityCheckerImpl::OnUrlRequestTimeout() { |
| 166 int bytes_read) { | 184 LOG(ERROR) << "time out"; |
| 167 NOTREACHED(); | 185 OnUrlRequestError(); |
| 168 } | 186 } |
| 169 | 187 |
| 170 void ConnectivityCheckerImpl::Cancel() { | 188 void ConnectivityCheckerImpl::Cancel() { |
| 171 if (url_request_.get()) { | 189 if (url_request_.get()) { |
| 172 VLOG(2) << "Cancel connectivity check in progress"; | 190 VLOG(2) << "Cancel connectivity check in progress"; |
| 173 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. | 191 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. |
|
derekjchow1
2015/06/09 22:19:07
timeout_.Cancel(); here as well.
wzhong
2015/06/09 22:34:57
Done.
| |
| 174 } | 192 } |
| 175 } | 193 } |
| 176 | 194 |
| 177 } // namespace chromecast | 195 } // namespace chromecast |
| OLD | NEW |