Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: chrome/browser/browsing_data/site_data_counting_helper_browsertest.cc

Issue 2594723002: Count number of origins with data affected by clearing "cookies and site data". (Closed)
Patch Set: fix review issues Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
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/site_data_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 SiteDataCountingHelperBrowserTest : public InProcessBrowserTest {
msramek 2017/01/10 13:46:32 Optional: Since StoragePartitionImpl seems to test
dullweber 2017/01/10 17:14:38 I converted it to a unittest
25 public:
26 const int64_t kTimeoutMs = 10;
27
28 void SetUpOnMainThread() override {
29 tasks_ = 0;
30 cookie_callback_ =
31 base::Bind(&SiteDataCountingHelperBrowserTest::CookieCallback,
32 base::Unretained(this));
33 }
34
35 void CookieCallback(int count) {
36 // Negative values represent an unexpected error.
37 DCHECK(count >= 0);
38 DCHECK_CURRENTLY_ON(BrowserThread::UI);
39 last_count_ = count;
40
41 if (run_loop_)
42 run_loop_->Quit();
43 }
44
45 void DoneOnIOThread(bool success) {
46 DCHECK(success);
47 if (--tasks_ > 0)
48 return;
49 BrowserThread::PostTask(
50 BrowserThread::UI, FROM_HERE,
51 base::Bind(&SiteDataCountingHelperBrowserTest::DoneCallback,
52 base::Unretained(this)));
53 }
54
55 void DoneCallback() {
56 DCHECK_CURRENTLY_ON(BrowserThread::UI);
57 if (run_loop_)
58 run_loop_->Quit();
59 }
60
61 void WaitForTasksOnIOThread() {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 run_loop_.reset(new base::RunLoop());
64 run_loop_->Run();
65 }
66
67 void CreateCookies(base::Time creation_time,
68 const std::vector<std::string>& urls) {
69 content::StoragePartition* partition =
70 content::BrowserContext::GetDefaultStoragePartition(
71 browser()->profile());
72 net::URLRequestContextGetter* rq_context =
73 partition->GetURLRequestContext();
74 BrowserThread::PostTask(
75 BrowserThread::IO, FROM_HERE,
76 base::Bind(&SiteDataCountingHelperBrowserTest::CreateCookiesOnIOThread,
77 base::Unretained(this), make_scoped_refptr(rq_context),
78 creation_time, urls));
79 }
80
81 void CreateLocalStorage(base::Time creation_time,
82 const std::vector<std::string>& storage_origins) {
83 // Note: This test depends on details of how the dom_storage library
84 // stores data in the host file system.
85 base::FilePath storage_path =
86 browser()->profile()->GetPath().AppendASCII("Local Storage");
87 base::CreateDirectory(storage_path);
88
89 // Write some files.
90 for (const std::string& origin : storage_origins) {
91 base::WriteFile(storage_path.Append(origin), NULL, 0);
92 base::TouchFile(storage_path.Append(origin), creation_time,
93 creation_time);
94 }
95 }
96
97 void CreateCookiesOnIOThread(
98 const scoped_refptr<net::URLRequestContextGetter>& rq_context,
99 base::Time creation_time,
100 std::vector<std::string> urls) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102
103 net::CookieStore* cookie_store =
104 rq_context->GetURLRequestContext()->cookie_store();
105
106 tasks_ = urls.size();
107 int i = 0;
108 for (const std::string& url_string : urls) {
109 GURL url(url_string);
110 // Cookies need a unique creation time.
111 base::Time time = creation_time + base::TimeDelta::FromMilliseconds(i++);
112 cookie_store->SetCookieWithDetailsAsync(
113 url, "name", "A=1", url.host(), url.path(), time, base::Time(), time,
114 true, false, net::CookieSameSite::DEFAULT_MODE, false,
115 net::COOKIE_PRIORITY_DEFAULT,
116 base::Bind(&SiteDataCountingHelperBrowserTest::DoneOnIOThread,
117 base::Unretained(this)));
118 }
119 }
120
121 void CountEntries(base::Time begin_time) {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
123 last_count_ = -1;
124 auto helper = new SiteDataCountingHelper(browser()->profile(), begin_time,
125 cookie_callback_);
126 helper->CountAndDestroySelfWhenFinished();
127 }
128
129 int64_t GetResult() {
130 DCHECK_GE(last_count_, 0);
131 return last_count_;
132 }
133
134 private:
135 base::Callback<void(int)> cookie_callback_;
136 std::unique_ptr<base::RunLoop> run_loop_;
137
138 int tasks_;
139 int64_t last_count_;
140 };
141
142 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CheckEmptyResult) {
143 CountEntries(base::Time());
144 WaitForTasksOnIOThread();
145
146 DCHECK_EQ(0, GetResult());
147 }
148
149 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CountCookies) {
150 base::Time now = base::Time::Now();
151 base::Time last_hour = now - base::TimeDelta::FromHours(1);
152 base::Time yesterday = now - base::TimeDelta::FromDays(1);
153
154 CreateCookies(last_hour, {"https://example.com"});
155 WaitForTasksOnIOThread();
156
157 CreateCookies(yesterday, {"https://google.com", "https://bing.com"});
158 WaitForTasksOnIOThread();
159
160 CountEntries(base::Time());
161 WaitForTasksOnIOThread();
162 DCHECK_EQ(3, GetResult());
163
164 CountEntries(yesterday);
165 WaitForTasksOnIOThread();
166 DCHECK_EQ(3, GetResult());
167
168 CountEntries(last_hour);
169 WaitForTasksOnIOThread();
170 DCHECK_EQ(1, GetResult());
171
172 CountEntries(now);
173 WaitForTasksOnIOThread();
174 DCHECK_EQ(0, GetResult());
175 }
176
177 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, LocalStorage) {
178 base::Time now = base::Time::Now();
179 CreateLocalStorage(now, {"https_example.com_443.localstorage",
180 "https_bing.com_443.localstorage"});
181
182 CountEntries(base::Time());
183 WaitForTasksOnIOThread();
184 DCHECK_EQ(2, GetResult());
185 }
186
187 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest,
188 CookiesAndLocalStorage) {
189 base::Time now = base::Time::Now();
190 CreateCookies(now, {"https://example.com", "https://google.com"});
191 CreateLocalStorage(now, {"https_example.com_443.localstorage",
192 "https_bing.com_443.localstorage"});
193 WaitForTasksOnIOThread();
194
195 CountEntries(base::Time());
196 WaitForTasksOnIOThread();
197 DCHECK_EQ(3, GetResult());
198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698