Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "net/websockets/websocket_stream.h" | 5 #include "net/websockets/websocket_stream.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| 11 #include "base/time/time.h" | |
| 12 #include "base/timer/timer.h" | |
| 11 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
| 12 #include "net/http/http_request_headers.h" | 14 #include "net/http/http_request_headers.h" |
| 13 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 14 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
| 15 #include "net/url_request/redirect_info.h" | 17 #include "net/url_request/redirect_info.h" |
| 16 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
| 17 #include "net/url_request/url_request_context.h" | 19 #include "net/url_request/url_request_context.h" |
| 18 #include "net/websockets/websocket_errors.h" | 20 #include "net/websockets/websocket_errors.h" |
| 19 #include "net/websockets/websocket_event_interface.h" | 21 #include "net/websockets/websocket_event_interface.h" |
| 20 #include "net/websockets/websocket_handshake_constants.h" | 22 #include "net/websockets/websocket_handshake_constants.h" |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 create_helper_); | 107 create_helper_); |
| 106 url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | | 108 url_request_->SetLoadFlags(LOAD_DISABLE_CACHE | |
| 107 LOAD_BYPASS_CACHE | | 109 LOAD_BYPASS_CACHE | |
| 108 LOAD_DO_NOT_PROMPT_FOR_LOGIN); | 110 LOAD_DO_NOT_PROMPT_FOR_LOGIN); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // Destroying this object destroys the URLRequest, which cancels the request | 113 // Destroying this object destroys the URLRequest, which cancels the request |
| 112 // and so terminates the handshake if it is incomplete. | 114 // and so terminates the handshake if it is incomplete. |
| 113 virtual ~StreamRequestImpl() {} | 115 virtual ~StreamRequestImpl() {} |
| 114 | 116 |
| 115 void Start() { | 117 void Start(scoped_ptr<base::Timer> timer) { |
| 118 const TimeDelta timeout = TimeDelta::FromMinutes(1); | |
|
Adam Rice
2014/09/11 07:52:42
The timeout should be defined as a constant at the
yhirano
2014/09/11 08:45:36
Done.
| |
| 119 DCHECK(timer); | |
| 120 timer_ = timer.Pass(); | |
| 121 timer_->Start(FROM_HERE, timeout, | |
| 122 base::Bind(&StreamRequestImpl::OnTimeout, | |
| 123 base::Unretained(this))); | |
| 116 url_request_->Start(); | 124 url_request_->Start(); |
| 117 } | 125 } |
| 118 | 126 |
| 119 void PerformUpgrade() { | 127 void PerformUpgrade() { |
| 128 DCHECK(timer_); | |
| 129 timer_->Stop(); | |
| 120 connect_delegate_->OnSuccess(create_helper_->Upgrade()); | 130 connect_delegate_->OnSuccess(create_helper_->Upgrade()); |
| 121 } | 131 } |
| 122 | 132 |
| 123 void ReportFailure() { | 133 void ReportFailure() { |
| 134 DCHECK(timer_); | |
| 135 timer_->Stop(); | |
| 124 if (failure_message_.empty()) { | 136 if (failure_message_.empty()) { |
| 125 switch (url_request_->status().status()) { | 137 switch (url_request_->status().status()) { |
| 126 case URLRequestStatus::SUCCESS: | 138 case URLRequestStatus::SUCCESS: |
| 127 case URLRequestStatus::IO_PENDING: | 139 case URLRequestStatus::IO_PENDING: |
| 128 break; | 140 break; |
| 129 case URLRequestStatus::CANCELED: | 141 case URLRequestStatus::CANCELED: |
| 130 failure_message_ = "WebSocket opening handshake was canceled"; | 142 if (url_request_->status().error() == ERR_TIMED_OUT) |
| 143 failure_message_ = "WebSocket opening handshake timed out"; | |
| 144 else | |
| 145 failure_message_ = "WebSocket opening handshake was canceled"; | |
| 131 break; | 146 break; |
| 132 case URLRequestStatus::FAILED: | 147 case URLRequestStatus::FAILED: |
| 133 failure_message_ = | 148 failure_message_ = |
| 134 std::string("Error in connection establishment: ") + | 149 std::string("Error in connection establishment: ") + |
| 135 ErrorToString(url_request_->status().error()); | 150 ErrorToString(url_request_->status().error()); |
| 136 break; | 151 break; |
| 137 } | 152 } |
| 138 } | 153 } |
| 139 ReportFailureWithMessage(failure_message_); | 154 ReportFailureWithMessage(failure_message_); |
| 140 } | 155 } |
| 141 | 156 |
| 142 void ReportFailureWithMessage(const std::string& failure_message) { | 157 void ReportFailureWithMessage(const std::string& failure_message) { |
| 143 connect_delegate_->OnFailure(failure_message); | 158 connect_delegate_->OnFailure(failure_message); |
| 144 } | 159 } |
| 145 | 160 |
| 146 void OnFinishOpeningHandshake() { | 161 void OnFinishOpeningHandshake() { |
| 147 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate(), | 162 WebSocketDispatchOnFinishOpeningHandshake(connect_delegate(), |
| 148 url_request_->url(), | 163 url_request_->url(), |
| 149 url_request_->response_headers(), | 164 url_request_->response_headers(), |
| 150 url_request_->response_time()); | 165 url_request_->response_time()); |
| 151 } | 166 } |
| 152 | 167 |
| 153 WebSocketStream::ConnectDelegate* connect_delegate() const { | 168 WebSocketStream::ConnectDelegate* connect_delegate() const { |
| 154 return connect_delegate_.get(); | 169 return connect_delegate_.get(); |
| 155 } | 170 } |
| 156 | 171 |
| 172 void OnTimeout() { | |
| 173 url_request_->CancelWithError(ERR_TIMED_OUT); | |
| 174 } | |
| 175 | |
| 157 private: | 176 private: |
| 158 // |delegate_| needs to be declared before |url_request_| so that it gets | 177 // |delegate_| needs to be declared before |url_request_| so that it gets |
| 159 // initialised first. | 178 // initialised first. |
| 160 scoped_ptr<Delegate> delegate_; | 179 scoped_ptr<Delegate> delegate_; |
| 161 | 180 |
| 162 // Deleting the StreamRequestImpl object deletes this URLRequest object, | 181 // Deleting the StreamRequestImpl object deletes this URLRequest object, |
| 163 // cancelling the whole connection. | 182 // cancelling the whole connection. |
| 164 scoped_ptr<URLRequest> url_request_; | 183 scoped_ptr<URLRequest> url_request_; |
| 165 | 184 |
| 166 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; | 185 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate_; |
| 167 | 186 |
| 168 // Owned by the URLRequest. | 187 // Owned by the URLRequest. |
| 169 WebSocketHandshakeStreamCreateHelper* create_helper_; | 188 WebSocketHandshakeStreamCreateHelper* create_helper_; |
| 170 | 189 |
| 171 // The failure message supplied by WebSocketBasicHandshakeStream, if any. | 190 // The failure message supplied by WebSocketBasicHandshakeStream, if any. |
| 172 std::string failure_message_; | 191 std::string failure_message_; |
| 192 | |
| 193 // A timer for handshake timeout. | |
| 194 scoped_ptr<base::Timer> timer_; | |
| 173 }; | 195 }; |
| 174 | 196 |
| 175 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { | 197 class SSLErrorCallbacks : public WebSocketEventInterface::SSLErrorCallbacks { |
| 176 public: | 198 public: |
| 177 explicit SSLErrorCallbacks(URLRequest* url_request) | 199 explicit SSLErrorCallbacks(URLRequest* url_request) |
| 178 : url_request_(url_request) {} | 200 : url_request_(url_request) {} |
| 179 | 201 |
| 180 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) OVERRIDE { | 202 virtual void CancelSSLRequest(int error, const SSLInfo* ssl_info) OVERRIDE { |
| 181 if (ssl_info) { | 203 if (ssl_info) { |
| 182 url_request_->CancelWithSSLError(error, *ssl_info); | 204 url_request_->CancelWithSSLError(error, *ssl_info); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 scoped_ptr<ConnectDelegate> connect_delegate) { | 301 scoped_ptr<ConnectDelegate> connect_delegate) { |
| 280 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( | 302 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper( |
| 281 new WebSocketHandshakeStreamCreateHelper(connect_delegate.get(), | 303 new WebSocketHandshakeStreamCreateHelper(connect_delegate.get(), |
| 282 requested_subprotocols)); | 304 requested_subprotocols)); |
| 283 scoped_ptr<StreamRequestImpl> request( | 305 scoped_ptr<StreamRequestImpl> request( |
| 284 new StreamRequestImpl(socket_url, | 306 new StreamRequestImpl(socket_url, |
| 285 url_request_context, | 307 url_request_context, |
| 286 origin, | 308 origin, |
| 287 connect_delegate.Pass(), | 309 connect_delegate.Pass(), |
| 288 create_helper.Pass())); | 310 create_helper.Pass())); |
| 289 request->Start(); | 311 request->Start(scoped_ptr<base::Timer>(new base::Timer(false, false))); |
| 290 return request.PassAs<WebSocketStreamRequest>(); | 312 return request.PassAs<WebSocketStreamRequest>(); |
| 291 } | 313 } |
| 292 | 314 |
| 293 // This is declared in websocket_test_util.h. | 315 // This is declared in websocket_test_util.h. |
| 294 scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamForTesting( | 316 scoped_ptr<WebSocketStreamRequest> CreateAndConnectStreamForTesting( |
| 295 const GURL& socket_url, | 317 const GURL& socket_url, |
| 296 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, | 318 scoped_ptr<WebSocketHandshakeStreamCreateHelper> create_helper, |
| 297 const url::Origin& origin, | 319 const url::Origin& origin, |
| 298 URLRequestContext* url_request_context, | 320 URLRequestContext* url_request_context, |
| 299 const BoundNetLog& net_log, | 321 const BoundNetLog& net_log, |
| 300 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate) { | 322 scoped_ptr<WebSocketStream::ConnectDelegate> connect_delegate, |
| 323 scoped_ptr<base::Timer> timer) { | |
| 301 scoped_ptr<StreamRequestImpl> request( | 324 scoped_ptr<StreamRequestImpl> request( |
| 302 new StreamRequestImpl(socket_url, | 325 new StreamRequestImpl(socket_url, |
| 303 url_request_context, | 326 url_request_context, |
| 304 origin, | 327 origin, |
| 305 connect_delegate.Pass(), | 328 connect_delegate.Pass(), |
| 306 create_helper.Pass())); | 329 create_helper.Pass())); |
| 307 request->Start(); | 330 request->Start(timer.Pass()); |
| 308 return request.PassAs<WebSocketStreamRequest>(); | 331 return request.PassAs<WebSocketStreamRequest>(); |
| 309 } | 332 } |
| 310 | 333 |
| 311 void WebSocketDispatchOnFinishOpeningHandshake( | 334 void WebSocketDispatchOnFinishOpeningHandshake( |
| 312 WebSocketStream::ConnectDelegate* connect_delegate, | 335 WebSocketStream::ConnectDelegate* connect_delegate, |
| 313 const GURL& url, | 336 const GURL& url, |
| 314 const scoped_refptr<HttpResponseHeaders>& headers, | 337 const scoped_refptr<HttpResponseHeaders>& headers, |
| 315 base::Time response_time) { | 338 base::Time response_time) { |
| 316 DCHECK(connect_delegate); | 339 DCHECK(connect_delegate); |
| 317 if (headers.get()) { | 340 if (headers.get()) { |
| 318 connect_delegate->OnFinishOpeningHandshake(make_scoped_ptr( | 341 connect_delegate->OnFinishOpeningHandshake(make_scoped_ptr( |
| 319 new WebSocketHandshakeResponseInfo(url, | 342 new WebSocketHandshakeResponseInfo(url, |
| 320 headers->response_code(), | 343 headers->response_code(), |
| 321 headers->GetStatusText(), | 344 headers->GetStatusText(), |
| 322 headers, | 345 headers, |
| 323 response_time))); | 346 response_time))); |
| 324 } | 347 } |
| 325 } | 348 } |
| 326 | 349 |
| 327 } // namespace net | 350 } // namespace net |
| OLD | NEW |