summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browsing_data/browsing_data_service_worker_helper.cc
blob: 74dfef394dc962dd69b20e843b89f1ebe8e17783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"

#include <tuple>
#include <vector>

#include "base/bind.h"
#include "base/location.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_worker_context.h"

using content::BrowserThread;
using content::ServiceWorkerContext;
using content::ServiceWorkerUsageInfo;

namespace {

void GetAllOriginsInfoCallback(
    const BrowsingDataServiceWorkerHelper::FetchCallback& callback,
    const std::vector<ServiceWorkerUsageInfo>& origins) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  DCHECK(!callback.is_null());

  std::list<ServiceWorkerUsageInfo> result;
  for (const ServiceWorkerUsageInfo& origin : origins) {
    if (!BrowsingDataHelper::HasWebScheme(origin.origin))
      continue;  // Non-websafe state is not considered browsing data.
    result.push_back(origin);
  }

  BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
                          base::Bind(callback, result));
}

void EmptySuccessCallback(bool success) {}

}  // namespace

BrowsingDataServiceWorkerHelper::BrowsingDataServiceWorkerHelper(
    ServiceWorkerContext* service_worker_context)
    : service_worker_context_(service_worker_context) {
  DCHECK(service_worker_context_);
}

BrowsingDataServiceWorkerHelper::~BrowsingDataServiceWorkerHelper() {}

void BrowsingDataServiceWorkerHelper::StartFetching(
    const FetchCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(!callback.is_null());
  BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
                          base::Bind(&BrowsingDataServiceWorkerHelper::
                                         FetchServiceWorkerUsageInfoOnIOThread,
                                     this, callback));
}

void BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(const GURL& origin) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  BrowserThread::PostTask(
      BrowserThread::IO,
      FROM_HERE,
      base::Bind(
          &BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread,
          this,
          origin));
}

void BrowsingDataServiceWorkerHelper::FetchServiceWorkerUsageInfoOnIOThread(
    const FetchCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  DCHECK(!callback.is_null());

  service_worker_context_->GetAllOriginsInfo(
      base::Bind(&GetAllOriginsInfoCallback, callback));
}

void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
    const GURL& origin) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  service_worker_context_->DeleteForOrigin(origin,
                                           base::Bind(&EmptySuccessCallback));
}

CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
    PendingServiceWorkerUsageInfo(const GURL& origin,
                                  const std::vector<GURL>& scopes)
    : origin(origin), scopes(scopes) {
}

CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
    PendingServiceWorkerUsageInfo(const PendingServiceWorkerUsageInfo& other) =
        default;

CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
    ~PendingServiceWorkerUsageInfo() {
}

bool CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
operator<(const PendingServiceWorkerUsageInfo& other) const {
  return std::tie(origin, scopes) < std::tie(other.origin, other.scopes);
}

CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
    content::ServiceWorkerContext* context)
    : BrowsingDataServiceWorkerHelper(context) {
}

CannedBrowsingDataServiceWorkerHelper::
    ~CannedBrowsingDataServiceWorkerHelper() {
}

void CannedBrowsingDataServiceWorkerHelper::AddServiceWorker(
    const GURL& origin, const std::vector<GURL>& scopes) {
  if (!BrowsingDataHelper::HasWebScheme(origin))
    return;  // Non-websafe state is not considered browsing data.

  pending_service_worker_info_.insert(
      PendingServiceWorkerUsageInfo(origin, scopes));
}

void CannedBrowsingDataServiceWorkerHelper::Reset() {
  pending_service_worker_info_.clear();
}

bool CannedBrowsingDataServiceWorkerHelper::empty() const {
  return pending_service_worker_info_.empty();
}

size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
  return pending_service_worker_info_.size();
}

const std::set<
    CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo>&
CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
  return pending_service_worker_info_;
}

void CannedBrowsingDataServiceWorkerHelper::StartFetching(
    const FetchCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  DCHECK(!callback.is_null());

  std::list<ServiceWorkerUsageInfo> result;
  for (const PendingServiceWorkerUsageInfo& pending_info :
       pending_service_worker_info_) {
    ServiceWorkerUsageInfo info(pending_info.origin, pending_info.scopes);
    result.push_back(info);
  }

  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
}

void CannedBrowsingDataServiceWorkerHelper::DeleteServiceWorkers(
    const GURL& origin) {
  for (std::set<PendingServiceWorkerUsageInfo>::iterator it =
           pending_service_worker_info_.begin();
       it != pending_service_worker_info_.end();) {
    if (it->origin == origin)
      pending_service_worker_info_.erase(it++);
    else
      ++it;
  }
  BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin);
}