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"
7 #include "content/browser/service_worker/service_worker_context_wrapper.h"
8 #include "content/public/browser/browser_thread.h"
10 using storage::QuotaClient
;
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
) {
23 origins
.insert(info
.origin
);
25 callback
.Run(origins
);
28 void ReportToQuotaStatus(const QuotaClient::DeletionCallback
& callback
,
30 callback
.Run(status
? storage::QuotaStatusCode::kQuotaStatusOk
31 : storage::QuotaStatusCode::kQuotaStatusUnknown
);
34 void FindUsageForOrigin(const QuotaClient::GetUsageCallback
& callback
,
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
);
47 ServiceWorkerQuotaClient::ServiceWorkerQuotaClient(
48 ServiceWorkerContextWrapper
* context
)
52 ServiceWorkerQuotaClient::~ServiceWorkerQuotaClient() {
55 QuotaClient::ID
ServiceWorkerQuotaClient::id() const {
56 return QuotaClient::kServiceWorker
;
59 void ServiceWorkerQuotaClient::OnQuotaManagerDestroyed() {
63 void ServiceWorkerQuotaClient::GetOriginUsage(
65 storage::StorageType type
,
66 const GetUsageCallback
& callback
) {
67 if (type
!= storage::StorageType::kStorageTypeTemporary
) {
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
>());
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
>());
93 context_
->GetAllOriginsInfo(base::Bind(&ReportOrigins
, callback
, true, host
));
96 void ServiceWorkerQuotaClient::DeleteOriginData(
98 storage::StorageType type
,
99 const DeletionCallback
& callback
) {
100 if (type
!= storage::StorageType::kStorageTypeTemporary
) {
101 callback
.Run(storage::QuotaStatusCode::kQuotaStatusOk
);
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