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/public/test/mock_storage_client.h"
7 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/singleton.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "base/stl_util.h"
13 #include "net/base/net_util.h"
14 #include "webkit/browser/quota/quota_manager_proxy.h"
16 using quota::kQuotaErrorInvalidModification
;
17 using quota::kQuotaStatusOk
;
23 MockStorageClient::MockStorageClient(
24 QuotaManagerProxy
* quota_manager_proxy
,
25 const MockOriginData
* mock_data
, QuotaClient::ID id
, size_t mock_data_size
)
26 : quota_manager_proxy_(quota_manager_proxy
),
28 mock_time_counter_(0),
30 Populate(mock_data
, mock_data_size
);
33 void MockStorageClient::Populate(
34 const MockOriginData
* mock_data
,
35 size_t mock_data_size
) {
36 for (size_t i
= 0; i
< mock_data_size
; ++i
) {
37 origin_data_
[make_pair(GURL(mock_data
[i
].origin
), mock_data
[i
].type
)] =
42 MockStorageClient::~MockStorageClient() {}
44 void MockStorageClient::AddOriginAndNotify(
45 const GURL
& origin_url
, StorageType type
, int64 size
) {
46 DCHECK(origin_data_
.find(make_pair(origin_url
, type
)) == origin_data_
.end());
48 origin_data_
[make_pair(origin_url
, type
)] = size
;
49 quota_manager_proxy_
->quota_manager()->NotifyStorageModifiedInternal(
50 id(), origin_url
, type
, size
, IncrementMockTime());
53 void MockStorageClient::ModifyOriginAndNotify(
54 const GURL
& origin_url
, StorageType type
, int64 delta
) {
55 OriginDataMap::iterator find
= origin_data_
.find(make_pair(origin_url
, type
));
56 DCHECK(find
!= origin_data_
.end());
57 find
->second
+= delta
;
58 DCHECK_GE(find
->second
, 0);
60 // TODO(tzik): Check quota to prevent usage exceed
61 quota_manager_proxy_
->quota_manager()->NotifyStorageModifiedInternal(
62 id(), origin_url
, type
, delta
, IncrementMockTime());
65 void MockStorageClient::TouchAllOriginsAndNotify() {
66 for (OriginDataMap::const_iterator itr
= origin_data_
.begin();
67 itr
!= origin_data_
.end();
69 quota_manager_proxy_
->quota_manager()->NotifyStorageModifiedInternal(
70 id(), itr
->first
.first
, itr
->first
.second
, 0, IncrementMockTime());
74 void MockStorageClient::AddOriginToErrorSet(
75 const GURL
& origin_url
, StorageType type
) {
76 error_origins_
.insert(make_pair(origin_url
, type
));
79 base::Time
MockStorageClient::IncrementMockTime() {
81 return base::Time::FromDoubleT(mock_time_counter_
* 10.0);
84 QuotaClient::ID
MockStorageClient::id() const {
88 void MockStorageClient::OnQuotaManagerDestroyed() {
92 void MockStorageClient::GetOriginUsage(const GURL
& origin_url
,
94 const GetUsageCallback
& callback
) {
95 base::MessageLoopProxy::current()->PostTask(
97 base::Bind(&MockStorageClient::RunGetOriginUsage
,
98 weak_factory_
.GetWeakPtr(), origin_url
, type
, callback
));
101 void MockStorageClient::GetOriginsForType(
102 StorageType type
, const GetOriginsCallback
& callback
) {
103 base::MessageLoopProxy::current()->PostTask(
105 base::Bind(&MockStorageClient::RunGetOriginsForType
,
106 weak_factory_
.GetWeakPtr(), type
, callback
));
109 void MockStorageClient::GetOriginsForHost(
110 StorageType type
, const std::string
& host
,
111 const GetOriginsCallback
& callback
) {
112 base::MessageLoopProxy::current()->PostTask(
114 base::Bind(&MockStorageClient::RunGetOriginsForHost
,
115 weak_factory_
.GetWeakPtr(), type
, host
, callback
));
118 void MockStorageClient::DeleteOriginData(
119 const GURL
& origin
, StorageType type
,
120 const DeletionCallback
& callback
) {
121 base::MessageLoopProxy::current()->PostTask(
123 base::Bind(&MockStorageClient::RunDeleteOriginData
,
124 weak_factory_
.GetWeakPtr(), origin
, type
, callback
));
127 bool MockStorageClient::DoesSupport(quota::StorageType type
) const {
131 void MockStorageClient::RunGetOriginUsage(
132 const GURL
& origin_url
, StorageType type
,
133 const GetUsageCallback
& callback
) {
134 OriginDataMap::iterator find
= origin_data_
.find(make_pair(origin_url
, type
));
135 if (find
== origin_data_
.end()) {
138 callback
.Run(find
->second
);
142 void MockStorageClient::RunGetOriginsForType(
143 StorageType type
, const GetOriginsCallback
& callback
) {
144 std::set
<GURL
> origins
;
145 for (OriginDataMap::iterator iter
= origin_data_
.begin();
146 iter
!= origin_data_
.end(); ++iter
) {
147 if (type
== iter
->first
.second
)
148 origins
.insert(iter
->first
.first
);
150 callback
.Run(origins
);
153 void MockStorageClient::RunGetOriginsForHost(
154 StorageType type
, const std::string
& host
,
155 const GetOriginsCallback
& callback
) {
156 std::set
<GURL
> origins
;
157 for (OriginDataMap::iterator iter
= origin_data_
.begin();
158 iter
!= origin_data_
.end(); ++iter
) {
159 std::string host_or_spec
= net::GetHostOrSpecFromURL(iter
->first
.first
);
160 if (type
== iter
->first
.second
&& host
== host_or_spec
)
161 origins
.insert(iter
->first
.first
);
163 callback
.Run(origins
);
166 void MockStorageClient::RunDeleteOriginData(
167 const GURL
& origin_url
,
169 const DeletionCallback
& callback
) {
170 ErrorOriginSet::iterator itr_error
=
171 error_origins_
.find(make_pair(origin_url
, type
));
172 if (itr_error
!= error_origins_
.end()) {
173 callback
.Run(kQuotaErrorInvalidModification
);
177 OriginDataMap::iterator itr
=
178 origin_data_
.find(make_pair(origin_url
, type
));
179 if (itr
!= origin_data_
.end()) {
180 int64 delta
= itr
->second
;
181 quota_manager_proxy_
->
182 NotifyStorageModified(id(), origin_url
, type
, -delta
);
183 origin_data_
.erase(itr
);
186 callback
.Run(kQuotaStatusOk
);
189 } // namespace content