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 ResourceContext
;
32 class ServiceWorkerContextCore
;
33 class ServiceWorkerContextObserver
;
34 class StoragePartitionImpl
;
36 // A refcounted wrapper class for our core object. Higher level content lib
37 // classes keep references to this class on mutliple threads. The inner core
38 // instance is strictly single threaded and is not refcounted, the core object
39 // is what is used internally in the service worker lib.
40 class CONTENT_EXPORT ServiceWorkerContextWrapper
41 : NON_EXPORTED_BASE(public ServiceWorkerContext
),
42 public base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
> {
44 using StatusCallback
= base::Callback
<void(ServiceWorkerStatusCode
)>;
45 using FindRegistrationCallback
=
46 ServiceWorkerStorage::FindRegistrationCallback
;
47 using GetRegistrationsInfosCallback
=
48 ServiceWorkerStorage::GetRegistrationsInfosCallback
;
49 using GetUserDataCallback
= ServiceWorkerStorage::GetUserDataCallback
;
50 using GetUserDataForAllRegistrationsCallback
=
51 ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
;
53 ServiceWorkerContextWrapper(BrowserContext
* browser_context
);
55 // Init and Shutdown are for use on the UI thread when the profile,
56 // storagepartition is being setup and torn down.
57 void Init(const base::FilePath
& user_data_directory
,
58 storage::QuotaManagerProxy
* quota_manager_proxy
,
59 storage::SpecialStoragePolicy
* special_storage_policy
);
62 // Deletes all files on disk and restarts the system asynchronously. This
63 // leaves the system in a disabled state until it's done. This should be
64 // called on the IO thread.
65 void DeleteAndStartOver();
67 // The StoragePartition should only be used on the UI thread.
68 // Can be null before/during init and during/after shutdown.
69 StoragePartitionImpl
* storage_partition() const;
71 void set_storage_partition(StoragePartitionImpl
* storage_partition
);
73 // The ResourceContext for the associated BrowserContext. This should only
74 // be accessed on the IO thread, and can be null during initialization and
76 ResourceContext
* resource_context();
78 void set_resource_context(ResourceContext
* resource_context
);
80 // The process manager can be used on either UI or IO.
81 ServiceWorkerProcessManager
* process_manager() {
82 return process_manager_
.get();
85 // ServiceWorkerContext implementation:
86 void RegisterServiceWorker(const GURL
& pattern
,
87 const GURL
& script_url
,
88 const ResultCallback
& continuation
) override
;
89 void UnregisterServiceWorker(const GURL
& pattern
,
90 const ResultCallback
& continuation
) override
;
91 void CanHandleMainResourceOffline(
93 const GURL
& first_party
,
94 const net::CompletionCallback
& callback
) override
;
95 void GetAllOriginsInfo(const GetUsageInfoCallback
& callback
) override
;
96 void DeleteForOrigin(const GURL
& origin
,
97 const ResultCallback
& callback
) override
;
98 void CheckHasServiceWorker(
100 const GURL
& other_url
,
101 const CheckHasServiceWorkerCallback
& callback
) override
;
102 void StopAllServiceWorkersForOrigin(const GURL
& origin
) override
;
103 void ClearAllServiceWorkersForTest(const base::Closure
& callback
) override
;
105 ServiceWorkerRegistration
* GetLiveRegistration(int64_t registration_id
);
106 ServiceWorkerVersion
* GetLiveVersion(int64_t version_id
);
107 std::vector
<ServiceWorkerRegistrationInfo
> GetAllLiveRegistrationInfo();
108 std::vector
<ServiceWorkerVersionInfo
> GetAllLiveVersionInfo();
110 void FindRegistrationForDocument(const GURL
& document_url
,
111 const FindRegistrationCallback
& callback
);
112 void FindRegistrationForId(int64_t registration_id
,
114 const FindRegistrationCallback
& callback
);
115 void GetAllRegistrations(const GetRegistrationsInfosCallback
& callback
);
116 void GetRegistrationUserData(int64_t registration_id
,
117 const std::string
& key
,
118 const GetUserDataCallback
& callback
);
119 void StoreRegistrationUserData(int64_t registration_id
,
121 const std::string
& key
,
122 const std::string
& data
,
123 const StatusCallback
& callback
);
124 void ClearRegistrationUserData(int64_t registration_id
,
125 const std::string
& key
,
126 const StatusCallback
& callback
);
127 void GetUserDataForAllRegistrations(
128 const std::string
& key
,
129 const GetUserDataForAllRegistrationsCallback
& callback
);
131 void StartServiceWorker(const GURL
& pattern
, const StatusCallback
& callback
);
132 void UpdateRegistration(const GURL
& pattern
);
133 void SimulateSkipWaiting(int64_t version_id
);
134 void SetForceUpdateOnPageLoad(int64_t registration_id
,
135 bool force_update_on_page_load
);
136 void AddObserver(ServiceWorkerContextObserver
* observer
);
137 void RemoveObserver(ServiceWorkerContextObserver
* observer
);
139 bool is_incognito() const { return is_incognito_
; }
142 friend class BackgroundSyncManagerTest
;
143 friend class base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
>;
144 friend class EmbeddedWorkerTestHelper
;
145 friend class EmbeddedWorkerBrowserTest
;
146 friend class ServiceWorkerDispatcherHost
;
147 friend class ServiceWorkerInternalsUI
;
148 friend class ServiceWorkerProcessManager
;
149 friend class ServiceWorkerRequestHandler
;
150 friend class ServiceWorkerVersionBrowserTest
;
151 friend class MockServiceWorkerContextWrapper
;
153 ~ServiceWorkerContextWrapper() override
;
156 const base::FilePath
& user_data_directory
,
157 scoped_ptr
<ServiceWorkerDatabaseTaskManager
> database_task_manager
,
158 const scoped_refptr
<base::SingleThreadTaskRunner
>& disk_cache_thread
,
159 storage::QuotaManagerProxy
* quota_manager_proxy
,
160 storage::SpecialStoragePolicy
* special_storage_policy
);
163 void DidDeleteAndStartOver(ServiceWorkerStatusCode status
);
165 void DidGetAllRegistrationsForGetAllOrigins(
166 const GetUsageInfoCallback
& callback
,
167 const std::vector
<ServiceWorkerRegistrationInfo
>& registrations
);
169 void DidFindRegistrationForCheckHasServiceWorker(
170 const GURL
& other_url
,
171 const CheckHasServiceWorkerCallback
& callback
,
172 ServiceWorkerStatusCode status
,
173 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
175 void DidFindRegistrationForUpdate(
176 ServiceWorkerStatusCode status
,
177 const scoped_refptr
<content::ServiceWorkerRegistration
>& registration
);
179 // The core context is only for use on the IO thread.
180 // Can be null before/during init, during/after shutdown, and after
181 // DeleteAndStartOver fails.
182 ServiceWorkerContextCore
* context();
184 const scoped_refptr
<base::ObserverListThreadSafe
<
185 ServiceWorkerContextObserver
>> observer_list_
;
186 const scoped_ptr
<ServiceWorkerProcessManager
> process_manager_
;
187 // Cleared in Shutdown():
188 scoped_ptr
<ServiceWorkerContextCore
> context_core_
;
190 // Initialized in Init(); true if the user data directory is empty.
193 // Raw pointer to the StoragePartitionImpl owning |this|.
194 StoragePartitionImpl
* storage_partition_
;
196 // The ResourceContext associated with this context.
197 ResourceContext
* resource_context_
;
199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContextWrapper
);
202 } // namespace content
204 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_