Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
|
msramek
2017/01/09 12:54:44
Ditto.
dullweber
2017/01/09 16:05:46
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <algorithm> | |
| 6 #include <memory> | |
| 7 #include <set> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/run_loop.h" | |
| 11 #include "chrome/browser/browsing_data/cookie_counting_helper.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/test/base/in_process_browser_test.h" | |
| 15 #include "content/public/browser/browser_context.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "content/public/browser/storage_partition.h" | |
| 18 #include "net/cookies/cookie_store.h" | |
| 19 #include "net/url_request/url_request_context.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 | |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 class CookieCountingHelperBrowserTest : public InProcessBrowserTest { | |
| 25 public: | |
| 26 const int64_t kTimeoutMs = 10; | |
| 27 | |
| 28 void SetUpOnMainThread() override { | |
| 29 tasks_ = 0; | |
| 30 cookie_callback_ = | |
| 31 base::Bind(&CookieCountingHelperBrowserTest::CookieCallback, | |
| 32 base::Unretained(this)); | |
| 33 } | |
| 34 | |
| 35 void TearDownOnMainThread() override {} | |
| 36 | |
| 37 void CookieCallback(int count) { | |
| 38 // Negative values represent an unexpected error. | |
| 39 DCHECK(count >= 0); | |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 41 last_count_ = count; | |
| 42 | |
| 43 if (run_loop_) | |
| 44 run_loop_->Quit(); | |
| 45 } | |
| 46 | |
| 47 void DoneOnIOThread(bool success) { | |
| 48 DCHECK(success); | |
| 49 LOG(ERROR) << "done: " << success; | |
| 50 if (--tasks_ > 0) | |
| 51 return; | |
| 52 BrowserThread::PostTask( | |
| 53 BrowserThread::UI, FROM_HERE, | |
| 54 base::Bind(&CookieCountingHelperBrowserTest::DoneCallback, | |
| 55 base::Unretained(this))); | |
| 56 } | |
| 57 | |
| 58 void DoneCallback() { | |
| 59 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 60 if (run_loop_) | |
| 61 run_loop_->Quit(); | |
| 62 } | |
| 63 | |
| 64 void WaitForTasksOnIOThread() { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 66 run_loop_.reset(new base::RunLoop()); | |
| 67 run_loop_->Run(); | |
| 68 } | |
| 69 | |
| 70 void CreateCookies(base::Time creation_time, std::vector<GURL> urls) { | |
| 71 content::StoragePartition* partition = | |
| 72 content::BrowserContext::GetDefaultStoragePartition( | |
| 73 browser()->profile()); | |
| 74 net::URLRequestContextGetter* rq_context = | |
| 75 partition->GetURLRequestContext(); | |
| 76 BrowserThread::PostTask( | |
| 77 BrowserThread::IO, FROM_HERE, | |
| 78 base::Bind(&CookieCountingHelperBrowserTest::CreateCookiesOnIOThread, | |
| 79 base::Unretained(this), make_scoped_refptr(rq_context), | |
| 80 creation_time, urls)); | |
| 81 } | |
| 82 | |
| 83 void CreateCookiesOnIOThread( | |
| 84 const scoped_refptr<net::URLRequestContextGetter>& rq_context, | |
| 85 base::Time creation_time, | |
| 86 std::vector<GURL> urls) { | |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 88 | |
| 89 net::CookieStore* cookie_store = | |
| 90 rq_context->GetURLRequestContext()->cookie_store(); | |
| 91 | |
| 92 tasks_ = urls.size(); | |
| 93 int i = 0; | |
| 94 for (const GURL& url : urls) { | |
| 95 // Cookies need a unique creation time. | |
| 96 base::Time time = creation_time + base::TimeDelta::FromMilliseconds(i++); | |
| 97 cookie_store->SetCookieWithDetailsAsync( | |
| 98 url, "name", "A=1", url.host(), url.path(), time, base::Time(), time, | |
| 99 true, false, net::CookieSameSite::DEFAULT_MODE, false, | |
| 100 net::COOKIE_PRIORITY_DEFAULT, | |
| 101 base::Bind(&CookieCountingHelperBrowserTest::DoneOnIOThread, | |
| 102 base::Unretained(this))); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 void CountEntries(base::Time begin_time) { | |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 108 last_count_ = -1; | |
| 109 auto helper = new CookieCountingHelper(browser()->profile(), begin_time, | |
| 110 cookie_callback_); | |
| 111 helper->CountAndDestroySelfWhenFinished(); | |
| 112 } | |
| 113 | |
| 114 int64_t GetResult() { | |
| 115 DCHECK_GE(last_count_, 0); | |
| 116 return last_count_; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 base::Callback<void(int)> cookie_callback_; | |
| 121 std::unique_ptr<base::RunLoop> run_loop_; | |
| 122 | |
| 123 volatile int tasks_; | |
| 124 int64_t last_count_; | |
| 125 }; | |
| 126 | |
| 127 IN_PROC_BROWSER_TEST_F(CookieCountingHelperBrowserTest, CheckEmptyResult) { | |
| 128 CountEntries(base::Time()); | |
| 129 WaitForTasksOnIOThread(); | |
| 130 | |
| 131 DCHECK_EQ(0, GetResult()); | |
| 132 } | |
| 133 | |
| 134 IN_PROC_BROWSER_TEST_F(CookieCountingHelperBrowserTest, CountCookies) { | |
| 135 base::Time now = base::Time::Now(); | |
| 136 base::Time last_hour = now - base::TimeDelta::FromHours(1); | |
| 137 base::Time yesterday = now - base::TimeDelta::FromDays(1); | |
| 138 | |
| 139 CreateCookies(last_hour, {GURL("https://example.com")}); | |
| 140 WaitForTasksOnIOThread(); | |
| 141 | |
| 142 CreateCookies(yesterday, | |
| 143 {GURL("https://google.com"), GURL("https://bing.com")}); | |
| 144 WaitForTasksOnIOThread(); | |
| 145 | |
| 146 CountEntries(base::Time()); | |
| 147 WaitForTasksOnIOThread(); | |
| 148 DCHECK_EQ(3, GetResult()); | |
| 149 | |
| 150 CountEntries(yesterday); | |
| 151 WaitForTasksOnIOThread(); | |
| 152 DCHECK_EQ(3, GetResult()); | |
| 153 | |
| 154 CountEntries(last_hour); | |
| 155 WaitForTasksOnIOThread(); | |
| 156 DCHECK_EQ(1, GetResult()); | |
| 157 | |
| 158 CountEntries(now); | |
| 159 WaitForTasksOnIOThread(); | |
| 160 DCHECK_EQ(0, GetResult()); | |
| 161 } | |
| OLD | NEW |