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
;
40 // This class manages communication with the browser process about
41 // registration of the service worker, exposed to renderer and worker
42 // scripts through methods like navigator.registerServiceWorker().
43 class ServiceWorkerDispatcher
: public WorkerTaskRunner::Observer
{
45 typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
46 WebServiceWorkerRegistrationCallbacks
;
48 blink::WebServiceWorkerProvider::WebServiceWorkerUnregistrationCallbacks
49 WebServiceWorkerUnregistrationCallbacks
;
51 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
52 WebServiceWorkerGetRegistrationCallbacks
;
54 explicit ServiceWorkerDispatcher(ThreadSafeSender
* thread_safe_sender
);
55 ~ServiceWorkerDispatcher() override
;
57 void OnMessageReceived(const IPC::Message
& msg
);
58 bool Send(IPC::Message
* msg
);
60 // Corresponds to navigator.serviceWorker.register()
61 void RegisterServiceWorker(
64 const GURL
& script_url
,
65 WebServiceWorkerRegistrationCallbacks
* callbacks
);
66 // Corresponds to navigator.serviceWorker.unregister()
67 void UnregisterServiceWorker(
70 WebServiceWorkerUnregistrationCallbacks
* callbacks
);
71 // Corresponds to navigator.serviceWorker.getRegistration()
74 const GURL
& document_url
,
75 WebServiceWorkerRegistrationCallbacks
* callbacks
);
77 // Called when a new provider context for a document is created. Usually
78 // this happens when a new document is being loaded, and is called much
79 // earlier than AddScriptClient.
80 // (This is attached only to the document thread's ServiceWorkerDispatcher)
81 void AddProviderContext(ServiceWorkerProviderContext
* provider_context
);
82 void RemoveProviderContext(ServiceWorkerProviderContext
* provider_context
);
84 // Called when navigator.serviceWorker is instantiated or detached
85 // for a document whose provider can be identified by |provider_id|.
86 void AddProviderClient(int provider_id
,
87 blink::WebServiceWorkerProviderClient
* client
);
88 void RemoveProviderClient(int provider_id
);
90 // If an existing WebServiceWorkerImpl exists for the Service
91 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
92 // created and its ownership is transferred to the caller. If
93 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
94 // adopted for the specified Service Worker.
96 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
97 // method can return an existing WebServiceWorkerImpl, in which case
98 // it is owned by a WebCore::ServiceWorker and the lifetime is not
99 // being transferred to the owner; or it can create a
100 // WebServiceWorkerImpl, in which case ownership is transferred to
101 // the caller who must bounce it to a method that will associate it
102 // with a WebCore::ServiceWorker.
103 WebServiceWorkerImpl
* GetServiceWorker(
104 const ServiceWorkerObjectInfo
& info
,
107 // Finds a WebServiceWorkerRegistrationImpl for the specified registration.
108 // If it's not found, returns NULL. If |adopt_handle| is true,
109 // a ServiceWorkerRegistrationHandleReference will be adopted for the
111 WebServiceWorkerRegistrationImpl
* FindServiceWorkerRegistration(
112 const ServiceWorkerRegistrationObjectInfo
& info
,
115 // Creates a WebServiceWorkerRegistrationImpl for the specified registration
116 // and transfers its ownership to the caller. If |adopt_handle| is true, a
117 // ServiceWorkerRegistrationHandleReference will be adopted for the
119 WebServiceWorkerRegistrationImpl
* CreateServiceWorkerRegistration(
120 const ServiceWorkerRegistrationObjectInfo
& info
,
123 // |thread_safe_sender| needs to be passed in because if the call leads to
124 // construction it will be needed.
125 static ServiceWorkerDispatcher
* GetOrCreateThreadSpecificInstance(
126 ThreadSafeSender
* thread_safe_sender
);
128 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
129 // instance if thread-local instance doesn't exist.
130 static ServiceWorkerDispatcher
* GetThreadSpecificInstance();
133 typedef IDMap
<WebServiceWorkerRegistrationCallbacks
,
134 IDMapOwnPointer
> RegistrationCallbackMap
;
135 typedef IDMap
<WebServiceWorkerUnregistrationCallbacks
,
136 IDMapOwnPointer
> UnregistrationCallbackMap
;
137 typedef IDMap
<WebServiceWorkerGetRegistrationCallbacks
,
138 IDMapOwnPointer
> GetRegistrationCallbackMap
;
140 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*>
142 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
143 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
144 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
145 typedef std::map
<int, WebServiceWorkerRegistrationImpl
*>
146 RegistrationObjectMap
;
148 friend class WebServiceWorkerImpl
;
149 friend class WebServiceWorkerRegistrationImpl
;
151 // WorkerTaskRunner::Observer implementation.
152 void OnWorkerRunLoopStopped() override
;
154 void OnAssociateRegistration(int thread_id
,
156 const ServiceWorkerRegistrationObjectInfo
& info
,
157 const ServiceWorkerVersionAttributes
& attrs
);
158 void OnDisassociateRegistration(int thread_id
,
160 void OnRegistered(int thread_id
,
162 const ServiceWorkerRegistrationObjectInfo
& info
,
163 const ServiceWorkerVersionAttributes
& attrs
);
164 void OnUnregistered(int thread_id
,
167 void OnDidGetRegistration(int thread_id
,
169 const ServiceWorkerRegistrationObjectInfo
& info
,
170 const ServiceWorkerVersionAttributes
& attrs
);
171 void OnRegistrationError(int thread_id
,
173 blink::WebServiceWorkerError::ErrorType error_type
,
174 const base::string16
& message
);
175 void OnUnregistrationError(int thread_id
,
177 blink::WebServiceWorkerError::ErrorType error_type
,
178 const base::string16
& message
);
179 void OnGetRegistrationError(
182 blink::WebServiceWorkerError::ErrorType error_type
,
183 const base::string16
& message
);
184 void OnServiceWorkerStateChanged(int thread_id
,
186 blink::WebServiceWorkerState state
);
187 void OnSetVersionAttributes(int thread_id
,
189 int registration_handle_id
,
191 const ServiceWorkerVersionAttributes
& attributes
);
192 void OnUpdateFound(int thread_id
,
193 const ServiceWorkerRegistrationObjectInfo
& info
);
194 void OnSetControllerServiceWorker(int thread_id
,
196 const ServiceWorkerObjectInfo
& info
,
197 bool should_notify_controllerchange
);
198 void OnPostMessage(int thread_id
,
200 const base::string16
& message
,
201 const std::vector
<int>& sent_message_port_ids
,
202 const std::vector
<int>& new_routing_ids
);
203 void OnGetClientInfo(int thread_id
,
204 int embedded_worker_id
,
208 void SetReadyRegistration(
210 int registration_handle_id
);
212 // Keeps map from handle_id to ServiceWorker object.
213 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
214 void RemoveServiceWorker(int handle_id
);
216 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
217 void AddServiceWorkerRegistration(
218 int registration_handle_id
,
219 WebServiceWorkerRegistrationImpl
* registration
);
220 void RemoveServiceWorkerRegistration(
221 int registration_handle_id
);
223 WebServiceWorkerRegistrationImpl
* FindOrCreateRegistration(
224 const ServiceWorkerRegistrationObjectInfo
& info
,
225 const ServiceWorkerVersionAttributes
& attrs
);
227 RegistrationCallbackMap pending_registration_callbacks_
;
228 UnregistrationCallbackMap pending_unregistration_callbacks_
;
229 GetRegistrationCallbackMap pending_get_registration_callbacks_
;
231 ProviderClientMap provider_clients_
;
232 ProviderContextMap provider_contexts_
;
234 WorkerObjectMap service_workers_
;
235 RegistrationObjectMap registrations_
;
237 // A map for ServiceWorkers that are associated to a particular document
238 // (e.g. as .current).
239 WorkerToProviderMap worker_to_provider_
;
241 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
243 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
246 } // namespace content
248 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_