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/browser/host_zoom_map_impl.h"
14 #include "content/common/dom_storage/dom_storage_types.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/dom_storage_context.h"
18 #include "content/public/browser/indexed_db_context.h"
19 #include "content/public/browser/local_storage_usage_info.h"
20 #include "content/public/browser/session_storage_usage_info.h"
21 #include "net/base/completion_callback.h"
22 #include "net/base/net_errors.h"
23 #include "net/cookies/cookie_monster.h"
24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "storage/browser/database/database_tracker.h"
27 #include "storage/browser/quota/quota_manager.h"
33 void OnClearedCookies(const base::Closure
& callback
, int num_deleted
) {
34 // The final callback needs to happen from UI thread.
35 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
36 BrowserThread::PostTask(
37 BrowserThread::UI
, FROM_HERE
,
38 base::Bind(&OnClearedCookies
, callback
, num_deleted
));
45 void ClearCookiesOnIOThread(
46 const scoped_refptr
<net::URLRequestContextGetter
>& rq_context
,
47 const base::Time begin
,
49 const GURL
& storage_origin
,
50 const base::Closure
& callback
) {
51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
52 net::CookieStore
* cookie_store
= rq_context
->
53 GetURLRequestContext()->cookie_store();
54 if (storage_origin
.is_empty()) {
55 cookie_store
->DeleteAllCreatedBetweenAsync(
58 base::Bind(&OnClearedCookies
, callback
));
60 cookie_store
->DeleteAllCreatedBetweenForHostAsync(
63 storage_origin
, base::Bind(&OnClearedCookies
, callback
));
67 void CheckQuotaManagedDataDeletionStatus(size_t* deletion_task_count
,
68 const base::Closure
& callback
) {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
70 if (*deletion_task_count
== 0) {
71 delete deletion_task_count
;
76 void OnQuotaManagedOriginDeleted(const GURL
& origin
,
77 storage::StorageType type
,
78 size_t* deletion_task_count
,
79 const base::Closure
& callback
,
80 storage::QuotaStatusCode status
) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
82 DCHECK_GT(*deletion_task_count
, 0u);
83 if (status
!= storage::kQuotaStatusOk
) {
84 DLOG(ERROR
) << "Couldn't remove data of type " << type
<< " for origin "
85 << origin
<< ". Status: " << status
;
88 (*deletion_task_count
)--;
89 CheckQuotaManagedDataDeletionStatus(deletion_task_count
, callback
);
92 void ClearedShaderCache(const base::Closure
& callback
) {
93 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
94 BrowserThread::PostTask(
95 BrowserThread::UI
, FROM_HERE
,
96 base::Bind(&ClearedShaderCache
, callback
));
102 void ClearShaderCacheOnIOThread(const base::FilePath
& path
,
103 const base::Time begin
,
104 const base::Time end
,
105 const base::Closure
& callback
) {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
107 ShaderCacheFactory::GetInstance()->ClearByPath(
108 path
, begin
, end
, base::Bind(&ClearedShaderCache
, callback
));
111 void OnLocalStorageUsageInfo(
112 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
113 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
114 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
115 const base::Time delete_begin
,
116 const base::Time delete_end
,
117 const base::Closure
& callback
,
118 const std::vector
<LocalStorageUsageInfo
>& infos
) {
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
121 for (size_t i
= 0; i
< infos
.size(); ++i
) {
122 if (!origin_matcher
.is_null() &&
123 !origin_matcher
.Run(infos
[i
].origin
, special_storage_policy
.get())) {
127 if (infos
[i
].last_modified
>= delete_begin
&&
128 infos
[i
].last_modified
<= delete_end
) {
129 dom_storage_context
->DeleteLocalStorage(infos
[i
].origin
);
135 void OnSessionStorageUsageInfo(
136 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
137 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
138 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
139 const base::Closure
& callback
,
140 const std::vector
<SessionStorageUsageInfo
>& infos
) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
143 for (size_t i
= 0; i
< infos
.size(); ++i
) {
144 if (!origin_matcher
.is_null() &&
145 !origin_matcher
.Run(infos
[i
].origin
, special_storage_policy
.get())) {
148 dom_storage_context
->DeleteSessionStorage(infos
[i
]);
154 void ClearLocalStorageOnUIThread(
155 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
156 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
157 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
158 const GURL
& storage_origin
,
159 const base::Time begin
,
160 const base::Time end
,
161 const base::Closure
& callback
) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
164 if (!storage_origin
.is_empty()) {
165 bool can_delete
= origin_matcher
.is_null() ||
166 origin_matcher
.Run(storage_origin
,
167 special_storage_policy
.get());
169 dom_storage_context
->DeleteLocalStorage(storage_origin
);
175 dom_storage_context
->GetLocalStorageUsage(
176 base::Bind(&OnLocalStorageUsageInfo
,
177 dom_storage_context
, special_storage_policy
, origin_matcher
,
178 begin
, end
, callback
));
181 void ClearSessionStorageOnUIThread(
182 const scoped_refptr
<DOMStorageContextWrapper
>& dom_storage_context
,
183 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
184 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
185 const base::Closure
& callback
) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
188 dom_storage_context
->GetSessionStorageUsage(
189 base::Bind(&OnSessionStorageUsageInfo
, dom_storage_context
,
190 special_storage_policy
, origin_matcher
,
197 STATIC_CONST_MEMBER_DEFINITION
const uint32
198 StoragePartition::REMOVE_DATA_MASK_APPCACHE
;
199 STATIC_CONST_MEMBER_DEFINITION
const uint32
200 StoragePartition::REMOVE_DATA_MASK_COOKIES
;
201 STATIC_CONST_MEMBER_DEFINITION
const uint32
202 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
;
203 STATIC_CONST_MEMBER_DEFINITION
const uint32
204 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
;
205 STATIC_CONST_MEMBER_DEFINITION
const uint32
206 StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE
;
207 STATIC_CONST_MEMBER_DEFINITION
const uint32
208 StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS
;
209 STATIC_CONST_MEMBER_DEFINITION
const uint32
210 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE
;
211 STATIC_CONST_MEMBER_DEFINITION
const uint32
212 StoragePartition::REMOVE_DATA_MASK_WEBSQL
;
213 STATIC_CONST_MEMBER_DEFINITION
const uint32
214 StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY
;
215 STATIC_CONST_MEMBER_DEFINITION
const uint32
216 StoragePartition::REMOVE_DATA_MASK_ALL
;
217 STATIC_CONST_MEMBER_DEFINITION
const uint32
218 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY
;
219 STATIC_CONST_MEMBER_DEFINITION
const uint32
220 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
;
221 STATIC_CONST_MEMBER_DEFINITION
const uint32
222 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE
;
223 STATIC_CONST_MEMBER_DEFINITION
const uint32
224 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL
;
227 int StoragePartitionImpl::GenerateQuotaClientMask(uint32 remove_mask
) {
228 int quota_client_mask
= 0;
230 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS
)
231 quota_client_mask
|= storage::QuotaClient::kFileSystem
;
232 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_WEBSQL
)
233 quota_client_mask
|= storage::QuotaClient::kDatabase
;
234 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_APPCACHE
)
235 quota_client_mask
|= storage::QuotaClient::kAppcache
;
236 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_INDEXEDDB
)
237 quota_client_mask
|= storage::QuotaClient::kIndexedDatabase
;
238 if (remove_mask
& StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS
) {
239 quota_client_mask
|= storage::QuotaClient::kServiceWorker
;
240 quota_client_mask
|= storage::QuotaClient::kServiceWorkerCache
;
244 return quota_client_mask
;
247 // Helper for deleting quota managed data from a partition.
249 // Most of the operations in this class are done on IO thread.
250 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper
{
251 QuotaManagedDataDeletionHelper(uint32 remove_mask
,
252 uint32 quota_storage_remove_mask
,
253 const GURL
& storage_origin
,
254 const base::Closure
& callback
)
255 : remove_mask(remove_mask
),
256 quota_storage_remove_mask(quota_storage_remove_mask
),
257 storage_origin(storage_origin
),
262 void IncrementTaskCountOnIO();
263 void DecrementTaskCountOnIO();
265 void ClearDataOnIOThread(
266 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
267 const base::Time begin
,
268 const scoped_refptr
<storage::SpecialStoragePolicy
>&
269 special_storage_policy
,
270 const StoragePartition::OriginMatcherFunction
& origin_matcher
);
272 void ClearOriginsOnIOThread(
273 storage::QuotaManager
* quota_manager
,
274 const scoped_refptr
<storage::SpecialStoragePolicy
>&
275 special_storage_policy
,
276 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
277 const base::Closure
& callback
,
278 const std::set
<GURL
>& origins
,
279 storage::StorageType quota_storage_type
);
281 // All of these data are accessed on IO thread.
283 uint32 quota_storage_remove_mask
;
285 const base::Closure callback
;
289 // Helper for deleting all sorts of data from a partition, keeps track of
292 // StoragePartitionImpl creates an instance of this class to keep track of
293 // data deletion progress. Deletion requires deleting multiple bits of data
294 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
295 // and IO thread. An instance of this class is created in the beginning of
296 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
297 // forwarded and updated on each (sub) deletion's callback. The instance is
298 // finally destroyed when deletion completes (and |callback| is invoked).
299 struct StoragePartitionImpl::DataDeletionHelper
{
300 DataDeletionHelper(uint32 remove_mask
,
301 uint32 quota_storage_remove_mask
,
302 const base::Closure
& callback
)
303 : remove_mask(remove_mask
),
304 quota_storage_remove_mask(quota_storage_remove_mask
),
309 void IncrementTaskCountOnUI();
310 void DecrementTaskCountOnUI();
312 void ClearDataOnUIThread(
313 const GURL
& storage_origin
,
314 const OriginMatcherFunction
& origin_matcher
,
315 const base::FilePath
& path
,
316 net::URLRequestContextGetter
* rq_context
,
317 DOMStorageContextWrapper
* dom_storage_context
,
318 storage::QuotaManager
* quota_manager
,
319 storage::SpecialStoragePolicy
* special_storage_policy
,
320 WebRTCIdentityStore
* webrtc_identity_store
,
321 const base::Time begin
,
322 const base::Time end
);
324 void ClearQuotaManagedDataOnIOThread(
325 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
326 const base::Time begin
,
327 const GURL
& storage_origin
,
328 const scoped_refptr
<storage::SpecialStoragePolicy
>&
329 special_storage_policy
,
330 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
331 const base::Closure
& callback
);
334 uint32 quota_storage_remove_mask
;
336 // Accessed on UI thread.
337 const base::Closure callback
;
338 // Accessed on UI thread.
342 void StoragePartitionImpl::DataDeletionHelper::ClearQuotaManagedDataOnIOThread(
343 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
344 const base::Time begin
,
345 const GURL
& storage_origin
,
346 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
347 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
348 const base::Closure
& callback
) {
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
351 StoragePartitionImpl::QuotaManagedDataDeletionHelper
* helper
=
352 new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
354 quota_storage_remove_mask
,
357 helper
->ClearDataOnIOThread(quota_manager
, begin
, special_storage_policy
,
361 StoragePartitionImpl::StoragePartitionImpl(
362 const base::FilePath
& partition_path
,
363 storage::QuotaManager
* quota_manager
,
364 ChromeAppCacheService
* appcache_service
,
365 storage::FileSystemContext
* filesystem_context
,
366 storage::DatabaseTracker
* database_tracker
,
367 DOMStorageContextWrapper
* dom_storage_context
,
368 IndexedDBContextImpl
* indexed_db_context
,
369 ServiceWorkerContextWrapper
* service_worker_context
,
370 WebRTCIdentityStore
* webrtc_identity_store
,
371 storage::SpecialStoragePolicy
* special_storage_policy
,
372 GeofencingManager
* geofencing_manager
,
373 HostZoomLevelContext
* host_zoom_level_context
)
374 : partition_path_(partition_path
),
375 quota_manager_(quota_manager
),
376 appcache_service_(appcache_service
),
377 filesystem_context_(filesystem_context
),
378 database_tracker_(database_tracker
),
379 dom_storage_context_(dom_storage_context
),
380 indexed_db_context_(indexed_db_context
),
381 service_worker_context_(service_worker_context
),
382 webrtc_identity_store_(webrtc_identity_store
),
383 special_storage_policy_(special_storage_policy
),
384 geofencing_manager_(geofencing_manager
),
385 host_zoom_level_context_(host_zoom_level_context
) {
388 StoragePartitionImpl::~StoragePartitionImpl() {
389 // These message loop checks are just to avoid leaks in unittests.
390 if (GetDatabaseTracker() &&
391 BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
392 BrowserThread::PostTask(
395 base::Bind(&storage::DatabaseTracker::Shutdown
, GetDatabaseTracker()));
398 if (GetFileSystemContext())
399 GetFileSystemContext()->Shutdown();
401 if (GetDOMStorageContext())
402 GetDOMStorageContext()->Shutdown();
404 if (GetServiceWorkerContext())
405 GetServiceWorkerContext()->Shutdown();
407 if (GetGeofencingManager())
408 GetGeofencingManager()->Shutdown();
411 // TODO(ajwong): Break the direct dependency on |context|. We only
412 // need 3 pieces of info from it.
413 StoragePartitionImpl
* StoragePartitionImpl::Create(
414 BrowserContext
* context
,
416 const base::FilePath
& partition_path
) {
417 // Ensure that these methods are called on the UI thread, except for
418 // unittests where a UI thread might not have been created.
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
) ||
420 !BrowserThread::IsMessageLoopValid(BrowserThread::UI
));
422 // All of the clients have to be created and registered with the
423 // QuotaManager prior to the QuotaManger being used. We do them
424 // all together here prior to handing out a reference to anything
425 // that utilizes the QuotaManager.
426 scoped_refptr
<storage::QuotaManager
> quota_manager
=
427 new storage::QuotaManager(
430 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
).get(),
431 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
432 context
->GetSpecialStoragePolicy());
434 // Each consumer is responsible for registering its QuotaClient during
436 scoped_refptr
<storage::FileSystemContext
> filesystem_context
=
437 CreateFileSystemContext(
438 context
, partition_path
, in_memory
, quota_manager
->proxy());
440 scoped_refptr
<storage::DatabaseTracker
> database_tracker
=
441 new storage::DatabaseTracker(partition_path
,
443 context
->GetSpecialStoragePolicy(),
444 quota_manager
->proxy(),
445 BrowserThread::GetMessageLoopProxyForThread(
446 BrowserThread::FILE).get());
448 base::FilePath path
= in_memory
? base::FilePath() : partition_path
;
449 scoped_refptr
<DOMStorageContextWrapper
> dom_storage_context
=
450 new DOMStorageContextWrapper(path
, context
->GetSpecialStoragePolicy());
452 // BrowserMainLoop may not be initialized in unit tests. Tests will
453 // need to inject their own task runner into the IndexedDBContext.
454 base::SequencedTaskRunner
* idb_task_runner
=
455 BrowserThread::CurrentlyOn(BrowserThread::UI
) &&
456 BrowserMainLoop::GetInstance()
457 ? BrowserMainLoop::GetInstance()->indexed_db_thread()
458 ->message_loop_proxy().get()
460 scoped_refptr
<IndexedDBContextImpl
> indexed_db_context
=
461 new IndexedDBContextImpl(path
,
462 context
->GetSpecialStoragePolicy(),
463 quota_manager
->proxy(),
466 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
467 new ServiceWorkerContextWrapper(context
);
468 service_worker_context
->Init(
469 path
, quota_manager
->proxy(), context
->GetSpecialStoragePolicy());
471 scoped_refptr
<ChromeAppCacheService
> appcache_service
=
472 new ChromeAppCacheService(quota_manager
->proxy());
474 scoped_refptr
<WebRTCIdentityStore
> webrtc_identity_store(
475 new WebRTCIdentityStore(path
, context
->GetSpecialStoragePolicy()));
477 scoped_refptr
<storage::SpecialStoragePolicy
> special_storage_policy(
478 context
->GetSpecialStoragePolicy());
480 scoped_refptr
<GeofencingManager
> geofencing_manager
=
481 new GeofencingManager(service_worker_context
);
482 geofencing_manager
->Init();
484 scoped_refptr
<HostZoomLevelContext
> host_zoom_level_context(
485 new HostZoomLevelContext(
486 context
->CreateZoomLevelDelegate(partition_path
)));
488 return new StoragePartitionImpl(partition_path
,
490 appcache_service
.get(),
491 filesystem_context
.get(),
492 database_tracker
.get(),
493 dom_storage_context
.get(),
494 indexed_db_context
.get(),
495 service_worker_context
.get(),
496 webrtc_identity_store
.get(),
497 special_storage_policy
.get(),
498 geofencing_manager
.get(),
499 host_zoom_level_context
.get());
502 base::FilePath
StoragePartitionImpl::GetPath() {
503 return partition_path_
;
506 net::URLRequestContextGetter
* StoragePartitionImpl::GetURLRequestContext() {
507 return url_request_context_
.get();
510 net::URLRequestContextGetter
*
511 StoragePartitionImpl::GetMediaURLRequestContext() {
512 return media_url_request_context_
.get();
515 storage::QuotaManager
* StoragePartitionImpl::GetQuotaManager() {
516 return quota_manager_
.get();
519 ChromeAppCacheService
* StoragePartitionImpl::GetAppCacheService() {
520 return appcache_service_
.get();
523 storage::FileSystemContext
* StoragePartitionImpl::GetFileSystemContext() {
524 return filesystem_context_
.get();
527 storage::DatabaseTracker
* StoragePartitionImpl::GetDatabaseTracker() {
528 return database_tracker_
.get();
531 DOMStorageContextWrapper
* StoragePartitionImpl::GetDOMStorageContext() {
532 return dom_storage_context_
.get();
535 IndexedDBContextImpl
* StoragePartitionImpl::GetIndexedDBContext() {
536 return indexed_db_context_
.get();
539 ServiceWorkerContextWrapper
* StoragePartitionImpl::GetServiceWorkerContext() {
540 return service_worker_context_
.get();
543 GeofencingManager
* StoragePartitionImpl::GetGeofencingManager() {
544 return geofencing_manager_
.get();
547 HostZoomMap
* StoragePartitionImpl::GetHostZoomMap() {
548 DCHECK(host_zoom_level_context_
.get());
549 return host_zoom_level_context_
->GetHostZoomMap();
552 HostZoomLevelContext
* StoragePartitionImpl::GetHostZoomLevelContext() {
553 return host_zoom_level_context_
.get();
556 ZoomLevelDelegate
* StoragePartitionImpl::GetZoomLevelDelegate() {
557 DCHECK(host_zoom_level_context_
.get());
558 return host_zoom_level_context_
->GetZoomLevelDelegate();
561 void StoragePartitionImpl::ClearDataImpl(
563 uint32 quota_storage_remove_mask
,
564 const GURL
& storage_origin
,
565 const OriginMatcherFunction
& origin_matcher
,
566 net::URLRequestContextGetter
* rq_context
,
567 const base::Time begin
,
568 const base::Time end
,
569 const base::Closure
& callback
) {
570 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
571 DataDeletionHelper
* helper
= new DataDeletionHelper(remove_mask
,
572 quota_storage_remove_mask
,
574 // |helper| deletes itself when done in
575 // DataDeletionHelper::DecrementTaskCountOnUI().
576 helper
->ClearDataOnUIThread(storage_origin
,
580 dom_storage_context_
.get(),
581 quota_manager_
.get(),
582 special_storage_policy_
.get(),
583 webrtc_identity_store_
.get(),
588 void StoragePartitionImpl::
589 QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
590 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
594 void StoragePartitionImpl::
595 QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
596 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
597 DCHECK_GT(task_count
, 0);
606 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
607 const scoped_refptr
<storage::QuotaManager
>& quota_manager
,
608 const base::Time begin
,
609 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
610 const StoragePartition::OriginMatcherFunction
& origin_matcher
) {
611 IncrementTaskCountOnIO();
612 base::Closure decrement_callback
= base::Bind(
613 &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO
,
614 base::Unretained(this));
616 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_PERSISTENT
) {
617 IncrementTaskCountOnIO();
618 // Ask the QuotaManager for all origins with persistent quota modified
619 // within the user-specified timeframe, and deal with the resulting set in
620 // ClearQuotaManagedOriginsOnIOThread().
621 quota_manager
->GetOriginsModifiedSince(
622 storage::kStorageTypePersistent
,
624 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
625 base::Unretained(this),
627 special_storage_policy
,
629 decrement_callback
));
632 // Do the same for temporary quota.
633 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_TEMPORARY
) {
634 IncrementTaskCountOnIO();
635 quota_manager
->GetOriginsModifiedSince(
636 storage::kStorageTypeTemporary
,
638 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
639 base::Unretained(this),
641 special_storage_policy
,
643 decrement_callback
));
646 // Do the same for syncable quota.
647 if (quota_storage_remove_mask
& QUOTA_MANAGED_STORAGE_MASK_SYNCABLE
) {
648 IncrementTaskCountOnIO();
649 quota_manager
->GetOriginsModifiedSince(
650 storage::kStorageTypeSyncable
,
652 base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread
,
653 base::Unretained(this),
655 special_storage_policy
,
657 decrement_callback
));
660 DecrementTaskCountOnIO();
664 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
665 storage::QuotaManager
* quota_manager
,
666 const scoped_refptr
<storage::SpecialStoragePolicy
>& special_storage_policy
,
667 const StoragePartition::OriginMatcherFunction
& origin_matcher
,
668 const base::Closure
& callback
,
669 const std::set
<GURL
>& origins
,
670 storage::StorageType quota_storage_type
) {
671 // The QuotaManager manages all storage other than cookies, LocalStorage,
672 // and SessionStorage. This loop wipes out most HTML5 storage for the given
674 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
675 if (!origins
.size()) {
680 size_t* deletion_task_count
= new size_t(0u);
681 (*deletion_task_count
)++;
682 for (std::set
<GURL
>::const_iterator origin
= origins
.begin();
683 origin
!= origins
.end(); ++origin
) {
684 // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
685 if (!storage_origin
.is_empty() && origin
->GetOrigin() != storage_origin
)
688 if (!origin_matcher
.is_null() &&
689 !origin_matcher
.Run(*origin
, special_storage_policy
.get())) {
693 (*deletion_task_count
)++;
694 quota_manager
->DeleteOriginData(
695 *origin
, quota_storage_type
,
696 StoragePartitionImpl::GenerateQuotaClientMask(remove_mask
),
697 base::Bind(&OnQuotaManagedOriginDeleted
,
698 origin
->GetOrigin(), quota_storage_type
,
699 deletion_task_count
, callback
));
701 (*deletion_task_count
)--;
703 CheckQuotaManagedDataDeletionStatus(deletion_task_count
, callback
);
706 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
707 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
711 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
712 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
713 BrowserThread::PostTask(
714 BrowserThread::UI
, FROM_HERE
,
715 base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI
,
716 base::Unretained(this)));
719 DCHECK_GT(task_count
, 0);
727 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
728 const GURL
& storage_origin
,
729 const OriginMatcherFunction
& origin_matcher
,
730 const base::FilePath
& path
,
731 net::URLRequestContextGetter
* rq_context
,
732 DOMStorageContextWrapper
* dom_storage_context
,
733 storage::QuotaManager
* quota_manager
,
734 storage::SpecialStoragePolicy
* special_storage_policy
,
735 WebRTCIdentityStore
* webrtc_identity_store
,
736 const base::Time begin
,
737 const base::Time end
) {
738 DCHECK_NE(remove_mask
, 0u);
739 DCHECK(!callback
.is_null());
741 IncrementTaskCountOnUI();
742 base::Closure decrement_callback
= base::Bind(
743 &DataDeletionHelper::DecrementTaskCountOnUI
, base::Unretained(this));
745 if (remove_mask
& REMOVE_DATA_MASK_COOKIES
) {
746 // Handle the cookies.
747 IncrementTaskCountOnUI();
748 BrowserThread::PostTask(
749 BrowserThread::IO
, FROM_HERE
,
750 base::Bind(&ClearCookiesOnIOThread
,
751 make_scoped_refptr(rq_context
), begin
, end
, storage_origin
,
752 decrement_callback
));
755 if (remove_mask
& REMOVE_DATA_MASK_INDEXEDDB
||
756 remove_mask
& REMOVE_DATA_MASK_WEBSQL
||
757 remove_mask
& REMOVE_DATA_MASK_APPCACHE
||
758 remove_mask
& REMOVE_DATA_MASK_FILE_SYSTEMS
||
759 remove_mask
& REMOVE_DATA_MASK_SERVICE_WORKERS
) {
760 IncrementTaskCountOnUI();
761 BrowserThread::PostTask(
762 BrowserThread::IO
, FROM_HERE
,
763 base::Bind(&DataDeletionHelper::ClearQuotaManagedDataOnIOThread
,
764 base::Unretained(this),
765 make_scoped_refptr(quota_manager
),
768 make_scoped_refptr(special_storage_policy
),
770 decrement_callback
));
773 if (remove_mask
& REMOVE_DATA_MASK_LOCAL_STORAGE
) {
774 IncrementTaskCountOnUI();
775 ClearLocalStorageOnUIThread(
776 make_scoped_refptr(dom_storage_context
),
777 make_scoped_refptr(special_storage_policy
),
779 storage_origin
, begin
, end
,
782 // ClearDataImpl cannot clear session storage data when a particular origin
783 // is specified. Therefore we ignore clearing session storage in this case.
784 // TODO(lazyboy): Fix.
785 if (storage_origin
.is_empty()) {
786 IncrementTaskCountOnUI();
787 ClearSessionStorageOnUIThread(
788 make_scoped_refptr(dom_storage_context
),
789 make_scoped_refptr(special_storage_policy
),
795 if (remove_mask
& REMOVE_DATA_MASK_SHADER_CACHE
) {
796 IncrementTaskCountOnUI();
797 BrowserThread::PostTask(
798 BrowserThread::IO
, FROM_HERE
,
799 base::Bind(&ClearShaderCacheOnIOThread
,
800 path
, begin
, end
, decrement_callback
));
803 if (remove_mask
& REMOVE_DATA_MASK_WEBRTC_IDENTITY
) {
804 IncrementTaskCountOnUI();
805 BrowserThread::PostTask(
808 base::Bind(&WebRTCIdentityStore::DeleteBetween
,
809 webrtc_identity_store
,
812 decrement_callback
));
815 DecrementTaskCountOnUI();
818 void StoragePartitionImpl::ClearDataForOrigin(
820 uint32 quota_storage_remove_mask
,
821 const GURL
& storage_origin
,
822 net::URLRequestContextGetter
* request_context_getter
,
823 const base::Closure
& callback
) {
824 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
825 ClearDataImpl(remove_mask
,
826 quota_storage_remove_mask
,
828 OriginMatcherFunction(),
829 request_context_getter
,
835 void StoragePartitionImpl::ClearData(
837 uint32 quota_storage_remove_mask
,
838 const GURL
& storage_origin
,
839 const OriginMatcherFunction
& origin_matcher
,
840 const base::Time begin
,
841 const base::Time end
,
842 const base::Closure
& callback
) {
843 ClearDataImpl(remove_mask
, quota_storage_remove_mask
, storage_origin
,
844 origin_matcher
, GetURLRequestContext(), begin
, end
, callback
);
847 WebRTCIdentityStore
* StoragePartitionImpl::GetWebRTCIdentityStore() {
848 return webrtc_identity_store_
.get();
851 void StoragePartitionImpl::OverrideQuotaManagerForTesting(
852 storage::QuotaManager
* quota_manager
) {
853 quota_manager_
= quota_manager
;
856 void StoragePartitionImpl::OverrideSpecialStoragePolicyForTesting(
857 storage::SpecialStoragePolicy
* special_storage_policy
) {
858 special_storage_policy_
= special_storage_policy
;
861 void StoragePartitionImpl::SetURLRequestContext(
862 net::URLRequestContextGetter
* url_request_context
) {
863 url_request_context_
= url_request_context
;
866 void StoragePartitionImpl::SetMediaURLRequestContext(
867 net::URLRequestContextGetter
* media_url_request_context
) {
868 media_url_request_context_
= media_url_request_context
;
871 } // namespace content