| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/synchronization/waitable_event.h" | |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | 7 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "content/public/test/browser_test_utils.h" |
| 15 #include "net/base/mock_host_resolver.h" | 17 #include "net/base/mock_host_resolver.h" |
| 16 #include "net/cookies/cookie_store.h" | |
| 17 #include "net/test/test_server.h" | 18 #include "net/test/test_server.h" |
| 18 #include "net/url_request/url_request_context.h" | |
| 19 #include "net/url_request/url_request_context_getter.h" | |
| 20 | 19 |
| 21 using content::BrowserThread; | 20 using content::BrowserThread; |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 void GetCookiesCallback(std::string* cookies_out, | |
| 26 base::WaitableEvent* event, | |
| 27 const std::string& cookies) { | |
| 28 *cookies_out = cookies; | |
| 29 event->Signal(); | |
| 30 } | |
| 31 | |
| 32 void GetCookiesOnIOThread(const GURL& url, | |
| 33 net::URLRequestContextGetter* context_getter, | |
| 34 base::WaitableEvent* event, | |
| 35 std::string* cookies) { | |
| 36 net::CookieStore* cookie_store = | |
| 37 context_getter->GetURLRequestContext()->cookie_store(); | |
| 38 cookie_store->GetCookiesWithOptionsAsync( | |
| 39 url, net::CookieOptions(), | |
| 40 base::Bind(&GetCookiesCallback, | |
| 41 base::Unretained(cookies), base::Unretained(event))); | |
| 42 } | |
| 43 | |
| 44 class CookiePolicyBrowserTest : public InProcessBrowserTest { | 24 class CookiePolicyBrowserTest : public InProcessBrowserTest { |
| 45 protected: | 25 protected: |
| 46 CookiePolicyBrowserTest() {} | 26 CookiePolicyBrowserTest() {} |
| 47 | 27 |
| 48 std::string GetCookies(const GURL& url) { | |
| 49 std::string cookies; | |
| 50 base::WaitableEvent event(true /* manual reset */, | |
| 51 false /* not initially signaled */); | |
| 52 net::URLRequestContextGetter* context_getter = | |
| 53 browser()->profile()->GetRequestContext(); | |
| 54 EXPECT_TRUE( | |
| 55 BrowserThread::PostTask( | |
| 56 BrowserThread::IO, FROM_HERE, | |
| 57 base::Bind(&GetCookiesOnIOThread, url, | |
| 58 make_scoped_refptr(context_getter), &event, &cookies))); | |
| 59 event.Wait(); | |
| 60 return cookies; | |
| 61 } | |
| 62 | |
| 63 private: | 28 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); | 29 DISALLOW_COPY_AND_ASSIGN(CookiePolicyBrowserTest); |
| 65 }; | 30 }; |
| 66 | 31 |
| 67 // Visits a page that sets a first-party cookie. | 32 // Visits a page that sets a first-party cookie. |
| 68 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { | 33 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { |
| 69 ASSERT_TRUE(test_server()->Start()); | 34 ASSERT_TRUE(test_server()->Start()); |
| 70 | 35 |
| 71 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, | 36 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 72 true); | 37 true); |
| 73 | 38 |
| 74 GURL url(test_server()->GetURL("set-cookie?cookie1")); | 39 GURL url(test_server()->GetURL("set-cookie?cookie1")); |
| 75 | 40 |
| 76 std::string cookie = GetCookies(url); | 41 TabContents* tab = chrome::GetActiveTabContents(browser()); |
| 42 std::string cookie = content::GetCookies(tab->profile(), url); |
| 77 ASSERT_EQ("", cookie); | 43 ASSERT_EQ("", cookie); |
| 78 | 44 |
| 79 ui_test_utils::NavigateToURL(browser(), url); | 45 ui_test_utils::NavigateToURL(browser(), url); |
| 80 | 46 |
| 81 cookie = GetCookies(url); | 47 cookie = content::GetCookies(tab->profile(), url); |
| 82 EXPECT_EQ("cookie1", cookie); | 48 EXPECT_EQ("cookie1", cookie); |
| 83 } | 49 } |
| 84 | 50 |
| 85 // Visits a page that is a redirect across domain boundary to a page that sets | 51 // Visits a page that is a redirect across domain boundary to a page that sets |
| 86 // a first-party cookie. | 52 // a first-party cookie. |
| 87 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, | 53 IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, |
| 88 AllowFirstPartyCookiesRedirect) { | 54 AllowFirstPartyCookiesRedirect) { |
| 89 ASSERT_TRUE(test_server()->Start()); | 55 ASSERT_TRUE(test_server()->Start()); |
| 90 | 56 |
| 91 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, | 57 browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, |
| 92 true); | 58 true); |
| 93 | 59 |
| 94 GURL url(test_server()->GetURL("server-redirect?")); | 60 GURL url(test_server()->GetURL("server-redirect?")); |
| 95 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); | 61 GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); |
| 96 | 62 |
| 97 // Change the host name from 127.0.0.1 to www.example.com so it triggers | 63 // Change the host name from 127.0.0.1 to www.example.com so it triggers |
| 98 // third-party cookie blocking if the first party for cookies URL is not | 64 // third-party cookie blocking if the first party for cookies URL is not |
| 99 // changed when we follow a redirect. | 65 // changed when we follow a redirect. |
| 100 ASSERT_EQ("127.0.0.1", redirected_url.host()); | 66 ASSERT_EQ("127.0.0.1", redirected_url.host()); |
| 101 GURL::Replacements replacements; | 67 GURL::Replacements replacements; |
| 102 std::string new_host("www.example.com"); | 68 std::string new_host("www.example.com"); |
| 103 replacements.SetHostStr(new_host); | 69 replacements.SetHostStr(new_host); |
| 104 redirected_url = redirected_url.ReplaceComponents(replacements); | 70 redirected_url = redirected_url.ReplaceComponents(replacements); |
| 105 | 71 |
| 106 std::string cookie = GetCookies(redirected_url); | 72 TabContents* tab = chrome::GetActiveTabContents(browser()); |
| 73 std::string cookie = content::GetCookies(tab->profile(), redirected_url); |
| 107 ASSERT_EQ("", cookie); | 74 ASSERT_EQ("", cookie); |
| 108 | 75 |
| 109 host_resolver()->AddRule("www.example.com", "127.0.0.1"); | 76 host_resolver()->AddRule("www.example.com", "127.0.0.1"); |
| 110 | 77 |
| 111 ui_test_utils::NavigateToURL(browser(), | 78 ui_test_utils::NavigateToURL(browser(), |
| 112 GURL(url.spec() + redirected_url.spec())); | 79 GURL(url.spec() + redirected_url.spec())); |
| 113 | 80 |
| 114 cookie = GetCookies(redirected_url); | 81 cookie = content::GetCookies(tab->profile(), redirected_url); |
| 115 EXPECT_EQ("cookie2", cookie); | 82 EXPECT_EQ("cookie2", cookie); |
| 116 } | 83 } |
| 117 | 84 |
| 118 } // namespace | 85 } // namespace |
| OLD | NEW |