| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_job.h" | 5 #include "net/websockets/websocket_job.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 // lower-case header names. | 30 // lower-case header names. |
| 31 const char* const kCookieHeaders[] = { | 31 const char* const kCookieHeaders[] = { |
| 32 "cookie", "cookie2" | 32 "cookie", "cookie2" |
| 33 }; | 33 }; |
| 34 const char* const kSetCookieHeaders[] = { | 34 const char* const kSetCookieHeaders[] = { |
| 35 "set-cookie", "set-cookie2" | 35 "set-cookie", "set-cookie2" |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 net::SocketStreamJob* WebSocketJobFactory( | 38 net::SocketStreamJob* WebSocketJobFactory( |
| 39 const GURL& url, net::SocketStream::Delegate* delegate) { | 39 const GURL& url, net::SocketStream::Delegate* delegate, |
| 40 net::URLRequestContext* context, net::CookieStore* cookie_store) { |
| 40 net::WebSocketJob* job = new net::WebSocketJob(delegate); | 41 net::WebSocketJob* job = new net::WebSocketJob(delegate); |
| 41 job->InitSocketStream(new net::SocketStream(url, job)); | 42 job->InitSocketStream(new net::SocketStream(url, job, context, cookie_store)); |
| 42 return job; | 43 return job; |
| 43 } | 44 } |
| 44 | 45 |
| 45 class WebSocketJobInitSingleton { | 46 class WebSocketJobInitSingleton { |
| 46 private: | 47 private: |
| 47 friend struct base::DefaultLazyInstanceTraits<WebSocketJobInitSingleton>; | 48 friend struct base::DefaultLazyInstanceTraits<WebSocketJobInitSingleton>; |
| 48 WebSocketJobInitSingleton() { | 49 WebSocketJobInitSingleton() { |
| 49 net::SocketStreamJob::RegisterProtocolFactory("ws", WebSocketJobFactory); | 50 net::SocketStreamJob::RegisterProtocolFactory("ws", WebSocketJobFactory); |
| 50 net::SocketStreamJob::RegisterProtocolFactory("wss", WebSocketJobFactory); | 51 net::SocketStreamJob::RegisterProtocolFactory("wss", WebSocketJobFactory); |
| 51 } | 52 } |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 } | 364 } |
| 364 | 365 |
| 365 void WebSocketJob::AddCookieHeaderAndSend() { | 366 void WebSocketJob::AddCookieHeaderAndSend() { |
| 366 bool allow = true; | 367 bool allow = true; |
| 367 if (delegate_ && !delegate_->CanGetCookies(socket_.get(), GetURLForCookies())) | 368 if (delegate_ && !delegate_->CanGetCookies(socket_.get(), GetURLForCookies())) |
| 368 allow = false; | 369 allow = false; |
| 369 | 370 |
| 370 if (socket_.get() && delegate_ && state_ == CONNECTING) { | 371 if (socket_.get() && delegate_ && state_ == CONNECTING) { |
| 371 handshake_request_->RemoveHeaders(kCookieHeaders, | 372 handshake_request_->RemoveHeaders(kCookieHeaders, |
| 372 arraysize(kCookieHeaders)); | 373 arraysize(kCookieHeaders)); |
| 373 if (allow && socket_->context()->cookie_store()) { | 374 if (allow && socket_->cookie_store()) { |
| 374 // Add cookies, including HttpOnly cookies. | 375 // Add cookies, including HttpOnly cookies. |
| 375 CookieOptions cookie_options; | 376 CookieOptions cookie_options; |
| 376 cookie_options.set_include_httponly(); | 377 cookie_options.set_include_httponly(); |
| 377 socket_->context()->cookie_store()->GetCookiesWithOptionsAsync( | 378 socket_->cookie_store()->GetCookiesWithOptionsAsync( |
| 378 GetURLForCookies(), cookie_options, | 379 GetURLForCookies(), cookie_options, |
| 379 base::Bind(&WebSocketJob::LoadCookieCallback, | 380 base::Bind(&WebSocketJob::LoadCookieCallback, |
| 380 weak_ptr_factory_.GetWeakPtr())); | 381 weak_ptr_factory_.GetWeakPtr())); |
| 381 } else { | 382 } else { |
| 382 DoSendData(); | 383 DoSendData(); |
| 383 } | 384 } |
| 384 } | 385 } |
| 385 } | 386 } |
| 386 | 387 |
| 387 void WebSocketJob::LoadCookieCallback(const std::string& cookie) { | 388 void WebSocketJob::LoadCookieCallback(const std::string& cookie) { |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); | 499 WebSocketThrottle::GetInstance()->RemoveFromQueue(this); |
| 499 } | 500 } |
| 500 | 501 |
| 501 void WebSocketJob::SaveNextCookie() { | 502 void WebSocketJob::SaveNextCookie() { |
| 502 if (!socket_.get() || !delegate_ || state_ != CONNECTING) | 503 if (!socket_.get() || !delegate_ || state_ != CONNECTING) |
| 503 return; | 504 return; |
| 504 | 505 |
| 505 callback_pending_ = false; | 506 callback_pending_ = false; |
| 506 save_next_cookie_running_ = true; | 507 save_next_cookie_running_ = true; |
| 507 | 508 |
| 508 if (socket_->context()->cookie_store()) { | 509 if (socket_->cookie_store()) { |
| 509 GURL url_for_cookies = GetURLForCookies(); | 510 GURL url_for_cookies = GetURLForCookies(); |
| 510 | 511 |
| 511 CookieOptions options; | 512 CookieOptions options; |
| 512 options.set_include_httponly(); | 513 options.set_include_httponly(); |
| 513 | 514 |
| 514 // Loop as long as SetCookieWithOptionsAsync completes synchronously. Since | 515 // Loop as long as SetCookieWithOptionsAsync completes synchronously. Since |
| 515 // CookieMonster's asynchronous operation APIs queue the callback to run it | 516 // CookieMonster's asynchronous operation APIs queue the callback to run it |
| 516 // on the thread where the API was called, there won't be race. I.e. unless | 517 // on the thread where the API was called, there won't be race. I.e. unless |
| 517 // the callback is run synchronously, it won't be run in parallel with this | 518 // the callback is run synchronously, it won't be run in parallel with this |
| 518 // method. | 519 // method. |
| 519 while (!callback_pending_ && | 520 while (!callback_pending_ && |
| 520 response_cookies_save_index_ < response_cookies_.size()) { | 521 response_cookies_save_index_ < response_cookies_.size()) { |
| 521 std::string cookie = response_cookies_[response_cookies_save_index_]; | 522 std::string cookie = response_cookies_[response_cookies_save_index_]; |
| 522 response_cookies_save_index_++; | 523 response_cookies_save_index_++; |
| 523 | 524 |
| 524 if (!delegate_->CanSetCookie( | 525 if (!delegate_->CanSetCookie( |
| 525 socket_.get(), url_for_cookies, cookie, &options)) | 526 socket_.get(), url_for_cookies, cookie, &options)) |
| 526 continue; | 527 continue; |
| 527 | 528 |
| 528 callback_pending_ = true; | 529 callback_pending_ = true; |
| 529 socket_->context()->cookie_store()->SetCookieWithOptionsAsync( | 530 socket_->cookie_store()->SetCookieWithOptionsAsync( |
| 530 url_for_cookies, cookie, options, | 531 url_for_cookies, cookie, options, |
| 531 base::Bind(&WebSocketJob::OnCookieSaved, | 532 base::Bind(&WebSocketJob::OnCookieSaved, |
| 532 weak_ptr_factory_.GetWeakPtr())); | 533 weak_ptr_factory_.GetWeakPtr())); |
| 533 } | 534 } |
| 534 } | 535 } |
| 535 | 536 |
| 536 save_next_cookie_running_ = false; | 537 save_next_cookie_running_ = false; |
| 537 | 538 |
| 538 if (callback_pending_) | 539 if (callback_pending_) |
| 539 return; | 540 return; |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 | 693 |
| 693 scoped_refptr<IOBufferWithSize> next_buffer = send_buffer_queue_.front(); | 694 scoped_refptr<IOBufferWithSize> next_buffer = send_buffer_queue_.front(); |
| 694 send_buffer_queue_.pop_front(); | 695 send_buffer_queue_.pop_front(); |
| 695 current_send_buffer_ = | 696 current_send_buffer_ = |
| 696 new DrainableIOBuffer(next_buffer.get(), next_buffer->size()); | 697 new DrainableIOBuffer(next_buffer.get(), next_buffer->size()); |
| 697 SendDataInternal(current_send_buffer_->data(), | 698 SendDataInternal(current_send_buffer_->data(), |
| 698 current_send_buffer_->BytesRemaining()); | 699 current_send_buffer_->BytesRemaining()); |
| 699 } | 700 } |
| 700 | 701 |
| 701 } // namespace net | 702 } // namespace net |
| OLD | NEW |