Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / storage_partition_impl.cc
blobd4e49f60f13482f61466d478c7d90823df66901a
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/geofencing/geofencing_manager.h"
12 #include "content/browser/gpu/shader_disk_cache.h"
13 #include "content/common/dom_storage/dom_storage_types.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/dom_storage_context.h"
17 #include "content/public/browser/indexed_db_context.h"
18 #include "content/public/browser/local_storage_usage_info.h"
19 #include "content/public/browser/session_storage_usage_info.h"
20 #include "net/base/completion_callback.h"
21 #include "net/base/net_errors.h"
22 #include "net/cookies/cookie_monster.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "storage/browser/database/database_tracker.h"
26 #include "storage/browser/quota/quota_manager.h"
28 namespace content {
30 namespace {
32 void OnClearedCookies(const base::Closure& callback, int num_deleted) {
33 // The final callback needs to happen from UI thread.
34 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
35 BrowserThread::PostTask(
36 BrowserThread::UI, FROM_HERE,
37 base::Bind(&OnClearedCookies, callback, num_deleted));
38 return;
41 callback.Run();
44 void ClearCookiesOnIOThread(
45 const scoped_refptr<net::URLRequestContextGetter>& rq_context,
46 const base::Time begin,
47 const base::Time end,
48 const GURL& storage_origin,
49 const base::Closure& callback) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51 net::CookieStore* cookie_store = rq_context->
52 GetURLRequestContext()->cookie_store();
53 if (storage_origin.is_empty()) {
54 cookie_store->DeleteAllCreatedBetweenAsync(
55 begin,
56 end,
57 base::Bind(&OnClearedCookies, callback));
58 } else {
59 cookie_store->DeleteAllCreatedBetweenForHostAsync(
60 begin,
61 end,
62 storage_origin, base::Bind(&OnClearedCookies, callback));
66 void CheckQuotaManagedDataDeletionStatus(size_t* deletion_task_count,
67 const base::Closure& callback) {
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
69 if (*deletion_task_count == 0) {
70 delete deletion_task_count;
71 callback.Run();
75 void OnQuotaManagedOriginDeleted(const GURL& origin,
76 storage::StorageType type,
77 size_t* deletion_task_count,
78 const base::Closure& callback,
79 storage::QuotaStatusCode status) {
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
81 DCHECK_GT(*deletion_task_count, 0u);
82 if (status != storage::kQuotaStatusOk) {
83 DLOG(ERROR) << "Couldn't remove data of type " << type << " for origin "
84 << origin << ". Status: " << status;
87 (*deletion_task_count)--;
88 CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
91 void ClearedShaderCache(const base::Closure& callback) {
92 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
93 BrowserThread::PostTask(
94 BrowserThread::UI, FROM_HERE,
95 base::Bind(&ClearedShaderCache, callback));
96 return;
98 callback.Run();
101 void ClearShaderCacheOnIOThread(const base::FilePath& path,
102 const base::Time begin,
103 const base::Time end,
104 const base::Closure& callback) {
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
106 ShaderCacheFactory::GetInstance()->ClearByPath(
107 path, begin, end, base::Bind(&ClearedShaderCache, callback));
110 void OnLocalStorageUsageInfo(
111 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
112 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
113 const StoragePartition::OriginMatcherFunction& origin_matcher,
114 const base::Time delete_begin,
115 const base::Time delete_end,
116 const base::Closure& callback,
117 const std::vector<LocalStorageUsageInfo>& infos) {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
120 for (size_t i = 0; i < infos.size(); ++i) {
121 if (!origin_matcher.is_null() &&
122 !origin_matcher.Run(infos[i].origin, special_storage_policy.get())) {
123 continue;
126 if (infos[i].last_modified >= delete_begin &&
127 infos[i].last_modified <= delete_end) {
128 dom_storage_context->DeleteLocalStorage(infos[i].origin);
131 callback.Run();
134 void OnSessionStorageUsageInfo(
135 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
136 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
137 const StoragePartition::OriginMatcherFunction& origin_matcher,
138 const base::Closure& callback,
139 const std::vector<SessionStorageUsageInfo>& infos) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 for (size_t i = 0; i < infos.size(); ++i) {
143 if (!origin_matcher.is_null() &&
144 !origin_matcher.Run(infos[i].origin, special_storage_policy.get())) {
145 continue;
147 dom_storage_context->DeleteSessionStorage(infos[i]);
150 callback.Run();
153 void ClearLocalStorageOnUIThread(
154 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
155 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
156 const StoragePartition::OriginMatcherFunction& origin_matcher,
157 const GURL& storage_origin,
158 const base::Time begin,
159 const base::Time end,
160 const base::Closure& callback) {
161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
163 if (!storage_origin.is_empty()) {
164 bool can_delete = origin_matcher.is_null() ||
165 origin_matcher.Run(storage_origin,
166 special_storage_policy.get());
167 if (can_delete)
168 dom_storage_context->DeleteLocalStorage(storage_origin);
170 callback.Run();
171 return;
174 dom_storage_context->GetLocalStorageUsage(
175 base::Bind(&OnLocalStorageUsageInfo,
176 dom_storage_context, special_storage_policy, origin_matcher,
177 begin, end, callback));
180 void ClearSessionStorageOnUIThread(
181 const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
182 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
183 const StoragePartition::OriginMatcherFunction& origin_matcher,
184 const base::Closure& callback) {
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
187 dom_storage_context->GetSessionStorageUsage(
188 base::Bind(&OnSessionStorageUsageInfo, dom_storage_context,
189 special_storage_policy, origin_matcher,
190 callback));
193 } // namespace
195 // static
196 STATIC_CONST_MEMBER_DEFINITION const uint32
197 StoragePartition::REMOVE_DATA_MASK_APPCACHE;
198 STATIC_CONST_MEMBER_DEFINITION const uint32
199 StoragePartition::REMOVE_DATA_MASK_COOKIES;
200 STATIC_CONST_MEMBER_DEFINITION const uint32
201 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
202 STATIC_CONST_MEMBER_DEFINITION const uint32
203 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
204 STATIC_CONST_MEMBER_DEFINITION const uint32
205 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
206 STATIC_CONST_MEMBER_DEFINITION const uint32
207 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
208 STATIC_CONST_MEMBER_DEFINITION const uint32
209 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
210 STATIC_CONST_MEMBER_DEFINITION const uint32
211 StoragePartition::REMOVE_DATA_MASK_WEBSQL;
212 STATIC_CONST_MEMBER_DEFINITION const uint32
213 StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
214 STATIC_CONST_MEMBER_DEFINITION const uint32
215 StoragePartition::REMOVE_DATA_MASK_ALL;
216 STATIC_CONST_MEMBER_DEFINITION const uint32
217 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
218 STATIC_CONST_MEMBER_DEFINITION const uint32
219 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
220 STATIC_CONST_MEMBER_DEFINITION const uint32
221 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE;
222 STATIC_CONST_MEMBER_DEFINITION const uint32
223 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
225 // Static.
226 int StoragePartitionImpl::GenerateQuotaClientMask(uint32 remove_mask) {
227 int quota_client_mask = 0;
229 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS)
230 quota_client_mask |= storage::QuotaClient::kFileSystem;
231 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_WEBSQL)
232 quota_client_mask |= storage::QuotaClient::kDatabase;
233 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_APPCACHE)
234 quota_client_mask |= storage::QuotaClient::kAppcache;
235 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_INDEXEDDB)
236 quota_client_mask |= storage::QuotaClient::kIndexedDatabase;
237 if (remove_mask & StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS) {
238 quota_client_mask |= storage::QuotaClient::kServiceWorker;
239 quota_client_mask |= storage::QuotaClient::kServiceWorkerCache;
243 return quota_client_mask;
246 // Helper for deleting quota managed data from a partition.
248 // Most of the operations in this class are done on IO thread.
249 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper {
250 QuotaManagedDataDeletionHelper(uint32 remove_mask,
251 uint32 quota_storage_remove_mask,
252 const GURL& storage_origin,
253 const base::Closure& callback)
254 : remove_mask(remove_mask),
255 quota_storage_remove_mask(quota_storage_remove_mask),
256 storage_origin(storage_origin),
257 callback(callback),
258 task_count(0) {
261 void IncrementTaskCountOnIO();
262 void DecrementTaskCountOnIO();
264 void ClearDataOnIOThread(
265 const scoped_refptr<storage::QuotaManager>& quota_manager,
266 const base::Time begin,
267 const scoped_refptr<storage::SpecialStoragePolicy>&
268 special_storage_policy,
269 const StoragePartition::OriginMatcherFunction& origin_matcher);
271 void ClearOriginsOnIOThread(
272 storage::QuotaManager* quota_manager,
273 const scoped_refptr<storage::SpecialStoragePolicy>&
274 special_storage_policy,
275 const StoragePartition::OriginMatcherFunction& origin_matcher,
276 const base::Closure& callback,
277 const std::set<GURL>& origins,
278 storage::StorageType quota_storage_type);
280 // All of these data are accessed on IO thread.
281 uint32 remove_mask;
282 uint32 quota_storage_remove_mask;
283 GURL storage_origin;
284 const base::Closure callback;
285 int task_count;
288 // Helper for deleting all sorts of data from a partition, keeps track of
289 // deletion status.
291 // StoragePartitionImpl creates an instance of this class to keep track of
292 // data deletion progress. Deletion requires deleting multiple bits of data
293 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
294 // and IO thread. An instance of this class is created in the beginning of
295 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
296 // forwarded and updated on each (sub) deletion's callback. The instance is
297 // finally destroyed when deletion completes (and |callback| is invoked).
298 struct StoragePartitionImpl::DataDeletionHelper {
299 DataDeletionHelper(uint32 remove_mask,
300 uint32 quota_storage_remove_mask,
301 const base::Closure& callback)
302 : remove_mask(remove_mask),
303 quota_storage_remove_mask(quota_storage_remove_mask),
304 callback(callback),
305 task_count(0) {
308 void IncrementTaskCountOnUI();
309 void DecrementTaskCountOnUI();
311 void ClearDataOnUIThread(
312 const GURL& storage_origin,
313 const OriginMatcherFunction& origin_matcher,
314 const base::FilePath& path,
315 net::URLRequestContextGetter* rq_context,
316 DOMStorageContextWrapper* dom_storage_context,
317 storage::QuotaManager* quota_manager,
318 storage::SpecialStoragePolicy* special_storage_policy,
319 WebRTCIdentityStore* webrtc_identity_store,
320 const base::Time begin,
321 const base::Time end);
323 void ClearQuotaManagedDataOnIOThread(
324 const scoped_refptr<storage::QuotaManager>& quota_manager,
325 const base::Time begin,
326 const GURL& storage_origin,
327 const scoped_refptr<storage::SpecialStoragePolicy>&
328 special_storage_policy,
329 const StoragePartition::OriginMatcherFunction& origin_matcher,
330 const base::Closure& callback);
332 uint32 remove_mask;
333 uint32 quota_storage_remove_mask;
335 // Accessed on UI thread.
336 const base::Closure callback;
337 // Accessed on UI thread.
338 int task_count;
341 void StoragePartitionImpl::DataDeletionHelper::ClearQuotaManagedDataOnIOThread(
342 const scoped_refptr<storage::QuotaManager>& quota_manager,
343 const base::Time begin,
344 const GURL& storage_origin,
345 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
346 const StoragePartition::OriginMatcherFunction& origin_matcher,
347 const base::Closure& callback) {
348 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
350 StoragePartitionImpl::QuotaManagedDataDeletionHelper* helper =
351 new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
352 remove_mask,
353 quota_storage_remove_mask,
354 storage_origin,
355 callback);
356 helper->ClearDataOnIOThread(quota_manager, begin, special_storage_policy,
357 origin_matcher);
360 StoragePartitionImpl::StoragePartitionImpl(
361 const base::FilePath& partition_path,
362 storage::QuotaManager* quota_manager,
363 ChromeAppCacheService* appcache_service,
364 storage::FileSystemContext* filesystem_context,
365 storage::DatabaseTracker* database_tracker,
366 DOMStorageContextWrapper* dom_storage_context,
367 IndexedDBContextImpl* indexed_db_context,
368 ServiceWorkerContextWrapper* service_worker_context,
369 WebRTCIdentityStore* webrtc_identity_store,
370 storage::SpecialStoragePolicy* special_storage_policy,
371 GeofencingManager* geofencing_manager)
372 : partition_path_(partition_path),
373 quota_manager_(quota_manager),
374 appcache_service_(appcache_service),
375 filesystem_context_(filesystem_context),
376 database_tracker_(database_tracker),
377 dom_storage_context_(dom_storage_context),
378 indexed_db_context_(indexed_db_context),
379 service_worker_context_(service_worker_context),
380 webrtc_identity_store_(webrtc_identity_store),
381 special_storage_policy_(special_storage_policy),
382 geofencing_manager_(geofencing_manager) {
385 StoragePartitionImpl::~StoragePartitionImpl() {
386 // These message loop checks are just to avoid leaks in unittests.
387 if (GetDatabaseTracker() &&
388 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
389 BrowserThread::PostTask(
390 BrowserThread::FILE,
391 FROM_HERE,
392 base::Bind(&storage::DatabaseTracker::Shutdown, GetDatabaseTracker()));
395 if (GetFileSystemContext())
396 GetFileSystemContext()->Shutdown();
398 if (GetDOMStorageContext())
399 GetDOMStorageContext()->Shutdown();
401 if (GetServiceWorkerContext())
402 GetServiceWorkerContext()->Shutdown();
404 if (GetGeofencingManager())
405 GetGeofencingManager()->Shutdown();
408 // TODO(ajwong): Break the direct dependency on |context|. We only
409 // need 3 pieces of info from it.
410 StoragePartitionImpl* StoragePartitionImpl::Create(
411 BrowserContext* context,
412 bool in_memory,
413 const base::FilePath& partition_path) {
414 // Ensure that these methods are called on the UI thread, except for
415 // unittests where a UI thread might not have been created.
416 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
417 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
419 // All of the clients have to be created and registered with the
420 // QuotaManager prior to the QuotaManger being used. We do them
421 // all together here prior to handing out a reference to anything
422 // that utilizes the QuotaManager.
423 scoped_refptr<storage::QuotaManager> quota_manager =
424 new storage::QuotaManager(
425 in_memory,
426 partition_path,
427 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
428 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
429 context->GetSpecialStoragePolicy());
431 // Each consumer is responsible for registering its QuotaClient during
432 // its construction.
433 scoped_refptr<storage::FileSystemContext> filesystem_context =
434 CreateFileSystemContext(
435 context, partition_path, in_memory, quota_manager->proxy());
437 scoped_refptr<storage::DatabaseTracker> database_tracker =
438 new storage::DatabaseTracker(partition_path,
439 in_memory,
440 context->GetSpecialStoragePolicy(),
441 quota_manager->proxy(),
442 BrowserThread::GetMessageLoopProxyForThread(
443 BrowserThread::FILE).get());
445 base::FilePath path = in_memory ? base::FilePath() : partition_path;
446 scoped_refptr<DOMStorageContextWrapper> dom_storage_context =
447 new DOMStorageContextWrapper(path, context->GetSpecialStoragePolicy());
449 // BrowserMainLoop may not be initialized in unit tests. Tests will
450 // need to inject their own task runner into the IndexedDBContext.
451 base::SequencedTaskRunner* idb_task_runner =
452 BrowserThread::CurrentlyOn(BrowserThread::UI) &&
453 BrowserMainLoop::GetInstance()
454 ? BrowserMainLoop::GetInstance()->indexed_db_thread()
455 ->message_loop_proxy().get()
456 : NULL;
457 scoped_refptr<IndexedDBContextImpl> indexed_db_context =
458 new IndexedDBContextImpl(path,
459 context->GetSpecialStoragePolicy(),
460 quota_manager->proxy(),
461 idb_task_runner);
463 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
464 new ServiceWorkerContextWrapper(context);
465 service_worker_context->Init(
466 path, quota_manager->proxy(), context->GetSpecialStoragePolicy());
468 scoped_refptr<ChromeAppCacheService> appcache_service =
469 new ChromeAppCacheService(quota_manager->proxy());
471 scoped_refptr<WebRTCIdentityStore> webrtc_identity_store(
472 new WebRTCIdentityStore(path, context->GetSpecialStoragePolicy()));
474 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy(
475 context->GetSpecialStoragePolicy());
477 scoped_refptr<GeofencingManager> geofencing_manager =
478 new GeofencingManager(service_worker_context);
479 geofencing_manager->Init();
481 return new StoragePartitionImpl(partition_path,
482 quota_manager.get(),
483 appcache_service.get(),
484 filesystem_context.get(),
485 database_tracker.get(),
486 dom_storage_context.get(),
487 indexed_db_context.get(),
488 service_worker_context.get(),
489 webrtc_identity_store.get(),
490 special_storage_policy.get(),
491 geofencing_manager.get());
494 base::FilePath StoragePartitionImpl::GetPath() {
495 return partition_path_;
498 net::URLRequestContextGetter* StoragePartitionImpl::GetURLRequestContext() {
499 return url_request_context_.get();
502 net::URLRequestContextGetter*
503 StoragePartitionImpl::GetMediaURLRequestContext() {
504 return media_url_request_context_.get();
507 storage::QuotaManager* StoragePartitionImpl::GetQuotaManager() {
508 return quota_manager_.get();
511 ChromeAppCacheService* StoragePartitionImpl::GetAppCacheService() {
512 return appcache_service_.get();
515 storage::FileSystemContext* StoragePartitionImpl::GetFileSystemContext() {
516 return filesystem_context_.get();
519 storage::DatabaseTracker* StoragePartitionImpl::GetDatabaseTracker() {
520 return database_tracker_.get();
523 DOMStorageContextWrapper* StoragePartitionImpl::GetDOMStorageContext() {
524 return dom_storage_context_.get();
527 IndexedDBContextImpl* StoragePartitionImpl::GetIndexedDBContext() {
528 return indexed_db_context_.get();
531 ServiceWorkerContextWrapper* StoragePartitionImpl::GetServiceWorkerContext() {
532 return service_worker_context_.get();
535 GeofencingManager* StoragePartitionImpl::GetGeofencingManager() {
536 return geofencing_manager_.get();
539 void StoragePartitionImpl::ClearDataImpl(
540 uint32 remove_mask,
541 uint32 quota_storage_remove_mask,
542 const GURL& storage_origin,
543 const OriginMatcherFunction& origin_matcher,
544 net::URLRequestContextGetter* rq_context,
545 const base::Time begin,
546 const base::Time end,
547 const base::Closure& callback) {
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
549 DataDeletionHelper* helper = new DataDeletionHelper(remove_mask,
550 quota_storage_remove_mask,
551 callback);
552 // |helper| deletes itself when done in
553 // DataDeletionHelper::DecrementTaskCountOnUI().
554 helper->ClearDataOnUIThread(storage_origin,
555 origin_matcher,
556 GetPath(),
557 rq_context,
558 dom_storage_context_.get(),
559 quota_manager_.get(),
560 special_storage_policy_.get(),
561 webrtc_identity_store_.get(),
562 begin,
563 end);
566 void StoragePartitionImpl::
567 QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
569 ++task_count;
572 void StoragePartitionImpl::
573 QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
575 DCHECK_GT(task_count, 0);
576 --task_count;
577 if (task_count)
578 return;
580 callback.Run();
581 delete this;
584 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
585 const scoped_refptr<storage::QuotaManager>& quota_manager,
586 const base::Time begin,
587 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
588 const StoragePartition::OriginMatcherFunction& origin_matcher) {
589 IncrementTaskCountOnIO();
590 base::Closure decrement_callback = base::Bind(
591 &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO,
592 base::Unretained(this));
594 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_PERSISTENT) {
595 IncrementTaskCountOnIO();
596 // Ask the QuotaManager for all origins with persistent quota modified
597 // within the user-specified timeframe, and deal with the resulting set in
598 // ClearQuotaManagedOriginsOnIOThread().
599 quota_manager->GetOriginsModifiedSince(
600 storage::kStorageTypePersistent,
601 begin,
602 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
603 base::Unretained(this),
604 quota_manager,
605 special_storage_policy,
606 origin_matcher,
607 decrement_callback));
610 // Do the same for temporary quota.
611 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_TEMPORARY) {
612 IncrementTaskCountOnIO();
613 quota_manager->GetOriginsModifiedSince(
614 storage::kStorageTypeTemporary,
615 begin,
616 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
617 base::Unretained(this),
618 quota_manager,
619 special_storage_policy,
620 origin_matcher,
621 decrement_callback));
624 // Do the same for syncable quota.
625 if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_SYNCABLE) {
626 IncrementTaskCountOnIO();
627 quota_manager->GetOriginsModifiedSince(
628 storage::kStorageTypeSyncable,
629 begin,
630 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
631 base::Unretained(this),
632 quota_manager,
633 special_storage_policy,
634 origin_matcher,
635 decrement_callback));
638 DecrementTaskCountOnIO();
641 void
642 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
643 storage::QuotaManager* quota_manager,
644 const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
645 const StoragePartition::OriginMatcherFunction& origin_matcher,
646 const base::Closure& callback,
647 const std::set<GURL>& origins,
648 storage::StorageType quota_storage_type) {
649 // The QuotaManager manages all storage other than cookies, LocalStorage,
650 // and SessionStorage. This loop wipes out most HTML5 storage for the given
651 // origins.
652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
653 if (!origins.size()) {
654 callback.Run();
655 return;
658 size_t* deletion_task_count = new size_t(0u);
659 (*deletion_task_count)++;
660 for (std::set<GURL>::const_iterator origin = origins.begin();
661 origin != origins.end(); ++origin) {
662 // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
663 if (!storage_origin.is_empty() && origin->GetOrigin() != storage_origin)
664 continue;
666 if (!origin_matcher.is_null() &&
667 !origin_matcher.Run(*origin, special_storage_policy.get())) {
668 continue;
671 (*deletion_task_count)++;
672 quota_manager->DeleteOriginData(
673 *origin, quota_storage_type,
674 StoragePartitionImpl::GenerateQuotaClientMask(remove_mask),
675 base::Bind(&OnQuotaManagedOriginDeleted,
676 origin->GetOrigin(), quota_storage_type,
677 deletion_task_count, callback));
679 (*deletion_task_count)--;
681 CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
684 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
685 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
686 ++task_count;
689 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
690 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
691 BrowserThread::PostTask(
692 BrowserThread::UI, FROM_HERE,
693 base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI,
694 base::Unretained(this)));
695 return;
697 DCHECK_GT(task_count, 0);
698 --task_count;
699 if (!task_count) {
700 callback.Run();
701 delete this;
705 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
706 const GURL& storage_origin,
707 const OriginMatcherFunction& origin_matcher,
708 const base::FilePath& path,
709 net::URLRequestContextGetter* rq_context,
710 DOMStorageContextWrapper* dom_storage_context,
711 storage::QuotaManager* quota_manager,
712 storage::SpecialStoragePolicy* special_storage_policy,
713 WebRTCIdentityStore* webrtc_identity_store,
714 const base::Time begin,
715 const base::Time end) {
716 DCHECK_NE(remove_mask, 0u);
717 DCHECK(!callback.is_null());
719 IncrementTaskCountOnUI();
720 base::Closure decrement_callback = base::Bind(
721 &DataDeletionHelper::DecrementTaskCountOnUI, base::Unretained(this));
723 if (remove_mask & REMOVE_DATA_MASK_COOKIES) {
724 // Handle the cookies.
725 IncrementTaskCountOnUI();
726 BrowserThread::PostTask(
727 BrowserThread::IO, FROM_HERE,
728 base::Bind(&ClearCookiesOnIOThread,
729 make_scoped_refptr(rq_context), begin, end, storage_origin,
730 decrement_callback));
733 if (remove_mask & REMOVE_DATA_MASK_INDEXEDDB ||
734 remove_mask & REMOVE_DATA_MASK_WEBSQL ||
735 remove_mask & REMOVE_DATA_MASK_APPCACHE ||
736 remove_mask & REMOVE_DATA_MASK_FILE_SYSTEMS ||
737 remove_mask & REMOVE_DATA_MASK_SERVICE_WORKERS) {
738 IncrementTaskCountOnUI();
739 BrowserThread::PostTask(
740 BrowserThread::IO, FROM_HERE,
741 base::Bind(&DataDeletionHelper::ClearQuotaManagedDataOnIOThread,
742 base::Unretained(this),
743 make_scoped_refptr(quota_manager),
744 begin,
745 storage_origin,
746 make_scoped_refptr(special_storage_policy),
747 origin_matcher,
748 decrement_callback));
751 if (remove_mask & REMOVE_DATA_MASK_LOCAL_STORAGE) {
752 IncrementTaskCountOnUI();
753 ClearLocalStorageOnUIThread(
754 make_scoped_refptr(dom_storage_context),
755 make_scoped_refptr(special_storage_policy),
756 origin_matcher,
757 storage_origin, begin, end,
758 decrement_callback);
760 // ClearDataImpl cannot clear session storage data when a particular origin
761 // is specified. Therefore we ignore clearing session storage in this case.
762 // TODO(lazyboy): Fix.
763 if (storage_origin.is_empty()) {
764 IncrementTaskCountOnUI();
765 ClearSessionStorageOnUIThread(
766 make_scoped_refptr(dom_storage_context),
767 make_scoped_refptr(special_storage_policy),
768 origin_matcher,
769 decrement_callback);
773 if (remove_mask & REMOVE_DATA_MASK_SHADER_CACHE) {
774 IncrementTaskCountOnUI();
775 BrowserThread::PostTask(
776 BrowserThread::IO, FROM_HERE,
777 base::Bind(&ClearShaderCacheOnIOThread,
778 path, begin, end, decrement_callback));
781 if (remove_mask & REMOVE_DATA_MASK_WEBRTC_IDENTITY) {
782 IncrementTaskCountOnUI();
783 BrowserThread::PostTask(
784 BrowserThread::IO,
785 FROM_HERE,
786 base::Bind(&WebRTCIdentityStore::DeleteBetween,
787 webrtc_identity_store,
788 begin,
789 end,
790 decrement_callback));
793 DecrementTaskCountOnUI();
796 void StoragePartitionImpl::ClearDataForOrigin(
797 uint32 remove_mask,
798 uint32 quota_storage_remove_mask,
799 const GURL& storage_origin,
800 net::URLRequestContextGetter* request_context_getter,
801 const base::Closure& callback) {
802 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
803 ClearDataImpl(remove_mask,
804 quota_storage_remove_mask,
805 storage_origin,
806 OriginMatcherFunction(),
807 request_context_getter,
808 base::Time(),
809 base::Time::Max(),
810 callback);
813 void StoragePartitionImpl::ClearData(
814 uint32 remove_mask,
815 uint32 quota_storage_remove_mask,
816 const GURL& storage_origin,
817 const OriginMatcherFunction& origin_matcher,
818 const base::Time begin,
819 const base::Time end,
820 const base::Closure& callback) {
821 ClearDataImpl(remove_mask, quota_storage_remove_mask, storage_origin,
822 origin_matcher, GetURLRequestContext(), begin, end, callback);
825 WebRTCIdentityStore* StoragePartitionImpl::GetWebRTCIdentityStore() {
826 return webrtc_identity_store_.get();
829 void StoragePartitionImpl::OverrideQuotaManagerForTesting(
830 storage::QuotaManager* quota_manager) {
831 quota_manager_ = quota_manager;
834 void StoragePartitionImpl::OverrideSpecialStoragePolicyForTesting(
835 storage::SpecialStoragePolicy* special_storage_policy) {
836 special_storage_policy_ = special_storage_policy;
839 void StoragePartitionImpl::SetURLRequestContext(
840 net::URLRequestContextGetter* url_request_context) {
841 url_request_context_ = url_request_context;
844 void StoragePartitionImpl::SetMediaURLRequestContext(
845 net::URLRequestContextGetter* media_url_request_context) {
846 media_url_request_context_ = media_url_request_context;
849 } // namespace content