Content settings: remove some plugin-related code/resources when... there are no...
[chromium-blink-merge.git] / content / public / test / mock_storage_client.cc
blob8370e6c217920b76f826890842ffde3646f212df
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.
5 #include "content/public/test/mock_storage_client.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/location.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "net/base/net_util.h"
16 #include "storage/browser/quota/quota_manager_proxy.h"
18 using storage::kQuotaErrorInvalidModification;
19 using storage::kQuotaStatusOk;
21 namespace content {
23 using std::make_pair;
25 MockStorageClient::MockStorageClient(
26 QuotaManagerProxy* quota_manager_proxy,
27 const MockOriginData* mock_data, QuotaClient::ID id, size_t mock_data_size)
28 : quota_manager_proxy_(quota_manager_proxy),
29 id_(id),
30 mock_time_counter_(0),
31 weak_factory_(this) {
32 Populate(mock_data, mock_data_size);
35 void MockStorageClient::Populate(
36 const MockOriginData* mock_data,
37 size_t mock_data_size) {
38 for (size_t i = 0; i < mock_data_size; ++i) {
39 origin_data_[make_pair(GURL(mock_data[i].origin), mock_data[i].type)] =
40 mock_data[i].usage;
44 MockStorageClient::~MockStorageClient() {}
46 void MockStorageClient::AddOriginAndNotify(
47 const GURL& origin_url, StorageType type, int64 size) {
48 DCHECK(origin_data_.find(make_pair(origin_url, type)) == origin_data_.end());
49 DCHECK_GE(size, 0);
50 origin_data_[make_pair(origin_url, type)] = size;
51 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
52 id(), origin_url, type, size, IncrementMockTime());
55 void MockStorageClient::ModifyOriginAndNotify(
56 const GURL& origin_url, StorageType type, int64 delta) {
57 OriginDataMap::iterator find = origin_data_.find(make_pair(origin_url, type));
58 DCHECK(find != origin_data_.end());
59 find->second += delta;
60 DCHECK_GE(find->second, 0);
62 // TODO(tzik): Check quota to prevent usage exceed
63 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
64 id(), origin_url, type, delta, IncrementMockTime());
67 void MockStorageClient::TouchAllOriginsAndNotify() {
68 for (OriginDataMap::const_iterator itr = origin_data_.begin();
69 itr != origin_data_.end();
70 ++itr) {
71 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
72 id(), itr->first.first, itr->first.second, 0, IncrementMockTime());
76 void MockStorageClient::AddOriginToErrorSet(
77 const GURL& origin_url, StorageType type) {
78 error_origins_.insert(make_pair(origin_url, type));
81 base::Time MockStorageClient::IncrementMockTime() {
82 ++mock_time_counter_;
83 return base::Time::FromDoubleT(mock_time_counter_ * 10.0);
86 QuotaClient::ID MockStorageClient::id() const {
87 return id_;
90 void MockStorageClient::OnQuotaManagerDestroyed() {
91 delete this;
94 void MockStorageClient::GetOriginUsage(const GURL& origin_url,
95 StorageType type,
96 const GetUsageCallback& callback) {
97 base::ThreadTaskRunnerHandle::Get()->PostTask(
98 FROM_HERE,
99 base::Bind(&MockStorageClient::RunGetOriginUsage,
100 weak_factory_.GetWeakPtr(), origin_url, type, callback));
103 void MockStorageClient::GetOriginsForType(
104 StorageType type, const GetOriginsCallback& callback) {
105 base::ThreadTaskRunnerHandle::Get()->PostTask(
106 FROM_HERE, base::Bind(&MockStorageClient::RunGetOriginsForType,
107 weak_factory_.GetWeakPtr(), type, callback));
110 void MockStorageClient::GetOriginsForHost(
111 StorageType type, const std::string& host,
112 const GetOriginsCallback& callback) {
113 base::ThreadTaskRunnerHandle::Get()->PostTask(
114 FROM_HERE, base::Bind(&MockStorageClient::RunGetOriginsForHost,
115 weak_factory_.GetWeakPtr(), type, host, callback));
118 void MockStorageClient::DeleteOriginData(
119 const GURL& origin, StorageType type,
120 const DeletionCallback& callback) {
121 base::ThreadTaskRunnerHandle::Get()->PostTask(
122 FROM_HERE,
123 base::Bind(&MockStorageClient::RunDeleteOriginData,
124 weak_factory_.GetWeakPtr(), origin, type, callback));
127 bool MockStorageClient::DoesSupport(storage::StorageType type) const {
128 return true;
131 void MockStorageClient::RunGetOriginUsage(
132 const GURL& origin_url, StorageType type,
133 const GetUsageCallback& callback) {
134 OriginDataMap::iterator find = origin_data_.find(make_pair(origin_url, type));
135 if (find == origin_data_.end()) {
136 callback.Run(0);
137 } else {
138 callback.Run(find->second);
142 void MockStorageClient::RunGetOriginsForType(
143 StorageType type, const GetOriginsCallback& callback) {
144 std::set<GURL> origins;
145 for (OriginDataMap::iterator iter = origin_data_.begin();
146 iter != origin_data_.end(); ++iter) {
147 if (type == iter->first.second)
148 origins.insert(iter->first.first);
150 callback.Run(origins);
153 void MockStorageClient::RunGetOriginsForHost(
154 StorageType type, const std::string& host,
155 const GetOriginsCallback& callback) {
156 std::set<GURL> origins;
157 for (OriginDataMap::iterator iter = origin_data_.begin();
158 iter != origin_data_.end(); ++iter) {
159 std::string host_or_spec = net::GetHostOrSpecFromURL(iter->first.first);
160 if (type == iter->first.second && host == host_or_spec)
161 origins.insert(iter->first.first);
163 callback.Run(origins);
166 void MockStorageClient::RunDeleteOriginData(
167 const GURL& origin_url,
168 StorageType type,
169 const DeletionCallback& callback) {
170 ErrorOriginSet::iterator itr_error =
171 error_origins_.find(make_pair(origin_url, type));
172 if (itr_error != error_origins_.end()) {
173 callback.Run(kQuotaErrorInvalidModification);
174 return;
177 OriginDataMap::iterator itr =
178 origin_data_.find(make_pair(origin_url, type));
179 if (itr != origin_data_.end()) {
180 int64 delta = itr->second;
181 quota_manager_proxy_->
182 NotifyStorageModified(id(), origin_url, type, -delta);
183 origin_data_.erase(itr);
186 callback.Run(kQuotaStatusOk);
189 } // namespace content