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
));
40 BrowsingDataServiceWorkerHelper::BrowsingDataServiceWorkerHelper(
41 ServiceWorkerContext
* service_worker_context
)
42 : service_worker_context_(service_worker_context
) {
43 DCHECK(service_worker_context_
);
46 BrowsingDataServiceWorkerHelper::~BrowsingDataServiceWorkerHelper() {}
48 void BrowsingDataServiceWorkerHelper::StartFetching(
49 const FetchCallback
& callback
) {
50 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
51 DCHECK(!callback
.is_null());
52 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
53 base::Bind(&BrowsingDataServiceWorkerHelper::
54 FetchServiceWorkerUsageInfoOnIOThread
,
58 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(const GURL
& origin
) {
59 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
60 BrowserThread::PostTask(
64 &BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread
,
69 void BrowsingDataServiceWorkerHelper::FetchServiceWorkerUsageInfoOnIOThread(
70 const FetchCallback
& callback
) {
71 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
72 DCHECK(!callback
.is_null());
74 service_worker_context_
->GetAllOriginsInfo(
75 base::Bind(&GetAllOriginsInfoCallback
, callback
));
78 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
80 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
81 service_worker_context_
->DeleteForOrigin(origin
);
84 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
85 PendingServiceWorkerUsageInfo(const GURL
& origin
,
86 const std::vector
<GURL
>& scopes
)
87 : origin(origin
), scopes(scopes
) {
90 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
91 ~PendingServiceWorkerUsageInfo() {
94 bool CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
95 operator<(const PendingServiceWorkerUsageInfo
& other
) const {
96 if (origin
== other
.origin
)
97 return scopes
< other
.scopes
;
98 return origin
< other
.origin
;
101 CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
102 content::ServiceWorkerContext
* context
)
103 : BrowsingDataServiceWorkerHelper(context
) {
106 CannedBrowsingDataServiceWorkerHelper::
107 ~CannedBrowsingDataServiceWorkerHelper() {
110 void CannedBrowsingDataServiceWorkerHelper::AddServiceWorker(
111 const GURL
& origin
, const std::vector
<GURL
>& scopes
) {
112 if (!BrowsingDataHelper::HasWebScheme(origin
))
113 return; // Non-websafe state is not considered browsing data.
115 pending_service_worker_info_
.insert(
116 PendingServiceWorkerUsageInfo(origin
, scopes
));
119 void CannedBrowsingDataServiceWorkerHelper::Reset() {
120 pending_service_worker_info_
.clear();
123 bool CannedBrowsingDataServiceWorkerHelper::empty() const {
124 return pending_service_worker_info_
.empty();
127 size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
128 return pending_service_worker_info_
.size();
132 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
>&
133 CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
134 return pending_service_worker_info_
;
137 void CannedBrowsingDataServiceWorkerHelper::StartFetching(
138 const FetchCallback
& callback
) {
139 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
140 DCHECK(!callback
.is_null());
142 std::list
<ServiceWorkerUsageInfo
> result
;
143 for (const PendingServiceWorkerUsageInfo
& pending_info
:
144 pending_service_worker_info_
) {
145 ServiceWorkerUsageInfo
info(pending_info
.origin
, pending_info
.scopes
);
146 result
.push_back(info
);
149 BrowserThread::PostTask(
150 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
153 void CannedBrowsingDataServiceWorkerHelper::DeleteServiceWorkers(
154 const GURL
& origin
) {
155 for (std::set
<PendingServiceWorkerUsageInfo
>::iterator it
=
156 pending_service_worker_info_
.begin();
157 it
!= pending_service_worker_info_
.end();) {
158 if (it
->origin
== origin
)
159 pending_service_worker_info_
.erase(it
++);
163 BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin
);