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