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 "storage/browser/database/database_util.h"
15 using storage::QuotaClient
;
16 using storage::DatabaseUtil
;
21 storage::QuotaStatusCode
DeleteOriginDataOnIndexedDBThread(
22 IndexedDBContextImpl
* context
,
24 context
->DeleteForOrigin(origin
);
25 return storage::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 (const auto& origin_url
: all_origins
) {
53 if (host
== net::GetHostOrSpecFromURL(origin_url
))
54 origins_to_return
->insert(origin_url
);
60 // IndexedDBQuotaClient --------------------------------------------------------
62 IndexedDBQuotaClient::IndexedDBQuotaClient(
63 IndexedDBContextImpl
* indexed_db_context
)
64 : indexed_db_context_(indexed_db_context
) {}
66 IndexedDBQuotaClient::~IndexedDBQuotaClient() {}
68 QuotaClient::ID
IndexedDBQuotaClient::id() const { return kIndexedDatabase
; }
70 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { delete this; }
72 void IndexedDBQuotaClient::GetOriginUsage(const GURL
& origin_url
,
73 storage::StorageType type
,
74 const GetUsageCallback
& callback
) {
75 DCHECK(!callback
.is_null());
76 DCHECK(indexed_db_context_
.get());
78 // IndexedDB is in the temp namespace for now.
79 if (type
!= storage::kStorageTypeTemporary
) {
84 // No task runner means unit test; no cleanup necessary.
85 if (!indexed_db_context_
->TaskRunner()) {
90 base::PostTaskAndReplyWithResult(
91 indexed_db_context_
->TaskRunner(),
94 &GetOriginUsageOnIndexedDBThread
, indexed_db_context_
, origin_url
),
98 void IndexedDBQuotaClient::GetOriginsForType(
99 storage::StorageType type
,
100 const GetOriginsCallback
& callback
) {
101 DCHECK(!callback
.is_null());
102 DCHECK(indexed_db_context_
.get());
104 // All databases are in the temp namespace for now.
105 if (type
!= storage::kStorageTypeTemporary
) {
106 callback
.Run(std::set
<GURL
>());
110 // No task runner means unit test; no cleanup necessary.
111 if (!indexed_db_context_
->TaskRunner()) {
112 callback
.Run(std::set
<GURL
>());
116 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
117 indexed_db_context_
->TaskRunner()->PostTaskAndReply(
119 base::Bind(&GetAllOriginsOnIndexedDBThread
,
121 base::Unretained(origins_to_return
)),
122 base::Bind(&DidGetOrigins
, callback
, base::Owned(origins_to_return
)));
125 void IndexedDBQuotaClient::GetOriginsForHost(
126 storage::StorageType type
,
127 const std::string
& host
,
128 const GetOriginsCallback
& callback
) {
129 DCHECK(!callback
.is_null());
130 DCHECK(indexed_db_context_
.get());
132 // All databases are in the temp namespace for now.
133 if (type
!= storage::kStorageTypeTemporary
) {
134 callback
.Run(std::set
<GURL
>());
138 // No task runner means unit test; no cleanup necessary.
139 if (!indexed_db_context_
->TaskRunner()) {
140 callback
.Run(std::set
<GURL
>());
144 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
145 indexed_db_context_
->TaskRunner()->PostTaskAndReply(
147 base::Bind(&GetOriginsForHostOnIndexedDBThread
,
150 base::Unretained(origins_to_return
)),
151 base::Bind(&DidGetOrigins
, callback
, base::Owned(origins_to_return
)));
154 void IndexedDBQuotaClient::DeleteOriginData(const GURL
& origin
,
155 storage::StorageType type
,
156 const DeletionCallback
& callback
) {
157 if (type
!= storage::kStorageTypeTemporary
) {
158 callback
.Run(storage::kQuotaErrorNotSupported
);
162 // No task runner means unit test; no cleanup necessary.
163 if (!indexed_db_context_
->TaskRunner()) {
164 callback
.Run(storage::kQuotaStatusOk
);
168 base::PostTaskAndReplyWithResult(
169 indexed_db_context_
->TaskRunner(),
172 &DeleteOriginDataOnIndexedDBThread
, indexed_db_context_
, origin
),
176 bool IndexedDBQuotaClient::DoesSupport(storage::StorageType type
) const {
177 return type
== storage::kStorageTypeTemporary
;
180 } // namespace content