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