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 "content/browser/indexed_db/indexed_db_quota_client.h"
9 #include "base/logging.h"
10 #include "content/browser/indexed_db/indexed_db_context_impl.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/net_util.h"
13 #include "webkit/browser/database/database_util.h"
15 using quota::QuotaClient
;
16 using webkit_database::DatabaseUtil
;
21 quota::QuotaStatusCode
DeleteOriginDataOnIndexedDBThread(
22 IndexedDBContextImpl
* context
,
24 context
->DeleteForOrigin(origin
);
25 return quota::kQuotaStatusOk
;
28 int64
GetOriginUsageOnIndexedDBThread(IndexedDBContextImpl
* context
,
30 DCHECK(context
->TaskRunner()->RunsTasksOnCurrentThread());
31 return context
->GetOriginDiskUsage(origin
);
34 void GetAllOriginsOnIndexedDBThread(IndexedDBContextImpl
* context
,
35 std::set
<GURL
>* origins_to_return
) {
36 DCHECK(context
->TaskRunner()->RunsTasksOnCurrentThread());
37 std::vector
<GURL
> all_origins
= context
->GetAllOrigins();
38 origins_to_return
->insert(all_origins
.begin(), all_origins
.end());
41 void DidGetOrigins(const IndexedDBQuotaClient::GetOriginsCallback
& callback
,
42 const std::set
<GURL
>* origins
) {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
44 callback
.Run(*origins
);
47 void GetOriginsForHostOnIndexedDBThread(IndexedDBContextImpl
* context
,
48 const std::string
& host
,
49 std::set
<GURL
>* origins_to_return
) {
50 DCHECK(context
->TaskRunner()->RunsTasksOnCurrentThread());
51 std::vector
<GURL
> all_origins
= context
->GetAllOrigins();
52 for (std::vector
<GURL
>::const_iterator iter
= all_origins
.begin();
53 iter
!= all_origins
.end();
55 if (host
== net::GetHostOrSpecFromURL(*iter
))
56 origins_to_return
->insert(*iter
);
62 // IndexedDBQuotaClient --------------------------------------------------------
64 IndexedDBQuotaClient::IndexedDBQuotaClient(
65 IndexedDBContextImpl
* indexed_db_context
)
66 : indexed_db_context_(indexed_db_context
) {}
68 IndexedDBQuotaClient::~IndexedDBQuotaClient() {}
70 QuotaClient::ID
IndexedDBQuotaClient::id() const { return kIndexedDatabase
; }
72 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { delete this; }
74 void IndexedDBQuotaClient::GetOriginUsage(const GURL
& origin_url
,
75 quota::StorageType type
,
76 const GetUsageCallback
& callback
) {
77 DCHECK(!callback
.is_null());
78 DCHECK(indexed_db_context_
);
80 // IndexedDB is in the temp namespace for now.
81 if (type
!= quota::kStorageTypeTemporary
) {
86 // No task runner means unit test; no cleanup necessary.
87 if (!indexed_db_context_
->TaskRunner()) {
92 base::PostTaskAndReplyWithResult(
93 indexed_db_context_
->TaskRunner(),
96 &GetOriginUsageOnIndexedDBThread
, indexed_db_context_
, origin_url
),
100 void IndexedDBQuotaClient::GetOriginsForType(
101 quota::StorageType type
,
102 const GetOriginsCallback
& callback
) {
103 DCHECK(!callback
.is_null());
104 DCHECK(indexed_db_context_
);
106 // All databases are in the temp namespace for now.
107 if (type
!= quota::kStorageTypeTemporary
) {
108 callback
.Run(std::set
<GURL
>());
112 // No task runner means unit test; no cleanup necessary.
113 if (!indexed_db_context_
->TaskRunner()) {
114 callback
.Run(std::set
<GURL
>());
118 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
119 indexed_db_context_
->TaskRunner()->PostTaskAndReply(
121 base::Bind(&GetAllOriginsOnIndexedDBThread
,
123 base::Unretained(origins_to_return
)),
124 base::Bind(&DidGetOrigins
, callback
, base::Owned(origins_to_return
)));
127 void IndexedDBQuotaClient::GetOriginsForHost(
128 quota::StorageType type
,
129 const std::string
& host
,
130 const GetOriginsCallback
& callback
) {
131 DCHECK(!callback
.is_null());
132 DCHECK(indexed_db_context_
);
134 // All databases are in the temp namespace for now.
135 if (type
!= quota::kStorageTypeTemporary
) {
136 callback
.Run(std::set
<GURL
>());
140 // No task runner means unit test; no cleanup necessary.
141 if (!indexed_db_context_
->TaskRunner()) {
142 callback
.Run(std::set
<GURL
>());
146 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
147 indexed_db_context_
->TaskRunner()->PostTaskAndReply(
149 base::Bind(&GetOriginsForHostOnIndexedDBThread
,
152 base::Unretained(origins_to_return
)),
153 base::Bind(&DidGetOrigins
, callback
, base::Owned(origins_to_return
)));
156 void IndexedDBQuotaClient::DeleteOriginData(const GURL
& origin
,
157 quota::StorageType type
,
158 const DeletionCallback
& callback
) {
159 if (type
!= quota::kStorageTypeTemporary
) {
160 callback
.Run(quota::kQuotaErrorNotSupported
);
164 // No task runner means unit test; no cleanup necessary.
165 if (!indexed_db_context_
->TaskRunner()) {
166 callback
.Run(quota::kQuotaStatusOk
);
170 base::PostTaskAndReplyWithResult(
171 indexed_db_context_
->TaskRunner(),
174 &DeleteOriginDataOnIndexedDBThread
, indexed_db_context_
, origin
),
178 bool IndexedDBQuotaClient::DoesSupport(quota::StorageType type
) const {
179 return type
== quota::kStorageTypeTemporary
;
182 } // namespace content