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/location.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
14 using storage::kQuotaStatusOk
;
18 MockQuotaManager::OriginInfo::OriginInfo(
21 int quota_client_mask
,
25 quota_client_mask(quota_client_mask
),
29 MockQuotaManager::OriginInfo::~OriginInfo() {}
31 MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max
) {}
32 MockQuotaManager::StorageInfo::~StorageInfo() {}
34 MockQuotaManager::MockQuotaManager(
36 const base::FilePath
& profile_path
,
37 const scoped_refptr
<base::SingleThreadTaskRunner
>& io_thread
,
38 const scoped_refptr
<base::SequencedTaskRunner
>& db_thread
,
39 const scoped_refptr
<SpecialStoragePolicy
>& special_storage_policy
)
40 : QuotaManager(is_incognito
,
44 special_storage_policy
),
48 void MockQuotaManager::GetUsageAndQuota(
50 storage::StorageType type
,
51 const GetUsageAndQuotaCallback
& callback
) {
52 StorageInfo
& info
= usage_and_quota_map_
[std::make_pair(origin
, type
)];
53 callback
.Run(storage::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::ThreadTaskRunnerHandle::Get()->PostTask(
98 FROM_HERE
, base::Bind(&MockQuotaManager::DidGetModifiedSince
,
99 weak_factory_
.GetWeakPtr(), callback
,
100 base::Owned(origins_to_return
), type
));
103 void MockQuotaManager::DeleteOriginData(
106 int quota_client_mask
,
107 const StatusCallback
& callback
) {
108 for (std::vector
<OriginInfo
>::iterator current
= origins_
.begin();
109 current
!= origins_
.end();
111 if (current
->origin
== origin
&& current
->type
== type
) {
112 // Modify the mask: if it's 0 after "deletion", remove the origin.
113 current
->quota_client_mask
&= ~quota_client_mask
;
114 if (current
->quota_client_mask
== 0)
115 origins_
.erase(current
);
120 base::ThreadTaskRunnerHandle::Get()->PostTask(
122 base::Bind(&MockQuotaManager::DidDeleteOriginData
,
123 weak_factory_
.GetWeakPtr(), callback
, kQuotaStatusOk
));
126 MockQuotaManager::~MockQuotaManager() {}
128 void MockQuotaManager::UpdateUsage(
129 const GURL
& origin
, StorageType type
, int64 delta
) {
130 usage_and_quota_map_
[std::make_pair(origin
, type
)].usage
+= delta
;
133 void MockQuotaManager::DidGetModifiedSince(
134 const GetOriginsCallback
& callback
,
135 std::set
<GURL
>* origins
,
136 StorageType storage_type
) {
137 callback
.Run(*origins
, storage_type
);
140 void MockQuotaManager::DidDeleteOriginData(
141 const StatusCallback
& callback
,
142 QuotaStatusCode status
) {
143 callback
.Run(status
);
146 } // namespace content