| 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_channel.h" | 5 #include "net/websockets/websocket_channel.h" |
| 6 | 6 |
| 7 #include <limits.h> // for INT_MAX | 7 #include <limits.h> // for INT_MAX |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 // destroyed first. | 330 // destroyed first. |
| 331 stream_.reset(); | 331 stream_.reset(); |
| 332 // The timer may have a callback pointing back to us, so stop it just in case | 332 // The timer may have a callback pointing back to us, so stop it just in case |
| 333 // someone decides to run the event loop from their destructor. | 333 // someone decides to run the event loop from their destructor. |
| 334 close_timer_.Stop(); | 334 close_timer_.Stop(); |
| 335 } | 335 } |
| 336 | 336 |
| 337 void WebSocketChannel::SendAddChannelRequest( | 337 void WebSocketChannel::SendAddChannelRequest( |
| 338 const GURL& socket_url, | 338 const GURL& socket_url, |
| 339 const std::vector<std::string>& requested_subprotocols, | 339 const std::vector<std::string>& requested_subprotocols, |
| 340 const url::Origin& origin) { | 340 const url::Origin& origin, |
| 341 const GURL& first_party_for_cookies) { |
| 341 // Delegate to the tested version. | 342 // Delegate to the tested version. |
| 342 SendAddChannelRequestWithSuppliedCreator( | 343 SendAddChannelRequestWithSuppliedCreator( |
| 343 socket_url, | 344 socket_url, requested_subprotocols, origin, first_party_for_cookies, |
| 344 requested_subprotocols, | |
| 345 origin, | |
| 346 base::Bind(&WebSocketStream::CreateAndConnectStream)); | 345 base::Bind(&WebSocketStream::CreateAndConnectStream)); |
| 347 } | 346 } |
| 348 | 347 |
| 349 void WebSocketChannel::SetState(State new_state) { | 348 void WebSocketChannel::SetState(State new_state) { |
| 350 DCHECK_NE(state_, new_state); | 349 DCHECK_NE(state_, new_state); |
| 351 | 350 |
| 352 if (new_state == CONNECTED) | 351 if (new_state == CONNECTED) |
| 353 established_on_ = base::TimeTicks::Now(); | 352 established_on_ = base::TimeTicks::Now(); |
| 354 if (state_ == CONNECTED && !established_on_.is_null()) { | 353 if (state_ == CONNECTED && !established_on_.is_null()) { |
| 355 UMA_HISTOGRAM_LONG_TIMES( | 354 UMA_HISTOGRAM_LONG_TIMES( |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 return CHANNEL_DELETED; | 537 return CHANNEL_DELETED; |
| 539 DCHECK_EQ(CONNECTED, state_); | 538 DCHECK_EQ(CONNECTED, state_); |
| 540 SetState(SEND_CLOSED); | 539 SetState(SEND_CLOSED); |
| 541 return CHANNEL_ALIVE; | 540 return CHANNEL_ALIVE; |
| 542 } | 541 } |
| 543 | 542 |
| 544 void WebSocketChannel::SendAddChannelRequestForTesting( | 543 void WebSocketChannel::SendAddChannelRequestForTesting( |
| 545 const GURL& socket_url, | 544 const GURL& socket_url, |
| 546 const std::vector<std::string>& requested_subprotocols, | 545 const std::vector<std::string>& requested_subprotocols, |
| 547 const url::Origin& origin, | 546 const url::Origin& origin, |
| 547 const GURL& first_party_for_cookies, |
| 548 const WebSocketStreamCreator& creator) { | 548 const WebSocketStreamCreator& creator) { |
| 549 SendAddChannelRequestWithSuppliedCreator( | 549 SendAddChannelRequestWithSuppliedCreator(socket_url, requested_subprotocols, |
| 550 socket_url, requested_subprotocols, origin, creator); | 550 origin, first_party_for_cookies, |
| 551 creator); |
| 551 } | 552 } |
| 552 | 553 |
| 553 void WebSocketChannel::SetClosingHandshakeTimeoutForTesting( | 554 void WebSocketChannel::SetClosingHandshakeTimeoutForTesting( |
| 554 base::TimeDelta delay) { | 555 base::TimeDelta delay) { |
| 555 closing_handshake_timeout_ = delay; | 556 closing_handshake_timeout_ = delay; |
| 556 } | 557 } |
| 557 | 558 |
| 558 void WebSocketChannel::SetUnderlyingConnectionCloseTimeoutForTesting( | 559 void WebSocketChannel::SetUnderlyingConnectionCloseTimeoutForTesting( |
| 559 base::TimeDelta delay) { | 560 base::TimeDelta delay) { |
| 560 underlying_connection_close_timeout_ = delay; | 561 underlying_connection_close_timeout_ = delay; |
| 561 } | 562 } |
| 562 | 563 |
| 563 void WebSocketChannel::SendAddChannelRequestWithSuppliedCreator( | 564 void WebSocketChannel::SendAddChannelRequestWithSuppliedCreator( |
| 564 const GURL& socket_url, | 565 const GURL& socket_url, |
| 565 const std::vector<std::string>& requested_subprotocols, | 566 const std::vector<std::string>& requested_subprotocols, |
| 566 const url::Origin& origin, | 567 const url::Origin& origin, |
| 568 const GURL& first_party_for_cookies, |
| 567 const WebSocketStreamCreator& creator) { | 569 const WebSocketStreamCreator& creator) { |
| 568 DCHECK_EQ(FRESHLY_CONSTRUCTED, state_); | 570 DCHECK_EQ(FRESHLY_CONSTRUCTED, state_); |
| 569 if (!socket_url.SchemeIsWSOrWSS()) { | 571 if (!socket_url.SchemeIsWSOrWSS()) { |
| 570 // TODO(ricea): Kill the renderer (this error should have been caught by | 572 // TODO(ricea): Kill the renderer (this error should have been caught by |
| 571 // Javascript). | 573 // Javascript). |
| 572 ignore_result(event_interface_->OnFailChannel("Invalid scheme")); | 574 ignore_result(event_interface_->OnFailChannel("Invalid scheme")); |
| 573 // |this| is deleted here. | 575 // |this| is deleted here. |
| 574 return; | 576 return; |
| 575 } | 577 } |
| 576 socket_url_ = socket_url; | 578 socket_url_ = socket_url; |
| 577 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate( | 579 std::unique_ptr<WebSocketStream::ConnectDelegate> connect_delegate( |
| 578 new ConnectDelegate(this)); | 580 new ConnectDelegate(this)); |
| 579 stream_request_ = creator.Run(socket_url_, requested_subprotocols, origin, | 581 stream_request_ = creator.Run(socket_url_, requested_subprotocols, origin, |
| 580 url_request_context_, BoundNetLog(), | 582 first_party_for_cookies, url_request_context_, |
| 581 std::move(connect_delegate)); | 583 BoundNetLog(), std::move(connect_delegate)); |
| 582 SetState(CONNECTING); | 584 SetState(CONNECTING); |
| 583 } | 585 } |
| 584 | 586 |
| 585 void WebSocketChannel::OnConnectSuccess( | 587 void WebSocketChannel::OnConnectSuccess( |
| 586 std::unique_ptr<WebSocketStream> stream) { | 588 std::unique_ptr<WebSocketStream> stream) { |
| 587 DCHECK(stream); | 589 DCHECK(stream); |
| 588 DCHECK_EQ(CONNECTING, state_); | 590 DCHECK_EQ(CONNECTING, state_); |
| 589 | 591 |
| 590 stream_ = std::move(stream); | 592 stream_ = std::move(stream); |
| 591 | 593 |
| (...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1172 } | 1174 } |
| 1173 | 1175 |
| 1174 void WebSocketChannel::CloseTimeout() { | 1176 void WebSocketChannel::CloseTimeout() { |
| 1175 stream_->Close(); | 1177 stream_->Close(); |
| 1176 SetState(CLOSED); | 1178 SetState(CLOSED); |
| 1177 DoDropChannel(false, kWebSocketErrorAbnormalClosure, ""); | 1179 DoDropChannel(false, kWebSocketErrorAbnormalClosure, ""); |
| 1178 // |this| has been deleted. | 1180 // |this| has been deleted. |
| 1179 } | 1181 } |
| 1180 | 1182 |
| 1181 } // namespace net | 1183 } // namespace net |
| OLD | NEW |