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 virtual ~ServiceWorkerDispatcher();
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 AddScriptClient(int provider_id
,
87 blink::WebServiceWorkerProviderClient
* client
);
88 void RemoveScriptClient(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
;
139 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*> ScriptClientMap
;
140 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
141 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
142 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
143 typedef std::map
<int, WebServiceWorkerRegistrationImpl
*>
144 RegistrationObjectMap
;
146 friend class WebServiceWorkerImpl
;
147 friend class WebServiceWorkerRegistrationImpl
;
149 // WorkerTaskRunner::Observer implementation.
150 virtual void OnWorkerRunLoopStopped() OVERRIDE
;
152 void OnAssociateRegistration(int thread_id
,
154 const ServiceWorkerRegistrationObjectInfo
& info
,
155 const ServiceWorkerVersionAttributes
& attrs
);
156 void OnDisassociateRegistration(int thread_id
,
158 void OnRegistered(int thread_id
,
160 const ServiceWorkerRegistrationObjectInfo
& info
,
161 const ServiceWorkerVersionAttributes
& attrs
);
162 void OnUnregistered(int thread_id
,
165 void OnDidGetRegistration(int thread_id
,
167 const ServiceWorkerRegistrationObjectInfo
& info
,
168 const ServiceWorkerVersionAttributes
& attrs
);
169 void OnRegistrationError(int thread_id
,
171 blink::WebServiceWorkerError::ErrorType error_type
,
172 const base::string16
& message
);
173 void OnUnregistrationError(int thread_id
,
175 blink::WebServiceWorkerError::ErrorType error_type
,
176 const base::string16
& message
);
177 void OnGetRegistrationError(
180 blink::WebServiceWorkerError::ErrorType error_type
,
181 const base::string16
& message
);
182 void OnServiceWorkerStateChanged(int thread_id
,
184 blink::WebServiceWorkerState state
);
185 void OnSetVersionAttributes(int thread_id
,
187 int registration_handle_id
,
189 const ServiceWorkerVersionAttributes
& attributes
);
190 void OnUpdateFound(int thread_id
,
191 const ServiceWorkerRegistrationObjectInfo
& info
);
192 void OnSetControllerServiceWorker(int thread_id
,
194 const ServiceWorkerObjectInfo
& info
);
195 void OnPostMessage(int thread_id
,
197 const base::string16
& message
,
198 const std::vector
<int>& sent_message_port_ids
,
199 const std::vector
<int>& new_routing_ids
);
201 void SetInstallingServiceWorker(
203 int registration_handle_id
,
204 const ServiceWorkerObjectInfo
& info
);
205 void SetWaitingServiceWorker(
207 int registration_handle_id
,
208 const ServiceWorkerObjectInfo
& info
);
209 void SetActiveServiceWorker(
211 int registration_handle_id
,
212 const ServiceWorkerObjectInfo
& info
);
213 void SetReadyRegistration(
215 int registration_handle_id
);
217 // Keeps map from handle_id to ServiceWorker object.
218 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
219 void RemoveServiceWorker(int handle_id
);
221 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
222 void AddServiceWorkerRegistration(
223 int registration_handle_id
,
224 WebServiceWorkerRegistrationImpl
* registration
);
225 void RemoveServiceWorkerRegistration(
226 int registration_handle_id
);
228 WebServiceWorkerRegistrationImpl
* FindOrCreateRegistration(
229 const ServiceWorkerRegistrationObjectInfo
& info
,
230 const ServiceWorkerVersionAttributes
& attrs
);
232 RegistrationCallbackMap pending_registration_callbacks_
;
233 UnregistrationCallbackMap pending_unregistration_callbacks_
;
234 GetRegistrationCallbackMap pending_get_registration_callbacks_
;
235 ScriptClientMap script_clients_
;
236 ProviderContextMap provider_contexts_
;
237 WorkerObjectMap service_workers_
;
238 RegistrationObjectMap registrations_
;
240 // A map for ServiceWorkers that are associated to a particular document
241 // (e.g. as .current).
242 WorkerToProviderMap worker_to_provider_
;
244 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
246 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
249 } // namespace content
251 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_