Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "content/browser/storage_partition_impl.h"
      6 
      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 "storage/browser/database/database_tracker.h"
     25 #include "storage/browser/quota/quota_manager.h"
     26 
     27 namespace content {
     28 
     29 namespace {
     30 
     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;
     38   }
     39 
     40   callback.Run();
     41 }
     42 
     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));
     62   }
     63 }
     64 
     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();
     71   }
     72 }
     73 
     74 void OnQuotaManagedOriginDeleted(const GURL& origin,
     75                                  storage::StorageType type,
     76                                  size_t* deletion_task_count,
     77                                  const base::Closure& callback,
     78                                  storage::QuotaStatusCode status) {
     79   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     80   DCHECK_GT(*deletion_task_count, 0u);
     81   if (status != storage::kQuotaStatusOk) {
     82     DLOG(ERROR) << "Couldn't remove data of type " << type << " for origin "
     83                 << origin << ". Status: " << status;
     84   }
     85 
     86   (*deletion_task_count)--;
     87   CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
     88 }
     89 
     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;
     96   }
     97   callback.Run();
     98 }
     99 
    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));
    107 }
    108 
    109 void OnLocalStorageUsageInfo(
    110     const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
    111     const scoped_refptr<storage::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));
    118 
    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;
    123     }
    124 
    125     if (infos[i].last_modified >= delete_begin &&
    126         infos[i].last_modified <= delete_end) {
    127       dom_storage_context->DeleteLocalStorage(infos[i].origin);
    128     }
    129   }
    130   callback.Run();
    131 }
    132 
    133 void OnSessionStorageUsageInfo(
    134     const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
    135     const scoped_refptr<storage::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));
    140 
    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;
    145     }
    146     dom_storage_context->DeleteSessionStorage(infos[i]);
    147   }
    148 
    149   callback.Run();
    150 }
    151 
    152 void ClearLocalStorageOnUIThread(
    153     const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
    154     const scoped_refptr<storage::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));
    161 
    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);
    168 
    169     callback.Run();
    170     return;
    171   }
    172 
    173   dom_storage_context->GetLocalStorageUsage(
    174       base::Bind(&OnLocalStorageUsageInfo,
    175                  dom_storage_context, special_storage_policy, origin_matcher,
    176                  begin, end, callback));
    177 }
    178 
    179 void ClearSessionStorageOnUIThread(
    180     const scoped_refptr<DOMStorageContextWrapper>& dom_storage_context,
    181     const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
    182     const StoragePartition::OriginMatcherFunction& origin_matcher,
    183     const base::Closure& callback) {
    184   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    185 
    186   dom_storage_context->GetSessionStorageUsage(
    187       base::Bind(&OnSessionStorageUsageInfo, dom_storage_context,
    188                  special_storage_policy, origin_matcher,
    189                  callback));
    190 }
    191 
    192 }  // namespace
    193 
    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_SERVICE_WORKERS;
    207 STATIC_CONST_MEMBER_DEFINITION const uint32
    208     StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
    209 STATIC_CONST_MEMBER_DEFINITION const uint32
    210     StoragePartition::REMOVE_DATA_MASK_WEBSQL;
    211 STATIC_CONST_MEMBER_DEFINITION const uint32
    212     StoragePartition::REMOVE_DATA_MASK_WEBRTC_IDENTITY;
    213 STATIC_CONST_MEMBER_DEFINITION const uint32
    214     StoragePartition::REMOVE_DATA_MASK_ALL;
    215 STATIC_CONST_MEMBER_DEFINITION const uint32
    216     StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
    217 STATIC_CONST_MEMBER_DEFINITION const uint32
    218     StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
    219 STATIC_CONST_MEMBER_DEFINITION const uint32
    220     StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE;
    221 STATIC_CONST_MEMBER_DEFINITION const uint32
    222     StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
    223 
    224 // Static.
    225 int StoragePartitionImpl::GenerateQuotaClientMask(uint32 remove_mask) {
    226   int quota_client_mask = 0;
    227 
    228   if (remove_mask & StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS)
    229     quota_client_mask |= storage::QuotaClient::kFileSystem;
    230   if (remove_mask & StoragePartition::REMOVE_DATA_MASK_WEBSQL)
    231     quota_client_mask |= storage::QuotaClient::kDatabase;
    232   if (remove_mask & StoragePartition::REMOVE_DATA_MASK_APPCACHE)
    233     quota_client_mask |= storage::QuotaClient::kAppcache;
    234   if (remove_mask & StoragePartition::REMOVE_DATA_MASK_INDEXEDDB)
    235     quota_client_mask |= storage::QuotaClient::kIndexedDatabase;
    236   // TODO(jsbell): StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS)
    237 
    238   return quota_client_mask;
    239 }
    240 
    241 // Helper for deleting quota managed data from a partition.
    242 //
    243 // Most of the operations in this class are done on IO thread.
    244 struct StoragePartitionImpl::QuotaManagedDataDeletionHelper {
    245   QuotaManagedDataDeletionHelper(uint32 remove_mask,
    246                                  uint32 quota_storage_remove_mask,
    247                                  const GURL& storage_origin,
    248                                  const base::Closure& callback)
    249       : remove_mask(remove_mask),
    250         quota_storage_remove_mask(quota_storage_remove_mask),
    251         storage_origin(storage_origin),
    252         callback(callback),
    253         task_count(0) {
    254   }
    255 
    256   void IncrementTaskCountOnIO();
    257   void DecrementTaskCountOnIO();
    258 
    259   void ClearDataOnIOThread(
    260       const scoped_refptr<storage::QuotaManager>& quota_manager,
    261       const base::Time begin,
    262       const scoped_refptr<storage::SpecialStoragePolicy>&
    263           special_storage_policy,
    264       const StoragePartition::OriginMatcherFunction& origin_matcher);
    265 
    266   void ClearOriginsOnIOThread(
    267       storage::QuotaManager* quota_manager,
    268       const scoped_refptr<storage::SpecialStoragePolicy>&
    269           special_storage_policy,
    270       const StoragePartition::OriginMatcherFunction& origin_matcher,
    271       const base::Closure& callback,
    272       const std::set<GURL>& origins,
    273       storage::StorageType quota_storage_type);
    274 
    275   // All of these data are accessed on IO thread.
    276   uint32 remove_mask;
    277   uint32 quota_storage_remove_mask;
    278   GURL storage_origin;
    279   const base::Closure callback;
    280   int task_count;
    281 };
    282 
    283 // Helper for deleting all sorts of data from a partition, keeps track of
    284 // deletion status.
    285 //
    286 // StoragePartitionImpl creates an instance of this class to keep track of
    287 // data deletion progress. Deletion requires deleting multiple bits of data
    288 // (e.g. cookies, local storage, session storage etc.) and hopping between UI
    289 // and IO thread. An instance of this class is created in the beginning of
    290 // deletion process (StoragePartitionImpl::ClearDataImpl) and the instance is
    291 // forwarded and updated on each (sub) deletion's callback. The instance is
    292 // finally destroyed when deletion completes (and |callback| is invoked).
    293 struct StoragePartitionImpl::DataDeletionHelper {
    294   DataDeletionHelper(uint32 remove_mask,
    295                      uint32 quota_storage_remove_mask,
    296                      const base::Closure& callback)
    297                      : remove_mask(remove_mask),
    298                        quota_storage_remove_mask(quota_storage_remove_mask),
    299                        callback(callback),
    300                        task_count(0) {
    301   }
    302 
    303   void IncrementTaskCountOnUI();
    304   void DecrementTaskCountOnUI();
    305 
    306   void ClearDataOnUIThread(
    307       const GURL& storage_origin,
    308       const OriginMatcherFunction& origin_matcher,
    309       const base::FilePath& path,
    310       net::URLRequestContextGetter* rq_context,
    311       DOMStorageContextWrapper* dom_storage_context,
    312       storage::QuotaManager* quota_manager,
    313       storage::SpecialStoragePolicy* special_storage_policy,
    314       WebRTCIdentityStore* webrtc_identity_store,
    315       const base::Time begin,
    316       const base::Time end);
    317 
    318   void 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>&
    323           special_storage_policy,
    324       const StoragePartition::OriginMatcherFunction& origin_matcher,
    325       const base::Closure& callback);
    326 
    327   uint32 remove_mask;
    328   uint32 quota_storage_remove_mask;
    329 
    330   // Accessed on UI thread.
    331   const base::Closure callback;
    332   // Accessed on UI thread.
    333   int task_count;
    334 };
    335 
    336 void StoragePartitionImpl::DataDeletionHelper::ClearQuotaManagedDataOnIOThread(
    337     const scoped_refptr<storage::QuotaManager>& quota_manager,
    338     const base::Time begin,
    339     const GURL& storage_origin,
    340     const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
    341     const StoragePartition::OriginMatcherFunction& origin_matcher,
    342     const base::Closure& callback) {
    343   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    344 
    345   StoragePartitionImpl::QuotaManagedDataDeletionHelper* helper =
    346       new StoragePartitionImpl::QuotaManagedDataDeletionHelper(
    347           remove_mask,
    348           quota_storage_remove_mask,
    349           storage_origin,
    350           callback);
    351   helper->ClearDataOnIOThread(quota_manager, begin, special_storage_policy,
    352                               origin_matcher);
    353 }
    354 
    355 StoragePartitionImpl::StoragePartitionImpl(
    356     const base::FilePath& partition_path,
    357     storage::QuotaManager* quota_manager,
    358     ChromeAppCacheService* appcache_service,
    359     storage::FileSystemContext* filesystem_context,
    360     storage::DatabaseTracker* database_tracker,
    361     DOMStorageContextWrapper* dom_storage_context,
    362     IndexedDBContextImpl* indexed_db_context,
    363     ServiceWorkerContextWrapper* service_worker_context,
    364     WebRTCIdentityStore* webrtc_identity_store,
    365     storage::SpecialStoragePolicy* special_storage_policy)
    366     : partition_path_(partition_path),
    367       quota_manager_(quota_manager),
    368       appcache_service_(appcache_service),
    369       filesystem_context_(filesystem_context),
    370       database_tracker_(database_tracker),
    371       dom_storage_context_(dom_storage_context),
    372       indexed_db_context_(indexed_db_context),
    373       service_worker_context_(service_worker_context),
    374       webrtc_identity_store_(webrtc_identity_store),
    375       special_storage_policy_(special_storage_policy) {
    376 }
    377 
    378 StoragePartitionImpl::~StoragePartitionImpl() {
    379   // These message loop checks are just to avoid leaks in unittests.
    380   if (GetDatabaseTracker() &&
    381       BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) {
    382     BrowserThread::PostTask(
    383         BrowserThread::FILE,
    384         FROM_HERE,
    385         base::Bind(&storage::DatabaseTracker::Shutdown, GetDatabaseTracker()));
    386   }
    387 
    388   if (GetFileSystemContext())
    389     GetFileSystemContext()->Shutdown();
    390 
    391   if (GetDOMStorageContext())
    392     GetDOMStorageContext()->Shutdown();
    393 
    394   if (GetServiceWorkerContext())
    395     GetServiceWorkerContext()->Shutdown();
    396 }
    397 
    398 // TODO(ajwong): Break the direct dependency on |context|. We only
    399 // need 3 pieces of info from it.
    400 StoragePartitionImpl* StoragePartitionImpl::Create(
    401     BrowserContext* context,
    402     bool in_memory,
    403     const base::FilePath& partition_path) {
    404   // Ensure that these methods are called on the UI thread, except for
    405   // unittests where a UI thread might not have been created.
    406   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
    407          !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
    408 
    409   // All of the clients have to be created and registered with the
    410   // QuotaManager prior to the QuotaManger being used. We do them
    411   // all together here prior to handing out a reference to anything
    412   // that utilizes the QuotaManager.
    413   scoped_refptr<storage::QuotaManager> quota_manager =
    414       new storage::QuotaManager(
    415           in_memory,
    416           partition_path,
    417           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
    418           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
    419           context->GetSpecialStoragePolicy());
    420 
    421   // Each consumer is responsible for registering its QuotaClient during
    422   // its construction.
    423   scoped_refptr<storage::FileSystemContext> filesystem_context =
    424       CreateFileSystemContext(
    425           context, partition_path, in_memory, quota_manager->proxy());
    426 
    427   scoped_refptr<storage::DatabaseTracker> database_tracker =
    428       new storage::DatabaseTracker(partition_path,
    429                                    in_memory,
    430                                    context->GetSpecialStoragePolicy(),
    431                                    quota_manager->proxy(),
    432                                    BrowserThread::GetMessageLoopProxyForThread(
    433                                        BrowserThread::FILE).get());
    434 
    435   base::FilePath path = in_memory ? base::FilePath() : partition_path;
    436   scoped_refptr<DOMStorageContextWrapper> dom_storage_context =
    437       new DOMStorageContextWrapper(path, context->GetSpecialStoragePolicy());
    438 
    439   // BrowserMainLoop may not be initialized in unit tests. Tests will
    440   // need to inject their own task runner into the IndexedDBContext.
    441   base::SequencedTaskRunner* idb_task_runner =
    442       BrowserThread::CurrentlyOn(BrowserThread::UI) &&
    443               BrowserMainLoop::GetInstance()
    444           ? BrowserMainLoop::GetInstance()->indexed_db_thread()
    445                 ->message_loop_proxy().get()
    446           : NULL;
    447   scoped_refptr<IndexedDBContextImpl> indexed_db_context =
    448       new IndexedDBContextImpl(path,
    449                                context->GetSpecialStoragePolicy(),
    450                                quota_manager->proxy(),
    451                                idb_task_runner);
    452 
    453   scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
    454       new ServiceWorkerContextWrapper(context);
    455   service_worker_context->Init(path, quota_manager->proxy());
    456 
    457   scoped_refptr<ChromeAppCacheService> appcache_service =
    458       new ChromeAppCacheService(quota_manager->proxy());
    459 
    460   scoped_refptr<WebRTCIdentityStore> webrtc_identity_store(
    461       new WebRTCIdentityStore(path, context->GetSpecialStoragePolicy()));
    462 
    463   scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy(
    464       context->GetSpecialStoragePolicy());
    465 
    466   return new StoragePartitionImpl(partition_path,
    467                                   quota_manager.get(),
    468                                   appcache_service.get(),
    469                                   filesystem_context.get(),
    470                                   database_tracker.get(),
    471                                   dom_storage_context.get(),
    472                                   indexed_db_context.get(),
    473                                   service_worker_context.get(),
    474                                   webrtc_identity_store.get(),
    475                                   special_storage_policy.get());
    476 }
    477 
    478 base::FilePath StoragePartitionImpl::GetPath() {
    479   return partition_path_;
    480 }
    481 
    482 net::URLRequestContextGetter* StoragePartitionImpl::GetURLRequestContext() {
    483   return url_request_context_.get();
    484 }
    485 
    486 net::URLRequestContextGetter*
    487 StoragePartitionImpl::GetMediaURLRequestContext() {
    488   return media_url_request_context_.get();
    489 }
    490 
    491 storage::QuotaManager* StoragePartitionImpl::GetQuotaManager() {
    492   return quota_manager_.get();
    493 }
    494 
    495 ChromeAppCacheService* StoragePartitionImpl::GetAppCacheService() {
    496   return appcache_service_.get();
    497 }
    498 
    499 storage::FileSystemContext* StoragePartitionImpl::GetFileSystemContext() {
    500   return filesystem_context_.get();
    501 }
    502 
    503 storage::DatabaseTracker* StoragePartitionImpl::GetDatabaseTracker() {
    504   return database_tracker_.get();
    505 }
    506 
    507 DOMStorageContextWrapper* StoragePartitionImpl::GetDOMStorageContext() {
    508   return dom_storage_context_.get();
    509 }
    510 
    511 IndexedDBContextImpl* StoragePartitionImpl::GetIndexedDBContext() {
    512   return indexed_db_context_.get();
    513 }
    514 
    515 ServiceWorkerContextWrapper* StoragePartitionImpl::GetServiceWorkerContext() {
    516   return service_worker_context_.get();
    517 }
    518 
    519 void StoragePartitionImpl::ClearDataImpl(
    520     uint32 remove_mask,
    521     uint32 quota_storage_remove_mask,
    522     const GURL& storage_origin,
    523     const OriginMatcherFunction& origin_matcher,
    524     net::URLRequestContextGetter* rq_context,
    525     const base::Time begin,
    526     const base::Time end,
    527     const base::Closure& callback) {
    528   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    529   DataDeletionHelper* helper = new DataDeletionHelper(remove_mask,
    530                                                       quota_storage_remove_mask,
    531                                                       callback);
    532   // |helper| deletes itself when done in
    533   // DataDeletionHelper::DecrementTaskCountOnUI().
    534   helper->ClearDataOnUIThread(storage_origin,
    535                               origin_matcher,
    536                               GetPath(),
    537                               rq_context,
    538                               dom_storage_context_.get(),
    539                               quota_manager_.get(),
    540                               special_storage_policy_.get(),
    541                               webrtc_identity_store_.get(),
    542                               begin,
    543                               end);
    544 }
    545 
    546 void StoragePartitionImpl::
    547     QuotaManagedDataDeletionHelper::IncrementTaskCountOnIO() {
    548   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    549   ++task_count;
    550 }
    551 
    552 void StoragePartitionImpl::
    553     QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO() {
    554   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    555   DCHECK_GT(task_count, 0);
    556   --task_count;
    557   if (task_count)
    558     return;
    559 
    560   callback.Run();
    561   delete this;
    562 }
    563 
    564 void StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearDataOnIOThread(
    565     const scoped_refptr<storage::QuotaManager>& quota_manager,
    566     const base::Time begin,
    567     const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
    568     const StoragePartition::OriginMatcherFunction& origin_matcher) {
    569   IncrementTaskCountOnIO();
    570   base::Closure decrement_callback = base::Bind(
    571       &QuotaManagedDataDeletionHelper::DecrementTaskCountOnIO,
    572       base::Unretained(this));
    573 
    574   if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_PERSISTENT) {
    575     IncrementTaskCountOnIO();
    576     // Ask the QuotaManager for all origins with persistent quota modified
    577     // within the user-specified timeframe, and deal with the resulting set in
    578     // ClearQuotaManagedOriginsOnIOThread().
    579     quota_manager->GetOriginsModifiedSince(
    580         storage::kStorageTypePersistent,
    581         begin,
    582         base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
    583                    base::Unretained(this),
    584                    quota_manager,
    585                    special_storage_policy,
    586                    origin_matcher,
    587                    decrement_callback));
    588   }
    589 
    590   // Do the same for temporary quota.
    591   if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_TEMPORARY) {
    592     IncrementTaskCountOnIO();
    593     quota_manager->GetOriginsModifiedSince(
    594         storage::kStorageTypeTemporary,
    595         begin,
    596         base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
    597                    base::Unretained(this),
    598                    quota_manager,
    599                    special_storage_policy,
    600                    origin_matcher,
    601                    decrement_callback));
    602   }
    603 
    604   // Do the same for syncable quota.
    605   if (quota_storage_remove_mask & QUOTA_MANAGED_STORAGE_MASK_SYNCABLE) {
    606     IncrementTaskCountOnIO();
    607     quota_manager->GetOriginsModifiedSince(
    608         storage::kStorageTypeSyncable,
    609         begin,
    610         base::Bind(&QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread,
    611                    base::Unretained(this),
    612                    quota_manager,
    613                    special_storage_policy,
    614                    origin_matcher,
    615                    decrement_callback));
    616   }
    617 
    618   DecrementTaskCountOnIO();
    619 }
    620 
    621 void
    622 StoragePartitionImpl::QuotaManagedDataDeletionHelper::ClearOriginsOnIOThread(
    623     storage::QuotaManager* quota_manager,
    624     const scoped_refptr<storage::SpecialStoragePolicy>& special_storage_policy,
    625     const StoragePartition::OriginMatcherFunction& origin_matcher,
    626     const base::Closure& callback,
    627     const std::set<GURL>& origins,
    628     storage::StorageType quota_storage_type) {
    629   // The QuotaManager manages all storage other than cookies, LocalStorage,
    630   // and SessionStorage. This loop wipes out most HTML5 storage for the given
    631   // origins.
    632   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    633   if (!origins.size()) {
    634     callback.Run();
    635     return;
    636   }
    637 
    638   size_t* deletion_task_count = new size_t(0u);
    639   (*deletion_task_count)++;
    640   for (std::set<GURL>::const_iterator origin = origins.begin();
    641        origin != origins.end(); ++origin) {
    642     // TODO(mkwst): Clean this up, it's slow. http://crbug.com/130746
    643     if (!storage_origin.is_empty() && origin->GetOrigin() != storage_origin)
    644       continue;
    645 
    646     if (!origin_matcher.is_null() &&
    647         !origin_matcher.Run(*origin, special_storage_policy.get())) {
    648       continue;
    649     }
    650 
    651     (*deletion_task_count)++;
    652     quota_manager->DeleteOriginData(
    653         *origin, quota_storage_type,
    654         StoragePartitionImpl::GenerateQuotaClientMask(remove_mask),
    655         base::Bind(&OnQuotaManagedOriginDeleted,
    656                    origin->GetOrigin(), quota_storage_type,
    657                    deletion_task_count, callback));
    658   }
    659   (*deletion_task_count)--;
    660 
    661   CheckQuotaManagedDataDeletionStatus(deletion_task_count, callback);
    662 }
    663 
    664 void StoragePartitionImpl::DataDeletionHelper::IncrementTaskCountOnUI() {
    665   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    666   ++task_count;
    667 }
    668 
    669 void StoragePartitionImpl::DataDeletionHelper::DecrementTaskCountOnUI() {
    670   if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    671     BrowserThread::PostTask(
    672         BrowserThread::UI, FROM_HERE,
    673         base::Bind(&DataDeletionHelper::DecrementTaskCountOnUI,
    674                    base::Unretained(this)));
    675     return;
    676   }
    677   DCHECK_GT(task_count, 0);
    678   --task_count;
    679   if (!task_count) {
    680     callback.Run();
    681     delete this;
    682   }
    683 }
    684 
    685 void StoragePartitionImpl::DataDeletionHelper::ClearDataOnUIThread(
    686     const GURL& storage_origin,
    687     const OriginMatcherFunction& origin_matcher,
    688     const base::FilePath& path,
    689     net::URLRequestContextGetter* rq_context,
    690     DOMStorageContextWrapper* dom_storage_context,
    691     storage::QuotaManager* quota_manager,
    692     storage::SpecialStoragePolicy* special_storage_policy,
    693     WebRTCIdentityStore* webrtc_identity_store,
    694     const base::Time begin,
    695     const base::Time end) {
    696   DCHECK_NE(remove_mask, 0u);
    697   DCHECK(!callback.is_null());
    698 
    699   IncrementTaskCountOnUI();
    700   base::Closure decrement_callback = base::Bind(
    701       &DataDeletionHelper::DecrementTaskCountOnUI, base::Unretained(this));
    702 
    703   if (remove_mask & REMOVE_DATA_MASK_COOKIES) {
    704     // Handle the cookies.
    705     IncrementTaskCountOnUI();
    706     BrowserThread::PostTask(
    707         BrowserThread::IO, FROM_HERE,
    708         base::Bind(&ClearCookiesOnIOThread,
    709                    make_scoped_refptr(rq_context), begin, end, storage_origin,
    710                    decrement_callback));
    711   }
    712 
    713   if (remove_mask & REMOVE_DATA_MASK_INDEXEDDB ||
    714       remove_mask & REMOVE_DATA_MASK_WEBSQL ||
    715       remove_mask & REMOVE_DATA_MASK_APPCACHE ||
    716       remove_mask & REMOVE_DATA_MASK_FILE_SYSTEMS ||
    717       remove_mask & REMOVE_DATA_MASK_SERVICE_WORKERS) {
    718     IncrementTaskCountOnUI();
    719     BrowserThread::PostTask(
    720         BrowserThread::IO, FROM_HERE,
    721         base::Bind(&DataDeletionHelper::ClearQuotaManagedDataOnIOThread,
    722                    base::Unretained(this),
    723                    make_scoped_refptr(quota_manager),
    724                    begin,
    725                    storage_origin,
    726                    make_scoped_refptr(special_storage_policy),
    727                    origin_matcher,
    728                    decrement_callback));
    729   }
    730 
    731   if (remove_mask & REMOVE_DATA_MASK_LOCAL_STORAGE) {
    732     IncrementTaskCountOnUI();
    733     ClearLocalStorageOnUIThread(
    734         make_scoped_refptr(dom_storage_context),
    735         make_scoped_refptr(special_storage_policy),
    736         origin_matcher,
    737         storage_origin, begin, end,
    738         decrement_callback);
    739 
    740     // ClearDataImpl cannot clear session storage data when a particular origin
    741     // is specified. Therefore we ignore clearing session storage in this case.
    742     // TODO(lazyboy): Fix.
    743     if (storage_origin.is_empty()) {
    744       IncrementTaskCountOnUI();
    745       ClearSessionStorageOnUIThread(
    746           make_scoped_refptr(dom_storage_context),
    747           make_scoped_refptr(special_storage_policy),
    748           origin_matcher,
    749           decrement_callback);
    750     }
    751   }
    752 
    753   if (remove_mask & REMOVE_DATA_MASK_SHADER_CACHE) {
    754     IncrementTaskCountOnUI();
    755     BrowserThread::PostTask(
    756         BrowserThread::IO, FROM_HERE,
    757         base::Bind(&ClearShaderCacheOnIOThread,
    758                    path, begin, end, decrement_callback));
    759   }
    760 
    761   if (remove_mask & REMOVE_DATA_MASK_WEBRTC_IDENTITY) {
    762     IncrementTaskCountOnUI();
    763     BrowserThread::PostTask(
    764         BrowserThread::IO,
    765         FROM_HERE,
    766         base::Bind(&WebRTCIdentityStore::DeleteBetween,
    767                    webrtc_identity_store,
    768                    begin,
    769                    end,
    770                    decrement_callback));
    771   }
    772 
    773   DecrementTaskCountOnUI();
    774 }
    775 
    776 void StoragePartitionImpl::ClearDataForOrigin(
    777     uint32 remove_mask,
    778     uint32 quota_storage_remove_mask,
    779     const GURL& storage_origin,
    780     net::URLRequestContextGetter* request_context_getter,
    781     const base::Closure& callback) {
    782   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    783   ClearDataImpl(remove_mask,
    784                 quota_storage_remove_mask,
    785                 storage_origin,
    786                 OriginMatcherFunction(),
    787                 request_context_getter,
    788                 base::Time(),
    789                 base::Time::Max(),
    790                 callback);
    791 }
    792 
    793 void StoragePartitionImpl::ClearData(
    794     uint32 remove_mask,
    795     uint32 quota_storage_remove_mask,
    796     const GURL& storage_origin,
    797     const OriginMatcherFunction& origin_matcher,
    798     const base::Time begin,
    799     const base::Time end,
    800     const base::Closure& callback) {
    801   ClearDataImpl(remove_mask, quota_storage_remove_mask, storage_origin,
    802                 origin_matcher, GetURLRequestContext(), begin, end, callback);
    803 }
    804 
    805 WebRTCIdentityStore* StoragePartitionImpl::GetWebRTCIdentityStore() {
    806   return webrtc_identity_store_.get();
    807 }
    808 
    809 void StoragePartitionImpl::OverrideQuotaManagerForTesting(
    810     storage::QuotaManager* quota_manager) {
    811   quota_manager_ = quota_manager;
    812 }
    813 
    814 void StoragePartitionImpl::OverrideSpecialStoragePolicyForTesting(
    815     storage::SpecialStoragePolicy* special_storage_policy) {
    816   special_storage_policy_ = special_storage_policy;
    817 }
    818 
    819 void StoragePartitionImpl::SetURLRequestContext(
    820     net::URLRequestContextGetter* url_request_context) {
    821   url_request_context_ = url_request_context;
    822 }
    823 
    824 void StoragePartitionImpl::SetMediaURLRequestContext(
    825     net::URLRequestContextGetter* media_url_request_context) {
    826   media_url_request_context_ = media_url_request_context;
    827 }
    828 
    829 }  // namespace content
    830