[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / in_process_webkit / indexed_db_quota_client.cc
blob3096c9598d48bfb88fe015ce505e82be4da1edfc
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"
7 #include <vector>
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;
20 namespace content {
21 namespace {
23 quota::QuotaStatusCode DeleteOriginDataOnWebKitThread(
24 IndexedDBContextImpl* context,
25 const GURL& origin) {
26 context->DeleteForOrigin(origin);
27 return quota::kQuotaStatusOk;
30 int64 GetOriginUsageOnWebKitThread(
31 IndexedDBContextImpl* context,
32 const GURL& origin) {
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());
45 void DidGetOrigins(
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);
66 } // namespace
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() {
85 delete this;
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) {
97 callback.Run(0);
98 return;
101 base::PostTaskAndReplyWithResult(
102 webkit_thread_message_loop_,
103 FROM_HERE,
104 base::Bind(&GetOriginUsageOnWebKitThread,
105 indexed_db_context_,
106 origin_url),
107 callback);
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);
119 return;
122 std::set<GURL>* origins_to_return = new std::set<GURL>();
123 webkit_thread_message_loop_->PostTaskAndReply(
124 FROM_HERE,
125 base::Bind(&GetAllOriginsOnWebKitThread,
126 indexed_db_context_,
127 base::Unretained(origins_to_return)),
128 base::Bind(&DidGetOrigins,
129 callback,
130 base::Owned(origins_to_return),
131 type));
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);
144 return;
147 std::set<GURL>* origins_to_return = new std::set<GURL>();
148 webkit_thread_message_loop_->PostTaskAndReply(
149 FROM_HERE,
150 base::Bind(&GetOriginsForHostOnWebKitThread,
151 indexed_db_context_,
152 host,
153 base::Unretained(origins_to_return)),
154 base::Bind(&DidGetOrigins,
155 callback,
156 base::Owned(origins_to_return),
157 type));
160 void IndexedDBQuotaClient::DeleteOriginData(
161 const GURL& origin,
162 quota::StorageType type,
163 const DeletionCallback& callback) {
164 if (type != quota::kStorageTypeTemporary) {
165 callback.Run(quota::kQuotaErrorNotSupported);
166 return;
169 base::PostTaskAndReplyWithResult(
170 webkit_thread_message_loop_,
171 FROM_HERE,
172 base::Bind(&DeleteOriginDataOnWebKitThread,
173 indexed_db_context_,
174 origin),
175 callback);
178 } // namespace content