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_quota_manager.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "googleurl/src/gurl.h"
19 MockQuotaManager::OriginInfo::OriginInfo(
22 int quota_client_mask
,
26 quota_client_mask(quota_client_mask
),
30 MockQuotaManager::OriginInfo::~OriginInfo() {}
32 MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max
) {}
33 MockQuotaManager::StorageInfo::~StorageInfo() {}
35 // MockQuotaManager ----------------------------------------------------------
37 MockQuotaManager::MockQuotaManager(
39 const base::FilePath
& profile_path
,
40 base::SingleThreadTaskRunner
* io_thread
,
41 base::SequencedTaskRunner
* db_thread
,
42 SpecialStoragePolicy
* special_storage_policy
)
43 : QuotaManager(is_incognito
, profile_path
, io_thread
, db_thread
,
44 special_storage_policy
),
48 void MockQuotaManager::GetUsageAndQuota(
50 quota::StorageType type
,
51 const GetUsageAndQuotaCallback
& callback
) {
52 StorageInfo
& info
= usage_and_quota_map_
[std::make_pair(origin
, type
)];
53 callback
.Run(quota::kQuotaStatusOk
, info
.usage
, info
.quota
);
56 void MockQuotaManager::SetQuota(const GURL
& origin
, StorageType type
,
58 usage_and_quota_map_
[std::make_pair(origin
, type
)].quota
= quota
;
61 bool MockQuotaManager::AddOrigin(
64 int quota_client_mask
,
65 base::Time modified
) {
66 origins_
.push_back(OriginInfo(origin
, type
, quota_client_mask
, modified
));
70 bool MockQuotaManager::OriginHasData(
73 QuotaClient::ID quota_client
) const {
74 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
75 current
!= origins_
.end();
77 if (current
->origin
== origin
&&
78 current
->type
== type
&&
79 current
->quota_client_mask
& quota_client
)
85 void MockQuotaManager::GetOriginsModifiedSince(
87 base::Time modified_since
,
88 const GetOriginsCallback
& callback
) {
89 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
90 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
91 current
!= origins_
.end();
93 if (current
->type
== type
&& current
->modified
>= modified_since
)
94 origins_to_return
->insert(current
->origin
);
97 base::MessageLoop::current()->PostTask(
99 base::Bind(&MockQuotaManager::DidGetModifiedSince
,
100 weak_factory_
.GetWeakPtr(),
102 base::Owned(origins_to_return
),
106 void MockQuotaManager::DeleteOriginData(
109 int quota_client_mask
,
110 const StatusCallback
& callback
) {
111 for (std::vector
<OriginInfo
>::iterator current
= origins_
.begin();
112 current
!= origins_
.end();
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
);
123 base::MessageLoop::current()->PostTask(
125 base::Bind(&MockQuotaManager::DidDeleteOriginData
,
126 weak_factory_
.GetWeakPtr(),
131 MockQuotaManager::~MockQuotaManager() {}
133 void MockQuotaManager::UpdateUsage(
134 const GURL
& origin
, StorageType type
, int64 delta
) {
135 usage_and_quota_map_
[std::make_pair(origin
, type
)].usage
+= delta
;
138 void MockQuotaManager::DidGetModifiedSince(
139 const GetOriginsCallback
& callback
,
140 std::set
<GURL
>* origins
,
141 StorageType storage_type
) {
142 callback
.Run(*origins
, storage_type
);
145 void MockQuotaManager::DidDeleteOriginData(
146 const StatusCallback
& callback
,
147 QuotaStatusCode status
) {
148 callback
.Run(status
);
151 // MockQuotaManagerProxy -----------------------------------------------------
153 MockQuotaManagerProxy::MockQuotaManagerProxy(
154 MockQuotaManager
* quota_manager
,
155 base::SingleThreadTaskRunner
* task_runner
)
156 : QuotaManagerProxy(quota_manager
, task_runner
),
157 storage_accessed_count_(0),
158 storage_modified_count_(0),
159 last_notified_type_(kStorageTypeUnknown
),
160 last_notified_delta_(0),
161 registered_client_(NULL
) {}
163 void MockQuotaManagerProxy::RegisterClient(QuotaClient
* client
) {
164 DCHECK(!registered_client_
);
165 registered_client_
= client
;
168 void MockQuotaManagerProxy::SimulateQuotaManagerDestroyed() {
169 if (registered_client_
) {
170 // We cannot call this in the destructor as the client (indirectly)
171 // holds a refptr of the proxy.
172 registered_client_
->OnQuotaManagerDestroyed();
173 registered_client_
= NULL
;
177 void MockQuotaManagerProxy::NotifyStorageAccessed(
178 QuotaClient::ID client_id
, const GURL
& origin
, StorageType type
) {
179 ++storage_accessed_count_
;
180 last_notified_origin_
= origin
;
181 last_notified_type_
= type
;
184 void MockQuotaManagerProxy::NotifyStorageModified(
185 QuotaClient::ID client_id
, const GURL
& origin
,
186 StorageType type
, int64 delta
) {
187 ++storage_modified_count_
;
188 last_notified_origin_
= origin
;
189 last_notified_type_
= type
;
190 last_notified_delta_
= delta
;
192 mock_manager()->UpdateUsage(origin
, type
, delta
);
195 MockQuotaManagerProxy::~MockQuotaManagerProxy() {
196 DCHECK(!registered_client_
);