diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-09 22:41:20 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-09 22:41:20 +0000 |
commit | 3dbb80bca45a4fdf674250ed833842a71de41838 (patch) | |
tree | 46a087293623a29e8bb2a2b8e1567be6ac177c02 /net/url_request | |
parent | 76477871b30938124add005d5df27c0ffb544ee4 (diff) | |
download | chromium_src-3dbb80bca45a4fdf674250ed833842a71de41838.zip chromium_src-3dbb80bca45a4fdf674250ed833842a71de41838.tar.gz chromium_src-3dbb80bca45a4fdf674250ed833842a71de41838.tar.bz2 |
Trigger the blocked cookie notification UI.
This change involves two significant bits:
1- Add URLRequest::Delegate methods to report a blocked attempt to set a cookie
or get cookies, respectively.
2- Generalize the mechanisms we use to proxy notifications to the RenderViewHost
from the IO thread. See render_view_host_notification_task.h.
Finally, these are used together in ResourceDispatcherHost.
Note: Additional work is required to notify when JS attempts to set a cookie
fail.
R=brettw,eroman
BUG=34573
TEST=Configure the browser to block cookies, and then visit a site that tries to
set a cookie (e.g., cnn.com). You should see a cookie icon appear in the location
bar. If you click that it should report that cookies were blocked.
Review URL: http://codereview.chromium.org/600009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38527 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request')
-rw-r--r-- | net/url_request/url_request.h | 8 | ||||
-rw-r--r-- | net/url_request/url_request_http_job.cc | 25 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.cc | 129 | ||||
-rw-r--r-- | net/url_request/url_request_unittest.h | 28 |
4 files changed, 182 insertions, 8 deletions
diff --git a/net/url_request/url_request.h b/net/url_request/url_request.h index e586e28..b420cf8 100644 --- a/net/url_request/url_request.h +++ b/net/url_request/url_request.h @@ -187,6 +187,14 @@ class URLRequest { request->Cancel(); } + // Called when unable to get cookies due to policy. + virtual void OnGetCookiesBlocked(URLRequest* request) { + } + + // Called when unable to set a cookie due to policy. + virtual void OnSetCookieBlocked(URLRequest* request) { + } + // After calling Start(), the delegate will receive an OnResponseStarted // callback when the request has completed. If an error occurred, the // request->status() will be set. On success, all redirects have been diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc index 05fd0f2..ef17774 100644 --- a/net/url_request/url_request_http_job.cc +++ b/net/url_request/url_request_http_job.cc @@ -432,7 +432,9 @@ bool URLRequestHttpJob::ReadRawData(net::IOBuffer* buf, int buf_size, void URLRequestHttpJob::OnCanGetCookiesCompleted(int policy) { // If the request was destroyed, then there is no more work to do. if (request_ && request_->delegate()) { - if (request_->context()->cookie_store() && policy == net::OK) { + if (policy != net::OK) { + request_->delegate()->OnGetCookiesBlocked(request_); + } else if (request_->context()->cookie_store()) { net::CookieOptions options; options.set_include_httponly(); std::string cookies = @@ -442,7 +444,12 @@ void URLRequestHttpJob::OnCanGetCookiesCompleted(int policy) { !cookies.empty()) request_info_.extra_headers += "Cookie: " + cookies + "\r\n"; } - StartTransaction(); + // We may have been canceled within OnGetCookiesBlocked. + if (GetStatus().is_success()) { + StartTransaction(); + } else { + NotifyCanceled(); + } } Release(); // Balance AddRef taken in AddCookieHeaderAndStart } @@ -450,9 +457,10 @@ void URLRequestHttpJob::OnCanGetCookiesCompleted(int policy) { void URLRequestHttpJob::OnCanSetCookieCompleted(int policy) { // If the request was destroyed, then there is no more work to do. if (request_ && request_->delegate()) { - if (request_->context()->cookie_store() && - (policy == net::OK || - policy == net::OK_FOR_SESSION_ONLY)) { + if (policy != net::OK && + policy != net::OK_FOR_SESSION_ONLY) { + request_->delegate()->OnSetCookieBlocked(request_); + } else if (request_->context()->cookie_store()) { // OK to save the current response cookie now. net::CookieOptions options; options.set_include_httponly(); @@ -463,7 +471,12 @@ void URLRequestHttpJob::OnCanSetCookieCompleted(int policy) { options); } response_cookies_save_index_++; - SaveNextCookie(); + // We may have been canceled within OnSetCookieBlocked. + if (GetStatus().is_success()) { + SaveNextCookie(); + } else { + NotifyCanceled(); + } } Release(); // Balance AddRef taken in SaveNextCookie } diff --git a/net/url_request/url_request_unittest.cc b/net/url_request/url_request_unittest.cc index d25ab08..16f4b3f 100644 --- a/net/url_request/url_request_unittest.cc +++ b/net/url_request/url_request_unittest.cc @@ -1241,6 +1241,8 @@ TEST_F(URLRequestTest, DoNotSendCookies) { 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()); } // Verify that the cookie is set. @@ -1253,6 +1255,8 @@ TEST_F(URLRequestTest, DoNotSendCookies) { 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()); } // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. @@ -1266,6 +1270,8 @@ TEST_F(URLRequestTest, DoNotSendCookies) { EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") == std::string::npos); + EXPECT_EQ(1, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } @@ -1283,6 +1289,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies) { 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()); } // Try to set-up another cookie and update the previous cookie. @@ -1295,6 +1304,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies) { req.Start(); MessageLoop::current()->Run(); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(2, d.blocked_set_cookie_count()); } // Verify the cookies weren't saved or updated. @@ -1309,6 +1321,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies) { == std::string::npos); EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") != std::string::npos); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } @@ -1325,6 +1340,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { 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()); } // Verify that the cookie is set. @@ -1337,6 +1355,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { 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()); } // Verify that the cookie isn't sent. @@ -1354,6 +1375,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { == std::string::npos); context->set_cookie_policy(NULL); + + EXPECT_EQ(1, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } @@ -1371,6 +1395,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { 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()); } // Try to set-up another cookie and update the previous cookie. @@ -1387,6 +1414,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { MessageLoop::current()->Run(); context->set_cookie_policy(NULL); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(2, d.blocked_set_cookie_count()); } @@ -1402,6 +1432,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { == std::string::npos); EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") != std::string::npos); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } @@ -1418,6 +1451,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { 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()); } // Verify that the cookie is set. @@ -1430,6 +1466,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { 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()); } // Verify that the cookie isn't sent. @@ -1448,6 +1487,9 @@ TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { == std::string::npos); context->set_cookie_policy(NULL); + + EXPECT_EQ(1, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } @@ -1465,6 +1507,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { 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()); } // Try to set-up another cookie and update the previous cookie. @@ -1482,6 +1527,9 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { MessageLoop::current()->Run(); context->set_cookie_policy(NULL); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(2, d.blocked_set_cookie_count()); } // Verify the cookies weren't saved or updated. @@ -1496,10 +1544,13 @@ TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { == std::string::npos); EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") != std::string::npos); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } } -TEST_F(URLRequestTest, CancelTest_DuringCookiePolicy) { +TEST_F(URLRequestTest, CancelTest_During_CookiePolicy) { scoped_refptr<HTTPTestServer> server = HTTPTestServer::CreateServer(L"", NULL); ASSERT_TRUE(NULL != server.get()); @@ -1516,7 +1567,78 @@ TEST_F(URLRequestTest, CancelTest_DuringCookiePolicy) { req.set_context(context); req.Start(); // Triggers an asynchronous cookie policy check. - // But, now we cancel the request. This should not cause a crash. + // But, now we cancel the request by letting it go out of scope. This + // should not cause a crash. + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); + } + + context->set_cookie_policy(NULL); + + // Let the cookie policy complete. Make sure it handles the destruction of + // the URLRequest properly. + MessageLoop::current()->RunAllPending(); +} + +TEST_F(URLRequestTest, CancelTest_During_OnGetCookiesBlocked) { + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"", NULL); + ASSERT_TRUE(NULL != server.get()); + scoped_refptr<URLRequestTestContext> context = new URLRequestTestContext(); + + TestCookiePolicy cookie_policy(TestCookiePolicy::NO_GET_COOKIES); + context->set_cookie_policy(&cookie_policy); + + // Set up a cookie. + { + TestDelegate d; + d.set_cancel_in_get_cookies_blocked(true); + URLRequest req(server->TestServerPage("set-cookie?A=1&B=2&C=3"), + &d); + req.set_context(context); + req.Start(); // Triggers an asynchronous cookie policy check. + + MessageLoop::current()->Run(); + + EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); + + EXPECT_EQ(1, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); + } + + context->set_cookie_policy(NULL); +} + +TEST_F(URLRequestTest, CancelTest_During_OnSetCookieBlocked) { + scoped_refptr<HTTPTestServer> server = + HTTPTestServer::CreateServer(L"", NULL); + ASSERT_TRUE(NULL != server.get()); + scoped_refptr<URLRequestTestContext> context = new URLRequestTestContext(); + + TestCookiePolicy cookie_policy(TestCookiePolicy::NO_SET_COOKIE); + context->set_cookie_policy(&cookie_policy); + + // Set up a cookie. + { + TestDelegate d; + d.set_cancel_in_set_cookie_blocked(true); + URLRequest req(server->TestServerPage("set-cookie?A=1&B=2&C=3"), + &d); + req.set_context(context); + req.Start(); // Triggers an asynchronous cookie policy check. + + MessageLoop::current()->Run(); + + EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); + + // Even though the response will contain 3 set-cookie headers, we expect + // only one to be blocked as that first one will cause OnSetCookieBlocked + // to be called, which will cancel the request. Once canceled, it should + // not attempt to set further cookies. + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(1, d.blocked_set_cookie_count()); } context->set_cookie_policy(NULL); @@ -1540,6 +1662,9 @@ TEST_F(URLRequestTest, CookiePolicy_ForceSession) { req.Start(); // Triggers an asynchronous cookie policy check. MessageLoop::current()->Run(); + + EXPECT_EQ(0, d.blocked_get_cookies_count()); + EXPECT_EQ(0, d.blocked_set_cookie_count()); } // Now, check the cookie store. diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h index 3f50335..16116d9 100644 --- a/net/url_request/url_request_unittest.h +++ b/net/url_request/url_request_unittest.h @@ -192,12 +192,16 @@ class TestDelegate : public URLRequest::Delegate { cancel_in_rs_(false), cancel_in_rd_(false), cancel_in_rd_pending_(false), + cancel_in_getcookiesblocked_(false), + cancel_in_setcookieblocked_(false), quit_on_complete_(true), quit_on_redirect_(false), allow_certificate_errors_(false), response_started_count_(0), received_bytes_count_(0), received_redirect_count_(0), + blocked_get_cookies_count_(0), + blocked_set_cookie_count_(0), received_data_before_response_(false), request_failed_(false), have_certificate_errors_(false), @@ -300,12 +304,30 @@ class TestDelegate : public URLRequest::Delegate { request->Cancel(); } + virtual void OnGetCookiesBlocked(URLRequest* request) { + blocked_get_cookies_count_++; + if (cancel_in_getcookiesblocked_) + request->Cancel(); + } + + virtual void OnSetCookieBlocked(URLRequest* request) { + blocked_set_cookie_count_++; + if (cancel_in_setcookieblocked_) + request->Cancel(); + } + void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } void set_cancel_in_received_data_pending(bool val) { cancel_in_rd_pending_ = val; } + void set_cancel_in_get_cookies_blocked(bool val) { + cancel_in_getcookiesblocked_ = val; + } + void set_cancel_in_set_cookie_blocked(bool val) { + cancel_in_setcookieblocked_ = val; + } void set_quit_on_complete(bool val) { quit_on_complete_ = val; } void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } void set_allow_certificate_errors(bool val) { @@ -319,6 +341,8 @@ class TestDelegate : public URLRequest::Delegate { int bytes_received() const { return static_cast<int>(data_received_.size()); } int response_started_count() const { return response_started_count_; } int received_redirect_count() const { return received_redirect_count_; } + int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } + int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } bool received_data_before_response() const { return received_data_before_response_; } @@ -332,6 +356,8 @@ class TestDelegate : public URLRequest::Delegate { bool cancel_in_rs_; bool cancel_in_rd_; bool cancel_in_rd_pending_; + bool cancel_in_getcookiesblocked_; + bool cancel_in_setcookieblocked_; bool quit_on_complete_; bool quit_on_redirect_; bool allow_certificate_errors_; @@ -343,6 +369,8 @@ class TestDelegate : public URLRequest::Delegate { int response_started_count_; int received_bytes_count_; int received_redirect_count_; + int blocked_get_cookies_count_; + int blocked_set_cookie_count_; bool received_data_before_response_; bool request_failed_; bool have_certificate_errors_; |