Home | History | Annotate | Download | only in browsing_data
      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.
      4 
      5 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/bind.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"
     18 
     19 using content::BrowserThread;
     20 using content::ServiceWorkerContext;
     21 using content::ServiceWorkerUsageInfo;
     22 
     23 BrowsingDataServiceWorkerHelper::BrowsingDataServiceWorkerHelper(
     24     ServiceWorkerContext* service_worker_context)
     25     : service_worker_context_(service_worker_context), is_fetching_(false) {
     26   DCHECK(service_worker_context_);
     27 }
     28 
     29 BrowsingDataServiceWorkerHelper::~BrowsingDataServiceWorkerHelper() {
     30 }
     31 
     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());
     37 
     38   is_fetching_ = true;
     39   completion_callback_ = callback;
     40   BrowserThread::PostTask(BrowserThread::IO,
     41                           FROM_HERE,
     42                           base::Bind(&BrowsingDataServiceWorkerHelper::
     43                                          FetchServiceWorkerUsageInfoOnIOThread,
     44                                      this));
     45 }
     46 
     47 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(const GURL& origin) {
     48   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     49   BrowserThread::PostTask(
     50       BrowserThread::IO,
     51       FROM_HERE,
     52       base::Bind(
     53           &BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread,
     54           this,
     55           origin));
     56 }
     57 
     58 void BrowsingDataServiceWorkerHelper::FetchServiceWorkerUsageInfoOnIOThread() {
     59   DCHECK_CURRENTLY_ON(BrowserThread::IO);
     60 
     61   service_worker_context_->GetAllOriginsInfo(base::Bind(
     62       &BrowsingDataServiceWorkerHelper::GetAllOriginsInfoCallback, this));
     63 }
     64 
     65 void BrowsingDataServiceWorkerHelper::GetAllOriginsInfoCallback(
     66     const std::vector<ServiceWorkerUsageInfo>& origins) {
     67   DCHECK_CURRENTLY_ON(BrowserThread::IO);
     68   for (std::vector<ServiceWorkerUsageInfo>::const_iterator iter =
     69            origins.begin();
     70        iter != origins.end();
     71        ++iter) {
     72     const ServiceWorkerUsageInfo& origin = *iter;
     73     if (!BrowsingDataHelper::HasWebScheme(origin.origin))
     74       continue;  // Non-websafe state is not considered browsing data.
     75 
     76     service_worker_info_.push_back(origin);
     77   }
     78 
     79   BrowserThread::PostTask(
     80       BrowserThread::UI,
     81       FROM_HERE,
     82       base::Bind(&BrowsingDataServiceWorkerHelper::NotifyOnUIThread, this));
     83 }
     84 
     85 void BrowsingDataServiceWorkerHelper::NotifyOnUIThread() {
     86   DCHECK_CURRENTLY_ON(BrowserThread::UI);
     87   DCHECK(is_fetching_);
     88   completion_callback_.Run(service_worker_info_);
     89   completion_callback_.Reset();
     90   is_fetching_ = false;
     91 }
     92 
     93 void BrowsingDataServiceWorkerHelper::DeleteServiceWorkersOnIOThread(
     94     const GURL& origin) {
     95   DCHECK_CURRENTLY_ON(BrowserThread::IO);
     96   service_worker_context_->DeleteForOrigin(origin);
     97 }
     98 
     99 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
    100     PendingServiceWorkerUsageInfo(const GURL& origin,
    101                                   const std::vector<GURL>& scopes)
    102     : origin(origin), scopes(scopes) {
    103 }
    104 
    105 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo::
    106     ~PendingServiceWorkerUsageInfo() {
    107 }
    108 
    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;
    114 }
    115 
    116 CannedBrowsingDataServiceWorkerHelper::CannedBrowsingDataServiceWorkerHelper(
    117     content::ServiceWorkerContext* context)
    118     : BrowsingDataServiceWorkerHelper(context) {
    119 }
    120 
    121 CannedBrowsingDataServiceWorkerHelper::
    122     ~CannedBrowsingDataServiceWorkerHelper() {
    123 }
    124 
    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.
    129 
    130   pending_service_worker_info_.insert(
    131       PendingServiceWorkerUsageInfo(origin, scopes));
    132 }
    133 
    134 void CannedBrowsingDataServiceWorkerHelper::Reset() {
    135   service_worker_info_.clear();
    136   pending_service_worker_info_.clear();
    137 }
    138 
    139 bool CannedBrowsingDataServiceWorkerHelper::empty() const {
    140   return service_worker_info_.empty() && pending_service_worker_info_.empty();
    141 }
    142 
    143 size_t CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerCount() const {
    144   return pending_service_worker_info_.size();
    145 }
    146 
    147 const std::set<
    148     CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo>&
    149 CannedBrowsingDataServiceWorkerHelper::GetServiceWorkerUsageInfo() const {
    150   return pending_service_worker_info_;
    151 }
    152 
    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());
    157 
    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();
    162        ++pending_info) {
    163     ServiceWorkerUsageInfo info(
    164         pending_info->origin, pending_info->scopes);
    165     result.push_back(info);
    166   }
    167 
    168   BrowserThread::PostTask(
    169       BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
    170 }
    171 
    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++);
    179     else
    180       ++it;
    181   }
    182   BrowsingDataServiceWorkerHelper::DeleteServiceWorkers(origin);
    183 }
    184