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/WebServiceWorkerState.h"
31 class ServiceWorkerMessageFilter
;
32 class ServiceWorkerProviderContext
;
33 class ThreadSafeSender
;
34 class WebServiceWorkerImpl
;
35 class WebServiceWorkerRegistrationImpl
;
36 struct ServiceWorkerObjectInfo
;
37 struct ServiceWorkerRegistrationObjectInfo
;
38 struct ServiceWorkerVersionAttributes
;
39 struct TransferredMessagePort
;
41 // This class manages communication with the browser process about
42 // registration of the service worker, exposed to renderer and worker
43 // scripts through methods like navigator.registerServiceWorker().
44 class CONTENT_EXPORT ServiceWorkerDispatcher
45 : public WorkerTaskRunner::Observer
{
47 typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
48 WebServiceWorkerRegistrationCallbacks
;
50 blink::WebServiceWorkerProvider::WebServiceWorkerUnregistrationCallbacks
51 WebServiceWorkerUnregistrationCallbacks
;
53 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
54 WebServiceWorkerGetRegistrationCallbacks
;
55 typedef blink::WebServiceWorkerProvider::
56 WebServiceWorkerGetRegistrationForReadyCallbacks
57 WebServiceWorkerGetRegistrationForReadyCallbacks
;
59 explicit ServiceWorkerDispatcher(ThreadSafeSender
* thread_safe_sender
);
60 ~ServiceWorkerDispatcher() override
;
62 void OnMessageReceived(const IPC::Message
& msg
);
63 bool Send(IPC::Message
* msg
);
65 // Corresponds to navigator.serviceWorker.register()
66 void RegisterServiceWorker(
69 const GURL
& script_url
,
70 WebServiceWorkerRegistrationCallbacks
* callbacks
);
71 // Corresponds to navigator.serviceWorker.unregister()
72 void UnregisterServiceWorker(
75 WebServiceWorkerUnregistrationCallbacks
* callbacks
);
76 // Corresponds to navigator.serviceWorker.getRegistration()
79 const GURL
& document_url
,
80 WebServiceWorkerRegistrationCallbacks
* callbacks
);
82 void GetRegistrationForReady(
84 WebServiceWorkerGetRegistrationForReadyCallbacks
* callbacks
);
86 // Called when a new provider context for a document is created. Usually
87 // this happens when a new document is being loaded, and is called much
88 // earlier than AddScriptClient.
89 // (This is attached only to the document thread's ServiceWorkerDispatcher)
90 void AddProviderContext(ServiceWorkerProviderContext
* provider_context
);
91 void RemoveProviderContext(ServiceWorkerProviderContext
* provider_context
);
93 // Called when navigator.serviceWorker is instantiated or detached
94 // for a document whose provider can be identified by |provider_id|.
95 void AddProviderClient(int provider_id
,
96 blink::WebServiceWorkerProviderClient
* client
);
97 void RemoveProviderClient(int provider_id
);
99 // If an existing WebServiceWorkerImpl exists for the Service
100 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
101 // created and its ownership is transferred to the caller. If
102 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
103 // adopted for the specified Service Worker.
105 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
106 // method can return an existing WebServiceWorkerImpl, in which case
107 // it is owned by a WebCore::ServiceWorker and the lifetime is not
108 // being transferred to the owner; or it can create a
109 // WebServiceWorkerImpl, in which case ownership is transferred to
110 // the caller who must bounce it to a method that will associate it
111 // with a WebCore::ServiceWorker.
112 WebServiceWorkerImpl
* GetServiceWorker(
113 const ServiceWorkerObjectInfo
& info
,
116 // Creates a WebServiceWorkerRegistrationImpl for the specified registration
117 // and transfers its ownership to the caller. If |adopt_handle| is true, a
118 // ServiceWorkerRegistrationHandleReference will be adopted for the
120 WebServiceWorkerRegistrationImpl
* CreateServiceWorkerRegistration(
121 const ServiceWorkerRegistrationObjectInfo
& info
,
124 // |thread_safe_sender| needs to be passed in because if the call leads to
125 // construction it will be needed.
126 static ServiceWorkerDispatcher
* GetOrCreateThreadSpecificInstance(
127 ThreadSafeSender
* thread_safe_sender
);
129 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
130 // instance if thread-local instance doesn't exist.
131 static ServiceWorkerDispatcher
* GetThreadSpecificInstance();
134 typedef IDMap
<WebServiceWorkerRegistrationCallbacks
,
135 IDMapOwnPointer
> RegistrationCallbackMap
;
136 typedef IDMap
<WebServiceWorkerUnregistrationCallbacks
,
137 IDMapOwnPointer
> UnregistrationCallbackMap
;
138 typedef IDMap
<WebServiceWorkerGetRegistrationCallbacks
,
139 IDMapOwnPointer
> GetRegistrationCallbackMap
;
140 typedef IDMap
<WebServiceWorkerGetRegistrationForReadyCallbacks
,
141 IDMapOwnPointer
> GetRegistrationForReadyCallbackMap
;
143 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*>
145 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
146 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
147 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
148 typedef std::map
<int, WebServiceWorkerRegistrationImpl
*>
149 RegistrationObjectMap
;
151 friend class ServiceWorkerDispatcherTest
;
152 friend class WebServiceWorkerImpl
;
153 friend class WebServiceWorkerRegistrationImpl
;
155 // WorkerTaskRunner::Observer implementation.
156 void OnWorkerRunLoopStopped() override
;
158 void OnAssociateRegistrationWithServiceWorker(
161 const ServiceWorkerRegistrationObjectInfo
& info
,
162 const ServiceWorkerVersionAttributes
& attrs
);
163 void OnAssociateRegistration(int thread_id
,
165 const ServiceWorkerRegistrationObjectInfo
& info
,
166 const ServiceWorkerVersionAttributes
& attrs
);
167 void OnDisassociateRegistration(int thread_id
,
169 void OnRegistered(int thread_id
,
171 const ServiceWorkerRegistrationObjectInfo
& info
,
172 const ServiceWorkerVersionAttributes
& attrs
);
173 void OnUnregistered(int thread_id
,
176 void OnDidGetRegistration(int thread_id
,
178 const ServiceWorkerRegistrationObjectInfo
& info
,
179 const ServiceWorkerVersionAttributes
& attrs
);
180 void OnDidGetRegistrationForReady(
183 const ServiceWorkerRegistrationObjectInfo
& info
,
184 const ServiceWorkerVersionAttributes
& attrs
);
185 void OnRegistrationError(int thread_id
,
187 blink::WebServiceWorkerError::ErrorType error_type
,
188 const base::string16
& message
);
189 void OnUnregistrationError(int thread_id
,
191 blink::WebServiceWorkerError::ErrorType error_type
,
192 const base::string16
& message
);
193 void OnGetRegistrationError(
196 blink::WebServiceWorkerError::ErrorType error_type
,
197 const base::string16
& message
);
198 void OnServiceWorkerStateChanged(int thread_id
,
200 blink::WebServiceWorkerState state
);
201 void OnSetVersionAttributes(int thread_id
,
203 int registration_handle_id
,
205 const ServiceWorkerVersionAttributes
& attributes
);
206 void OnUpdateFound(int thread_id
,
207 int registration_handle_id
);
208 void OnSetControllerServiceWorker(int thread_id
,
210 const ServiceWorkerObjectInfo
& info
,
211 bool should_notify_controllerchange
);
215 const base::string16
& message
,
216 const std::vector
<TransferredMessagePort
>& sent_message_ports
,
217 const std::vector
<int>& new_routing_ids
);
219 // Keeps map from handle_id to ServiceWorker object.
220 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
221 void RemoveServiceWorker(int handle_id
);
223 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
224 void AddServiceWorkerRegistration(
225 int registration_handle_id
,
226 WebServiceWorkerRegistrationImpl
* registration
);
227 void RemoveServiceWorkerRegistration(
228 int registration_handle_id
);
230 // Returns an existing registration or new one filled in with version
231 // attributes. This function assumes given |info| and |attrs| retain handle
232 // references and always adopts them.
233 // TODO(nhiroki): This assumption seems to impair readability. We could
234 // explictly pass ServiceWorker(Registration)HandleReference instead.
235 WebServiceWorkerRegistrationImpl
* FindOrCreateRegistration(
236 const ServiceWorkerRegistrationObjectInfo
& info
,
237 const ServiceWorkerVersionAttributes
& attrs
);
239 RegistrationCallbackMap pending_registration_callbacks_
;
240 UnregistrationCallbackMap pending_unregistration_callbacks_
;
241 GetRegistrationCallbackMap pending_get_registration_callbacks_
;
242 GetRegistrationForReadyCallbackMap get_for_ready_callbacks_
;
244 ProviderClientMap provider_clients_
;
245 ProviderContextMap provider_contexts_
;
247 WorkerObjectMap service_workers_
;
248 RegistrationObjectMap registrations_
;
250 // A map for ServiceWorkers that are associated to a particular document
251 // (e.g. as .current).
252 WorkerToProviderMap worker_to_provider_
;
254 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
256 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
259 } // namespace content
261 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_