[Mac] Implement Ambient Light API
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_quota_client.cc
blobd8af36c55668513ba66e1ac3046c1b6070705704
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"
7 #include <vector>
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;
18 namespace content {
19 namespace {
21 storage::QuotaStatusCode DeleteOriginDataOnIndexedDBThread(
22 IndexedDBContextImpl* context,
23 const GURL& origin) {
24 context->DeleteForOrigin(origin);
25 return storage::kQuotaStatusOk;
28 int64 GetOriginUsageOnIndexedDBThread(IndexedDBContextImpl* context,
29 const GURL& origin) {
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);
58 } // namespace
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) {
80 callback.Run(0);
81 return;
84 // No task runner means unit test; no cleanup necessary.
85 if (!indexed_db_context_->TaskRunner()) {
86 callback.Run(0);
87 return;
90 base::PostTaskAndReplyWithResult(
91 indexed_db_context_->TaskRunner(),
92 FROM_HERE,
93 base::Bind(
94 &GetOriginUsageOnIndexedDBThread, indexed_db_context_, origin_url),
95 callback);
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>());
107 return;
110 // No task runner means unit test; no cleanup necessary.
111 if (!indexed_db_context_->TaskRunner()) {
112 callback.Run(std::set<GURL>());
113 return;
116 std::set<GURL>* origins_to_return = new std::set<GURL>();
117 indexed_db_context_->TaskRunner()->PostTaskAndReply(
118 FROM_HERE,
119 base::Bind(&GetAllOriginsOnIndexedDBThread,
120 indexed_db_context_,
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>());
135 return;
138 // No task runner means unit test; no cleanup necessary.
139 if (!indexed_db_context_->TaskRunner()) {
140 callback.Run(std::set<GURL>());
141 return;
144 std::set<GURL>* origins_to_return = new std::set<GURL>();
145 indexed_db_context_->TaskRunner()->PostTaskAndReply(
146 FROM_HERE,
147 base::Bind(&GetOriginsForHostOnIndexedDBThread,
148 indexed_db_context_,
149 host,
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);
159 return;
162 // No task runner means unit test; no cleanup necessary.
163 if (!indexed_db_context_->TaskRunner()) {
164 callback.Run(storage::kQuotaStatusOk);
165 return;
168 base::PostTaskAndReplyWithResult(
169 indexed_db_context_->TaskRunner(),
170 FROM_HERE,
171 base::Bind(
172 &DeleteOriginDataOnIndexedDBThread, indexed_db_context_, origin),
173 callback);
176 bool IndexedDBQuotaClient::DoesSupport(storage::StorageType type) const {
177 return type == storage::kStorageTypeTemporary;
180 } // namespace content