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 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_thread
,
37 const scoped_refptr
<base::SequencedTaskRunner
>& db_thread
,
38 const scoped_refptr
<SpecialStoragePolicy
>& special_storage_policy
)
39 : QuotaManager(is_incognito
,
43 special_storage_policy
),
47 void MockQuotaManager::GetUsageAndQuota(
49 storage::StorageType type
,
50 const GetUsageAndQuotaCallback
& callback
) {
51 StorageInfo
& info
= usage_and_quota_map_
[std::make_pair(origin
, type
)];
52 callback
.Run(storage::kQuotaStatusOk
, info
.usage
, info
.quota
);
55 void MockQuotaManager::SetQuota(const GURL
& origin
, StorageType type
,
57 usage_and_quota_map_
[std::make_pair(origin
, type
)].quota
= quota
;
60 bool MockQuotaManager::AddOrigin(
63 int quota_client_mask
,
64 base::Time modified
) {
65 origins_
.push_back(OriginInfo(origin
, type
, quota_client_mask
, modified
));
69 bool MockQuotaManager::OriginHasData(
72 QuotaClient::ID quota_client
) const {
73 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
74 current
!= origins_
.end();
76 if (current
->origin
== origin
&&
77 current
->type
== type
&&
78 current
->quota_client_mask
& quota_client
)
84 void MockQuotaManager::GetOriginsModifiedSince(
86 base::Time modified_since
,
87 const GetOriginsCallback
& callback
) {
88 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
89 for (std::vector
<OriginInfo
>::const_iterator current
= origins_
.begin();
90 current
!= origins_
.end();
92 if (current
->type
== type
&& current
->modified
>= modified_since
)
93 origins_to_return
->insert(current
->origin
);
96 base::MessageLoop::current()->PostTask(
98 base::Bind(&MockQuotaManager::DidGetModifiedSince
,
99 weak_factory_
.GetWeakPtr(),
101 base::Owned(origins_to_return
),
105 void MockQuotaManager::DeleteOriginData(
108 int quota_client_mask
,
109 const StatusCallback
& callback
) {
110 for (std::vector
<OriginInfo
>::iterator current
= origins_
.begin();
111 current
!= origins_
.end();
113 if (current
->origin
== origin
&& current
->type
== type
) {
114 // Modify the mask: if it's 0 after "deletion", remove the origin.
115 current
->quota_client_mask
&= ~quota_client_mask
;
116 if (current
->quota_client_mask
== 0)
117 origins_
.erase(current
);
122 base::MessageLoop::current()->PostTask(
124 base::Bind(&MockQuotaManager::DidDeleteOriginData
,
125 weak_factory_
.GetWeakPtr(),
130 MockQuotaManager::~MockQuotaManager() {}
132 void MockQuotaManager::UpdateUsage(
133 const GURL
& origin
, StorageType type
, int64 delta
) {
134 usage_and_quota_map_
[std::make_pair(origin
, type
)].usage
+= delta
;
137 void MockQuotaManager::DidGetModifiedSince(
138 const GetOriginsCallback
& callback
,
139 std::set
<GURL
>* origins
,
140 StorageType storage_type
) {
141 callback
.Run(*origins
, storage_type
);
144 void MockQuotaManager::DidDeleteOriginData(
145 const StatusCallback
& callback
,
146 QuotaStatusCode status
) {
147 callback
.Run(status
);
150 } // namespace content