Home | History | Annotate | Download | only in quota
      1 // Copyright 2014 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/quota/mock_quota_manager.h"
      6 
      7 #include "base/memory/ref_counted.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/single_thread_task_runner.h"
     11 #include "url/gurl.h"
     12 
     13 using quota::kQuotaStatusOk;
     14 
     15 namespace content {
     16 
     17 MockQuotaManager::OriginInfo::OriginInfo(
     18     const GURL& origin,
     19     StorageType type,
     20     int quota_client_mask,
     21     base::Time modified)
     22     : origin(origin),
     23       type(type),
     24       quota_client_mask(quota_client_mask),
     25       modified(modified) {
     26 }
     27 
     28 MockQuotaManager::OriginInfo::~OriginInfo() {}
     29 
     30 MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max) {}
     31 MockQuotaManager::StorageInfo::~StorageInfo() {}
     32 
     33 MockQuotaManager::MockQuotaManager(
     34     bool is_incognito,
     35     const base::FilePath& profile_path,
     36     base::SingleThreadTaskRunner* io_thread,
     37     base::SequencedTaskRunner* db_thread,
     38     SpecialStoragePolicy* special_storage_policy)
     39     : QuotaManager(is_incognito, profile_path, io_thread, db_thread,
     40         special_storage_policy),
     41       weak_factory_(this) {
     42 }
     43 
     44 void MockQuotaManager::GetUsageAndQuota(
     45     const GURL& origin,
     46     quota::StorageType type,
     47     const GetUsageAndQuotaCallback& callback) {
     48   StorageInfo& info = usage_and_quota_map_[std::make_pair(origin, type)];
     49   callback.Run(quota::kQuotaStatusOk, info.usage, info.quota);
     50 }
     51 
     52 void MockQuotaManager::SetQuota(const GURL& origin, StorageType type,
     53                                 int64 quota) {
     54   usage_and_quota_map_[std::make_pair(origin, type)].quota = quota;
     55 }
     56 
     57 bool MockQuotaManager::AddOrigin(
     58     const GURL& origin,
     59     StorageType type,
     60     int quota_client_mask,
     61     base::Time modified) {
     62   origins_.push_back(OriginInfo(origin, type, quota_client_mask, modified));
     63   return true;
     64 }
     65 
     66 bool MockQuotaManager::OriginHasData(
     67     const GURL& origin,
     68     StorageType type,
     69     QuotaClient::ID quota_client) const {
     70   for (std::vector<OriginInfo>::const_iterator current = origins_.begin();
     71        current != origins_.end();
     72        ++current) {
     73     if (current->origin == origin &&
     74         current->type == type &&
     75         current->quota_client_mask & quota_client)
     76       return true;
     77   }
     78   return false;
     79 }
     80 
     81 void MockQuotaManager::GetOriginsModifiedSince(
     82     StorageType type,
     83     base::Time modified_since,
     84     const GetOriginsCallback& callback) {
     85   std::set<GURL>* origins_to_return = new std::set<GURL>();
     86   for (std::vector<OriginInfo>::const_iterator current = origins_.begin();
     87        current != origins_.end();
     88        ++current) {
     89     if (current->type == type && current->modified >= modified_since)
     90       origins_to_return->insert(current->origin);
     91   }
     92 
     93   base::MessageLoop::current()->PostTask(
     94       FROM_HERE,
     95       base::Bind(&MockQuotaManager::DidGetModifiedSince,
     96                  weak_factory_.GetWeakPtr(),
     97                  callback,
     98                  base::Owned(origins_to_return),
     99                  type));
    100 }
    101 
    102 void MockQuotaManager::DeleteOriginData(
    103     const GURL& origin,
    104     StorageType type,
    105     int quota_client_mask,
    106     const StatusCallback& callback) {
    107   for (std::vector<OriginInfo>::iterator current = origins_.begin();
    108        current != origins_.end();
    109        ++current) {
    110     if (current->origin == origin && current->type == type) {
    111       // Modify the mask: if it's 0 after "deletion", remove the origin.
    112       current->quota_client_mask &= ~quota_client_mask;
    113       if (current->quota_client_mask == 0)
    114         origins_.erase(current);
    115       break;
    116     }
    117   }
    118 
    119   base::MessageLoop::current()->PostTask(
    120       FROM_HERE,
    121       base::Bind(&MockQuotaManager::DidDeleteOriginData,
    122                  weak_factory_.GetWeakPtr(),
    123                  callback,
    124                  kQuotaStatusOk));
    125 }
    126 
    127 MockQuotaManager::~MockQuotaManager() {}
    128 
    129 void MockQuotaManager::UpdateUsage(
    130     const GURL& origin, StorageType type, int64 delta) {
    131   usage_and_quota_map_[std::make_pair(origin, type)].usage += delta;
    132 }
    133 
    134 void MockQuotaManager::DidGetModifiedSince(
    135     const GetOriginsCallback& callback,
    136     std::set<GURL>* origins,
    137     StorageType storage_type) {
    138   callback.Run(*origins, storage_type);
    139 }
    140 
    141 void MockQuotaManager::DidDeleteOriginData(
    142     const StatusCallback& callback,
    143     QuotaStatusCode status) {
    144   callback.Run(status);
    145 }
    146 
    147 }  // namespace content
    148