browsing_data_service_worker_helper.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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#ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_
6#define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_
7
8#include <list>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/callback.h"
14#include "base/compiler_specific.h"
15#include "base/files/file_path.h"
16#include "base/memory/ref_counted.h"
17#include "base/synchronization/lock.h"
18#include "base/time/time.h"
19#include "content/public/browser/service_worker_context.h"
20#include "content/public/browser/service_worker_usage_info.h"
21#include "url/gurl.h"
22
23class Profile;
24
25// BrowsingDataServiceWorkerHelper is an interface for classes dealing with
26// aggregating and deleting browsing data stored for Service Workers -
27// registrations, scripts, and caches.
28// A client of this class need to call StartFetching from the UI thread to
29// initiate the flow, and it'll be notified by the callback in its UI thread at
30// some later point.
31class BrowsingDataServiceWorkerHelper
32    : public base::RefCountedThreadSafe<BrowsingDataServiceWorkerHelper> {
33 public:
34  // Create a BrowsingDataServiceWorkerHelper instance for the Service Workers
35  // stored in |context|'s associated profile's user data directory.
36  explicit BrowsingDataServiceWorkerHelper(
37      content::ServiceWorkerContext* context);
38
39  // Starts the fetching process, which will notify its completion via
40  // |callback|. This must be called only in the UI thread.
41  virtual void StartFetching(const base::Callback<
42      void(const std::list<content::ServiceWorkerUsageInfo>&)>& callback);
43  // Requests the Service Worker data for an origin be deleted.
44  virtual void DeleteServiceWorkers(const GURL& origin);
45
46 protected:
47  virtual ~BrowsingDataServiceWorkerHelper();
48
49  // Owned by the profile.
50  content::ServiceWorkerContext* service_worker_context_;
51
52  // Access to |service_worker_info_| is triggered indirectly via the UI
53  // thread and guarded by |is_fetching_|. This means |service_worker_info_|
54  // is only accessed while |is_fetching_| is true. The flag |is_fetching_| is
55  // only accessed on the UI thread.
56  // In the context of this class |service_worker_info_| is only accessed on the
57  // context's ServiceWorker thread.
58  std::list<content::ServiceWorkerUsageInfo> service_worker_info_;
59
60  // This member is only mutated on the UI thread.
61  base::Callback<void(const std::list<content::ServiceWorkerUsageInfo>&)>
62      completion_callback_;
63
64  // Indicates whether or not we're currently fetching information:
65  // it's true when StartFetching() is called in the UI thread, and it's reset
66  // after we notified the callback in the UI thread.
67  // This member is only mutated on the UI thread.
68  bool is_fetching_;
69
70 private:
71  friend class base::RefCountedThreadSafe<BrowsingDataServiceWorkerHelper>;
72
73  // Enumerates all Service Worker instances on the IO thread.
74  void FetchServiceWorkerUsageInfoOnIOThread();
75
76  // Notifies the completion callback in the UI thread.
77  void NotifyOnUIThread();
78
79  // Deletes Service Workers for an origin the IO thread.
80  void DeleteServiceWorkersOnIOThread(const GURL& origin);
81
82  // Callback from ServiceWorkerContext::GetAllOriginsInfo()
83  void GetAllOriginsInfoCallback(
84      const std::vector<content::ServiceWorkerUsageInfo>& origins);
85
86  DISALLOW_COPY_AND_ASSIGN(BrowsingDataServiceWorkerHelper);
87};
88
89// This class is an implementation of BrowsingDataServiceWorkerHelper that does
90// not fetch its information from the Service Worker context, but is passed the
91// info as a parameter.
92class CannedBrowsingDataServiceWorkerHelper
93    : public BrowsingDataServiceWorkerHelper {
94 public:
95  // Contains information about a Service Worker.
96  struct PendingServiceWorkerUsageInfo {
97    PendingServiceWorkerUsageInfo(const GURL& origin,
98                                  const std::vector<GURL>& scopes);
99    ~PendingServiceWorkerUsageInfo();
100
101    bool operator<(const PendingServiceWorkerUsageInfo& other) const;
102
103    GURL origin;
104    std::vector<GURL> scopes;
105  };
106
107  explicit CannedBrowsingDataServiceWorkerHelper(
108      content::ServiceWorkerContext* context);
109
110  // Add a Service Worker to the set of canned Service Workers that is
111  // returned by this helper.
112  void AddServiceWorker(const GURL& origin, const std::vector<GURL>& scopes);
113
114  // Clear the list of canned Service Workers.
115  void Reset();
116
117  // True if no Service Workers are currently stored.
118  bool empty() const;
119
120  // Returns the number of currently stored Service Workers.
121  size_t GetServiceWorkerCount() const;
122
123  // Returns the current list of Service Workers.
124  const std::set<
125      CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo>&
126      GetServiceWorkerUsageInfo() const;
127
128  // BrowsingDataServiceWorkerHelper methods.
129  virtual void StartFetching(const base::Callback<void(
130      const std::list<content::ServiceWorkerUsageInfo>&)>& callback) OVERRIDE;
131  virtual void DeleteServiceWorkers(const GURL& origin) OVERRIDE;
132
133 private:
134  virtual ~CannedBrowsingDataServiceWorkerHelper();
135
136  std::set<PendingServiceWorkerUsageInfo> pending_service_worker_info_;
137
138  DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataServiceWorkerHelper);
139};
140
141#endif  // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_
142