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
) {
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 (const ServiceWorkerUsageInfo
& origin
: origins
) {
69 if (!BrowsingDataHelper::HasWebScheme(origin
.origin
))
70 continue; // Non-websafe state is not considered browsing data.
72 service_worker_info_
.push_back(origin
);
75 BrowserThread::PostTask(
78 base::Bind(&BrowsingDataServiceWorkerHelper::NotifyOnUIThread
, this));
81 void BrowsingDataServiceWorkerHelper::NotifyOnUIThread() {
82 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
84 completion_callback_
.Run(service_worker_info_
);
85 completion_callback_
.Reset();
89 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
91 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
92 service_worker_context_
->DeleteForOrigin(origin
);
95 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
96 PendingServiceWorkerUsageInfo(const GURL
& origin
,
97 const std::vector
<GURL
>& scopes
)
98 : origin(origin
), scopes(scopes
) {
101 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
102 ~PendingServiceWorkerUsageInfo() {
105 bool CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
106 operator<(const PendingServiceWorkerUsageInfo
& other
) const {
107 if (origin
== other
.origin
)
108 return scopes
< other
.scopes
;
109 return origin
< other
.origin
;
112 CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
113 content::ServiceWorkerContext
* context
)
114 : BrowsingDataServiceWorkerHelper(context
) {
117 CannedBrowsingDataServiceWorkerHelper::
118 ~CannedBrowsingDataServiceWorkerHelper() {
121 void CannedBrowsingDataServiceWorkerHelper::AddServiceWorker(
122 const GURL
& origin
, const std::vector
<GURL
>& scopes
) {
123 if (!BrowsingDataHelper::HasWebScheme(origin
))
124 return; // Non-websafe state is not considered browsing data.
126 pending_service_worker_info_
.insert(
127 PendingServiceWorkerUsageInfo(origin
, scopes
));
130 void CannedBrowsingDataServiceWorkerHelper::Reset() {
131 service_worker_info_
.clear();
132 pending_service_worker_info_
.clear();
135 bool CannedBrowsingDataServiceWorkerHelper::empty() const {
136 return service_worker_info_
.empty() && pending_service_worker_info_
.empty();
139 size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
140 return pending_service_worker_info_
.size();
144 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo
>&
145 CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
146 return pending_service_worker_info_
;
149 void CannedBrowsingDataServiceWorkerHelper::StartFetching(const base::Callback
<
150 void(const std::list
<ServiceWorkerUsageInfo
>&)>& callback
) {
151 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
152 DCHECK(!callback
.is_null());
154 std::list
<ServiceWorkerUsageInfo
> result
;
155 for (const PendingServiceWorkerUsageInfo
& pending_info
:
156 pending_service_worker_info_
) {
157 ServiceWorkerUsageInfo
info(pending_info
.origin
, pending_info
.scopes
);
158 result
.push_back(info
);
161 BrowserThread::PostTask(
162 BrowserThread::UI
, FROM_HERE
, base::Bind(callback
, result
));
165 void CannedBrowsingDataServiceWorkerHelper::DeleteServiceWorkers(
166 const GURL
& origin
) {
167 for (std::set
<PendingServiceWorkerUsageInfo
>::iterator it
=
168 pending_service_worker_info_
.begin();
169 it
!= pending_service_worker_info_
.end();) {
170 if (it
->origin
== origin
)
171 pending_service_worker_info_
.erase(it
++);
175 BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin
);