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