Revert "Merged all Chromoting Host code into remoting_core.dll (Windows)."
[chromium-blink-merge.git] / webkit / quota / mock_storage_client.cc
blob32435c1c5f172a332f697f2685afa769a6ffefcf
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.
5 #include "webkit/quota/mock_storage_client.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/singleton.h"
11 #include "base/message_loop_proxy.h"
12 #include "base/stl_util.h"
13 #include "net/base/net_util.h"
14 #include "webkit/quota/quota_manager.h"
16 namespace quota {
18 using std::make_pair;
20 MockStorageClient::MockStorageClient(
21 QuotaManagerProxy* quota_manager_proxy,
22 const MockOriginData* mock_data, QuotaClient::ID id, size_t mock_data_size)
23 : quota_manager_proxy_(quota_manager_proxy),
24 id_(id),
25 mock_time_counter_(0),
26 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
27 Populate(mock_data, mock_data_size);
30 void MockStorageClient::Populate(
31 const MockOriginData* mock_data,
32 size_t mock_data_size) {
33 for (size_t i = 0; i < mock_data_size; ++i) {
34 origin_data_[make_pair(GURL(mock_data[i].origin), mock_data[i].type)] =
35 mock_data[i].usage;
39 MockStorageClient::~MockStorageClient() {}
41 void MockStorageClient::AddOriginAndNotify(
42 const GURL& origin_url, StorageType type, int64 size) {
43 DCHECK(origin_data_.find(make_pair(origin_url, type)) == origin_data_.end());
44 DCHECK_GE(size, 0);
45 origin_data_[make_pair(origin_url, type)] = size;
46 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
47 id(), origin_url, type, size, IncrementMockTime());
50 void MockStorageClient::ModifyOriginAndNotify(
51 const GURL& origin_url, StorageType type, int64 delta) {
52 OriginDataMap::iterator find = origin_data_.find(make_pair(origin_url, type));
53 DCHECK(find != origin_data_.end());
54 find->second += delta;
55 DCHECK_GE(find->second, 0);
57 // TODO(tzik): Check quota to prevent usage exceed
58 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
59 id(), origin_url, type, delta, IncrementMockTime());
62 void MockStorageClient::TouchAllOriginsAndNotify() {
63 for (OriginDataMap::const_iterator itr = origin_data_.begin();
64 itr != origin_data_.end();
65 ++itr) {
66 quota_manager_proxy_->quota_manager()->NotifyStorageModifiedInternal(
67 id(), itr->first.first, itr->first.second, 0, IncrementMockTime());
71 void MockStorageClient::AddOriginToErrorSet(
72 const GURL& origin_url, StorageType type) {
73 error_origins_.insert(make_pair(origin_url, type));
76 base::Time MockStorageClient::IncrementMockTime() {
77 ++mock_time_counter_;
78 return base::Time::FromDoubleT(mock_time_counter_ * 10.0);
81 QuotaClient::ID MockStorageClient::id() const {
82 return id_;
85 void MockStorageClient::OnQuotaManagerDestroyed() {
86 delete this;
89 void MockStorageClient::GetOriginUsage(const GURL& origin_url,
90 StorageType type,
91 const GetUsageCallback& callback) {
92 base::MessageLoopProxy::current()->PostTask(
93 FROM_HERE,
94 base::Bind(&MockStorageClient::RunGetOriginUsage,
95 weak_factory_.GetWeakPtr(), origin_url, type, callback));
98 void MockStorageClient::GetOriginsForType(
99 StorageType type, const GetOriginsCallback& callback) {
100 base::MessageLoopProxy::current()->PostTask(
101 FROM_HERE,
102 base::Bind(&MockStorageClient::RunGetOriginsForType,
103 weak_factory_.GetWeakPtr(), type, callback));
106 void MockStorageClient::GetOriginsForHost(
107 StorageType type, const std::string& host,
108 const GetOriginsCallback& callback) {
109 base::MessageLoopProxy::current()->PostTask(
110 FROM_HERE,
111 base::Bind(&MockStorageClient::RunGetOriginsForHost,
112 weak_factory_.GetWeakPtr(), type, host, callback));
115 void MockStorageClient::DeleteOriginData(
116 const GURL& origin, StorageType type,
117 const DeletionCallback& callback) {
118 base::MessageLoopProxy::current()->PostTask(
119 FROM_HERE,
120 base::Bind(&MockStorageClient::RunDeleteOriginData,
121 weak_factory_.GetWeakPtr(), origin, type, callback));
124 void MockStorageClient::RunGetOriginUsage(
125 const GURL& origin_url, StorageType type,
126 const GetUsageCallback& callback) {
127 OriginDataMap::iterator find = origin_data_.find(make_pair(origin_url, type));
128 if (find == origin_data_.end()) {
129 callback.Run(0);
130 } else {
131 callback.Run(find->second);
135 void MockStorageClient::RunGetOriginsForType(
136 StorageType type, const GetOriginsCallback& callback) {
137 std::set<GURL> origins;
138 for (OriginDataMap::iterator iter = origin_data_.begin();
139 iter != origin_data_.end(); ++iter) {
140 if (type == iter->first.second)
141 origins.insert(iter->first.first);
143 callback.Run(origins, type);
146 void MockStorageClient::RunGetOriginsForHost(
147 StorageType type, const std::string& host,
148 const GetOriginsCallback& callback) {
149 std::set<GURL> origins;
150 for (OriginDataMap::iterator iter = origin_data_.begin();
151 iter != origin_data_.end(); ++iter) {
152 std::string host_or_spec = net::GetHostOrSpecFromURL(iter->first.first);
153 if (type == iter->first.second && host == host_or_spec)
154 origins.insert(iter->first.first);
156 callback.Run(origins, type);
159 void MockStorageClient::RunDeleteOriginData(
160 const GURL& origin_url,
161 StorageType type,
162 const DeletionCallback& callback) {
163 ErrorOriginSet::iterator itr_error =
164 error_origins_.find(make_pair(origin_url, type));
165 if (itr_error != error_origins_.end()) {
166 callback.Run(kQuotaErrorInvalidModification);
167 return;
170 OriginDataMap::iterator itr =
171 origin_data_.find(make_pair(origin_url, type));
172 if (itr != origin_data_.end()) {
173 int64 delta = itr->second;
174 quota_manager_proxy_->
175 NotifyStorageModified(id(), origin_url, type, -delta);
176 origin_data_.erase(itr);
179 callback.Run(kQuotaStatusOk);
182 } // namespace quota