Make crazy linker work in gn build
[chromium-blink-merge.git] / content / browser / storage_partition_impl.cc
blob1e262553c703d7a880710e853456630b1721cada
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
3 // found in the LICENSE file.
5 #include "content/browser/storage_partition_impl.h"
7 #include "base/sequenced_task_runner.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/browser/browser_main_loop.h"
10 #include "content/browser/fileapi/browser_file_system_helper.h"
11 #include "content/browser/gpu/shader_disk_cache.h"
12 #include "content/common/dom_storage/dom_storage_types.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/dom_storage_context.h"
16 #include "content/public/browser/indexed_db_context.h"
17 #include "content/public/browser/local_storage_usage_info.h"
18 #include "content/public/browser/session_storage_usage_info.h"
19 #include "net/base/completion_callback.h"
20 #include "net/base/net_errors.h"
21 #include "net/cookies/cookie_monster.h"
22 #include "net/url_request/url_request_context.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "storage/browser/database/database_tracker.h"
25 #include "storage/browser/quota/quota_manager.h"
27 namespace content {
29 namespace {
31 void OnClearedCookies(const base::Closure& callback, int num_deleted) {
32 // The final callback needs to happen from UI thread.
33 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
34 BrowserThread::PostTask(
35 BrowserThread::UI, FROM_HERE,
36 base::Bind(&OnClearedCookies, callback, num_deleted));
37 return;
40 callback.Run();
43 void ClearCookiesOnIOThread(
44 const scoped_refptr<net::URLRequestContextGetter>& rq_context,
45 const base::Time begin,
46 const base::Time end,
47 const GURL& storage_origin,
48 const base::Closure& callback) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
50 net::CookieStore* cookie_store = rq_context->
51 GetURLRequestContext()->cookie_store();
52 if (storage_origin.is_empty()) {
53 cookie_store->DeleteAllCreatedBetweenAsync(
54 begin,
55 end,
56 base::Bind(&OnClearedCookies, callback));
57 } else {
58 cookie_store->DeleteAllCreatedBetweenForHostAsync(
59 begin,
60 end,
61 storage_origin, base::Bind(&OnClearedCookies, callback));
65 void CheckQuotaManagedDataDeletionStatus(size_t* deletion_task_count,
66 const base::Closure& callback) {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
68 if (*deletion_task_count == 0) {
69 delete deletion_task_count;
70 callback.Run();
74 void OnQuotaManagedOriginDeleted(const GURL& origin,
75 storage::StorageType type,
76 size_t* deletion_task_count,
77 const base::Closure& callback,
78 storage::QuotaStatusCode status) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
80 DCHECK_GT(*deletion_task_count, 0u);
81 if (status != storage::kQuotaStatusOk) {
82 DLOG(ERROR) << "Couldn't remove data of type " << type << " for origin "
83 << origin << ". Status: " << status;
86 (*deletion_task_count)--;
87 CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
90 void ClearedShaderCache(const base::Closure& callback) {
91 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
92 BrowserThread::PostTask(
93 BrowserThread::UI, FROM_HERE,
94 base::Bind(&ClearedShaderCache, callback));
95 return;
97 callback.Run();
100 void ClearShaderCacheOnIOThread(const base::FilePath& path,
101 const base::Time begin,
102 const base::Time end,
103 const base::Closure& callback) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
105 ShaderCacheFactory::GetInstance()->ClearByPath(
106 path, begin, end, base::Bind(&ClearedShaderCache, callback));
109 void OnLocalStorageUsageInfo(
110 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
111 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
112 const StoragePartition::OriginMatcherFunction& origin_matcher,
113 const base::Time delete_begin,
114 const base::Time delete_end,
115 const base::Closure& callback,
116 const std::vector<LocalStorageUsageInfo>& infos) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
119 for (size_t i = 0; i < infos.size(); ++i) {
120 if (!origin_matcher.is_null() &&
121 !origin_matcher.Run(infos[i].origin, special_storage_policy.get())) {
122 continue;
125 if (infos[i].last_modified >= delete_begin &&
126 infos[i].last_modified <= delete_end) {
127 dom_storage_context->DeleteLocalStorage(infos[i].origin);
130 callback.Run();
133 void OnSessionStorageUsageInfo(
134 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
135 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
136 const StoragePartition::OriginMatcherFunction& origin_matcher,
137 const base::Closure& callback,
138 const std::vector<SessionStorageUsageInfo>& infos) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
141 for (size_t i = 0; i < infos.size(); ++i) {
142 if (!origin_matcher.is_null() &&
143 !origin_matcher.Run(infos[i].origin, special_storage_policy.get())) {
144 continue;
146 dom_storage_context->DeleteSessionStorage(infos[i]);
149 callback.Run();
152 void ClearLocalStorageOnUIThread(
153 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
154 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
155 const StoragePartition::OriginMatcherFunction& origin_matcher,
156 const GURL& storage_origin,
157 const base::Time begin,
158 const base::Time end,
159 const base::Closure& callback) {
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
162 if (!storage_origin.is_empty()) {
163 bool can_delete = origin_matcher.is_null() ||
164 origin_matcher.Run(storage_origin,
165 special_storage_policy.get());
166 if (can_delete)
167 dom_storage_context->DeleteLocalStorage(storage_origin);
169 callback.Run();
170 return;
173 dom_storage_context->GetLocalStorageUsage(
174 base::Bind(&OnLocalStorageUsageInfo,
175 dom_storage_context, special_storage_policy, origin_matcher,
176 begin, end, callback));
179 void ClearSessionStorageOnUIThread(
180 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
181 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
182 const StoragePartition::OriginMatcherFunction& origin_matcher,
183 const base::Closure& callback) {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
186 dom_storage_context->GetSessionStorageUsage(
187 base::Bind(&OnSessionStorageUsageInfo, dom_storage_context,
188 special_storage_policy, origin_matcher,
189 callback));
192 } // namespace
194 // static
195 STATIC_CONST_MEMBER_DEFINITION const uint32
196 StoragePartition::REMOVE_DATA_MASK_APPCACHE;
197 STATIC_CONST_MEMBER_DEFINITION const uint32
198 StoragePartition::REMOVE_DATA_MASK_COOKIES;
199 STATIC_CONST_MEMBER_DEFINITION const uint32
200 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
201 STATIC_CONST_MEMBER_DEFINITION const uint32
202 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
203 STATIC_CONST_MEMBER_DEFINITION const uint32
204 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
205 STATIC_CONST_MEMBER_DEFINITION const uint32
206 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
207 STATIC_CONST_MEMBER_DEFINITION const uint32
208 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
209 STATIC_CONST_MEMBER_DEFINITION const uint32
210 StoragePartition::REMOVE_DATA_MASK_WEBSQL;
211 STATIC_CONST_MEMBER_DEFINITION const uint32
212 StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
213 STATIC_CONST_MEMBER_DEFINITION const uint32
214 StoragePartition::REMOVE_DATA_MASK_ALL;
215 STATIC_CONST_MEMBER_DEFINITION const uint32
216 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
217 STATIC_CONST_MEMBER_DEFINITION const uint32
218 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
219 STATIC_CONST_MEMBER_DEFINITION const uint32
220 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE;
221 STATIC_CONST_MEMBER_DEFINITION const uint32
222 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
224 // Static.
225 int StoragePartitionImpl::GenerateQuotaClientMask(uint32 remove_mask) {
226 int quota_client_mask = 0;
228 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS)
229 quota_client_mask |= storage::QuotaClient::kFileSystem;
230 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_WEBSQL)
231 quota_client_mask |= storage::QuotaClient::kDatabase;
232 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_APPCACHE)
233 quota_client_mask |= storage::QuotaClient::kAppcache;
234 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_INDEXEDDB)
235 quota_client_mask |= storage::QuotaClient::kIndexedDatabase;
236 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS)
237 quota_client_mask |= storage::QuotaClient::kServiceWorker;
239 return quota_client_mask;
242 // Helper for deleting quota managed data from a partition.
244 // Most of the operations in this class are done on IO thread.
245 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper {
246 QuotaManagedDataDeletionHelper(uint32 remove_mask,
247 uint32 quota_storage_remove_mask,
248 const GURL& storage_origin,
249 const base::Closure& callback)
250 : remove_mask(remove_mask),
251 quota_storage_remove_mask(quota_storage_remove_mask),
252 storage_origin(storage_origin),
253 callback(callback),
254 task_count(0) {
257 void IncrementTaskCountOnIO();
258 void DecrementTaskCountOnIO();
260 void ClearDataOnIOThread(
261 const scoped_refptr<storage::QuotaManager>& quota_manager,
262 const base::Time begin,
263 const scoped_refptr<storage::SpecialStoragePolicy>&
264 special_storage_policy,
265 const StoragePartition::OriginMatcherFunction& origin_matcher);
267 void ClearOriginsOnIOThread(
268 storage::QuotaManager* quota_manager,
269 const scoped_refptr<storage::SpecialStoragePolicy>&
270 special_storage_policy,
271 const StoragePartition::OriginMatcherFunction& origin_matcher,
272 const base::Closure& callback,
273 const std::set<GURL>& origins,
274 storage::StorageType quota_storage_type);
276 // All of these data are accessed on IO thread.
277 uint32 remove_mask;
278 uint32 quota_storage_remove_mask;
279 GURL storage_origin;
280 const base::Closure callback;
281 int task_count;
284 // Helper for deleting all sorts of data from a partition, keeps track of
285 // deletion status.
287 // StoragePartitionImpl creates an instance of this class to keep track of
288 // data deletion progress. Deletion requires deleting multiple bits of data
289 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
290 // and IO thread. An instance of this class is created in the beginning of
291 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
292 // forwarded and updated on each (sub) deletion's callback. The instance is
293 // finally destroyed when deletion completes (and |callback| is invoked).
294 struct StoragePartitionImpl::DataDeletionHelper {
295 DataDeletionHelper(uint32 remove_mask,
296 uint32 quota_storage_remove_mask,
297 const base::Closure& callback)
298 : remove_mask(remove_mask),
299 quota_storage_remove_mask(quota_storage_remove_mask),
300 callback(callback),
301 task_count(0) {
304 void IncrementTaskCountOnUI();
305 void DecrementTaskCountOnUI();
307 void ClearDataOnUIThread(
308 const GURL& storage_origin,
309 const OriginMatcherFunction& origin_matcher,
310 const base::FilePath& path,
311 net::URLRequestContextGetter* rq_context,
312 DOMStorageContextWrapper* dom_storage_context,
313 storage::QuotaManager* quota_manager,
314 storage::SpecialStoragePolicy* special_storage_policy,
315 WebRTCIdentityStore* webrtc_identity_store,
316 const base::Time begin,
317 const base::Time end);
319 void ClearQuotaManagedDataOnIOThread(
320 const scoped_refptr<storage::QuotaManager>& quota_manager,
321 const base::Time begin,
322 const GURL& storage_origin,
323 const scoped_refptr<storage::SpecialStoragePolicy>&
324 special_storage_policy,
325 const StoragePartition::OriginMatcherFunction& origin_matcher,
326 const base::Closure& callback);
328 uint32 remove_mask;
329 uint32 quota_storage_remove_mask;
331 // Accessed on UI thread.
332 const base::Closure callback;
333 // Accessed on UI thread.
334 int task_count;
337 void StoragePartitionImpl::DataDeletionHelper::ClearQuotaManagedDataOnIOThread(
338 const scoped_refptr<storage::QuotaManager>& quota_manager,
339 const base::Time begin,
340 const GURL& storage_origin,
341 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
342 const StoragePartition::OriginMatcherFunction& origin_matcher,
343 const base::Closure& callback) {
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
346 StoragePartitionImpl::QuotaManagedDataDeletionHelper* helper =
347 new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
348 remove_mask,
349 quota_storage_remove_mask,
350 storage_origin,
351 callback);
352 helper->ClearDataOnIOThread(quota_manager, begin, special_storage_policy,
353 origin_matcher);
356 StoragePartitionImpl::StoragePartitionImpl(
357 const base::FilePath& partition_path,
358 storage::QuotaManager* quota_manager,
359 ChromeAppCacheService* appcache_service,
360 storage::FileSystemContext* filesystem_context,
361 storage::DatabaseTracker* database_tracker,
362 DOMStorageContextWrapper* dom_storage_context,
363 IndexedDBContextImpl* indexed_db_context,
364 ServiceWorkerContextWrapper* service_worker_context,
365 WebRTCIdentityStore* webrtc_identity_store,
366 storage::SpecialStoragePolicy* special_storage_policy)
367 : partition_path_(partition_path),
368 quota_manager_(quota_manager),
369 appcache_service_(appcache_service),
370 filesystem_context_(filesystem_context),
371 database_tracker_(database_tracker),
372 dom_storage_context_(dom_storage_context),
373 indexed_db_context_(indexed_db_context),
374 service_worker_context_(service_worker_context),
375 webrtc_identity_store_(webrtc_identity_store),
376 special_storage_policy_(special_storage_policy) {
379 StoragePartitionImpl::~StoragePartitionImpl() {
380 // These message loop checks are just to avoid leaks in unittests.
381 if (GetDatabaseTracker() &&
382 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
383 BrowserThread::PostTask(
384 BrowserThread::FILE,
385 FROM_HERE,
386 base::Bind(&storage::DatabaseTracker::Shutdown, GetDatabaseTracker()));
389 if (GetFileSystemContext())
390 GetFileSystemContext()->Shutdown();
392 if (GetDOMStorageContext())
393 GetDOMStorageContext()->Shutdown();
395 if (GetServiceWorkerContext())
396 GetServiceWorkerContext()->Shutdown();
399 // TODO(ajwong): Break the direct dependency on |context|. We only
400 // need 3 pieces of info from it.
401 StoragePartitionImpl* StoragePartitionImpl::Create(
402 BrowserContext* context,
403 bool in_memory,
404 const base::FilePath& partition_path) {
405 // Ensure that these methods are called on the UI thread, except for
406 // unittests where a UI thread might not have been created.
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
408 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
410 // All of the clients have to be created and registered with the
411 // QuotaManager prior to the QuotaManger being used. We do them
412 // all together here prior to handing out a reference to anything
413 // that utilizes the QuotaManager.
414 scoped_refptr<storage::QuotaManager> quota_manager =
415 new storage::QuotaManager(
416 in_memory,
417 partition_path,
418 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
419 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
420 context->GetSpecialStoragePolicy());
422 // Each consumer is responsible for registering its QuotaClient during
423 // its construction.
424 scoped_refptr<storage::FileSystemContext> filesystem_context =
425 CreateFileSystemContext(
426 context, partition_path, in_memory, quota_manager->proxy());
428 scoped_refptr<storage::DatabaseTracker> database_tracker =
429 new storage::DatabaseTracker(partition_path,
430 in_memory,
431 context->GetSpecialStoragePolicy(),
432 quota_manager->proxy(),
433 BrowserThread::GetMessageLoopProxyForThread(
434 BrowserThread::FILE).get());
436 base::FilePath path = in_memory ? base::FilePath() : partition_path;
437 scoped_refptr<DOMStorageContextWrapper> dom_storage_context =
438 new DOMStorageContextWrapper(path, context->GetSpecialStoragePolicy());
440 // BrowserMainLoop may not be initialized in unit tests. Tests will
441 // need to inject their own task runner into the IndexedDBContext.
442 base::SequencedTaskRunner* idb_task_runner =
443 BrowserThread::CurrentlyOn(BrowserThread::UI) &&
444 BrowserMainLoop::GetInstance()
445 ? BrowserMainLoop::GetInstance()->indexed_db_thread()
446 ->message_loop_proxy().get()
447 : NULL;
448 scoped_refptr<IndexedDBContextImpl> indexed_db_context =
449 new IndexedDBContextImpl(path,
450 context->GetSpecialStoragePolicy(),
451 quota_manager->proxy(),
452 idb_task_runner);
454 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
455 new ServiceWorkerContextWrapper(context);
456 service_worker_context->Init(
457 path, quota_manager->proxy(), context->GetSpecialStoragePolicy());
459 scoped_refptr<ChromeAppCacheService> appcache_service =
460 new ChromeAppCacheService(quota_manager->proxy());
462 scoped_refptr<WebRTCIdentityStore> webrtc_identity_store(
463 new WebRTCIdentityStore(path, context->GetSpecialStoragePolicy()));
465 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy(
466 context->GetSpecialStoragePolicy());
468 return new StoragePartitionImpl(partition_path,
469 quota_manager.get(),
470 appcache_service.get(),
471 filesystem_context.get(),
472 database_tracker.get(),
473 dom_storage_context.get(),
474 indexed_db_context.get(),
475 service_worker_context.get(),
476 webrtc_identity_store.get(),
477 special_storage_policy.get());
480 base::FilePath StoragePartitionImpl::GetPath() {
481 return partition_path_;
484 net::URLRequestContextGetter* StoragePartitionImpl::GetURLRequestContext() {
485 return url_request_context_.get();
488 net::URLRequestContextGetter*
489 StoragePartitionImpl::GetMediaURLRequestContext() {
490 return media_url_request_context_.get();
493 storage::QuotaManager* StoragePartitionImpl::GetQuotaManager() {
494 return quota_manager_.get();
497 ChromeAppCacheService* StoragePartitionImpl::GetAppCacheService() {
498 return appcache_service_.get();
501 storage::FileSystemContext* StoragePartitionImpl::GetFileSystemContext() {
502 return filesystem_context_.get();
505 storage::DatabaseTracker* StoragePartitionImpl::GetDatabaseTracker() {
506 return database_tracker_.get();
509 DOMStorageContextWrapper* StoragePartitionImpl::GetDOMStorageContext() {
510 return dom_storage_context_.get();
513 IndexedDBContextImpl* StoragePartitionImpl::GetIndexedDBContext() {
514 return indexed_db_context_.get();
517 ServiceWorkerContextWrapper* StoragePartitionImpl::GetServiceWorkerContext() {
518 return service_worker_context_.get();
521 void StoragePartitionImpl::ClearDataImpl(
522 uint32 remove_mask,
523 uint32 quota_storage_remove_mask,
524 const GURL& storage_origin,
525 const OriginMatcherFunction& origin_matcher,
526 net::URLRequestContextGetter* rq_context,
527 const base::Time begin,
528 const base::Time end,
529 const base::Closure& callback) {
530 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
531 DataDeletionHelper* helper = new DataDeletionHelper(remove_mask,
532 quota_storage_remove_mask,
533 callback);
534 // |helper| deletes itself when done in
535 // DataDeletionHelper::DecrementTaskCountOnUI().
536 helper->ClearDataOnUIThread(storage_origin,
537 origin_matcher,
538 GetPath(),
539 rq_context,
540 dom_storage_context_.get(),
541 quota_manager_.get(),
542 special_storage_policy_.get(),
543 webrtc_identity_store_.get(),
544 begin,
545 end);
548 void StoragePartitionImpl::
549 QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
550 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
551 ++task_count;
554 void StoragePartitionImpl::
555 QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
556 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
557 DCHECK_GT(task_count, 0);
558 --task_count;
559 if (task_count)
560 return;
562 callback.Run();
563 delete this;
566 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
567 const scoped_refptr<storage::QuotaManager>& quota_manager,
568 const base::Time begin,
569 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
570 const StoragePartition::OriginMatcherFunction& origin_matcher) {
571 IncrementTaskCountOnIO();
572 base::Closure decrement_callback = base::Bind(
573 &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO,
574 base::Unretained(this));
576 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_PERSISTENT) {
577 IncrementTaskCountOnIO();
578 // Ask the QuotaManager for all origins with persistent quota modified
579 // within the user-specified timeframe, and deal with the resulting set in
580 // ClearQuotaManagedOriginsOnIOThread().
581 quota_manager->GetOriginsModifiedSince(
582 storage::kStorageTypePersistent,
583 begin,
584 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
585 base::Unretained(this),
586 quota_manager,
587 special_storage_policy,
588 origin_matcher,
589 decrement_callback));
592 // Do the same for temporary quota.
593 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_TEMPORARY) {
594 IncrementTaskCountOnIO();
595 quota_manager->GetOriginsModifiedSince(
596 storage::kStorageTypeTemporary,
597 begin,
598 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
599 base::Unretained(this),
600 quota_manager,
601 special_storage_policy,
602 origin_matcher,
603 decrement_callback));
606 // Do the same for syncable quota.
607 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_SYNCABLE) {
608 IncrementTaskCountOnIO();
609 quota_manager->GetOriginsModifiedSince(
610 storage::kStorageTypeSyncable,
611 begin,
612 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
613 base::Unretained(this),
614 quota_manager,
615 special_storage_policy,
616 origin_matcher,
617 decrement_callback));
620 DecrementTaskCountOnIO();
623 void
624 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
625 storage::QuotaManager* quota_manager,
626 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
627 const StoragePartition::OriginMatcherFunction& origin_matcher,
628 const base::Closure& callback,
629 const std::set<GURL>& origins,
630 storage::StorageType quota_storage_type) {
631 // The QuotaManager manages all storage other than cookies, LocalStorage,
632 // and SessionStorage. This loop wipes out most HTML5 storage for the given
633 // origins.
634 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
635 if (!origins.size()) {
636 callback.Run();
637 return;
640 size_t* deletion_task_count = new size_t(0u);
641 (*deletion_task_count)++;
642 for (std::set<GURL>::const_iterator origin = origins.begin();
643 origin != origins.end(); ++origin) {
644 // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
645 if (!storage_origin.is_empty() && origin->GetOrigin() != storage_origin)
646 continue;
648 if (!origin_matcher.is_null() &&
649 !origin_matcher.Run(*origin, special_storage_policy.get())) {
650 continue;
653 (*deletion_task_count)++;
654 quota_manager->DeleteOriginData(
655 *origin, quota_storage_type,
656 StoragePartitionImpl::GenerateQuotaClientMask(remove_mask),
657 base::Bind(&OnQuotaManagedOriginDeleted,
658 origin->GetOrigin(), quota_storage_type,
659 deletion_task_count, callback));
661 (*deletion_task_count)--;
663 CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
666 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
667 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
668 ++task_count;
671 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
672 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
673 BrowserThread::PostTask(
674 BrowserThread::UI, FROM_HERE,
675 base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI,
676 base::Unretained(this)));
677 return;
679 DCHECK_GT(task_count, 0);
680 --task_count;
681 if (!task_count) {
682 callback.Run();
683 delete this;
687 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
688 const GURL& storage_origin,
689 const OriginMatcherFunction& origin_matcher,
690 const base::FilePath& path,
691 net::URLRequestContextGetter* rq_context,
692 DOMStorageContextWrapper* dom_storage_context,
693 storage::QuotaManager* quota_manager,
694 storage::SpecialStoragePolicy* special_storage_policy,
695 WebRTCIdentityStore* webrtc_identity_store,
696 const base::Time begin,
697 const base::Time end) {
698 DCHECK_NE(remove_mask, 0u);
699 DCHECK(!callback.is_null());
701 IncrementTaskCountOnUI();
702 base::Closure decrement_callback = base::Bind(
703 &DataDeletionHelper::DecrementTaskCountOnUI, base::Unretained(this));
705 if (remove_mask & REMOVE_DATA_MASK_COOKIES) {
706 // Handle the cookies.
707 IncrementTaskCountOnUI();
708 BrowserThread::PostTask(
709 BrowserThread::IO, FROM_HERE,
710 base::Bind(&ClearCookiesOnIOThread,
711 make_scoped_refptr(rq_context), begin, end, storage_origin,
712 decrement_callback));
715 if (remove_mask & REMOVE_DATA_MASK_INDEXEDDB ||
716 remove_mask & REMOVE_DATA_MASK_WEBSQL ||
717 remove_mask & REMOVE_DATA_MASK_APPCACHE ||
718 remove_mask & REMOVE_DATA_MASK_FILE_SYSTEMS ||
719 remove_mask & REMOVE_DATA_MASK_SERVICE_WORKERS) {
720 IncrementTaskCountOnUI();
721 BrowserThread::PostTask(
722 BrowserThread::IO, FROM_HERE,
723 base::Bind(&DataDeletionHelper::ClearQuotaManagedDataOnIOThread,
724 base::Unretained(this),
725 make_scoped_refptr(quota_manager),
726 begin,
727 storage_origin,
728 make_scoped_refptr(special_storage_policy),
729 origin_matcher,
730 decrement_callback));
733 if (remove_mask & REMOVE_DATA_MASK_LOCAL_STORAGE) {
734 IncrementTaskCountOnUI();
735 ClearLocalStorageOnUIThread(
736 make_scoped_refptr(dom_storage_context),
737 make_scoped_refptr(special_storage_policy),
738 origin_matcher,
739 storage_origin, begin, end,
740 decrement_callback);
742 // ClearDataImpl cannot clear session storage data when a particular origin
743 // is specified. Therefore we ignore clearing session storage in this case.
744 // TODO(lazyboy): Fix.
745 if (storage_origin.is_empty()) {
746 IncrementTaskCountOnUI();
747 ClearSessionStorageOnUIThread(
748 make_scoped_refptr(dom_storage_context),
749 make_scoped_refptr(special_storage_policy),
750 origin_matcher,
751 decrement_callback);
755 if (remove_mask & REMOVE_DATA_MASK_SHADER_CACHE) {
756 IncrementTaskCountOnUI();
757 BrowserThread::PostTask(
758 BrowserThread::IO, FROM_HERE,
759 base::Bind(&ClearShaderCacheOnIOThread,
760 path, begin, end, decrement_callback));
763 if (remove_mask & REMOVE_DATA_MASK_WEBRTC_IDENTITY) {
764 IncrementTaskCountOnUI();
765 BrowserThread::PostTask(
766 BrowserThread::IO,
767 FROM_HERE,
768 base::Bind(&WebRTCIdentityStore::DeleteBetween,
769 webrtc_identity_store,
770 begin,
771 end,
772 decrement_callback));
775 DecrementTaskCountOnUI();
778 void StoragePartitionImpl::ClearDataForOrigin(
779 uint32 remove_mask,
780 uint32 quota_storage_remove_mask,
781 const GURL& storage_origin,
782 net::URLRequestContextGetter* request_context_getter,
783 const base::Closure& callback) {
784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
785 ClearDataImpl(remove_mask,
786 quota_storage_remove_mask,
787 storage_origin,
788 OriginMatcherFunction(),
789 request_context_getter,
790 base::Time(),
791 base::Time::Max(),
792 callback);
795 void StoragePartitionImpl::ClearData(
796 uint32 remove_mask,
797 uint32 quota_storage_remove_mask,
798 const GURL& storage_origin,
799 const OriginMatcherFunction& origin_matcher,
800 const base::Time begin,
801 const base::Time end,
802 const base::Closure& callback) {
803 ClearDataImpl(remove_mask, quota_storage_remove_mask, storage_origin,
804 origin_matcher, GetURLRequestContext(), begin, end, callback);
807 WebRTCIdentityStore* StoragePartitionImpl::GetWebRTCIdentityStore() {
808 return webrtc_identity_store_.get();
811 void StoragePartitionImpl::OverrideQuotaManagerForTesting(
812 storage::QuotaManager* quota_manager) {
813 quota_manager_ = quota_manager;
816 void StoragePartitionImpl::OverrideSpecialStoragePolicyForTesting(
817 storage::SpecialStoragePolicy* special_storage_policy) {
818 special_storage_policy_ = special_storage_policy;
821 void StoragePartitionImpl::SetURLRequestContext(
822 net::URLRequestContextGetter* url_request_context) {
823 url_request_context_ = url_request_context;
826 void StoragePartitionImpl::SetMediaURLRequestContext(
827 net::URLRequestContextGetter* media_url_request_context) {
828 media_url_request_context_ = media_url_request_context;
831 } // namespace content