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/browser/database/database_quota_client.h"
10 #include "base/bind_helpers.h"
11 #include "base/location.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/task_runner_util.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h"
17 #include "webkit/browser/database/database_tracker.h"
18 #include "webkit/browser/database/database_util.h"
19 #include "webkit/common/database/database_identifier.h"
21 using quota::QuotaClient
;
23 namespace webkit_database
{
27 int64
GetOriginUsageOnDBThread(
28 DatabaseTracker
* db_tracker
,
29 const GURL
& origin_url
) {
31 if (db_tracker
->GetOriginInfo(
32 webkit_database::GetIdentifierFromOrigin(origin_url
), &info
))
33 return info
.TotalSize();
37 void GetOriginsOnDBThread(
38 DatabaseTracker
* db_tracker
,
39 std::set
<GURL
>* origins_ptr
) {
40 std::vector
<std::string
> origin_identifiers
;
41 if (db_tracker
->GetAllOriginIdentifiers(&origin_identifiers
)) {
42 for (std::vector
<std::string
>::const_iterator iter
=
43 origin_identifiers
.begin();
44 iter
!= origin_identifiers
.end(); ++iter
) {
45 GURL origin
= webkit_database::GetOriginFromIdentifier(*iter
);
46 origins_ptr
->insert(origin
);
51 void GetOriginsForHostOnDBThread(
52 DatabaseTracker
* db_tracker
,
53 std::set
<GURL
>* origins_ptr
,
54 const std::string
& host
) {
55 std::vector
<std::string
> origin_identifiers
;
56 if (db_tracker
->GetAllOriginIdentifiers(&origin_identifiers
)) {
57 for (std::vector
<std::string
>::const_iterator iter
=
58 origin_identifiers
.begin();
59 iter
!= origin_identifiers
.end(); ++iter
) {
60 GURL origin
= webkit_database::GetOriginFromIdentifier(*iter
);
61 if (host
== net::GetHostOrSpecFromURL(origin
))
62 origins_ptr
->insert(origin
);
68 const QuotaClient::GetOriginsCallback
& callback
,
69 std::set
<GURL
>* origins_ptr
) {
70 callback
.Run(*origins_ptr
);
73 void DidDeleteOriginData(
74 base::SingleThreadTaskRunner
* original_task_runner
,
75 const QuotaClient::DeletionCallback
& callback
,
77 if (result
== net::ERR_IO_PENDING
) {
78 // The callback will be invoked via
79 // DatabaseTracker::ScheduleDatabasesForDeletion.
83 quota::QuotaStatusCode status
;
84 if (result
== net::OK
)
85 status
= quota::kQuotaStatusOk
;
87 status
= quota::kQuotaStatusUnknown
;
89 if (original_task_runner
->BelongsToCurrentThread())
92 original_task_runner
->PostTask(FROM_HERE
, base::Bind(callback
, status
));
97 DatabaseQuotaClient::DatabaseQuotaClient(
98 base::MessageLoopProxy
* db_tracker_thread
,
99 DatabaseTracker
* db_tracker
)
100 : db_tracker_thread_(db_tracker_thread
), db_tracker_(db_tracker
) {
103 DatabaseQuotaClient::~DatabaseQuotaClient() {
104 if (db_tracker_thread_
.get() &&
105 !db_tracker_thread_
->RunsTasksOnCurrentThread() && db_tracker_
.get()) {
106 DatabaseTracker
* tracker
= db_tracker_
.get();
109 if (!db_tracker_thread_
->ReleaseSoon(FROM_HERE
, tracker
))
114 QuotaClient::ID
DatabaseQuotaClient::id() const {
118 void DatabaseQuotaClient::OnQuotaManagerDestroyed() {
122 void DatabaseQuotaClient::GetOriginUsage(
123 const GURL
& origin_url
,
124 quota::StorageType type
,
125 const GetUsageCallback
& callback
) {
126 DCHECK(!callback
.is_null());
127 DCHECK(db_tracker_
.get());
129 // All databases are in the temp namespace for now.
130 if (type
!= quota::kStorageTypeTemporary
) {
135 base::PostTaskAndReplyWithResult(
136 db_tracker_thread_
.get(),
138 base::Bind(&GetOriginUsageOnDBThread
, db_tracker_
, origin_url
),
142 void DatabaseQuotaClient::GetOriginsForType(
143 quota::StorageType type
,
144 const GetOriginsCallback
& callback
) {
145 DCHECK(!callback
.is_null());
146 DCHECK(db_tracker_
.get());
148 // All databases are in the temp namespace for now.
149 if (type
!= quota::kStorageTypeTemporary
) {
150 callback
.Run(std::set
<GURL
>());
154 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
155 db_tracker_thread_
->PostTaskAndReply(
157 base::Bind(&GetOriginsOnDBThread
,
159 base::Unretained(origins_ptr
)),
160 base::Bind(&DidGetOrigins
,
162 base::Owned(origins_ptr
)));
165 void DatabaseQuotaClient::GetOriginsForHost(
166 quota::StorageType type
,
167 const std::string
& host
,
168 const GetOriginsCallback
& callback
) {
169 DCHECK(!callback
.is_null());
170 DCHECK(db_tracker_
.get());
172 // All databases are in the temp namespace for now.
173 if (type
!= quota::kStorageTypeTemporary
) {
174 callback
.Run(std::set
<GURL
>());
178 std::set
<GURL
>* origins_ptr
= new std::set
<GURL
>();
179 db_tracker_thread_
->PostTaskAndReply(
181 base::Bind(&GetOriginsForHostOnDBThread
,
183 base::Unretained(origins_ptr
),
185 base::Bind(&DidGetOrigins
,
187 base::Owned(origins_ptr
)));
190 void DatabaseQuotaClient::DeleteOriginData(
192 quota::StorageType type
,
193 const DeletionCallback
& callback
) {
194 DCHECK(!callback
.is_null());
195 DCHECK(db_tracker_
.get());
197 // All databases are in the temp namespace for now, so nothing to delete.
198 if (type
!= quota::kStorageTypeTemporary
) {
199 callback
.Run(quota::kQuotaStatusOk
);
203 base::Callback
<void(int)> delete_callback
=
204 base::Bind(&DidDeleteOriginData
,
205 base::MessageLoopProxy::current(),
208 PostTaskAndReplyWithResult(
209 db_tracker_thread_
.get(),
211 base::Bind(&DatabaseTracker::DeleteDataForOrigin
,
213 webkit_database::GetIdentifierFromOrigin(origin
),
218 } // namespace webkit_database