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