summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorycxiao@chromium.org <ycxiao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-01 18:13:55 +0000
committerycxiao@chromium.org <ycxiao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-01 18:13:55 +0000
commitced4b5462001a474781e626c53d0aa436e644025 (patch)
tree2cf3c4d124ef76429dfd159121c6144a418651ca
parent7d6fd8a472c86db1fd55be5bad2e4e08273c0a02 (diff)
downloadchromium_src-ced4b5462001a474781e626c53d0aa436e644025.zip
chromium_src-ced4b5462001a474781e626c53d0aa436e644025.tar.gz
chromium_src-ced4b5462001a474781e626c53d0aa436e644025.tar.bz2
Apply the asynchronous CookieMonster API to WebsocketJob.
BUG=68657 TEST=XXXX Review URL: http://codereview.chromium.org/7278026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94937 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--net/websockets/websocket_job.cc84
-rw-r--r--net/websockets/websocket_job.h5
-rw-r--r--net/websockets/websocket_job_unittest.cc54
3 files changed, 104 insertions, 39 deletions
diff --git a/net/websockets/websocket_job.cc b/net/websockets/websocket_job.cc
index 7646a14..72796b1 100644
--- a/net/websockets/websocket_job.cc
+++ b/net/websockets/websocket_job.cc
@@ -6,6 +6,7 @@
#include <algorithm>
+#include "base/bind.h"
#include "base/lazy_instance.h"
#include "base/string_tokenizer.h"
#include "googleurl/src/gurl.h"
@@ -83,7 +84,8 @@ WebSocketJob::WebSocketJob(SocketStream::Delegate* delegate)
response_cookies_save_index_(0),
send_frame_handler_(new WebSocketFrameHandler),
receive_frame_handler_(new WebSocketFrameHandler),
- ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
+ weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}
WebSocketJob::~WebSocketJob() {
@@ -166,6 +168,7 @@ void WebSocketJob::DetachDelegate() {
WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary();
scoped_refptr<WebSocketJob> protect(this);
+ weak_ptr_factory_.InvalidateWeakPtrs();
delegate_ = NULL;
if (socket_)
@@ -269,6 +272,7 @@ void WebSocketJob::OnClose(SocketStream* socket) {
WebSocketThrottle::GetInstance()->WakeupSocketIfNecessary();
scoped_refptr<WebSocketJob> protect(this);
+ weak_ptr_factory_.InvalidateWeakPtrs();
SocketStream::Delegate* delegate = delegate_;
delegate_ = NULL;
@@ -369,8 +373,6 @@ bool WebSocketJob::SendHandshakeRequest(const char* data, int len) {
handshake_response_->set_protocol_version(
handshake_request_->protocol_version());
AddCookieHeaderAndSend();
- // Just buffered in |handshake_request_|.
- started_to_send_handshake_request_ = true;
return true;
}
@@ -382,38 +384,47 @@ void WebSocketJob::AddCookieHeaderAndSend() {
if (socket_ && delegate_ && state_ == CONNECTING) {
handshake_request_->RemoveHeaders(
kCookieHeaders, arraysize(kCookieHeaders));
- if (allow) {
+ if (allow && socket_->context()->cookie_store()) {
// Add cookies, including HttpOnly cookies.
- if (socket_->context()->cookie_store()) {
- CookieOptions cookie_options;
- cookie_options.set_include_httponly();
- std::string cookie =
- socket_->context()->cookie_store()->GetCookiesWithOptions(
- GetURLForCookies(), cookie_options);
- if (!cookie.empty())
- handshake_request_->AppendHeaderIfMissing("Cookie", cookie);
- }
- }
-
- if (spdy_websocket_stream_.get()) {
- linked_ptr<spdy::SpdyHeaderBlock> headers(new spdy::SpdyHeaderBlock);
- handshake_request_->GetRequestHeaderBlock(
- socket_->url(), headers.get(), &challenge_);
- spdy_websocket_stream_->SendRequest(headers);
+ CookieOptions cookie_options;
+ cookie_options.set_include_httponly();
+ socket_->context()->cookie_store()->GetCookiesWithOptionsAsync(
+ GetURLForCookies(), cookie_options,
+ base::Bind(&WebSocketJob::LoadCookieCallback,
+ weak_ptr_factory_.GetWeakPtr()));
} else {
- const std::string& handshake_request =
- handshake_request_->GetRawRequest();
- handshake_request_sent_ = 0;
- socket_->net_log()->AddEvent(
- NetLog::TYPE_WEB_SOCKET_SEND_REQUEST_HEADERS,
- make_scoped_refptr(
- new NetLogWebSocketHandshakeParameter(handshake_request)));
- socket_->SendData(handshake_request.data(),
- handshake_request.size());
+ DoSendData();
}
}
}
+void WebSocketJob::LoadCookieCallback(const std::string& cookie) {
+ if (!cookie.empty())
+ handshake_request_->AppendHeaderIfMissing("Cookie", cookie);
+ DoSendData();
+}
+
+void WebSocketJob::DoSendData() {
+ if (spdy_websocket_stream_.get()) {
+ linked_ptr<spdy::SpdyHeaderBlock> headers(new spdy::SpdyHeaderBlock);
+ handshake_request_->GetRequestHeaderBlock(
+ socket_->url(), headers.get(), &challenge_);
+ spdy_websocket_stream_->SendRequest(headers);
+ } else {
+ const std::string& handshake_request =
+ handshake_request_->GetRawRequest();
+ handshake_request_sent_ = 0;
+ socket_->net_log()->AddEvent(
+ NetLog::TYPE_WEB_SOCKET_SEND_REQUEST_HEADERS,
+ make_scoped_refptr(
+ new NetLogWebSocketHandshakeParameter(handshake_request)));
+ socket_->SendData(handshake_request.data(),
+ handshake_request.size());
+ }
+ // Just buffered in |handshake_request_|.
+ started_to_send_handshake_request_ = true;
+}
+
void WebSocketJob::OnSentHandshakeRequest(
SocketStream* socket, int amount_sent) {
DCHECK_EQ(state_, CONNECTING);
@@ -511,16 +522,23 @@ void WebSocketJob::SaveNextCookie() {
allow = false;
if (socket_ && delegate_ && state_ == CONNECTING) {
+ response_cookies_save_index_++;
if (allow && socket_->context()->cookie_store()) {
options.set_include_httponly();
- socket_->context()->cookie_store()->SetCookieWithOptions(
- url, cookie, options);
+ socket_->context()->cookie_store()->SetCookieWithOptionsAsync(
+ url, cookie, options,
+ base::Bind(&WebSocketJob::SaveCookieCallback,
+ weak_ptr_factory_.GetWeakPtr()));
+ } else {
+ SaveNextCookie();
}
- response_cookies_save_index_++;
- SaveNextCookie();
}
}
+void WebSocketJob::SaveCookieCallback(bool cookie_status) {
+ SaveNextCookie();
+}
+
GURL WebSocketJob::GetURLForCookies() const {
GURL url = socket_->url();
std::string scheme = socket_->is_secure() ? "https" : "http";
diff --git a/net/websockets/websocket_job.h b/net/websockets/websocket_job.h
index 2b02d6e..190595e 100644
--- a/net/websockets/websocket_job.h
+++ b/net/websockets/websocket_job.h
@@ -9,6 +9,7 @@
#include <string>
#include <vector>
+#include "base/memory/weak_ptr.h"
#include "base/string16.h"
#include "net/base/address_list.h"
#include "net/base/completion_callback.h"
@@ -88,12 +89,15 @@ class NET_API WebSocketJob
bool SendHandshakeRequest(const char* data, int len);
void AddCookieHeaderAndSend();
+ void LoadCookieCallback(const std::string& cookie);
void OnSentHandshakeRequest(SocketStream* socket, int amount_sent);
void OnReceivedHandshakeResponse(
SocketStream* socket, const char* data, int len);
void SaveCookiesAndNotifyHeaderComplete();
void SaveNextCookie();
+ void SaveCookieCallback(bool cookie_status);
+ void DoSendData();
GURL GetURLForCookies() const;
@@ -134,6 +138,7 @@ class NET_API WebSocketJob
std::string challenge_;
ScopedRunnableMethodFactory<WebSocketJob> method_factory_;
+ base::WeakPtrFactory<WebSocketJob> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(WebSocketJob);
};
diff --git a/net/websockets/websocket_job_unittest.cc b/net/websockets/websocket_job_unittest.cc
index 40ef195..3555014 100644
--- a/net/websockets/websocket_job_unittest.cc
+++ b/net/websockets/websocket_job_unittest.cc
@@ -13,6 +13,7 @@
#include "googleurl/src/gurl.h"
#include "net/base/completion_callback.h"
#include "net/base/cookie_store.h"
+#include "net/base/cookie_store_test_helpers.h"
#include "net/base/mock_host_resolver.h"
#include "net/base/net_errors.h"
#include "net/base/ssl_config_service.h"
@@ -191,22 +192,23 @@ class MockCookieStore : public net::CookieStore {
const net::CookieOptions& options,
std::string* cookie_line,
std::vector<CookieInfo>* cookie_infos) {
- NOTREACHED();
+ ADD_FAILURE();
}
virtual void GetCookiesWithInfoAsync(
const GURL& url,
const net::CookieOptions& options,
const GetCookieInfoCallback& callback) {
- NOTREACHED();
+ ADD_FAILURE();
}
virtual void DeleteCookie(const GURL& url,
- const std::string& cookie_name) {}
-
+ const std::string& cookie_name) {
+ ADD_FAILURE();
+ }
virtual void DeleteCookieAsync(const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
- NOTREACHED();
-}
+ ADD_FAILURE();
+ }
virtual net::CookieMonster* GetCookieMonster() { return NULL; }
@@ -618,6 +620,46 @@ void WebSocketJobTest::TestSlowHandshake() {
CloseWebSocketJob();
}
+TEST_F(WebSocketJobTest, DelayedCookies) {
+ WebSocketJob::set_websocket_over_spdy_enabled(true);
+ GURL url("ws://example.com/demo");
+ GURL cookieUrl("http://example.com/demo");
+ CookieOptions cookie_options;
+ scoped_refptr<DelayedCookieMonster> cookie_store = new DelayedCookieMonster();
+ context_->set_cookie_store(cookie_store);
+ cookie_store->SetCookieWithOptionsAsync(
+ cookieUrl, "CR-test=1", cookie_options,
+ net::CookieMonster::SetCookiesCallback());
+ cookie_options.set_include_httponly();
+ cookie_store->SetCookieWithOptionsAsync(
+ cookieUrl, "CR-test-httponly=1", cookie_options,
+ net::CookieMonster::SetCookiesCallback());
+
+ MockSocketStreamDelegate delegate;
+ InitWebSocketJob(url, &delegate, STREAM_MOCK_SOCKET);
+ SkipToConnecting();
+
+ bool sent = websocket_->SendData(kHandshakeRequestWithCookie,
+ kHandshakeRequestWithCookieLength);
+ EXPECT_TRUE(sent);
+ MessageLoop::current()->RunAllPending();
+ EXPECT_EQ(kHandshakeRequestWithFilteredCookie, sent_data());
+ EXPECT_EQ(WebSocketJob::CONNECTING, GetWebSocketJobState());
+ websocket_->OnSentData(socket_,
+ kHandshakeRequestWithFilteredCookieLength);
+ EXPECT_EQ(kHandshakeRequestWithCookieLength,
+ delegate.amount_sent());
+
+ websocket_->OnReceivedData(socket_.get(),
+ kHandshakeResponseWithCookie,
+ kHandshakeResponseWithCookieLength);
+ MessageLoop::current()->RunAllPending();
+ EXPECT_EQ(kHandshakeResponseWithoutCookie, delegate.received_data());
+ EXPECT_EQ(WebSocketJob::OPEN, GetWebSocketJobState());
+
+ CloseWebSocketJob();
+}
+
void WebSocketJobTest::TestHandshakeWithCookie() {
GURL url("ws://example.com/demo");
GURL cookieUrl("http://example.com/demo");