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

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: remove comments 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 {
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 TearDownOnMainThread() override {}
msramek 2017/01/10 10:23:02 Do we need this?
dullweber 2017/01/10 13:18:34 Done.
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 if (--tasks_ > 0)
50 return;
51 BrowserThread::PostTask(
52 BrowserThread::UI, FROM_HERE,
53 base::Bind(&SiteDataCountingHelperBrowserTest::DoneCallback,
54 base::Unretained(this)));
55 }
56
57 void DoneCallback() {
58 DCHECK_CURRENTLY_ON(BrowserThread::UI);
59 if (run_loop_)
60 run_loop_->Quit();
61 }
62
63 void WaitForTasksOnIOThread() {
64 DCHECK_CURRENTLY_ON(BrowserThread::UI);
65 run_loop_.reset(new base::RunLoop());
66 run_loop_->Run();
67 }
68
69 void CreateCookies(base::Time creation_time, std::vector<GURL> urls) {
70 content::StoragePartition* partition =
71 content::BrowserContext::GetDefaultStoragePartition(
72 browser()->profile());
73 net::URLRequestContextGetter* rq_context =
74 partition->GetURLRequestContext();
75 BrowserThread::PostTask(
76 BrowserThread::IO, FROM_HERE,
77 base::Bind(&SiteDataCountingHelperBrowserTest::CreateCookiesOnIOThread,
78 base::Unretained(this), make_scoped_refptr(rq_context),
79 creation_time, urls));
80 }
81
82 void CreateCookiesOnIOThread(
83 const scoped_refptr<net::URLRequestContextGetter>& rq_context,
84 base::Time creation_time,
85 std::vector<GURL> urls) {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO);
87
88 net::CookieStore* cookie_store =
89 rq_context->GetURLRequestContext()->cookie_store();
90
91 tasks_ = urls.size();
92 int i = 0;
93 for (const GURL& url : urls) {
94 // Cookies need a unique creation time.
95 base::Time time = creation_time + base::TimeDelta::FromMilliseconds(i++);
96 cookie_store->SetCookieWithDetailsAsync(
97 url, "name", "A=1", url.host(), url.path(), time, base::Time(), time,
98 true, false, net::CookieSameSite::DEFAULT_MODE, false,
99 net::COOKIE_PRIORITY_DEFAULT,
100 base::Bind(&SiteDataCountingHelperBrowserTest::DoneOnIOThread,
101 base::Unretained(this)));
102 }
103 }
104
105 void CountEntries(base::Time begin_time) {
106 DCHECK_CURRENTLY_ON(BrowserThread::UI);
107 last_count_ = -1;
108 auto helper = new SiteDataCountingHelper(browser()->profile(), begin_time,
109 cookie_callback_);
110 helper->CountAndDestroySelfWhenFinished();
111 }
112
113 int64_t GetResult() {
114 DCHECK_GE(last_count_, 0);
115 return last_count_;
116 }
117
118 private:
119 base::Callback<void(int)> cookie_callback_;
120 std::unique_ptr<base::RunLoop> run_loop_;
121
122 volatile int tasks_;
msramek 2017/01/10 10:23:02 I don't think volatile is necessary here.
dullweber 2017/01/10 13:18:34 Done.
123 int64_t last_count_;
124 };
125
msramek 2017/01/10 10:23:02 Could you please add a test for one other data sto
dullweber 2017/01/10 13:18:34 added a test for localstorage and the combination
126 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CheckEmptyResult) {
127 CountEntries(base::Time());
128 WaitForTasksOnIOThread();
129
130 DCHECK_EQ(0, GetResult());
131 }
132
133 IN_PROC_BROWSER_TEST_F(SiteDataCountingHelperBrowserTest, CountCookies) {
134 base::Time now = base::Time::Now();
135 base::Time last_hour = now - base::TimeDelta::FromHours(1);
136 base::Time yesterday = now - base::TimeDelta::FromDays(1);
137
138 CreateCookies(last_hour, {GURL("https://example.com")});
139 WaitForTasksOnIOThread();
140
141 CreateCookies(yesterday,
142 {GURL("https://google.com"), GURL("https://bing.com")});
143 WaitForTasksOnIOThread();
144
145 CountEntries(base::Time());
146 WaitForTasksOnIOThread();
147 DCHECK_EQ(3, GetResult());
148
149 CountEntries(yesterday);
150 WaitForTasksOnIOThread();
151 DCHECK_EQ(3, GetResult());
152
153 CountEntries(last_hour);
154 WaitForTasksOnIOThread();
155 DCHECK_EQ(1, GetResult());
156
157 CountEntries(now);
158 WaitForTasksOnIOThread();
159 DCHECK_EQ(0, GetResult());
160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698