Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / browsing_data / browsing_data_service_worker_helper.h
blob100741fb1099d0c92b6709c5a3770323c6d3e20f
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 #ifndef CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_
6 #define CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_
8 #include <list>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
23 class Profile;
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.
31 class 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);
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);
46 protected:
47 virtual ~BrowsingDataServiceWorkerHelper();
49 // Owned by the profile.
50 content::ServiceWorkerContext* service_worker_context_;
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_;
60 // This member is only mutated on the UI thread.
61 base::Callback<void(const std::list<content::ServiceWorkerUsageInfo>&)>
62 completion_callback_;
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_;
70 private:
71 friend class base::RefCountedThreadSafe<BrowsingDataServiceWorkerHelper>;
73 // Enumerates all Service Worker instances on the IO thread.
74 void FetchServiceWorkerUsageInfoOnIOThread();
76 // Notifies the completion callback in the UI thread.
77 void NotifyOnUIThread();
79 // Deletes Service Workers for an origin the IO thread.
80 void DeleteServiceWorkersOnIOThread(const GURL& origin);
82 // Callback from ServiceWorkerContext::GetAllOriginsInfo()
83 void GetAllOriginsInfoCallback(
84 const std::vector<content::ServiceWorkerUsageInfo>& origins);
86 DISALLOW_COPY_AND_ASSIGN(BrowsingDataServiceWorkerHelper);
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.
92 class 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();
101 bool operator<(const PendingServiceWorkerUsageInfo& other) const;
103 GURL origin;
104 std::vector<GURL> scopes;
107 explicit CannedBrowsingDataServiceWorkerHelper(
108 content::ServiceWorkerContext* context);
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);
114 // Clear the list of canned Service Workers.
115 void Reset();
117 // True if no Service Workers are currently stored.
118 bool empty() const;
120 // Returns the number of currently stored Service Workers.
121 size_t GetServiceWorkerCount() const;
123 // Returns the current list of Service Workers.
124 const std::set<
125 CannedBrowsingDataServiceWorkerHelper::PendingServiceWorkerUsageInfo>&
126 GetServiceWorkerUsageInfo() const;
128 // BrowsingDataServiceWorkerHelper methods.
129 void StartFetching(const base::Callback<void(
130 const std::list<content::ServiceWorkerUsageInfo>&)>& callback) override;
131 void DeleteServiceWorkers(const GURL& origin) override;
133 private:
134 ~CannedBrowsingDataServiceWorkerHelper() override;
136 std::set<PendingServiceWorkerUsageInfo> pending_service_worker_info_;
138 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataServiceWorkerHelper);
141 #endif // CHROME_BROWSER_BROWSING_DATA_BROWSING_DATA_SERVICE_WORKER_HELPER_H_