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

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

Issue 2594723002: Count number of origins with data affected by clearing "cookies and site data". (Closed)
Patch Set: small fixes Created 3 years, 9 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 "chrome/browser/browsing_data/site_data_counting_helper.h"
6
7 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h"
8 #include "chrome/browser/browsing_data/browsing_data_helper.h"
9 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/dom_storage_context.h"
14 #include "content/public/browser/local_storage_usage_info.h"
15 #include "content/public/browser/session_storage_usage_info.h"
16 #include "content/public/browser/storage_partition.h"
17 #include "net/cookies/cookie_store.h"
18 #include "net/cookies/cookie_util.h"
19 #include "net/ssl/channel_id_service.h"
20 #include "net/ssl/channel_id_store.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "ppapi/features/features.h"
24 #include "storage/browser/quota/quota_manager.h"
25
26 using content::BrowserThread;
27
28 SiteDataCountingHelper::SiteDataCountingHelper(
29 Profile* profile,
30 base::Time begin,
31 base::Callback<void(int)> completion_callback)
32 : profile_(profile),
33 begin_(begin),
34 completion_callback_(completion_callback),
35 tasks_(0) {}
36
37 SiteDataCountingHelper::~SiteDataCountingHelper() {}
38
39 void SiteDataCountingHelper::CountAndDestroySelfWhenFinished() {
40 content::StoragePartition* partition =
41 content::BrowserContext::GetDefaultStoragePartition(profile_);
42
43 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy(
44 profile_->GetSpecialStoragePolicy());
45
46 net::URLRequestContextGetter* rq_context = partition->GetURLRequestContext();
47
48 tasks_ += 1;
49 // Count origins with cookies.
50 BrowserThread::PostTask(
51 BrowserThread::IO, FROM_HERE,
52 base::Bind(&SiteDataCountingHelper::GetCookiesOnIOThread,
53 base::Unretained(this), make_scoped_refptr(rq_context)));
54
55 storage::QuotaManager* quota_manager = partition->GetQuotaManager();
56 if (quota_manager) {
57 // Count origins with filesystem, websql, appcache, indexeddb,
58 // serviceworkers and cachestorage using quota manager.
59 storage::GetOriginsCallback origins_callback =
60 base::Bind(&SiteDataCountingHelper::GetQuotaOriginsCallback,
61 base::Unretained(this));
62 const storage::StorageType types[] = {storage::kStorageTypeTemporary,
63 storage::kStorageTypePersistent,
64 storage::kStorageTypeSyncable};
65 for (auto type : types) {
66 tasks_ += 1;
67 BrowserThread::PostTask(
68 BrowserThread::IO, FROM_HERE,
69 base::Bind(&storage::QuotaManager::GetOriginsModifiedSince,
70 quota_manager, type, begin_, origins_callback));
71 }
72 }
73
74 // Count origins with local storage or session storage.
75 content::DOMStorageContext* dom_storage = partition->GetDOMStorageContext();
76 if (dom_storage) {
77 tasks_ += 1;
78 auto local_callback =
79 base::Bind(&SiteDataCountingHelper::GetLocalStorageUsageInfoCallback,
80 base::Unretained(this), special_storage_policy);
81 dom_storage->GetLocalStorageUsage(local_callback);
82 tasks_ += 1;
83 auto session_callback =
84 base::Bind(&SiteDataCountingHelper::GetSessionStorageUsageInfoCallback,
85 base::Unretained(this), special_storage_policy);
86 dom_storage->GetSessionStorageUsage(session_callback);
87 }
88
89 #if BUILDFLAG(ENABLE_PLUGINS)
90 // Count origins with flash data.
91 flash_lso_helper_ = BrowsingDataFlashLSOHelper::Create(profile_);
92 if (flash_lso_helper_) {
93 tasks_ += 1;
94 flash_lso_helper_->StartFetching(
95 base::Bind(&SiteDataCountingHelper::SitesWithFlashDataCallback,
96 base::Unretained(this)));
97 }
98 #endif
99
100 // Counting site usage data and durable permissions.
101 auto* hcsm = HostContentSettingsMapFactory::GetForProfile(profile_);
102 const ContentSettingsType content_settings[] = {
103 CONTENT_SETTINGS_TYPE_DURABLE_STORAGE,
104 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, CONTENT_SETTINGS_TYPE_APP_BANNER};
105 for (auto type : content_settings) {
106 tasks_ += 1;
107 GetOriginsFromHostContentSettignsMap(hcsm, type);
108 }
109 // Count origins with channel ids.
110 tasks_ += 1;
111 BrowserThread::PostTask(
112 BrowserThread::IO, FROM_HERE,
113 base::Bind(&SiteDataCountingHelper::GetChannelIDsOnIOThread,
114 base::Unretained(this), make_scoped_refptr(rq_context)));
115 }
116
117 void SiteDataCountingHelper::GetOriginsFromHostContentSettignsMap(
118 HostContentSettingsMap* hcsm,
119 ContentSettingsType type) {
120 std::set<GURL> origins;
121 ContentSettingsForOneType settings;
122 hcsm->GetSettingsForOneType(type, std::string(), &settings);
123 for (const ContentSettingPatternSource& rule : settings) {
124 GURL url(rule.primary_pattern.ToString());
125 if (!url.is_empty()) {
126 origins.insert(url);
127 }
128 }
129 Done(std::vector<GURL>(origins.begin(), origins.end()));
130 }
131
132 void SiteDataCountingHelper::GetCookiesOnIOThread(
133 const scoped_refptr<net::URLRequestContextGetter>& rq_context) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO);
135
136 net::CookieStore* cookie_store =
137 rq_context->GetURLRequestContext()->cookie_store();
138
139 if (cookie_store) {
140 cookie_store->GetAllCookiesAsync(base::Bind(
141 &SiteDataCountingHelper::GetCookiesCallback, base::Unretained(this)));
142 } else {
143 BrowserThread::PostTask(
144 BrowserThread::UI, FROM_HERE,
145 base::Bind(&SiteDataCountingHelper::Done, base::Unretained(this),
146 std::vector<GURL>()));
147 }
148 }
149
150 void SiteDataCountingHelper::GetCookiesCallback(
151 const net::CookieList& cookies) {
152 DCHECK_CURRENTLY_ON(BrowserThread::IO);
153 std::vector<GURL> origins;
154 for (const net::CanonicalCookie& cookie : cookies) {
155 if (cookie.CreationDate() >= begin_) {
156 GURL url = net::cookie_util::CookieOriginToURL(cookie.Domain(),
157 cookie.IsSecure());
158 origins.push_back(url);
159 }
160 }
161 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
162 base::Bind(&SiteDataCountingHelper::Done,
163 base::Unretained(this), origins));
164 }
165
166 void SiteDataCountingHelper::GetQuotaOriginsCallback(
167 const std::set<GURL>& origin_set,
168 storage::StorageType type) {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170 std::vector<GURL> origins(origin_set.begin(), origin_set.end());
171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
172 base::Bind(&SiteDataCountingHelper::Done,
173 base::Unretained(this), origins));
174 }
175
176 void SiteDataCountingHelper::GetLocalStorageUsageInfoCallback(
177 const scoped_refptr<storage::SpecialStoragePolicy>& policy,
178 const std::vector<content::LocalStorageUsageInfo>& infos) {
179 std::vector<GURL> origins;
180 for (const auto& info : infos) {
181 if (info.last_modified >= begin_ &&
182 (!policy || !policy->IsStorageProtected(info.origin))) {
183 origins.push_back(info.origin);
184 }
185 }
186 Done(origins);
187 }
188
189 void SiteDataCountingHelper::GetSessionStorageUsageInfoCallback(
190 const scoped_refptr<storage::SpecialStoragePolicy>& policy,
191 const std::vector<content::SessionStorageUsageInfo>& infos) {
192 std::vector<GURL> origins;
193 for (const auto& info : infos) {
194 // Session storage doesn't know about creation time.
195 if (!policy || !policy->IsStorageProtected(info.origin)) {
196 origins.push_back(info.origin);
197 }
198 }
199 Done(origins);
200 }
201
202 void SiteDataCountingHelper::SitesWithFlashDataCallback(
203 const std::vector<std::string>& sites) {
204 std::vector<GURL> origins;
205 for (const std::string& site : sites) {
206 origins.push_back(GURL(site));
207 }
208 Done(origins);
209 }
210
211 void SiteDataCountingHelper::GetChannelIDsOnIOThread(
212 const scoped_refptr<net::URLRequestContextGetter>& rq_context) {
213 DCHECK_CURRENTLY_ON(BrowserThread::IO);
214 net::ChannelIDService* channel_id_service =
215 rq_context->GetURLRequestContext()->channel_id_service();
216 channel_id_service->GetChannelIDStore()->GetAllChannelIDs(base::Bind(
217 &SiteDataCountingHelper::GetChannelIDsCallback, base::Unretained(this)));
218 }
219
220 void SiteDataCountingHelper::GetChannelIDsCallback(
221 const net::ChannelIDStore::ChannelIDList& channel_ids) {
222 std::vector<GURL> origins;
223 for (const net::ChannelIDStore::ChannelID& channel_id : channel_ids) {
224 if (channel_id.creation_time() >= begin_) {
225 // Assume url is https://<server_identifier> on default port because
226 // channel ids don't know about their scheme or port.
227 origins.push_back(GURL("https://" + channel_id.server_identifier()));
228 }
229 }
230 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
231 base::Bind(&SiteDataCountingHelper::Done,
232 base::Unretained(this), origins));
233 }
234
235 void SiteDataCountingHelper::Done(const std::vector<GURL>& origins) {
236 DCHECK_CURRENTLY_ON(BrowserThread::UI);
237 DCHECK(tasks_ > 0);
238 for (const GURL& origin : origins) {
239 unique_origins_.insert(origin);
240 }
241 if (--tasks_ > 0)
242 return;
243 base::ThreadTaskRunnerHandle::Get()->PostTask(
244 FROM_HERE, base::Bind(completion_callback_, unique_origins_.size()));
245 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
246 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698