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/public/child/worker_thread.h"
15 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerError.h"
16 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerProvider.h"
17 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
18 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerState.h"
23 class SingleThreadTaskRunner
;
34 struct ServiceWorkerMsg_MessageToDocument_Params
;
38 class ServiceWorkerMessageFilter
;
39 class ServiceWorkerProviderContext
;
40 class ServiceWorkerRegistrationHandleReference
;
41 class ThreadSafeSender
;
42 class WebServiceWorkerImpl
;
43 class WebServiceWorkerRegistrationImpl
;
44 struct ServiceWorkerObjectInfo
;
45 struct ServiceWorkerRegistrationObjectInfo
;
46 struct ServiceWorkerVersionAttributes
;
48 // This class manages communication with the browser process about
49 // registration of the service worker, exposed to renderer and worker
50 // scripts through methods like navigator.registerServiceWorker().
51 class CONTENT_EXPORT ServiceWorkerDispatcher
: public WorkerThread::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().
94 void GetRegistration(int provider_id
,
95 const GURL
& document_url
,
96 WebServiceWorkerGetRegistrationCallbacks
* callbacks
);
97 // Corresponds to navigator.serviceWorker.getRegistrations().
98 void GetRegistrations(
100 WebServiceWorkerGetRegistrationsCallbacks
* callbacks
);
102 void GetRegistrationForReady(
104 WebServiceWorkerGetRegistrationForReadyCallbacks
* callbacks
);
106 // Called when a new provider context for a document is created. Usually
107 // this happens when a new document is being loaded, and is called much
108 // earlier than AddScriptClient.
109 // (This is attached only to the document thread's ServiceWorkerDispatcher)
110 void AddProviderContext(ServiceWorkerProviderContext
* provider_context
);
111 void RemoveProviderContext(ServiceWorkerProviderContext
* provider_context
);
113 // Called when navigator.serviceWorker is instantiated or detached
114 // for a document whose provider can be identified by |provider_id|.
115 void AddProviderClient(int provider_id
,
116 blink::WebServiceWorkerProviderClient
* client
);
117 void RemoveProviderClient(int provider_id
);
119 // If an existing WebServiceWorkerImpl exists for the Service
120 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
121 // created and its ownership is transferred to the caller. If
122 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
123 // adopted for the specified Service Worker.
125 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
126 // method can return an existing WebServiceWorkerImpl, in which case
127 // it is owned by a WebCore::ServiceWorker and the lifetime is not
128 // being transferred to the owner; or it can create a
129 // WebServiceWorkerImpl, in which case ownership is transferred to
130 // the caller who must bounce it to a method that will associate it
131 // with a WebCore::ServiceWorker.
132 WebServiceWorkerImpl
* GetServiceWorker(
133 const ServiceWorkerObjectInfo
& info
,
136 // Returns a new registration filled in with version attributes.
137 scoped_refptr
<WebServiceWorkerRegistrationImpl
> CreateRegistration(
138 const ServiceWorkerRegistrationObjectInfo
& info
,
139 const ServiceWorkerVersionAttributes
& attrs
);
140 scoped_refptr
<WebServiceWorkerRegistrationImpl
> AdoptRegistration(
141 const ServiceWorkerRegistrationObjectInfo
& info
,
142 const ServiceWorkerVersionAttributes
& attrs
);
144 static ServiceWorkerDispatcher
* GetOrCreateThreadSpecificInstance(
145 ThreadSafeSender
* thread_safe_sender
,
146 base::SingleThreadTaskRunner
* main_thread_task_runner
);
148 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
149 // instance if thread-local instance doesn't exist.
150 static ServiceWorkerDispatcher
* GetThreadSpecificInstance();
152 base::SingleThreadTaskRunner
* main_thread_task_runner() {
153 return main_thread_task_runner_
.get();
157 typedef IDMap
<WebServiceWorkerRegistrationCallbacks
,
158 IDMapOwnPointer
> RegistrationCallbackMap
;
159 typedef IDMap
<WebServiceWorkerUpdateCallbacks
, IDMapOwnPointer
>
161 typedef IDMap
<WebServiceWorkerUnregistrationCallbacks
,
162 IDMapOwnPointer
> UnregistrationCallbackMap
;
163 typedef IDMap
<WebServiceWorkerGetRegistrationCallbacks
,
164 IDMapOwnPointer
> GetRegistrationCallbackMap
;
165 typedef IDMap
<WebServiceWorkerGetRegistrationsCallbacks
,
166 IDMapOwnPointer
> GetRegistrationsCallbackMap
;
167 typedef IDMap
<WebServiceWorkerGetRegistrationForReadyCallbacks
,
168 IDMapOwnPointer
> GetRegistrationForReadyCallbackMap
;
170 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*>
172 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
173 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
174 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
175 typedef std::map
<int, WebServiceWorkerRegistrationImpl
*>
176 RegistrationObjectMap
;
178 friend class ServiceWorkerDispatcherTest
;
179 friend class WebServiceWorkerImpl
;
180 friend class WebServiceWorkerRegistrationImpl
;
182 // WorkerThread::Observer implementation.
183 void WillStopCurrentWorkerThread() override
;
185 void OnAssociateRegistrationWithServiceWorker(
188 const ServiceWorkerRegistrationObjectInfo
& info
,
189 const ServiceWorkerVersionAttributes
& attrs
);
190 void OnAssociateRegistration(int thread_id
,
192 const ServiceWorkerRegistrationObjectInfo
& info
,
193 const ServiceWorkerVersionAttributes
& attrs
);
194 void OnDisassociateRegistration(int thread_id
,
196 void OnRegistered(int thread_id
,
198 const ServiceWorkerRegistrationObjectInfo
& info
,
199 const ServiceWorkerVersionAttributes
& attrs
);
200 void OnUpdated(int thread_id
, int request_id
);
201 void OnUnregistered(int thread_id
,
204 void OnDidGetRegistration(int thread_id
,
206 const ServiceWorkerRegistrationObjectInfo
& info
,
207 const ServiceWorkerVersionAttributes
& attrs
);
208 void OnDidGetRegistrations(
211 const std::vector
<ServiceWorkerRegistrationObjectInfo
>& infos
,
212 const std::vector
<ServiceWorkerVersionAttributes
>& attrs
);
213 void OnDidGetRegistrationForReady(
216 const ServiceWorkerRegistrationObjectInfo
& info
,
217 const ServiceWorkerVersionAttributes
& attrs
);
218 void OnRegistrationError(int thread_id
,
220 blink::WebServiceWorkerError::ErrorType error_type
,
221 const base::string16
& message
);
222 void OnUpdateError(int thread_id
,
224 blink::WebServiceWorkerError::ErrorType error_type
,
225 const base::string16
& message
);
226 void OnUnregistrationError(int thread_id
,
228 blink::WebServiceWorkerError::ErrorType error_type
,
229 const base::string16
& message
);
230 void OnGetRegistrationError(
233 blink::WebServiceWorkerError::ErrorType error_type
,
234 const base::string16
& message
);
235 void OnGetRegistrationsError(
238 blink::WebServiceWorkerError::ErrorType error_type
,
239 const base::string16
& message
);
240 void OnServiceWorkerStateChanged(int thread_id
,
242 blink::WebServiceWorkerState state
);
243 void OnSetVersionAttributes(int thread_id
,
245 int registration_handle_id
,
247 const ServiceWorkerVersionAttributes
& attributes
);
248 void OnUpdateFound(int thread_id
,
249 int registration_handle_id
);
250 void OnSetControllerServiceWorker(int thread_id
,
252 const ServiceWorkerObjectInfo
& info
,
253 bool should_notify_controllerchange
);
254 void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params
& params
);
256 // Keeps map from handle_id to ServiceWorker object.
257 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
258 void RemoveServiceWorker(int handle_id
);
260 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
261 void AddServiceWorkerRegistration(
262 int registration_handle_id
,
263 WebServiceWorkerRegistrationImpl
* registration
);
264 void RemoveServiceWorkerRegistration(
265 int registration_handle_id
);
267 scoped_refptr
<WebServiceWorkerRegistrationImpl
> CreateRegistrationInternal(
268 scoped_ptr
<ServiceWorkerRegistrationHandleReference
> handle_ref
,
269 const ServiceWorkerVersionAttributes
& attrs
,
272 RegistrationCallbackMap pending_registration_callbacks_
;
273 UpdateCallbackMap pending_update_callbacks_
;
274 UnregistrationCallbackMap pending_unregistration_callbacks_
;
275 GetRegistrationCallbackMap pending_get_registration_callbacks_
;
276 GetRegistrationsCallbackMap pending_get_registrations_callbacks_
;
277 GetRegistrationForReadyCallbackMap get_for_ready_callbacks_
;
279 ProviderClientMap provider_clients_
;
280 ProviderContextMap provider_contexts_
;
282 WorkerObjectMap service_workers_
;
283 RegistrationObjectMap registrations_
;
285 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
286 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner_
;
288 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
291 } // namespace content
293 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_