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/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "chrome/browser/browsing_data/browsing_data_helper.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/service_worker_context.h"
19 using content::BrowserThread
;
20 using content::ServiceWorkerContext
;
21 using content::ServiceWorkerUsageInfo
;
23 BrowsingDataServiceWorkerHelper::BrowsingDataServiceWorkerHelper(
24 ServiceWorkerContext
* service_worker_context
)
25 : service_worker_context_(service_worker_context
), is_fetching_(false) {
26 DCHECK(service_worker_context_
);
29 BrowsingDataServiceWorkerHelper::~BrowsingDataServiceWorkerHelper() {
32 void BrowsingDataServiceWorkerHelper::StartFetching(const base::Callback
<
33 void(const std::list
<ServiceWorkerUsageInfo
>&)>& callback
) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
35 DCHECK(!is_fetching_
);
36 DCHECK(!callback
.is_null());
39 completion_callback_
= callback
;
40 BrowserThread::PostTask(BrowserThread::IO
,
42 base::Bind(&BrowsingDataServiceWorkerHelper::
43 FetchServiceWorkerUsageInfoOnIOThread
,
47 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(const GURL
& origin
) {
48 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
49 BrowserThread::PostTask(
53 &BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread
,
58 void BrowsingDataServiceWorkerHelper::FetchServiceWorkerUsageInfoOnIOThread() {
59 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
61 service_worker_context_
->GetAllOriginsInfo(base::Bind(
62 &BrowsingDataServiceWorkerHelper::GetAllOriginsInfoCallback
, this));
65 void BrowsingDataServiceWorkerHelper::GetAllOriginsInfoCallback(
66 const std::vector
<ServiceWorkerUsageInfo
>& origins
) {
67 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
68 for (std::vector
<ServiceWorkerUsageInfo
>::const_iterator iter
=
70 iter
!= origins
.end();
72 const ServiceWorkerUsageInfo
& origin
= *iter
;
73 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
74 continue; // Non-websafe state is not considered browsing data.
76 service_worker_info_
.push_back(origin
);
79 BrowserThread::PostTask(
82 base::Bind(&BrowsingDataServiceWorkerHelper::NotifyOnUIThread
, this));
85 void BrowsingDataServiceWorkerHelper::NotifyOnUIThread() {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
88 completion_callback_
.Run(service_worker_info_
);
89 completion_callback_
.Reset();
93 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
95 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
96 service_worker_context_
->DeleteForOrigin(origin
);
99 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
100 PendingServiceWorkerUsageInfo(const GURL
& origin
,
101 const std::vector
<GURL
>& scopes
)
102 : origin(origin
), scopes(scopes
) {
105 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
106 ~PendingServiceWorkerUsageInfo() {
109 bool CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
110 operator<(const PendingServiceWorkerUsageInfo
& other
) const {
111 if (origin
== other
.origin
)
112 return scopes
< other
.scopes
;
113 return origin
< other
.origin
;
116 CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
117 content::ServiceWorkerContext
* context
)
118 : BrowsingDataServiceWorkerHelper(context
) {
121 CannedBrowsingDataServiceWorkerHelper::
122 ~CannedBrowsingDataServiceWorkerHelper() {
125 void CannedBrowsingDataServiceWorkerHelper::AddServiceWorker(
126 const GURL
& origin
, const std::vector
<GURL
>& scopes
) {
127 if (!BrowsingDataHelper::HasWebScheme(origin
))
128 return; // Non-websafe state is not considered browsing data.
130 pending_service_worker_info_
.insert(
131 PendingServiceWorkerUsageInfo(origin
, scopes
));
134 void CannedBrowsingDataServiceWorkerHelper::Reset() {
135 service_worker_info_
.clear();
136 pending_service_worker_info_
.clear();
139 bool CannedBrowsingDataServiceWorkerHelper::empty() const {
140 return service_worker_info_
.empty() && pending_service_worker_info_
.empty();
143 size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
144 return pending_service_worker_info_
.size();
148 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
>&
149 CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
150 return pending_service_worker_info_
;
153 void CannedBrowsingDataServiceWorkerHelper::StartFetching(const base::Callback
<
154 void(const std::list
<ServiceWorkerUsageInfo
>&)>& callback
) {
155 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
156 DCHECK(!callback
.is_null());
158 std::list
<ServiceWorkerUsageInfo
> result
;
159 for (std::set
<PendingServiceWorkerUsageInfo
>::const_iterator pending_info
=
160 pending_service_worker_info_
.begin();
161 pending_info
!= pending_service_worker_info_
.end();
163 ServiceWorkerUsageInfo
info(
164 pending_info
->origin
, pending_info
->scopes
);
165 result
.push_back(info
);
168 BrowserThread::PostTask(
169 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
172 void CannedBrowsingDataServiceWorkerHelper::DeleteServiceWorkers(
173 const GURL
& origin
) {
174 for (std::set
<PendingServiceWorkerUsageInfo
>::iterator it
=
175 pending_service_worker_info_
.begin();
176 it
!= pending_service_worker_info_
.end();) {
177 if (it
->origin
== origin
)
178 pending_service_worker_info_
.erase(it
++);
182 BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin
);