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_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
6 #define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
11 #include "base/id_map.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/string16.h"
14 #include "content/child/worker_task_runner.h"
15 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
16 #include "third_party/WebKit/public/platform/WebServiceWorkerProvider.h"
17 #include "third_party/WebKit/public/platform/WebServiceWorkerRegistration.h"
18 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
23 class SingleThreadTaskRunner
;
34 struct ServiceWorkerMsg_MessageToDocument_Params
;
38 class ServiceWorkerMessageFilter
;
39 class ServiceWorkerProviderContext
;
40 class ThreadSafeSender
;
41 class WebServiceWorkerImpl
;
42 class WebServiceWorkerRegistrationImpl
;
43 struct ServiceWorkerObjectInfo
;
44 struct ServiceWorkerRegistrationObjectInfo
;
45 struct ServiceWorkerVersionAttributes
;
47 // This class manages communication with the browser process about
48 // registration of the service worker, exposed to renderer and worker
49 // scripts through methods like navigator.registerServiceWorker().
50 class CONTENT_EXPORT ServiceWorkerDispatcher
51 : public WorkerTaskRunner::Observer
{
53 typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
54 WebServiceWorkerRegistrationCallbacks
;
55 typedef blink::WebServiceWorkerRegistration::WebServiceWorkerUpdateCallbacks
56 WebServiceWorkerUpdateCallbacks
;
57 typedef blink::WebServiceWorkerRegistration::
58 WebServiceWorkerUnregistrationCallbacks
59 WebServiceWorkerUnregistrationCallbacks
;
61 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
62 WebServiceWorkerGetRegistrationCallbacks
;
64 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationsCallbacks
65 WebServiceWorkerGetRegistrationsCallbacks
;
66 typedef blink::WebServiceWorkerProvider::
67 WebServiceWorkerGetRegistrationForReadyCallbacks
68 WebServiceWorkerGetRegistrationForReadyCallbacks
;
70 ServiceWorkerDispatcher(
71 ThreadSafeSender
* thread_safe_sender
,
72 base::SingleThreadTaskRunner
* main_thread_task_runner
);
73 ~ServiceWorkerDispatcher() override
;
75 void OnMessageReceived(const IPC::Message
& msg
);
76 bool Send(IPC::Message
* msg
);
78 // Corresponds to navigator.serviceWorker.register().
79 void RegisterServiceWorker(
82 const GURL
& script_url
,
83 WebServiceWorkerRegistrationCallbacks
* callbacks
);
84 // Corresponds to ServiceWorkerRegistration.update().
85 void UpdateServiceWorker(int provider_id
,
86 int64 registration_id
,
87 WebServiceWorkerUpdateCallbacks
* callbacks
);
88 // Corresponds to ServiceWorkerRegistration.unregister().
89 void UnregisterServiceWorker(
91 int64 registration_id
,
92 WebServiceWorkerUnregistrationCallbacks
* callbacks
);
93 // Corresponds to navigator.serviceWorker.getRegistration().
96 const GURL
& document_url
,
97 WebServiceWorkerRegistrationCallbacks
* callbacks
);
98 // Corresponds to navigator.serviceWorker.getRegistrations().
99 void GetRegistrations(
101 WebServiceWorkerGetRegistrationsCallbacks
* callbacks
);
103 void GetRegistrationForReady(
105 WebServiceWorkerGetRegistrationForReadyCallbacks
* callbacks
);
107 // Called when a new provider context for a document is created. Usually
108 // this happens when a new document is being loaded, and is called much
109 // earlier than AddScriptClient.
110 // (This is attached only to the document thread's ServiceWorkerDispatcher)
111 void AddProviderContext(ServiceWorkerProviderContext
* provider_context
);
112 void RemoveProviderContext(ServiceWorkerProviderContext
* provider_context
);
114 // Called when navigator.serviceWorker is instantiated or detached
115 // for a document whose provider can be identified by |provider_id|.
116 void AddProviderClient(int provider_id
,
117 blink::WebServiceWorkerProviderClient
* client
);
118 void RemoveProviderClient(int provider_id
);
120 // If an existing WebServiceWorkerImpl exists for the Service
121 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
122 // created and its ownership is transferred to the caller. If
123 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
124 // adopted for the specified Service Worker.
126 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
127 // method can return an existing WebServiceWorkerImpl, in which case
128 // it is owned by a WebCore::ServiceWorker and the lifetime is not
129 // being transferred to the owner; or it can create a
130 // WebServiceWorkerImpl, in which case ownership is transferred to
131 // the caller who must bounce it to a method that will associate it
132 // with a WebCore::ServiceWorker.
133 WebServiceWorkerImpl
* GetServiceWorker(
134 const ServiceWorkerObjectInfo
& info
,
137 // Creates a WebServiceWorkerRegistrationImpl for the specified registration
138 // and transfers its ownership to the caller. If |adopt_handle| is true, a
139 // ServiceWorkerRegistrationHandleReference will be adopted for the
141 WebServiceWorkerRegistrationImpl
* CreateServiceWorkerRegistration(
142 const ServiceWorkerRegistrationObjectInfo
& info
,
145 static ServiceWorkerDispatcher
* GetOrCreateThreadSpecificInstance(
146 ThreadSafeSender
* thread_safe_sender
,
147 base::SingleThreadTaskRunner
* main_thread_task_runner
);
149 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
150 // instance if thread-local instance doesn't exist.
151 static ServiceWorkerDispatcher
* GetThreadSpecificInstance();
153 base::SingleThreadTaskRunner
* main_thread_task_runner() {
154 return main_thread_task_runner_
.get();
158 typedef IDMap
<WebServiceWorkerRegistrationCallbacks
,
159 IDMapOwnPointer
> RegistrationCallbackMap
;
160 typedef IDMap
<WebServiceWorkerUpdateCallbacks
, IDMapOwnPointer
>
162 typedef IDMap
<WebServiceWorkerUnregistrationCallbacks
,
163 IDMapOwnPointer
> UnregistrationCallbackMap
;
164 typedef IDMap
<WebServiceWorkerGetRegistrationCallbacks
,
165 IDMapOwnPointer
> GetRegistrationCallbackMap
;
166 typedef IDMap
<WebServiceWorkerGetRegistrationsCallbacks
,
167 IDMapOwnPointer
> GetRegistrationsCallbackMap
;
168 typedef IDMap
<WebServiceWorkerGetRegistrationForReadyCallbacks
,
169 IDMapOwnPointer
> GetRegistrationForReadyCallbackMap
;
171 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*>
173 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
174 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
175 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
176 typedef std::map
<int, WebServiceWorkerRegistrationImpl
*>
177 RegistrationObjectMap
;
179 friend class ServiceWorkerDispatcherTest
;
180 friend class WebServiceWorkerImpl
;
181 friend class WebServiceWorkerRegistrationImpl
;
183 // WorkerTaskRunner::Observer implementation.
184 void OnWorkerRunLoopStopped() override
;
186 void OnAssociateRegistrationWithServiceWorker(
189 const ServiceWorkerRegistrationObjectInfo
& info
,
190 const ServiceWorkerVersionAttributes
& attrs
);
191 void OnAssociateRegistration(int thread_id
,
193 const ServiceWorkerRegistrationObjectInfo
& info
,
194 const ServiceWorkerVersionAttributes
& attrs
);
195 void OnDisassociateRegistration(int thread_id
,
197 void OnRegistered(int thread_id
,
199 const ServiceWorkerRegistrationObjectInfo
& info
,
200 const ServiceWorkerVersionAttributes
& attrs
);
201 void OnUpdated(int thread_id
, int request_id
);
202 void OnUnregistered(int thread_id
,
205 void OnDidGetRegistration(int thread_id
,
207 const ServiceWorkerRegistrationObjectInfo
& info
,
208 const ServiceWorkerVersionAttributes
& attrs
);
209 void OnDidGetRegistrations(
212 const std::vector
<ServiceWorkerRegistrationObjectInfo
>& infos
,
213 const std::vector
<ServiceWorkerVersionAttributes
>& attrs
);
214 void OnDidGetRegistrationForReady(
217 const ServiceWorkerRegistrationObjectInfo
& info
,
218 const ServiceWorkerVersionAttributes
& attrs
);
219 void OnRegistrationError(int thread_id
,
221 blink::WebServiceWorkerError::ErrorType error_type
,
222 const base::string16
& message
);
223 void OnUpdateError(int thread_id
,
225 blink::WebServiceWorkerError::ErrorType error_type
,
226 const base::string16
& message
);
227 void OnUnregistrationError(int thread_id
,
229 blink::WebServiceWorkerError::ErrorType error_type
,
230 const base::string16
& message
);
231 void OnGetRegistrationError(
234 blink::WebServiceWorkerError::ErrorType error_type
,
235 const base::string16
& message
);
236 void OnGetRegistrationsError(
239 blink::WebServiceWorkerError::ErrorType error_type
,
240 const base::string16
& message
);
241 void OnServiceWorkerStateChanged(int thread_id
,
243 blink::WebServiceWorkerState state
);
244 void OnSetVersionAttributes(int thread_id
,
246 int registration_handle_id
,
248 const ServiceWorkerVersionAttributes
& attributes
);
249 void OnUpdateFound(int thread_id
,
250 int registration_handle_id
);
251 void OnSetControllerServiceWorker(int thread_id
,
253 const ServiceWorkerObjectInfo
& info
,
254 bool should_notify_controllerchange
);
255 void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params
& params
);
257 // Keeps map from handle_id to ServiceWorker object.
258 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
259 void RemoveServiceWorker(int handle_id
);
261 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
262 void AddServiceWorkerRegistration(
263 int registration_handle_id
,
264 WebServiceWorkerRegistrationImpl
* registration
);
265 void RemoveServiceWorkerRegistration(
266 int registration_handle_id
);
268 // Returns an existing registration or new one filled in with version
269 // attributes. This function assumes given |info| and |attrs| retain handle
270 // references and always adopts them.
271 // TODO(nhiroki): This assumption seems to impair readability. We could
272 // explictly pass ServiceWorker(Registration)HandleReference instead.
273 WebServiceWorkerRegistrationImpl
* FindOrCreateRegistration(
274 const ServiceWorkerRegistrationObjectInfo
& info
,
275 const ServiceWorkerVersionAttributes
& attrs
);
277 RegistrationCallbackMap pending_registration_callbacks_
;
278 UpdateCallbackMap pending_update_callbacks_
;
279 UnregistrationCallbackMap pending_unregistration_callbacks_
;
280 GetRegistrationCallbackMap pending_get_registration_callbacks_
;
281 GetRegistrationsCallbackMap pending_get_registrations_callbacks_
;
282 GetRegistrationForReadyCallbackMap get_for_ready_callbacks_
;
284 ProviderClientMap provider_clients_
;
285 ProviderContextMap provider_contexts_
;
287 WorkerObjectMap service_workers_
;
288 RegistrationObjectMap registrations_
;
290 // A map for ServiceWorkers that are associated to a particular document
291 // (e.g. as .current).
292 WorkerToProviderMap worker_to_provider_
;
294 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
295 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
297 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
300 } // namespace content
302 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_