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.
5 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
10 #include "base/location.h"
11 #include "chrome/browser/browsing_data/browsing_data_helper.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/service_worker_context.h"
15 using content::BrowserThread
;
16 using content::ServiceWorkerContext
;
17 using content::ServiceWorkerUsageInfo
;
21 void GetAllOriginsInfoCallback(
22 const BrowsingDataServiceWorkerHelper::FetchCallback
& callback
,
23 const std::vector
<ServiceWorkerUsageInfo
>& origins
) {
24 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
25 DCHECK(!callback
.is_null());
27 std::list
<ServiceWorkerUsageInfo
> result
;
28 for (const ServiceWorkerUsageInfo
& origin
: origins
) {
29 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
30 continue; // Non-websafe state is not considered browsing data.
31 result
.push_back(origin
);
34 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
35 base::Bind(callback
, result
));
38 void EmptySuccessCallback(bool success
) {}
42 BrowsingDataServiceWorkerHelper::BrowsingDataServiceWorkerHelper(
43 ServiceWorkerContext
* service_worker_context
)
44 : service_worker_context_(service_worker_context
) {
45 DCHECK(service_worker_context_
);
48 BrowsingDataServiceWorkerHelper::~BrowsingDataServiceWorkerHelper() {}
50 void BrowsingDataServiceWorkerHelper::StartFetching(
51 const FetchCallback
& callback
) {
52 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
53 DCHECK(!callback
.is_null());
54 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
55 base::Bind(&BrowsingDataServiceWorkerHelper::
56 FetchServiceWorkerUsageInfoOnIOThread
,
60 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(const GURL
& origin
) {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
62 BrowserThread::PostTask(
66 &BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread
,
71 void BrowsingDataServiceWorkerHelper::FetchServiceWorkerUsageInfoOnIOThread(
72 const FetchCallback
& callback
) {
73 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
74 DCHECK(!callback
.is_null());
76 service_worker_context_
->GetAllOriginsInfo(
77 base::Bind(&GetAllOriginsInfoCallback
, callback
));
80 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
82 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
83 service_worker_context_
->DeleteForOrigin(origin
,
84 base::Bind(&EmptySuccessCallback
));
87 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
88 PendingServiceWorkerUsageInfo(const GURL
& origin
,
89 const std::vector
<GURL
>& scopes
)
90 : origin(origin
), scopes(scopes
) {
93 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
94 ~PendingServiceWorkerUsageInfo() {
97 bool CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
98 operator<(const PendingServiceWorkerUsageInfo
& other
) const {
99 if (origin
== other
.origin
)
100 return scopes
< other
.scopes
;
101 return origin
< other
.origin
;
104 CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
105 content::ServiceWorkerContext
* context
)
106 : BrowsingDataServiceWorkerHelper(context
) {
109 CannedBrowsingDataServiceWorkerHelper::
110 ~CannedBrowsingDataServiceWorkerHelper() {
113 void CannedBrowsingDataServiceWorkerHelper::AddServiceWorker(
114 const GURL
& origin
, const std::vector
<GURL
>& scopes
) {
115 if (!BrowsingDataHelper::HasWebScheme(origin
))
116 return; // Non-websafe state is not considered browsing data.
118 pending_service_worker_info_
.insert(
119 PendingServiceWorkerUsageInfo(origin
, scopes
));
122 void CannedBrowsingDataServiceWorkerHelper::Reset() {
123 pending_service_worker_info_
.clear();
126 bool CannedBrowsingDataServiceWorkerHelper::empty() const {
127 return pending_service_worker_info_
.empty();
130 size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
131 return pending_service_worker_info_
.size();
135 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
>&
136 CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
137 return pending_service_worker_info_
;
140 void CannedBrowsingDataServiceWorkerHelper::StartFetching(
141 const FetchCallback
& callback
) {
142 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
143 DCHECK(!callback
.is_null());
145 std::list
<ServiceWorkerUsageInfo
> result
;
146 for (const PendingServiceWorkerUsageInfo
& pending_info
:
147 pending_service_worker_info_
) {
148 ServiceWorkerUsageInfo
info(pending_info
.origin
, pending_info
.scopes
);
149 result
.push_back(info
);
152 BrowserThread::PostTask(
153 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
156 void CannedBrowsingDataServiceWorkerHelper::DeleteServiceWorkers(
157 const GURL
& origin
) {
158 for (std::set
<PendingServiceWorkerUsageInfo
>::iterator it
=
159 pending_service_worker_info_
.begin();
160 it
!= pending_service_worker_info_
.end();) {
161 if (it
->origin
== origin
)
162 pending_service_worker_info_
.erase(it
++);
166 BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin
);