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"
10 #include "base/location.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "content/browser/browser_main_loop.h"
15 #include "content/browser/fileapi/browser_file_system_helper.h"
16 #include "content/browser/geofencing/geofencing_manager.h"
17 #include "content/browser/gpu/shader_disk_cache.h"
18 #include "content/browser/host_zoom_map_impl.h"
19 #include "content/browser/navigator_connect/navigator_connect_context_impl.h"
20 #include "content/browser/notifications/platform_notification_context_impl.h"
21 #include "content/common/dom_storage/dom_storage_types.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/dom_storage_context.h"
25 #include "content/public/browser/indexed_db_context.h"
26 #include "content/public/browser/local_storage_usage_info.h"
27 #include "content/public/browser/session_storage_usage_info.h"
28 #include "net/base/completion_callback.h"
29 #include "net/base/net_errors.h"
30 #include "net/cookies/cookie_monster.h"
31 #include "net/url_request/url_request_context.h"
32 #include "net/url_request/url_request_context_getter.h"
33 #include "storage/browser/database/database_tracker.h"
34 #include "storage/browser/quota/quota_manager.h"
40 void OnClearedCookies(const base::Closure
& callback
, int num_deleted
) {
41 // The final callback needs to happen from UI thread.
42 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
43 BrowserThread::PostTask(
44 BrowserThread::UI
, FROM_HERE
,
45 base::Bind(&OnClearedCookies
, callback
, num_deleted
));
52 void ClearCookiesOnIOThread(
53 const scoped_refptr
<net::URLRequestContextGetter
>& rq_context
,
54 const base::Time begin
,
56 const GURL
& storage_origin
,
57 const base::Closure
& callback
) {
58 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
59 net::CookieStore
* cookie_store
= rq_context
->
60 GetURLRequestContext()->cookie_store();
61 if (storage_origin
.is_empty()) {
62 cookie_store
->DeleteAllCreatedBetweenAsync(
65 base::Bind(&OnClearedCookies
, callback
));
67 cookie_store
->DeleteAllCreatedBetweenForHostAsync(
70 storage_origin
, base::Bind(&OnClearedCookies
, callback
));
74 void CheckQuotaManagedDataDeletionStatus(size_t* deletion_task_count
,
75 const base::Closure
& callback
) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
77 if (*deletion_task_count
== 0) {
78 delete deletion_task_count
;
83 void OnQuotaManagedOriginDeleted(const GURL
& origin
,
84 storage::StorageType type
,
85 size_t* deletion_task_count
,
86 const base::Closure
& callback
,
87 storage::QuotaStatusCode status
) {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
89 DCHECK_GT(*deletion_task_count
, 0u);
90 if (status
!= storage::kQuotaStatusOk
) {
91 DLOG(ERROR
) << "Couldn't remove data of type " << type
<< " for origin "
92 << origin
<< ". Status: " << status
;
95 (*deletion_task_count
)--;
96 CheckQuotaManagedDataDeletionStatus(deletion_task_count
, callback
);
99 void ClearedShaderCache(const base::Closure
& callback
) {
100 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
101 BrowserThread::PostTask(
102 BrowserThread::UI
, FROM_HERE
,
103 base::Bind(&ClearedShaderCache
, callback
));
109 void ClearShaderCacheOnIOThread(const base::FilePath
& path
,
110 const base::Time begin
,
111 const base::Time end
,
112 const base::Closure
& callback
) {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
114 ShaderCacheFactory::GetInstance()->ClearByPath(
115 path
, begin
, end
, base::Bind(&ClearedShaderCache
, callback
));
118 void OnLocalStorageUsageInfo(
119 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
120 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
121 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
122 const base::Time delete_begin
,
123 const base::Time delete_end
,
124 const base::Closure
& callback
,
125 const std::vector
<LocalStorageUsageInfo
>& infos
) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
128 for (size_t i
= 0; i
< infos
.size(); ++i
) {
129 if (!origin_matcher
.is_null() &&
130 !origin_matcher
.Run(infos
[i
].origin
, special_storage_policy
.get())) {
134 if (infos
[i
].last_modified
>= delete_begin
&&
135 infos
[i
].last_modified
<= delete_end
) {
136 dom_storage_context
->DeleteLocalStorage(infos
[i
].origin
);
142 void OnSessionStorageUsageInfo(
143 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
144 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
145 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
146 const base::Closure
& callback
,
147 const std::vector
<SessionStorageUsageInfo
>& infos
) {
148 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
150 for (size_t i
= 0; i
< infos
.size(); ++i
) {
151 if (!origin_matcher
.is_null() &&
152 !origin_matcher
.Run(infos
[i
].origin
, special_storage_policy
.get())) {
155 dom_storage_context
->DeleteSessionStorage(infos
[i
]);
161 void ClearLocalStorageOnUIThread(
162 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
163 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
164 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
165 const GURL
& storage_origin
,
166 const base::Time begin
,
167 const base::Time end
,
168 const base::Closure
& callback
) {
169 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
171 if (!storage_origin
.is_empty()) {
172 bool can_delete
= origin_matcher
.is_null() ||
173 origin_matcher
.Run(storage_origin
,
174 special_storage_policy
.get());
176 dom_storage_context
->DeleteLocalStorage(storage_origin
);
182 dom_storage_context
->GetLocalStorageUsage(
183 base::Bind(&OnLocalStorageUsageInfo
,
184 dom_storage_context
, special_storage_policy
, origin_matcher
,
185 begin
, end
, callback
));
188 void ClearSessionStorageOnUIThread(
189 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
190 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
191 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
192 const base::Closure
& callback
) {
193 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
195 dom_storage_context
->GetSessionStorageUsage(
196 base::Bind(&OnSessionStorageUsageInfo
, dom_storage_context
,
197 special_storage_policy
, origin_matcher
,
204 int StoragePartitionImpl::GenerateQuotaClientMask(uint32 remove_mask
) {
205 int quota_client_mask
= 0;
207 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
)
208 quota_client_mask
|= storage::QuotaClient::kFileSystem
;
209 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_WEBSQL
)
210 quota_client_mask
|= storage::QuotaClient::kDatabase
;
211 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_APPCACHE
)
212 quota_client_mask
|= storage::QuotaClient::kAppcache
;
213 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
)
214 quota_client_mask
|= storage::QuotaClient::kIndexedDatabase
;
215 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS
) {
216 quota_client_mask
|= storage::QuotaClient::kServiceWorker
;
217 quota_client_mask
|= storage::QuotaClient::kServiceWorkerCache
;
221 return quota_client_mask
;
224 // Helper for deleting quota managed data from a partition.
226 // Most of the operations in this class are done on IO thread.
227 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper
{
228 QuotaManagedDataDeletionHelper(uint32 remove_mask
,
229 uint32 quota_storage_remove_mask
,
230 const GURL
& storage_origin
,
231 const base::Closure
& callback
)
232 : remove_mask(remove_mask
),
233 quota_storage_remove_mask(quota_storage_remove_mask
),
234 storage_origin(storage_origin
),
239 void IncrementTaskCountOnIO();
240 void DecrementTaskCountOnIO();
242 void ClearDataOnIOThread(
243 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
244 const base::Time begin
,
245 const scoped_refptr
<storage::SpecialStoragePolicy
>&
246 special_storage_policy
,
247 const StoragePartition::OriginMatcherFunction
& origin_matcher
);
249 void ClearOriginsOnIOThread(
250 storage::QuotaManager
* quota_manager
,
251 const scoped_refptr
<storage::SpecialStoragePolicy
>&
252 special_storage_policy
,
253 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
254 const base::Closure
& callback
,
255 const std::set
<GURL
>& origins
,
256 storage::StorageType quota_storage_type
);
258 // All of these data are accessed on IO thread.
260 uint32 quota_storage_remove_mask
;
262 const base::Closure callback
;
266 // Helper for deleting all sorts of data from a partition, keeps track of
269 // StoragePartitionImpl creates an instance of this class to keep track of
270 // data deletion progress. Deletion requires deleting multiple bits of data
271 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
272 // and IO thread. An instance of this class is created in the beginning of
273 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
274 // forwarded and updated on each (sub) deletion's callback. The instance is
275 // finally destroyed when deletion completes (and |callback| is invoked).
276 struct StoragePartitionImpl::DataDeletionHelper
{
277 DataDeletionHelper(uint32 remove_mask
,
278 uint32 quota_storage_remove_mask
,
279 const base::Closure
& callback
)
280 : remove_mask(remove_mask
),
281 quota_storage_remove_mask(quota_storage_remove_mask
),
286 void IncrementTaskCountOnUI();
287 void DecrementTaskCountOnUI();
289 void ClearDataOnUIThread(
290 const GURL
& storage_origin
,
291 const OriginMatcherFunction
& origin_matcher
,
292 const base::FilePath
& path
,
293 net::URLRequestContextGetter
* rq_context
,
294 DOMStorageContextWrapper
* dom_storage_context
,
295 storage::QuotaManager
* quota_manager
,
296 storage::SpecialStoragePolicy
* special_storage_policy
,
297 WebRTCIdentityStore
* webrtc_identity_store
,
298 const base::Time begin
,
299 const base::Time end
);
301 void ClearQuotaManagedDataOnIOThread(
302 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
303 const base::Time begin
,
304 const GURL
& storage_origin
,
305 const scoped_refptr
<storage::SpecialStoragePolicy
>&
306 special_storage_policy
,
307 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
308 const base::Closure
& callback
);
311 uint32 quota_storage_remove_mask
;
313 // Accessed on UI thread.
314 const base::Closure callback
;
315 // Accessed on UI thread.
319 void StoragePartitionImpl::DataDeletionHelper::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
>& special_storage_policy
,
324 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
325 const base::Closure
& callback
) {
326 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
328 StoragePartitionImpl::QuotaManagedDataDeletionHelper
* helper
=
329 new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
331 quota_storage_remove_mask
,
334 helper
->ClearDataOnIOThread(quota_manager
, begin
, special_storage_policy
,
338 StoragePartitionImpl::StoragePartitionImpl(
339 BrowserContext
* browser_context
,
340 const base::FilePath
& partition_path
,
341 storage::QuotaManager
* quota_manager
,
342 ChromeAppCacheService
* appcache_service
,
343 storage::FileSystemContext
* filesystem_context
,
344 storage::DatabaseTracker
* database_tracker
,
345 DOMStorageContextWrapper
* dom_storage_context
,
346 IndexedDBContextImpl
* indexed_db_context
,
347 CacheStorageContextImpl
* cache_storage_context
,
348 ServiceWorkerContextWrapper
* service_worker_context
,
349 WebRTCIdentityStore
* webrtc_identity_store
,
350 storage::SpecialStoragePolicy
* special_storage_policy
,
351 GeofencingManager
* geofencing_manager
,
352 HostZoomLevelContext
* host_zoom_level_context
,
353 NavigatorConnectContextImpl
* navigator_connect_context
,
354 PlatformNotificationContextImpl
* platform_notification_context
,
355 BackgroundSyncContextImpl
* background_sync_context
)
356 : partition_path_(partition_path
),
357 quota_manager_(quota_manager
),
358 appcache_service_(appcache_service
),
359 filesystem_context_(filesystem_context
),
360 database_tracker_(database_tracker
),
361 dom_storage_context_(dom_storage_context
),
362 indexed_db_context_(indexed_db_context
),
363 cache_storage_context_(cache_storage_context
),
364 service_worker_context_(service_worker_context
),
365 webrtc_identity_store_(webrtc_identity_store
),
366 special_storage_policy_(special_storage_policy
),
367 geofencing_manager_(geofencing_manager
),
368 host_zoom_level_context_(host_zoom_level_context
),
369 navigator_connect_context_(navigator_connect_context
),
370 platform_notification_context_(platform_notification_context
),
371 background_sync_context_(background_sync_context
),
372 browser_context_(browser_context
) {
375 StoragePartitionImpl::~StoragePartitionImpl() {
376 browser_context_
= nullptr;
378 // These message loop checks are just to avoid leaks in unittests.
379 if (GetDatabaseTracker() &&
380 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
381 BrowserThread::PostTask(
384 base::Bind(&storage::DatabaseTracker::Shutdown
, GetDatabaseTracker()));
387 if (GetFileSystemContext())
388 GetFileSystemContext()->Shutdown();
390 if (GetDOMStorageContext())
391 GetDOMStorageContext()->Shutdown();
393 if (GetServiceWorkerContext())
394 GetServiceWorkerContext()->Shutdown();
396 if (GetCacheStorageContext())
397 GetCacheStorageContext()->Shutdown();
399 if (GetGeofencingManager())
400 GetGeofencingManager()->Shutdown();
402 if (GetPlatformNotificationContext())
403 GetPlatformNotificationContext()->Shutdown();
405 if (GetBackgroundSyncContext())
406 GetBackgroundSyncContext()->Shutdown();
409 StoragePartitionImpl
* StoragePartitionImpl::Create(
410 BrowserContext
* context
,
412 const base::FilePath
& partition_path
) {
413 // Ensure that these methods are called on the UI thread, except for
414 // unittests where a UI thread might not have been created.
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
) ||
416 !BrowserThread::IsMessageLoopValid(BrowserThread::UI
));
418 // All of the clients have to be created and registered with the
419 // QuotaManager prior to the QuotaManger being used. We do them
420 // all together here prior to handing out a reference to anything
421 // that utilizes the QuotaManager.
422 scoped_refptr
<storage::QuotaManager
> quota_manager
=
423 new storage::QuotaManager(
426 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
427 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
428 context
->GetSpecialStoragePolicy());
430 // Each consumer is responsible for registering its QuotaClient during
432 scoped_refptr
<storage::FileSystemContext
> filesystem_context
=
433 CreateFileSystemContext(
434 context
, partition_path
, in_memory
, quota_manager
->proxy());
436 scoped_refptr
<storage::DatabaseTracker
> database_tracker
=
437 new storage::DatabaseTracker(partition_path
,
439 context
->GetSpecialStoragePolicy(),
440 quota_manager
->proxy(),
441 BrowserThread::GetMessageLoopProxyForThread(
442 BrowserThread::FILE).get());
444 base::FilePath path
= in_memory
? base::FilePath() : partition_path
;
445 scoped_refptr
<DOMStorageContextWrapper
> dom_storage_context
=
446 new DOMStorageContextWrapper(path
, context
->GetSpecialStoragePolicy());
448 // BrowserMainLoop may not be initialized in unit tests. Tests will
449 // need to inject their own task runner into the IndexedDBContext.
450 base::SequencedTaskRunner
* idb_task_runner
=
451 BrowserThread::CurrentlyOn(BrowserThread::UI
) &&
452 BrowserMainLoop::GetInstance()
453 ? BrowserMainLoop::GetInstance()
454 ->indexed_db_thread()
458 scoped_refptr
<IndexedDBContextImpl
> indexed_db_context
=
459 new IndexedDBContextImpl(path
,
460 context
->GetSpecialStoragePolicy(),
461 quota_manager
->proxy(),
464 scoped_refptr
<CacheStorageContextImpl
> cache_storage_context
=
465 new CacheStorageContextImpl(context
);
466 cache_storage_context
->Init(path
, quota_manager
->proxy(),
467 context
->GetSpecialStoragePolicy());
469 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
470 new ServiceWorkerContextWrapper(context
);
471 service_worker_context
->Init(path
, quota_manager
->proxy(),
472 context
->GetSpecialStoragePolicy());
474 scoped_refptr
<ChromeAppCacheService
> appcache_service
=
475 new ChromeAppCacheService(quota_manager
->proxy());
477 scoped_refptr
<WebRTCIdentityStore
> webrtc_identity_store(
478 new WebRTCIdentityStore(path
, context
->GetSpecialStoragePolicy()));
480 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy(
481 context
->GetSpecialStoragePolicy());
483 scoped_refptr
<GeofencingManager
> geofencing_manager
=
484 new GeofencingManager(service_worker_context
);
485 geofencing_manager
->Init();
487 scoped_refptr
<HostZoomLevelContext
> host_zoom_level_context(
488 new HostZoomLevelContext(
489 context
->CreateZoomLevelDelegate(partition_path
)));
491 scoped_refptr
<NavigatorConnectContextImpl
> navigator_connect_context
=
492 new NavigatorConnectContextImpl(service_worker_context
);
494 scoped_refptr
<PlatformNotificationContextImpl
> platform_notification_context
=
495 new PlatformNotificationContextImpl(path
, context
,
496 service_worker_context
);
497 platform_notification_context
->Initialize();
499 scoped_refptr
<BackgroundSyncContextImpl
> background_sync_context
=
500 new BackgroundSyncContextImpl();
501 background_sync_context
->Init(service_worker_context
);
503 StoragePartitionImpl
* storage_partition
= new StoragePartitionImpl(
504 context
, partition_path
, quota_manager
.get(), appcache_service
.get(),
505 filesystem_context
.get(), database_tracker
.get(),
506 dom_storage_context
.get(), indexed_db_context
.get(),
507 cache_storage_context
.get(), service_worker_context
.get(),
508 webrtc_identity_store
.get(), special_storage_policy
.get(),
509 geofencing_manager
.get(), host_zoom_level_context
.get(),
510 navigator_connect_context
.get(), platform_notification_context
.get(),
511 background_sync_context
.get());
513 service_worker_context
->set_storage_partition(storage_partition
);
515 return storage_partition
;
518 base::FilePath
StoragePartitionImpl::GetPath() {
519 return partition_path_
;
522 net::URLRequestContextGetter
* StoragePartitionImpl::GetURLRequestContext() {
523 return url_request_context_
.get();
526 net::URLRequestContextGetter
*
527 StoragePartitionImpl::GetMediaURLRequestContext() {
528 return media_url_request_context_
.get();
531 storage::QuotaManager
* StoragePartitionImpl::GetQuotaManager() {
532 return quota_manager_
.get();
535 ChromeAppCacheService
* StoragePartitionImpl::GetAppCacheService() {
536 return appcache_service_
.get();
539 storage::FileSystemContext
* StoragePartitionImpl::GetFileSystemContext() {
540 return filesystem_context_
.get();
543 storage::DatabaseTracker
* StoragePartitionImpl::GetDatabaseTracker() {
544 return database_tracker_
.get();
547 DOMStorageContextWrapper
* StoragePartitionImpl::GetDOMStorageContext() {
548 return dom_storage_context_
.get();
551 IndexedDBContextImpl
* StoragePartitionImpl::GetIndexedDBContext() {
552 return indexed_db_context_
.get();
555 CacheStorageContextImpl
* StoragePartitionImpl::GetCacheStorageContext() {
556 return cache_storage_context_
.get();
559 ServiceWorkerContextWrapper
* StoragePartitionImpl::GetServiceWorkerContext() {
560 return service_worker_context_
.get();
563 GeofencingManager
* StoragePartitionImpl::GetGeofencingManager() {
564 return geofencing_manager_
.get();
567 HostZoomMap
* StoragePartitionImpl::GetHostZoomMap() {
568 DCHECK(host_zoom_level_context_
.get());
569 return host_zoom_level_context_
->GetHostZoomMap();
572 HostZoomLevelContext
* StoragePartitionImpl::GetHostZoomLevelContext() {
573 return host_zoom_level_context_
.get();
576 ZoomLevelDelegate
* StoragePartitionImpl::GetZoomLevelDelegate() {
577 DCHECK(host_zoom_level_context_
.get());
578 return host_zoom_level_context_
->GetZoomLevelDelegate();
581 NavigatorConnectContextImpl
*
582 StoragePartitionImpl::GetNavigatorConnectContext() {
583 return navigator_connect_context_
.get();
586 PlatformNotificationContextImpl
*
587 StoragePartitionImpl::GetPlatformNotificationContext() {
588 return platform_notification_context_
.get();
591 BackgroundSyncContextImpl
* StoragePartitionImpl::GetBackgroundSyncContext() {
592 return background_sync_context_
.get();
595 void StoragePartitionImpl::ClearDataImpl(
597 uint32 quota_storage_remove_mask
,
598 const GURL
& storage_origin
,
599 const OriginMatcherFunction
& origin_matcher
,
600 net::URLRequestContextGetter
* rq_context
,
601 const base::Time begin
,
602 const base::Time end
,
603 const base::Closure
& callback
) {
604 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
605 DataDeletionHelper
* helper
= new DataDeletionHelper(remove_mask
,
606 quota_storage_remove_mask
,
608 // |helper| deletes itself when done in
609 // DataDeletionHelper::DecrementTaskCountOnUI().
610 helper
->ClearDataOnUIThread(storage_origin
,
614 dom_storage_context_
.get(),
615 quota_manager_
.get(),
616 special_storage_policy_
.get(),
617 webrtc_identity_store_
.get(),
622 void StoragePartitionImpl::
623 QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
624 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
628 void StoragePartitionImpl::
629 QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
630 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
631 DCHECK_GT(task_count
, 0);
640 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
641 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
642 const base::Time begin
,
643 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
644 const StoragePartition::OriginMatcherFunction
& origin_matcher
) {
645 IncrementTaskCountOnIO();
646 base::Closure decrement_callback
= base::Bind(
647 &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO
,
648 base::Unretained(this));
650 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
) {
651 IncrementTaskCountOnIO();
652 // Ask the QuotaManager for all origins with persistent quota modified
653 // within the user-specified timeframe, and deal with the resulting set in
654 // ClearQuotaManagedOriginsOnIOThread().
655 quota_manager
->GetOriginsModifiedSince(
656 storage::kStorageTypePersistent
,
658 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
659 base::Unretained(this),
661 special_storage_policy
,
663 decrement_callback
));
666 // Do the same for temporary quota.
667 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_TEMPORARY
) {
668 IncrementTaskCountOnIO();
669 quota_manager
->GetOriginsModifiedSince(
670 storage::kStorageTypeTemporary
,
672 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
673 base::Unretained(this),
675 special_storage_policy
,
677 decrement_callback
));
680 // Do the same for syncable quota.
681 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_SYNCABLE
) {
682 IncrementTaskCountOnIO();
683 quota_manager
->GetOriginsModifiedSince(
684 storage::kStorageTypeSyncable
,
686 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
687 base::Unretained(this),
689 special_storage_policy
,
691 decrement_callback
));
694 DecrementTaskCountOnIO();
698 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
699 storage::QuotaManager
* quota_manager
,
700 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
701 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
702 const base::Closure
& callback
,
703 const std::set
<GURL
>& origins
,
704 storage::StorageType quota_storage_type
) {
705 // The QuotaManager manages all storage other than cookies, LocalStorage,
706 // and SessionStorage. This loop wipes out most HTML5 storage for the given
708 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
709 if (!origins
.size()) {
714 size_t* deletion_task_count
= new size_t(0u);
715 (*deletion_task_count
)++;
716 for (std::set
<GURL
>::const_iterator origin
= origins
.begin();
717 origin
!= origins
.end(); ++origin
) {
718 // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
719 if (!storage_origin
.is_empty() && origin
->GetOrigin() != storage_origin
)
722 if (!origin_matcher
.is_null() &&
723 !origin_matcher
.Run(*origin
, special_storage_policy
.get())) {
727 (*deletion_task_count
)++;
728 quota_manager
->DeleteOriginData(
729 *origin
, quota_storage_type
,
730 StoragePartitionImpl::GenerateQuotaClientMask(remove_mask
),
731 base::Bind(&OnQuotaManagedOriginDeleted
,
732 origin
->GetOrigin(), quota_storage_type
,
733 deletion_task_count
, callback
));
735 (*deletion_task_count
)--;
737 CheckQuotaManagedDataDeletionStatus(deletion_task_count
, callback
);
740 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
741 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
745 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
746 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
747 BrowserThread::PostTask(
748 BrowserThread::UI
, FROM_HERE
,
749 base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI
,
750 base::Unretained(this)));
753 DCHECK_GT(task_count
, 0);
761 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
762 const GURL
& storage_origin
,
763 const OriginMatcherFunction
& origin_matcher
,
764 const base::FilePath
& path
,
765 net::URLRequestContextGetter
* rq_context
,
766 DOMStorageContextWrapper
* dom_storage_context
,
767 storage::QuotaManager
* quota_manager
,
768 storage::SpecialStoragePolicy
* special_storage_policy
,
769 WebRTCIdentityStore
* webrtc_identity_store
,
770 const base::Time begin
,
771 const base::Time end
) {
772 DCHECK_NE(remove_mask
, 0u);
773 DCHECK(!callback
.is_null());
775 IncrementTaskCountOnUI();
776 base::Closure decrement_callback
= base::Bind(
777 &DataDeletionHelper::DecrementTaskCountOnUI
, base::Unretained(this));
779 if (remove_mask
& REMOVE_DATA_MASK_COOKIES
) {
780 // Handle the cookies.
781 IncrementTaskCountOnUI();
782 BrowserThread::PostTask(
783 BrowserThread::IO
, FROM_HERE
,
784 base::Bind(&ClearCookiesOnIOThread
,
785 make_scoped_refptr(rq_context
), begin
, end
, storage_origin
,
786 decrement_callback
));
789 if (remove_mask
& REMOVE_DATA_MASK_INDEXEDDB
||
790 remove_mask
& REMOVE_DATA_MASK_WEBSQL
||
791 remove_mask
& REMOVE_DATA_MASK_APPCACHE
||
792 remove_mask
& REMOVE_DATA_MASK_FILE_SYSTEMS
||
793 remove_mask
& REMOVE_DATA_MASK_SERVICE_WORKERS
) {
794 IncrementTaskCountOnUI();
795 BrowserThread::PostTask(
796 BrowserThread::IO
, FROM_HERE
,
797 base::Bind(&DataDeletionHelper::ClearQuotaManagedDataOnIOThread
,
798 base::Unretained(this),
799 make_scoped_refptr(quota_manager
),
802 make_scoped_refptr(special_storage_policy
),
804 decrement_callback
));
807 if (remove_mask
& REMOVE_DATA_MASK_LOCAL_STORAGE
) {
808 IncrementTaskCountOnUI();
809 ClearLocalStorageOnUIThread(
810 make_scoped_refptr(dom_storage_context
),
811 make_scoped_refptr(special_storage_policy
),
813 storage_origin
, begin
, end
,
816 // ClearDataImpl cannot clear session storage data when a particular origin
817 // is specified. Therefore we ignore clearing session storage in this case.
818 // TODO(lazyboy): Fix.
819 if (storage_origin
.is_empty()) {
820 IncrementTaskCountOnUI();
821 ClearSessionStorageOnUIThread(
822 make_scoped_refptr(dom_storage_context
),
823 make_scoped_refptr(special_storage_policy
),
829 if (remove_mask
& REMOVE_DATA_MASK_SHADER_CACHE
) {
830 IncrementTaskCountOnUI();
831 BrowserThread::PostTask(
832 BrowserThread::IO
, FROM_HERE
,
833 base::Bind(&ClearShaderCacheOnIOThread
,
834 path
, begin
, end
, decrement_callback
));
837 if (remove_mask
& REMOVE_DATA_MASK_WEBRTC_IDENTITY
) {
838 IncrementTaskCountOnUI();
839 BrowserThread::PostTask(
842 base::Bind(&WebRTCIdentityStore::DeleteBetween
,
843 webrtc_identity_store
,
846 decrement_callback
));
849 DecrementTaskCountOnUI();
852 void StoragePartitionImpl::ClearDataForOrigin(
854 uint32 quota_storage_remove_mask
,
855 const GURL
& storage_origin
,
856 net::URLRequestContextGetter
* request_context_getter
,
857 const base::Closure
& callback
) {
858 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
859 ClearDataImpl(remove_mask
,
860 quota_storage_remove_mask
,
862 OriginMatcherFunction(),
863 request_context_getter
,
869 void StoragePartitionImpl::ClearData(
871 uint32 quota_storage_remove_mask
,
872 const GURL
& storage_origin
,
873 const OriginMatcherFunction
& origin_matcher
,
874 const base::Time begin
,
875 const base::Time end
,
876 const base::Closure
& callback
) {
877 ClearDataImpl(remove_mask
, quota_storage_remove_mask
, storage_origin
,
878 origin_matcher
, GetURLRequestContext(), begin
, end
, callback
);
881 void StoragePartitionImpl::Flush() {
882 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
883 if (GetDOMStorageContext())
884 GetDOMStorageContext()->Flush();
887 WebRTCIdentityStore
* StoragePartitionImpl::GetWebRTCIdentityStore() {
888 return webrtc_identity_store_
.get();
891 BrowserContext
* StoragePartitionImpl::browser_context() const {
892 return browser_context_
;
895 void StoragePartitionImpl::OverrideQuotaManagerForTesting(
896 storage::QuotaManager
* quota_manager
) {
897 quota_manager_
= quota_manager
;
900 void StoragePartitionImpl::OverrideSpecialStoragePolicyForTesting(
901 storage::SpecialStoragePolicy
* special_storage_policy
) {
902 special_storage_policy_
= special_storage_policy
;
905 void StoragePartitionImpl::SetURLRequestContext(
906 net::URLRequestContextGetter
* url_request_context
) {
907 url_request_context_
= url_request_context
;
910 void StoragePartitionImpl::SetMediaURLRequestContext(
911 net::URLRequestContextGetter
* media_url_request_context
) {
912 media_url_request_context_
= media_url_request_context
;
915 } // namespace content