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 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_CACHE_STORAGE
)
218 quota_client_mask
|= storage::QuotaClient::kServiceWorkerCache
;
220 return quota_client_mask
;
223 // Helper for deleting quota managed data from a partition.
225 // Most of the operations in this class are done on IO thread.
226 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper
{
227 QuotaManagedDataDeletionHelper(uint32 remove_mask
,
228 uint32 quota_storage_remove_mask
,
229 const GURL
& storage_origin
,
230 const base::Closure
& callback
)
231 : remove_mask(remove_mask
),
232 quota_storage_remove_mask(quota_storage_remove_mask
),
233 storage_origin(storage_origin
),
238 void IncrementTaskCountOnIO();
239 void DecrementTaskCountOnIO();
241 void ClearDataOnIOThread(
242 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
243 const base::Time begin
,
244 const scoped_refptr
<storage::SpecialStoragePolicy
>&
245 special_storage_policy
,
246 const StoragePartition::OriginMatcherFunction
& origin_matcher
);
248 void ClearOriginsOnIOThread(
249 storage::QuotaManager
* quota_manager
,
250 const scoped_refptr
<storage::SpecialStoragePolicy
>&
251 special_storage_policy
,
252 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
253 const base::Closure
& callback
,
254 const std::set
<GURL
>& origins
,
255 storage::StorageType quota_storage_type
);
257 // All of these data are accessed on IO thread.
259 uint32 quota_storage_remove_mask
;
261 const base::Closure callback
;
265 // Helper for deleting all sorts of data from a partition, keeps track of
268 // StoragePartitionImpl creates an instance of this class to keep track of
269 // data deletion progress. Deletion requires deleting multiple bits of data
270 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
271 // and IO thread. An instance of this class is created in the beginning of
272 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
273 // forwarded and updated on each (sub) deletion's callback. The instance is
274 // finally destroyed when deletion completes (and |callback| is invoked).
275 struct StoragePartitionImpl::DataDeletionHelper
{
276 DataDeletionHelper(uint32 remove_mask
,
277 uint32 quota_storage_remove_mask
,
278 const base::Closure
& callback
)
279 : remove_mask(remove_mask
),
280 quota_storage_remove_mask(quota_storage_remove_mask
),
285 void IncrementTaskCountOnUI();
286 void DecrementTaskCountOnUI();
288 void ClearDataOnUIThread(
289 const GURL
& storage_origin
,
290 const OriginMatcherFunction
& origin_matcher
,
291 const base::FilePath
& path
,
292 net::URLRequestContextGetter
* rq_context
,
293 DOMStorageContextWrapper
* dom_storage_context
,
294 storage::QuotaManager
* quota_manager
,
295 storage::SpecialStoragePolicy
* special_storage_policy
,
296 WebRTCIdentityStore
* webrtc_identity_store
,
297 const base::Time begin
,
298 const base::Time end
);
300 void ClearQuotaManagedDataOnIOThread(
301 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
302 const base::Time begin
,
303 const GURL
& storage_origin
,
304 const scoped_refptr
<storage::SpecialStoragePolicy
>&
305 special_storage_policy
,
306 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
307 const base::Closure
& callback
);
310 uint32 quota_storage_remove_mask
;
312 // Accessed on UI thread.
313 const base::Closure callback
;
314 // Accessed on UI thread.
318 void StoragePartitionImpl::DataDeletionHelper::ClearQuotaManagedDataOnIOThread(
319 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
320 const base::Time begin
,
321 const GURL
& storage_origin
,
322 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
323 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
324 const base::Closure
& callback
) {
325 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
327 StoragePartitionImpl::QuotaManagedDataDeletionHelper
* helper
=
328 new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
330 quota_storage_remove_mask
,
333 helper
->ClearDataOnIOThread(quota_manager
, begin
, special_storage_policy
,
337 StoragePartitionImpl::StoragePartitionImpl(
338 BrowserContext
* browser_context
,
339 const base::FilePath
& partition_path
,
340 storage::QuotaManager
* quota_manager
,
341 ChromeAppCacheService
* appcache_service
,
342 storage::FileSystemContext
* filesystem_context
,
343 storage::DatabaseTracker
* database_tracker
,
344 DOMStorageContextWrapper
* dom_storage_context
,
345 IndexedDBContextImpl
* indexed_db_context
,
346 CacheStorageContextImpl
* cache_storage_context
,
347 ServiceWorkerContextWrapper
* service_worker_context
,
348 WebRTCIdentityStore
* webrtc_identity_store
,
349 storage::SpecialStoragePolicy
* special_storage_policy
,
350 GeofencingManager
* geofencing_manager
,
351 HostZoomLevelContext
* host_zoom_level_context
,
352 NavigatorConnectContextImpl
* navigator_connect_context
,
353 PlatformNotificationContextImpl
* platform_notification_context
,
354 BackgroundSyncContextImpl
* background_sync_context
)
355 : partition_path_(partition_path
),
356 quota_manager_(quota_manager
),
357 appcache_service_(appcache_service
),
358 filesystem_context_(filesystem_context
),
359 database_tracker_(database_tracker
),
360 dom_storage_context_(dom_storage_context
),
361 indexed_db_context_(indexed_db_context
),
362 cache_storage_context_(cache_storage_context
),
363 service_worker_context_(service_worker_context
),
364 webrtc_identity_store_(webrtc_identity_store
),
365 special_storage_policy_(special_storage_policy
),
366 geofencing_manager_(geofencing_manager
),
367 host_zoom_level_context_(host_zoom_level_context
),
368 navigator_connect_context_(navigator_connect_context
),
369 platform_notification_context_(platform_notification_context
),
370 background_sync_context_(background_sync_context
),
371 browser_context_(browser_context
) {
374 StoragePartitionImpl::~StoragePartitionImpl() {
375 browser_context_
= nullptr;
377 // These message loop checks are just to avoid leaks in unittests.
378 if (GetDatabaseTracker() &&
379 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
380 BrowserThread::PostTask(
383 base::Bind(&storage::DatabaseTracker::Shutdown
, GetDatabaseTracker()));
386 if (GetFileSystemContext())
387 GetFileSystemContext()->Shutdown();
389 if (GetDOMStorageContext())
390 GetDOMStorageContext()->Shutdown();
392 if (GetServiceWorkerContext())
393 GetServiceWorkerContext()->Shutdown();
395 if (GetCacheStorageContext())
396 GetCacheStorageContext()->Shutdown();
398 if (GetGeofencingManager())
399 GetGeofencingManager()->Shutdown();
401 if (GetPlatformNotificationContext())
402 GetPlatformNotificationContext()->Shutdown();
404 if (GetBackgroundSyncContext())
405 GetBackgroundSyncContext()->Shutdown();
408 StoragePartitionImpl
* StoragePartitionImpl::Create(
409 BrowserContext
* context
,
411 const base::FilePath
& partition_path
) {
412 // Ensure that these methods are called on the UI thread, except for
413 // unittests where a UI thread might not have been created.
414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
) ||
415 !BrowserThread::IsMessageLoopValid(BrowserThread::UI
));
417 // All of the clients have to be created and registered with the
418 // QuotaManager prior to the QuotaManger being used. We do them
419 // all together here prior to handing out a reference to anything
420 // that utilizes the QuotaManager.
421 scoped_refptr
<storage::QuotaManager
> quota_manager
=
422 new storage::QuotaManager(
425 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
426 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
427 context
->GetSpecialStoragePolicy());
429 // Each consumer is responsible for registering its QuotaClient during
431 scoped_refptr
<storage::FileSystemContext
> filesystem_context
=
432 CreateFileSystemContext(
433 context
, partition_path
, in_memory
, quota_manager
->proxy());
435 scoped_refptr
<storage::DatabaseTracker
> database_tracker
=
436 new storage::DatabaseTracker(partition_path
,
438 context
->GetSpecialStoragePolicy(),
439 quota_manager
->proxy(),
440 BrowserThread::GetMessageLoopProxyForThread(
441 BrowserThread::FILE).get());
443 base::FilePath path
= in_memory
? base::FilePath() : partition_path
;
444 scoped_refptr
<DOMStorageContextWrapper
> dom_storage_context
=
445 new DOMStorageContextWrapper(path
, context
->GetSpecialStoragePolicy());
447 // BrowserMainLoop may not be initialized in unit tests. Tests will
448 // need to inject their own task runner into the IndexedDBContext.
449 base::SequencedTaskRunner
* idb_task_runner
=
450 BrowserThread::CurrentlyOn(BrowserThread::UI
) &&
451 BrowserMainLoop::GetInstance()
452 ? BrowserMainLoop::GetInstance()
453 ->indexed_db_thread()
457 scoped_refptr
<IndexedDBContextImpl
> indexed_db_context
=
458 new IndexedDBContextImpl(path
,
459 context
->GetSpecialStoragePolicy(),
460 quota_manager
->proxy(),
463 scoped_refptr
<CacheStorageContextImpl
> cache_storage_context
=
464 new CacheStorageContextImpl(context
);
465 cache_storage_context
->Init(path
, quota_manager
->proxy(),
466 context
->GetSpecialStoragePolicy());
468 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
469 new ServiceWorkerContextWrapper(context
);
470 service_worker_context
->Init(path
, quota_manager
->proxy(),
471 context
->GetSpecialStoragePolicy());
473 scoped_refptr
<ChromeAppCacheService
> appcache_service
=
474 new ChromeAppCacheService(quota_manager
->proxy());
476 scoped_refptr
<WebRTCIdentityStore
> webrtc_identity_store(
477 new WebRTCIdentityStore(path
, context
->GetSpecialStoragePolicy()));
479 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy(
480 context
->GetSpecialStoragePolicy());
482 scoped_refptr
<GeofencingManager
> geofencing_manager
=
483 new GeofencingManager(service_worker_context
);
484 geofencing_manager
->Init();
486 scoped_refptr
<HostZoomLevelContext
> host_zoom_level_context(
487 new HostZoomLevelContext(
488 context
->CreateZoomLevelDelegate(partition_path
)));
490 scoped_refptr
<NavigatorConnectContextImpl
> navigator_connect_context
=
491 new NavigatorConnectContextImpl(service_worker_context
);
493 scoped_refptr
<PlatformNotificationContextImpl
> platform_notification_context
=
494 new PlatformNotificationContextImpl(path
, context
,
495 service_worker_context
);
496 platform_notification_context
->Initialize();
498 scoped_refptr
<BackgroundSyncContextImpl
> background_sync_context
=
499 new BackgroundSyncContextImpl();
500 background_sync_context
->Init(service_worker_context
);
502 StoragePartitionImpl
* storage_partition
= new StoragePartitionImpl(
503 context
, partition_path
, quota_manager
.get(), appcache_service
.get(),
504 filesystem_context
.get(), database_tracker
.get(),
505 dom_storage_context
.get(), indexed_db_context
.get(),
506 cache_storage_context
.get(), service_worker_context
.get(),
507 webrtc_identity_store
.get(), special_storage_policy
.get(),
508 geofencing_manager
.get(), host_zoom_level_context
.get(),
509 navigator_connect_context
.get(), platform_notification_context
.get(),
510 background_sync_context
.get());
512 service_worker_context
->set_storage_partition(storage_partition
);
514 return storage_partition
;
517 base::FilePath
StoragePartitionImpl::GetPath() {
518 return partition_path_
;
521 net::URLRequestContextGetter
* StoragePartitionImpl::GetURLRequestContext() {
522 return url_request_context_
.get();
525 net::URLRequestContextGetter
*
526 StoragePartitionImpl::GetMediaURLRequestContext() {
527 return media_url_request_context_
.get();
530 storage::QuotaManager
* StoragePartitionImpl::GetQuotaManager() {
531 return quota_manager_
.get();
534 ChromeAppCacheService
* StoragePartitionImpl::GetAppCacheService() {
535 return appcache_service_
.get();
538 storage::FileSystemContext
* StoragePartitionImpl::GetFileSystemContext() {
539 return filesystem_context_
.get();
542 storage::DatabaseTracker
* StoragePartitionImpl::GetDatabaseTracker() {
543 return database_tracker_
.get();
546 DOMStorageContextWrapper
* StoragePartitionImpl::GetDOMStorageContext() {
547 return dom_storage_context_
.get();
550 IndexedDBContextImpl
* StoragePartitionImpl::GetIndexedDBContext() {
551 return indexed_db_context_
.get();
554 CacheStorageContextImpl
* StoragePartitionImpl::GetCacheStorageContext() {
555 return cache_storage_context_
.get();
558 ServiceWorkerContextWrapper
* StoragePartitionImpl::GetServiceWorkerContext() {
559 return service_worker_context_
.get();
562 GeofencingManager
* StoragePartitionImpl::GetGeofencingManager() {
563 return geofencing_manager_
.get();
566 HostZoomMap
* StoragePartitionImpl::GetHostZoomMap() {
567 DCHECK(host_zoom_level_context_
.get());
568 return host_zoom_level_context_
->GetHostZoomMap();
571 HostZoomLevelContext
* StoragePartitionImpl::GetHostZoomLevelContext() {
572 return host_zoom_level_context_
.get();
575 ZoomLevelDelegate
* StoragePartitionImpl::GetZoomLevelDelegate() {
576 DCHECK(host_zoom_level_context_
.get());
577 return host_zoom_level_context_
->GetZoomLevelDelegate();
580 NavigatorConnectContextImpl
*
581 StoragePartitionImpl::GetNavigatorConnectContext() {
582 return navigator_connect_context_
.get();
585 PlatformNotificationContextImpl
*
586 StoragePartitionImpl::GetPlatformNotificationContext() {
587 return platform_notification_context_
.get();
590 BackgroundSyncContextImpl
* StoragePartitionImpl::GetBackgroundSyncContext() {
591 return background_sync_context_
.get();
594 void StoragePartitionImpl::ClearDataImpl(
596 uint32 quota_storage_remove_mask
,
597 const GURL
& storage_origin
,
598 const OriginMatcherFunction
& origin_matcher
,
599 net::URLRequestContextGetter
* rq_context
,
600 const base::Time begin
,
601 const base::Time end
,
602 const base::Closure
& callback
) {
603 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
604 DataDeletionHelper
* helper
= new DataDeletionHelper(remove_mask
,
605 quota_storage_remove_mask
,
607 // |helper| deletes itself when done in
608 // DataDeletionHelper::DecrementTaskCountOnUI().
609 helper
->ClearDataOnUIThread(storage_origin
,
613 dom_storage_context_
.get(),
614 quota_manager_
.get(),
615 special_storage_policy_
.get(),
616 webrtc_identity_store_
.get(),
621 void StoragePartitionImpl::
622 QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
623 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
627 void StoragePartitionImpl::
628 QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
629 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
630 DCHECK_GT(task_count
, 0);
639 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
640 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
641 const base::Time begin
,
642 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
643 const StoragePartition::OriginMatcherFunction
& origin_matcher
) {
644 IncrementTaskCountOnIO();
645 base::Closure decrement_callback
= base::Bind(
646 &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO
,
647 base::Unretained(this));
649 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
) {
650 IncrementTaskCountOnIO();
651 // Ask the QuotaManager for all origins with persistent quota modified
652 // within the user-specified timeframe, and deal with the resulting set in
653 // ClearQuotaManagedOriginsOnIOThread().
654 quota_manager
->GetOriginsModifiedSince(
655 storage::kStorageTypePersistent
,
657 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
658 base::Unretained(this),
660 special_storage_policy
,
662 decrement_callback
));
665 // Do the same for temporary quota.
666 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_TEMPORARY
) {
667 IncrementTaskCountOnIO();
668 quota_manager
->GetOriginsModifiedSince(
669 storage::kStorageTypeTemporary
,
671 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
672 base::Unretained(this),
674 special_storage_policy
,
676 decrement_callback
));
679 // Do the same for syncable quota.
680 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_SYNCABLE
) {
681 IncrementTaskCountOnIO();
682 quota_manager
->GetOriginsModifiedSince(
683 storage::kStorageTypeSyncable
,
685 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
686 base::Unretained(this),
688 special_storage_policy
,
690 decrement_callback
));
693 DecrementTaskCountOnIO();
697 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
698 storage::QuotaManager
* quota_manager
,
699 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
700 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
701 const base::Closure
& callback
,
702 const std::set
<GURL
>& origins
,
703 storage::StorageType quota_storage_type
) {
704 // The QuotaManager manages all storage other than cookies, LocalStorage,
705 // and SessionStorage. This loop wipes out most HTML5 storage for the given
707 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
708 if (!origins
.size()) {
713 size_t* deletion_task_count
= new size_t(0u);
714 (*deletion_task_count
)++;
715 for (std::set
<GURL
>::const_iterator origin
= origins
.begin();
716 origin
!= origins
.end(); ++origin
) {
717 // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
718 if (!storage_origin
.is_empty() && origin
->GetOrigin() != storage_origin
)
721 if (!origin_matcher
.is_null() &&
722 !origin_matcher
.Run(*origin
, special_storage_policy
.get())) {
726 (*deletion_task_count
)++;
727 quota_manager
->DeleteOriginData(
728 *origin
, quota_storage_type
,
729 StoragePartitionImpl::GenerateQuotaClientMask(remove_mask
),
730 base::Bind(&OnQuotaManagedOriginDeleted
,
731 origin
->GetOrigin(), quota_storage_type
,
732 deletion_task_count
, callback
));
734 (*deletion_task_count
)--;
736 CheckQuotaManagedDataDeletionStatus(deletion_task_count
, callback
);
739 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
740 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
744 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
745 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
746 BrowserThread::PostTask(
747 BrowserThread::UI
, FROM_HERE
,
748 base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI
,
749 base::Unretained(this)));
752 DCHECK_GT(task_count
, 0);
760 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
761 const GURL
& storage_origin
,
762 const OriginMatcherFunction
& origin_matcher
,
763 const base::FilePath
& path
,
764 net::URLRequestContextGetter
* rq_context
,
765 DOMStorageContextWrapper
* dom_storage_context
,
766 storage::QuotaManager
* quota_manager
,
767 storage::SpecialStoragePolicy
* special_storage_policy
,
768 WebRTCIdentityStore
* webrtc_identity_store
,
769 const base::Time begin
,
770 const base::Time end
) {
771 DCHECK_NE(remove_mask
, 0u);
772 DCHECK(!callback
.is_null());
774 IncrementTaskCountOnUI();
775 base::Closure decrement_callback
= base::Bind(
776 &DataDeletionHelper::DecrementTaskCountOnUI
, base::Unretained(this));
778 if (remove_mask
& REMOVE_DATA_MASK_COOKIES
) {
779 // Handle the cookies.
780 IncrementTaskCountOnUI();
781 BrowserThread::PostTask(
782 BrowserThread::IO
, FROM_HERE
,
783 base::Bind(&ClearCookiesOnIOThread
,
784 make_scoped_refptr(rq_context
), begin
, end
, storage_origin
,
785 decrement_callback
));
788 if (remove_mask
& REMOVE_DATA_MASK_INDEXEDDB
||
789 remove_mask
& REMOVE_DATA_MASK_WEBSQL
||
790 remove_mask
& REMOVE_DATA_MASK_APPCACHE
||
791 remove_mask
& REMOVE_DATA_MASK_FILE_SYSTEMS
||
792 remove_mask
& REMOVE_DATA_MASK_SERVICE_WORKERS
||
793 remove_mask
& REMOVE_DATA_MASK_CACHE_STORAGE
) {
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