diff options
author | ycxiao@chromium.org <ycxiao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-25 21:54:46 +0000 |
---|---|---|
committer | ycxiao@chromium.org <ycxiao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-25 21:54:46 +0000 |
commit | 54f4c9365c700f6622125f06e9af19a2a1226643 (patch) | |
tree | adf11ef3c1df24598fc3d20fd60354b64494edd7 | |
parent | b3c9dd097b9db87290001715c0add9ebbc309b5f (diff) | |
download | chromium_src-54f4c9365c700f6622125f06e9af19a2a1226643.zip chromium_src-54f4c9365c700f6622125f06e9af19a2a1226643.tar.gz chromium_src-54f4c9365c700f6622125f06e9af19a2a1226643.tar.bz2 |
Update the url_request_http_job according to the asynchronous CookieMonster API.
BUG=68657
TEST=XXX
Review URL: http://codereview.chromium.org/7210046
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93966 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome_frame/test/net/fake_external_tab.cc | 1 | ||||
-rw-r--r-- | net/base/cookie_store_test_helpers.cc | 148 | ||||
-rw-r--r-- | net/base/cookie_store_test_helpers.h | 100 | ||||
-rw-r--r-- | net/net.gyp | 2 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.cc | 45 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.h | 9 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 38 |
7 files changed, 330 insertions, 13 deletions
diff --git a/chrome_frame/test/net/fake_external_tab.cc b/chrome_frame/test/net/fake_external_tab.cc index 53b33ba..5acd9f4 100644 --- a/chrome_frame/test/net/fake_external_tab.cc +++ b/chrome_frame/test/net/fake_external_tab.cc @@ -480,6 +480,7 @@ void FilterDisabledTests() { // later by using the new INTERNET_OPTION_SUPPRESS_BEHAVIOR flags // See http://msdn.microsoft.com/en-us/library/aa385328(VS.85).aspx "URLRequestTest.DoNotSaveCookies", + "URLRequestTest.DelayedCookieCallback", // TODO(ananta): This test has been consistently failing. Disabling it for // now. diff --git a/net/base/cookie_store_test_helpers.cc b/net/base/cookie_store_test_helpers.cc new file mode 100644 index 0000000..8ab3e1a --- /dev/null +++ b/net/base/cookie_store_test_helpers.cc @@ -0,0 +1,148 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/base/cookie_store_test_helpers.h" + +#include "base/message_loop.h" + +namespace net { + +const int kDelayedTime = 0; + +DelayedCookieMonster::DelayedCookieMonster() + : cookie_monster_(new CookieMonster(NULL, NULL)), + did_run_(false), + result_(false) { +} + +DelayedCookieMonster::~DelayedCookieMonster() { +} + +void DelayedCookieMonster::GetCookiesInternalCallback( + std::string* cookie_line, + std::vector<CookieStore::CookieInfo>* cookie_info) { + cookie_line_ = *cookie_line; + cookie_info_ = *cookie_info; + did_run_ = true; +} + +void DelayedCookieMonster::SetCookiesInternalCallback(bool result) { + result_ = result; + did_run_ = true; +} + +void DelayedCookieMonster::GetCookiesWithOptionsInternalCallback( + std::string cookie) { + cookie_ = cookie; + did_run_ = true; +} + +void DelayedCookieMonster::GetCookiesWithInfoAsync( + const GURL& url, + const CookieOptions& options, + const CookieMonster::GetCookieInfoCallback& callback) { + did_run_ = false; + cookie_monster_->GetCookiesWithInfoAsync( + url, options, + base::Bind(&DelayedCookieMonster::GetCookiesInternalCallback, + base::Unretained(this))); + DCHECK_EQ(did_run_, true); + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&DelayedCookieMonster::InvokeGetCookiesCallback, + base::Unretained(this), callback), + kDelayedTime); +} + +void DelayedCookieMonster::SetCookieWithOptionsAsync( + const GURL& url, const std::string& cookie_line, + const CookieOptions& options, + const CookieMonster::SetCookiesCallback& callback) { + did_run_ = false; + cookie_monster_->SetCookieWithOptionsAsync( + url, cookie_line, options, + base::Bind(&DelayedCookieMonster::SetCookiesInternalCallback, + base::Unretained(this))); + DCHECK_EQ(did_run_, true); + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&DelayedCookieMonster::InvokeSetCookiesCallback, + base::Unretained(this), callback), + kDelayedTime); +} + +void DelayedCookieMonster::GetCookiesWithOptionsAsync( + const GURL& url, const CookieOptions& options, + const CookieMonster::GetCookiesCallback& callback) { + did_run_ = false; + cookie_monster_->GetCookiesWithOptionsAsync( + url, options, + base::Bind(&DelayedCookieMonster::GetCookiesWithOptionsInternalCallback, + base::Unretained(this))); + DCHECK_EQ(did_run_, true); + MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&DelayedCookieMonster::InvokeGetCookieStringCallback, + base::Unretained(this), callback), + kDelayedTime); +} + +void DelayedCookieMonster::InvokeGetCookiesCallback( + const CookieMonster::GetCookieInfoCallback& callback) { + if (!callback.is_null()) + callback.Run(&cookie_line_, &cookie_info_); +} + +void DelayedCookieMonster::InvokeSetCookiesCallback( + const CookieMonster::SetCookiesCallback& callback) { + if (!callback.is_null()) + callback.Run(result_); +} + +void DelayedCookieMonster::InvokeGetCookieStringCallback( + const CookieMonster::GetCookiesCallback& callback) { + if (!callback.is_null()) + callback.Run(cookie_); +} + +bool DelayedCookieMonster::SetCookieWithOptions( + const GURL& url, + const std::string& cookie_line, + const CookieOptions& options) { + ADD_FAILURE(); + return false; +} + +std::string DelayedCookieMonster::GetCookiesWithOptions( + const GURL& url, + const CookieOptions& options) { + ADD_FAILURE(); + return ""; +} + +void DelayedCookieMonster::GetCookiesWithInfo( + const GURL& url, + const CookieOptions& options, + std::string* cookie_line, + std::vector<CookieInfo>* cookie_infos) { + ADD_FAILURE(); +} + +void DelayedCookieMonster::DeleteCookie(const GURL& url, + const std::string& cookie_name) { + ADD_FAILURE(); +} + +void DelayedCookieMonster::DeleteCookieAsync(const GURL& url, + const std::string& cookie_name, + const base::Closure& callback) { + ADD_FAILURE(); +} + +CookieMonster* DelayedCookieMonster::GetCookieMonster() { + ADD_FAILURE(); + return NULL; +} + +} // namespace net diff --git a/net/base/cookie_store_test_helpers.h b/net/base/cookie_store_test_helpers.h new file mode 100644 index 0000000..568bbe5 --- /dev/null +++ b/net/base/cookie_store_test_helpers.h @@ -0,0 +1,100 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef NET_BASE_COOKIE_STORE_TEST_HELPERS_H_ +#define NET_BASE_COOKIE_STORE_TEST_HELPERS_H_ +#pragma once + +#include "net/base/cookie_monster.h" + +#include <string> +#include <vector> + +#include "base/bind.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { + +class DelayedCookieMonster : public CookieStore { + public: + DelayedCookieMonster(); + + // Call the asynchronous CookieMonster function, expect it to immediately + // invoke the internal callback. + // Post a delayed task to invoke the original callback with the results. + + virtual void SetCookieWithOptionsAsync( + const GURL& url, + const std::string& cookie_line, + const CookieOptions& options, + const CookieMonster::SetCookiesCallback& callback); + + virtual void GetCookiesWithOptionsAsync( + const GURL& url, const CookieOptions& options, + const CookieMonster::GetCookiesCallback& callback); + + virtual void GetCookiesWithInfoAsync( + const GURL& url, + const CookieOptions& options, + const CookieMonster::GetCookieInfoCallback& callback); + + virtual bool SetCookieWithOptions(const GURL& url, + const std::string& cookie_line, + const CookieOptions& options); + + virtual std::string GetCookiesWithOptions(const GURL& url, + const CookieOptions& options); + + virtual void GetCookiesWithInfo(const GURL& url, + const CookieOptions& options, + std::string* cookie_line, + std::vector<CookieInfo>* cookie_infos); + + virtual void DeleteCookie(const GURL& url, + const std::string& cookie_name); + + virtual void DeleteCookieAsync(const GURL& url, + const std::string& cookie_name, + const base::Closure& callback); + + virtual CookieMonster* GetCookieMonster(); + + private: + + // Be called immediately from CookieMonster. + + void GetCookiesInternalCallback( + std::string* cookie_line, + std::vector<CookieStore::CookieInfo>* cookie_info); + + void SetCookiesInternalCallback(bool result); + + void GetCookiesWithOptionsInternalCallback(std::string cookie); + + // Invoke the original callbacks. + + void InvokeGetCookiesCallback( + const CookieMonster::GetCookieInfoCallback& callback); + + void InvokeSetCookiesCallback( + const CookieMonster::SetCookiesCallback& callback); + + void InvokeGetCookieStringCallback( + const CookieMonster::GetCookiesCallback& callback); + + friend class base::RefCountedThreadSafe<DelayedCookieMonster>; + virtual ~DelayedCookieMonster(); + + scoped_refptr<CookieMonster> cookie_monster_; + + bool did_run_; + bool result_; + std::string cookie_; + std::string cookie_line_; + std::vector<CookieStore::CookieInfo> cookie_info_; +}; + +} // namespace net + +#endif // NET_BASE_COOKIE_STORE_TEST_HELPERS_H_ diff --git a/net/net.gyp b/net/net.gyp index 949266b..3e96a26 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -1189,6 +1189,8 @@ 'base/cert_test_util.h', 'base/cookie_monster_store_test.cc', 'base/cookie_monster_store_test.h', + 'base/cookie_store_test_helpers.cc', + 'base/cookie_store_test_helpers.h', 'base/net_test_suite.cc', 'base/net_test_suite.h', 'base/test_completion_callback.cc', diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc index 4007282..f404ea3 100644 --- a/net/url_request/url_request_http_job.cc +++ b/net/url_request/url_request_http_job.cc @@ -4,6 +4,7 @@ #include "net/url_request/url_request_http_job.h" +#include "base/bind.h" #include "base/base_switches.h" #include "base/command_line.h" #include "base/compiler_specific.h" @@ -16,7 +17,7 @@ #include "base/string_util.h" #include "base/time.h" #include "net/base/cert_status_flags.h" -#include "net/base/cookie_store.h" +#include "net/base/cookie_monster.h" #include "net/base/filter.h" #include "net/base/host_port_pair.h" #include "net/base/load_flags.h" @@ -521,17 +522,27 @@ void URLRequestHttpJob::AddCookieHeaderAndStart() { if (request_->context()->cookie_store() && allow) { CookieOptions options; options.set_include_httponly(); - std::string cookie_line; - std::vector<CookieStore::CookieInfo> cookie_infos; - request_->context()->cookie_store()->GetCookiesWithInfo( - request_->url(), options, &cookie_line, &cookie_infos); - if (!cookie_line.empty()) { - request_info_.extra_headers.SetHeader( - HttpRequestHeaders::kCookie, cookie_line); - } - if (URLRequest::AreMacCookiesEnabled()) - AddAuthorizationHeader(cookie_infos, &request_info_); + request_->context()->cookie_store()->GetCookiesWithInfoAsync( + request_->url(), options, + base::Bind(&URLRequestHttpJob::OnCookiesLoaded, this)); + } else { + DoStartTransaction(); + } +} + +void URLRequestHttpJob::OnCookiesLoaded( + std::string* cookie_line, + std::vector<net::CookieStore::CookieInfo>* cookie_infos) { + if (!cookie_line->empty()) { + request_info_.extra_headers.SetHeader( + HttpRequestHeaders::kCookie, *cookie_line); } + if (URLRequest::AreMacCookiesEnabled()) + AddAuthorizationHeader(*cookie_infos, &request_info_); + DoStartTransaction(); +} + +void URLRequestHttpJob::DoStartTransaction() { // We may have been canceled within CanGetCookies. if (GetStatus().is_success()) { StartTransaction(); @@ -575,12 +586,20 @@ void URLRequestHttpJob::SaveNextCookie() { options.set_include_httponly(); if (CanSetCookie( response_cookies_[response_cookies_save_index_], &options)) { - request_->context()->cookie_store()->SetCookieWithOptions( + request_->context()->cookie_store()->SetCookieWithOptionsAsync( request_->url(), response_cookies_[response_cookies_save_index_], - options); + options, base::Bind(&URLRequestHttpJob::OnCookieSaved, this)); + return; } } + CookieHandled(); +} + +void URLRequestHttpJob::OnCookieSaved(bool cookie_status) { + CookieHandled(); +} +void URLRequestHttpJob::CookieHandled() { response_cookies_save_index_++; // We may have been canceled within OnSetCookie. if (GetStatus().is_success()) { diff --git a/net/url_request/url_request_http_job.h b/net/url_request/url_request_http_job.h index c5b045d..94fb83f 100644 --- a/net/url_request/url_request_http_job.h +++ b/net/url_request/url_request_http_job.h @@ -15,6 +15,7 @@ #include "base/time.h" #include "net/base/auth.h" #include "net/base/completion_callback.h" +#include "net/base/cookie_store.h" #include "net/http/http_request_info.h" #include "net/url_request/url_request_job.h" #include "net/url_request/url_request_throttler_entry_interface.h" @@ -161,6 +162,14 @@ class URLRequestHttpJob : public URLRequestJob { void RecordPerfHistograms(CompletionCause reason); void DoneWithRequest(CompletionCause reason); + // Callback functions for Cookie Monster + void OnCookiesLoaded( + std::string* cookie_line, + std::vector<CookieStore::CookieInfo>* cookie_infos); + void DoStartTransaction(); + void OnCookieSaved(bool cookie_status); + void CookieHandled(); + // Some servers send the body compressed, but specify the content length as // the uncompressed size. If this is the case, we return true in order // to request to work around this non-adherence to the HTTP standard. diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index 7fe23bf..f6721ba 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -25,6 +25,7 @@ #include "base/stringprintf.h" #include "base/utf_string_conversions.h" #include "net/base/cookie_monster.h" +#include "net/base/cookie_store_test_helpers.h" #include "net/base/load_flags.h" #include "net/base/mock_host_resolver.h" #include "net/base/net_errors.h" @@ -1631,6 +1632,43 @@ TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { } } +TEST_F(URLRequestTest, DelayedCookieCallback) { + TestServer test_server(TestServer::TYPE_HTTP, FilePath()); + ASSERT_TRUE(test_server.Start()); + + scoped_refptr<URLRequestContext> context(new TestURLRequestContext()); + scoped_refptr<DelayedCookieMonster> delayed_cm = + new DelayedCookieMonster(); + scoped_refptr<CookieStore> cookie_store = delayed_cm; + context->set_cookie_store(delayed_cm); + + // Set up a cookie. + { + TestDelegate d; + URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); + req.set_context(context); + req.Start(); + MessageLoop::current()->Run(); + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); + EXPECT_EQ(1, d.set_cookie_count()); + } + + // Verify that the cookie is set. + { + TestDelegate d; + TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); + req.set_context(context); + req.Start(); + MessageLoop::current()->Run(); + + EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") + != std::string::npos); + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); + } +} + TEST_F(URLRequestTest, DoNotSendCookies) { TestServer test_server(TestServer::TYPE_HTTP, FilePath()); ASSERT_TRUE(test_server.Start()); |