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_
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"
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
31 class BrowsingDataServiceWorkerHelper
32 : public base::RefCountedThreadSafe
<BrowsingDataServiceWorkerHelper
> {
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
);
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
>&)>
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_
= false;
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
{
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;
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.
117 // True if no Service Workers are currently stored.
120 // Returns the number of currently stored Service Workers.
121 size_t GetServiceWorkerCount() const;
123 // Returns the current list of Service Workers.
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
;
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_