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/in_process_webkit/indexed_db_quota_client.h"
9 #include "base/file_util.h"
10 #include "base/logging.h"
11 #include "base/message_loop_proxy.h"
12 #include "content/browser/in_process_webkit/indexed_db_context_impl.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/base/net_util.h"
15 #include "webkit/database/database_util.h"
17 using quota::QuotaClient
;
18 using webkit_database::DatabaseUtil
;
23 quota::QuotaStatusCode
DeleteOriginDataOnWebKitThread(
24 IndexedDBContextImpl
* context
,
26 context
->DeleteForOrigin(origin
);
27 return quota::kQuotaStatusOk
;
30 int64
GetOriginUsageOnWebKitThread(
31 IndexedDBContextImpl
* context
,
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED
));
34 return context
->GetOriginDiskUsage(origin
);
37 void GetAllOriginsOnWebKitThread(
38 IndexedDBContextImpl
* context
,
39 std::set
<GURL
>* origins_to_return
) {
40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED
));
41 std::vector
<GURL
> all_origins
= context
->GetAllOrigins();
42 origins_to_return
->insert(all_origins
.begin(), all_origins
.end());
46 const IndexedDBQuotaClient::GetOriginsCallback
& callback
,
47 const std::set
<GURL
>* origins
,
48 quota::StorageType storage_type
) {
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
50 callback
.Run(*origins
, storage_type
);
53 void GetOriginsForHostOnWebKitThread(
54 IndexedDBContextImpl
* context
,
55 const std::string
& host
,
56 std::set
<GURL
>* origins_to_return
) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED
));
58 std::vector
<GURL
> all_origins
= context
->GetAllOrigins();
59 for (std::vector
<GURL
>::const_iterator iter
= all_origins
.begin();
60 iter
!= all_origins
.end(); ++iter
) {
61 if (host
== net::GetHostOrSpecFromURL(*iter
))
62 origins_to_return
->insert(*iter
);
68 // IndexedDBQuotaClient --------------------------------------------------------
70 IndexedDBQuotaClient::IndexedDBQuotaClient(
71 base::MessageLoopProxy
* webkit_thread_message_loop
,
72 IndexedDBContextImpl
* indexed_db_context
)
73 : webkit_thread_message_loop_(webkit_thread_message_loop
),
74 indexed_db_context_(indexed_db_context
) {
77 IndexedDBQuotaClient::~IndexedDBQuotaClient() {
80 QuotaClient::ID
IndexedDBQuotaClient::id() const {
81 return kIndexedDatabase
;
84 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() {
88 void IndexedDBQuotaClient::GetOriginUsage(
89 const GURL
& origin_url
,
90 quota::StorageType type
,
91 const GetUsageCallback
& callback
) {
92 DCHECK(!callback
.is_null());
93 DCHECK(indexed_db_context_
.get());
95 // IndexedDB is in the temp namespace for now.
96 if (type
!= quota::kStorageTypeTemporary
) {
101 base::PostTaskAndReplyWithResult(
102 webkit_thread_message_loop_
,
104 base::Bind(&GetOriginUsageOnWebKitThread
,
110 void IndexedDBQuotaClient::GetOriginsForType(
111 quota::StorageType type
,
112 const GetOriginsCallback
& callback
) {
113 DCHECK(!callback
.is_null());
114 DCHECK(indexed_db_context_
.get());
116 // All databases are in the temp namespace for now.
117 if (type
!= quota::kStorageTypeTemporary
) {
118 callback
.Run(std::set
<GURL
>(), type
);
122 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
123 webkit_thread_message_loop_
->PostTaskAndReply(
125 base::Bind(&GetAllOriginsOnWebKitThread
,
127 base::Unretained(origins_to_return
)),
128 base::Bind(&DidGetOrigins
,
130 base::Owned(origins_to_return
),
134 void IndexedDBQuotaClient::GetOriginsForHost(
135 quota::StorageType type
,
136 const std::string
& host
,
137 const GetOriginsCallback
& callback
) {
138 DCHECK(!callback
.is_null());
139 DCHECK(indexed_db_context_
.get());
141 // All databases are in the temp namespace for now.
142 if (type
!= quota::kStorageTypeTemporary
) {
143 callback
.Run(std::set
<GURL
>(), type
);
147 std::set
<GURL
>* origins_to_return
= new std::set
<GURL
>();
148 webkit_thread_message_loop_
->PostTaskAndReply(
150 base::Bind(&GetOriginsForHostOnWebKitThread
,
153 base::Unretained(origins_to_return
)),
154 base::Bind(&DidGetOrigins
,
156 base::Owned(origins_to_return
),
160 void IndexedDBQuotaClient::DeleteOriginData(
162 quota::StorageType type
,
163 const DeletionCallback
& callback
) {
164 if (type
!= quota::kStorageTypeTemporary
) {
165 callback
.Run(quota::kQuotaErrorNotSupported
);
169 base::PostTaskAndReplyWithResult(
170 webkit_thread_message_loop_
,
172 base::Bind(&DeleteOriginDataOnWebKitThread
,
178 } // namespace content