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/browser/quota/mock_quota_manager.h"
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"
13 using storage::kQuotaStatusOk
;
17 MockQuotaManager::OriginInfo::OriginInfo(
20 int quota_client_mask
,
24 quota_client_mask(quota_client_mask
),
28 MockQuotaManager::OriginInfo::~OriginInfo() {}
30 MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max
) {}
31 MockQuotaManager::StorageInfo::~StorageInfo() {}
33 MockQuotaManager::MockQuotaManager(
35 const base::FilePath
& profile_path
,
36 base::SingleThreadTaskRunner
* io_thread
,
37 base::SequencedTaskRunner
* db_thread
,
38 SpecialStoragePolicy
* special_storage_policy
)
39 : QuotaManager(is_incognito
, profile_path
, io_thread
, db_thread
,
40 special_storage_policy
),
44 void MockQuotaManager::GetUsageAndQuota(
46 storage::StorageType type
,
47 const GetUsageAndQuotaCallback
& callback
) {
48 StorageInfo
& info
= usage_and_quota_map_
[std::make_pair(origin
, type
)];
49 callback
.Run(storage::kQuotaStatusOk
, info
.usage
, info
.quota
);
52 void MockQuotaManager::SetQuota(const GURL
& origin
, StorageType type
,
54 usage_and_quota_map_
[std::make_pair(origin
, type
)].quota
= quota
;
57 bool MockQuotaManager::AddOrigin(
60 int quota_client_mask
,
61 base::Time modified
) {
62 origins_
.push_back(OriginInfo(origin
, type
, quota_client_mask
, modified
));
66 bool MockQuotaManager::OriginHasData(
69 QuotaClient::ID quota_client
) const {
70 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
71 current
!= origins_
.end();
73 if (current
->origin
== origin
&&
74 current
->type
== type
&&
75 current
->quota_client_mask
& quota_client
)
81 void MockQuotaManager::GetOriginsModifiedSince(
83 base::Time modified_since
,
84 const GetOriginsCallback
& callback
) {
85 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
86 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
87 current
!= origins_
.end();
89 if (current
->type
== type
&& current
->modified
>= modified_since
)
90 origins_to_return
->insert(current
->origin
);
93 base::MessageLoop::current()->PostTask(
95 base::Bind(&MockQuotaManager::DidGetModifiedSince
,
96 weak_factory_
.GetWeakPtr(),
98 base::Owned(origins_to_return
),
102 void MockQuotaManager::DeleteOriginData(
105 int quota_client_mask
,
106 const StatusCallback
& callback
) {
107 for (std::vector
<OriginInfo
>::iterator current
= origins_
.begin();
108 current
!= origins_
.end();
110 if (current
->origin
== origin
&& current
->type
== type
) {
111 // Modify the mask: if it's 0 after "deletion", remove the origin.
112 current
->quota_client_mask
&= ~quota_client_mask
;
113 if (current
->quota_client_mask
== 0)
114 origins_
.erase(current
);
119 base::MessageLoop::current()->PostTask(
121 base::Bind(&MockQuotaManager::DidDeleteOriginData
,
122 weak_factory_
.GetWeakPtr(),
127 MockQuotaManager::~MockQuotaManager() {}
129 void MockQuotaManager::UpdateUsage(
130 const GURL
& origin
, StorageType type
, int64 delta
) {
131 usage_and_quota_map_
[std::make_pair(origin
, type
)].usage
+= delta
;
134 void MockQuotaManager::DidGetModifiedSince(
135 const GetOriginsCallback
& callback
,
136 std::set
<GURL
>* origins
,
137 StorageType storage_type
) {
138 callback
.Run(*origins
, storage_type
);
141 void MockQuotaManager::DidDeleteOriginData(
142 const StatusCallback
& callback
,
143 QuotaStatusCode status
) {
144 callback
.Run(status
);
147 } // namespace content