1 // Copyright 2013 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 CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/service_worker_context.h"
19 class SequencedTaskRunner
;
20 class SingleThreadTaskRunner
;
24 class QuotaManagerProxy
;
25 class SpecialStoragePolicy
;
31 class ServiceWorkerContextCore
;
32 class ServiceWorkerContextObserver
;
33 class StoragePartitionImpl
;
35 // A refcounted wrapper class for our core object. Higher level content lib
36 // classes keep references to this class on mutliple threads. The inner core
37 // instance is strictly single threaded and is not refcounted, the core object
38 // is what is used internally in the service worker lib.
39 class CONTENT_EXPORT ServiceWorkerContextWrapper
40 : NON_EXPORTED_BASE(public ServiceWorkerContext
),
41 public base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
> {
43 using StatusCallback
= base::Callback
<void(ServiceWorkerStatusCode
)>;
44 using FindRegistrationCallback
=
45 ServiceWorkerStorage::FindRegistrationCallback
;
46 using GetRegistrationsInfosCallback
=
47 ServiceWorkerStorage::GetRegistrationsInfosCallback
;
48 using GetUserDataCallback
= ServiceWorkerStorage::GetUserDataCallback
;
49 using GetUserDataForAllRegistrationsCallback
=
50 ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
;
52 ServiceWorkerContextWrapper(BrowserContext
* browser_context
);
54 // Init and Shutdown are for use on the UI thread when the profile,
55 // storagepartition is being setup and torn down.
56 void Init(const base::FilePath
& user_data_directory
,
57 storage::QuotaManagerProxy
* quota_manager_proxy
,
58 storage::SpecialStoragePolicy
* special_storage_policy
);
61 // Deletes all files on disk and restarts the system asynchronously. This
62 // leaves the system in a disabled state until it's done. This should be
63 // called on the IO thread.
64 void DeleteAndStartOver();
66 // The StoragePartition should only be used on the UI thread.
67 // Can be null before/during init and during/after shutdown.
68 StoragePartitionImpl
* storage_partition() const;
70 void set_storage_partition(StoragePartitionImpl
* storage_partition
);
72 // The process manager can be used on either UI or IO.
73 ServiceWorkerProcessManager
* process_manager() {
74 return process_manager_
.get();
77 // ServiceWorkerContext implementation:
78 void RegisterServiceWorker(const GURL
& pattern
,
79 const GURL
& script_url
,
80 const ResultCallback
& continuation
) override
;
81 void UnregisterServiceWorker(const GURL
& pattern
,
82 const ResultCallback
& continuation
) override
;
83 void CanHandleMainResourceOffline(
85 const GURL
& first_party
,
86 const net::CompletionCallback
& callback
) override
;
87 void GetAllOriginsInfo(const GetUsageInfoCallback
& callback
) override
;
88 void DeleteForOrigin(const GURL
& origin
) override
;
89 void CheckHasServiceWorker(
91 const GURL
& other_url
,
92 const CheckHasServiceWorkerCallback
& callback
) override
;
94 ServiceWorkerRegistration
* GetLiveRegistration(int64_t registration_id
);
95 ServiceWorkerVersion
* GetLiveVersion(int64_t version_id
);
96 std::vector
<ServiceWorkerRegistrationInfo
> GetAllLiveRegistrationInfo();
97 std::vector
<ServiceWorkerVersionInfo
> GetAllLiveVersionInfo();
99 void FindRegistrationForDocument(const GURL
& document_url
,
100 const FindRegistrationCallback
& callback
);
101 void FindRegistrationForId(int64_t registration_id
,
103 const FindRegistrationCallback
& callback
);
104 void GetAllRegistrations(const GetRegistrationsInfosCallback
& callback
);
105 void GetRegistrationUserData(int64_t registration_id
,
106 const std::string
& key
,
107 const GetUserDataCallback
& callback
);
108 void StoreRegistrationUserData(int64_t registration_id
,
110 const std::string
& key
,
111 const std::string
& data
,
112 const StatusCallback
& callback
);
113 void ClearRegistrationUserData(int64_t registration_id
,
114 const std::string
& key
,
115 const StatusCallback
& callback
);
116 void GetUserDataForAllRegistrations(
117 const std::string
& key
,
118 const GetUserDataForAllRegistrationsCallback
& callback
);
120 // DeleteForOrigin with completion callback. Does not exit early, and returns
121 // false if one or more of the deletions fail.
122 virtual void DeleteForOrigin(const GURL
& origin
, const ResultCallback
& done
);
124 void StartServiceWorker(const GURL
& pattern
, const StatusCallback
& callback
);
125 void UpdateRegistration(const GURL
& pattern
);
126 void SimulateSkipWaiting(int64_t version_id
);
127 void AddObserver(ServiceWorkerContextObserver
* observer
);
128 void RemoveObserver(ServiceWorkerContextObserver
* observer
);
130 bool is_incognito() const { return is_incognito_
; }
133 friend class BackgroundSyncManagerTest
;
134 friend class base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
>;
135 friend class EmbeddedWorkerTestHelper
;
136 friend class EmbeddedWorkerBrowserTest
;
137 friend class ServiceWorkerDispatcherHost
;
138 friend class ServiceWorkerInternalsUI
;
139 friend class ServiceWorkerProcessManager
;
140 friend class ServiceWorkerRequestHandler
;
141 friend class ServiceWorkerVersionBrowserTest
;
142 friend class MockServiceWorkerContextWrapper
;
144 ~ServiceWorkerContextWrapper() override
;
147 const base::FilePath
& user_data_directory
,
148 scoped_ptr
<ServiceWorkerDatabaseTaskManager
> database_task_manager
,
149 const scoped_refptr
<base::SingleThreadTaskRunner
>& disk_cache_thread
,
150 storage::QuotaManagerProxy
* quota_manager_proxy
,
151 storage::SpecialStoragePolicy
* special_storage_policy
);
154 void DidDeleteAndStartOver(ServiceWorkerStatusCode status
);
156 void DidGetAllRegistrationsForGetAllOrigins(
157 const GetUsageInfoCallback
& callback
,
158 const std::vector
<ServiceWorkerRegistrationInfo
>& registrations
);
160 void DidFindRegistrationForCheckHasServiceWorker(
161 const GURL
& other_url
,
162 const CheckHasServiceWorkerCallback
& callback
,
163 ServiceWorkerStatusCode status
,
164 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
166 void DidFindRegistrationForUpdate(
167 ServiceWorkerStatusCode status
,
168 const scoped_refptr
<content::ServiceWorkerRegistration
>& registration
);
170 // The core context is only for use on the IO thread.
171 // Can be null before/during init, during/after shutdown, and after
172 // DeleteAndStartOver fails.
173 ServiceWorkerContextCore
* context();
175 const scoped_refptr
<base::ObserverListThreadSafe
<
176 ServiceWorkerContextObserver
>> observer_list_
;
177 const scoped_ptr
<ServiceWorkerProcessManager
> process_manager_
;
178 // Cleared in Shutdown():
179 scoped_ptr
<ServiceWorkerContextCore
> context_core_
;
181 // Initialized in Init(); true if the user data directory is empty.
184 // Raw pointer to the StoragePartitionImpl owning |this|.
185 StoragePartitionImpl
* storage_partition_
;
188 } // namespace content
190 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_