cleanup: Use ExtensionRegistryObserver in Uber
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_quota_client.cc
blob2a7842ff4d4067d4d9aa52e50084a589b61801c0
1 // Copyright 2014 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.
4 #include "content/browser/service_worker/service_worker_quota_client.h"
6 #include "base/bind.h"
7 #include "content/browser/service_worker/service_worker_context_wrapper.h"
8 #include "content/public/browser/browser_thread.h"
10 using storage::QuotaClient;
12 namespace content {
13 namespace {
14 void ReportOrigins(const QuotaClient::GetOriginsCallback& callback,
15 bool restrict_on_host,
16 const std::string host,
17 const std::vector<ServiceWorkerUsageInfo>& usage_info) {
18 std::set<GURL> origins;
19 for (const ServiceWorkerUsageInfo& info : usage_info) {
20 if (restrict_on_host && info.origin.host() != host) {
21 continue;
23 origins.insert(info.origin);
25 callback.Run(origins);
28 void ReportToQuotaStatus(const QuotaClient::DeletionCallback& callback,
29 bool status) {
30 callback.Run(status ? storage::QuotaStatusCode::kQuotaStatusOk
31 : storage::QuotaStatusCode::kQuotaStatusUnknown);
34 void FindUsageForOrigin(const QuotaClient::GetUsageCallback& callback,
35 const GURL& origin,
36 const std::vector<ServiceWorkerUsageInfo>& usage_info) {
37 for (const auto& info : usage_info) {
38 if (info.origin == origin) {
39 callback.Run(info.total_size_bytes);
40 return;
43 callback.Run(0);
45 } // namespace
47 ServiceWorkerQuotaClient::ServiceWorkerQuotaClient(
48 ServiceWorkerContextWrapper* context)
49 : context_(context) {
52 ServiceWorkerQuotaClient::~ServiceWorkerQuotaClient() {
55 QuotaClient::ID ServiceWorkerQuotaClient::id() const {
56 return QuotaClient::kServiceWorker;
59 void ServiceWorkerQuotaClient::OnQuotaManagerDestroyed() {
60 delete this;
63 void ServiceWorkerQuotaClient::GetOriginUsage(
64 const GURL& origin,
65 storage::StorageType type,
66 const GetUsageCallback& callback) {
67 if (type != storage::StorageType::kStorageTypeTemporary) {
68 callback.Run(0);
69 return;
71 context_->GetAllOriginsInfo(
72 base::Bind(&FindUsageForOrigin, callback, origin));
75 void ServiceWorkerQuotaClient::GetOriginsForType(
76 storage::StorageType type,
77 const GetOriginsCallback& callback) {
78 if (type != storage::StorageType::kStorageTypeTemporary) {
79 callback.Run(std::set<GURL>());
80 return;
82 context_->GetAllOriginsInfo(base::Bind(&ReportOrigins, callback, false, ""));
85 void ServiceWorkerQuotaClient::GetOriginsForHost(
86 storage::StorageType type,
87 const std::string& host,
88 const GetOriginsCallback& callback) {
89 if (type != storage::StorageType::kStorageTypeTemporary) {
90 callback.Run(std::set<GURL>());
91 return;
93 context_->GetAllOriginsInfo(base::Bind(&ReportOrigins, callback, true, host));
96 void ServiceWorkerQuotaClient::DeleteOriginData(
97 const GURL& origin,
98 storage::StorageType type,
99 const DeletionCallback& callback) {
100 if (type != storage::StorageType::kStorageTypeTemporary) {
101 callback.Run(storage::QuotaStatusCode::kQuotaStatusOk);
102 return;
104 context_->DeleteForOrigin(origin, base::Bind(&ReportToQuotaStatus, callback));
107 bool ServiceWorkerQuotaClient::DoesSupport(storage::StorageType type) const {
108 return type == storage::StorageType::kStorageTypeTemporary;
111 } // namespace content