Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / child / service_worker / service_worker_dispatcher.h
blob6d0f1a0c44608f1e7bcacb3adf7d70efed149f38
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_
8 #include <map>
9 #include <vector>
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"
20 class GURL;
22 namespace base {
23 class SingleThreadTaskRunner;
26 namespace blink {
27 class WebURL;
30 namespace IPC {
31 class Message;
34 struct ServiceWorkerMsg_MessageToDocument_Params;
36 namespace content {
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 {
52 public:
53 typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
54 WebServiceWorkerRegistrationCallbacks;
55 typedef blink::WebServiceWorkerRegistration::
56 WebServiceWorkerUnregistrationCallbacks
57 WebServiceWorkerUnregistrationCallbacks;
58 typedef
59 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
60 WebServiceWorkerGetRegistrationCallbacks;
61 typedef
62 blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationsCallbacks
63 WebServiceWorkerGetRegistrationsCallbacks;
64 typedef blink::WebServiceWorkerProvider::
65 WebServiceWorkerGetRegistrationForReadyCallbacks
66 WebServiceWorkerGetRegistrationForReadyCallbacks;
68 ServiceWorkerDispatcher(
69 ThreadSafeSender* thread_safe_sender,
70 base::SingleThreadTaskRunner* main_thread_task_runner);
71 ~ServiceWorkerDispatcher() override;
73 void OnMessageReceived(const IPC::Message& msg);
74 bool Send(IPC::Message* msg);
76 // Corresponds to navigator.serviceWorker.register().
77 void RegisterServiceWorker(
78 int provider_id,
79 const GURL& pattern,
80 const GURL& script_url,
81 WebServiceWorkerRegistrationCallbacks* callbacks);
82 // Corresponds to ServiceWorkerRegistration.update().
83 void UpdateServiceWorker(int provider_id, int64 registration_id);
84 // Corresponds to ServiceWorkerRegistration.unregister().
85 void UnregisterServiceWorker(
86 int provider_id,
87 int64 registration_id,
88 WebServiceWorkerUnregistrationCallbacks* callbacks);
89 // Corresponds to navigator.serviceWorker.getRegistration().
90 void GetRegistration(
91 int provider_id,
92 const GURL& document_url,
93 WebServiceWorkerRegistrationCallbacks* callbacks);
94 // Corresponds to navigator.serviceWorker.getRegistrations().
95 void GetRegistrations(
96 int provider_id,
97 WebServiceWorkerGetRegistrationsCallbacks* callbacks);
99 void GetRegistrationForReady(
100 int provider_id,
101 WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks);
103 // Called when a new provider context for a document is created. Usually
104 // this happens when a new document is being loaded, and is called much
105 // earlier than AddScriptClient.
106 // (This is attached only to the document thread's ServiceWorkerDispatcher)
107 void AddProviderContext(ServiceWorkerProviderContext* provider_context);
108 void RemoveProviderContext(ServiceWorkerProviderContext* provider_context);
110 // Called when navigator.serviceWorker is instantiated or detached
111 // for a document whose provider can be identified by |provider_id|.
112 void AddProviderClient(int provider_id,
113 blink::WebServiceWorkerProviderClient* client);
114 void RemoveProviderClient(int provider_id);
116 // If an existing WebServiceWorkerImpl exists for the Service
117 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
118 // created and its ownership is transferred to the caller. If
119 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
120 // adopted for the specified Service Worker.
122 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
123 // method can return an existing WebServiceWorkerImpl, in which case
124 // it is owned by a WebCore::ServiceWorker and the lifetime is not
125 // being transferred to the owner; or it can create a
126 // WebServiceWorkerImpl, in which case ownership is transferred to
127 // the caller who must bounce it to a method that will associate it
128 // with a WebCore::ServiceWorker.
129 WebServiceWorkerImpl* GetServiceWorker(
130 const ServiceWorkerObjectInfo& info,
131 bool adopt_handle);
133 // Creates a WebServiceWorkerRegistrationImpl for the specified registration
134 // and transfers its ownership to the caller. If |adopt_handle| is true, a
135 // ServiceWorkerRegistrationHandleReference will be adopted for the
136 // registration.
137 WebServiceWorkerRegistrationImpl* CreateServiceWorkerRegistration(
138 const ServiceWorkerRegistrationObjectInfo& info,
139 bool adopt_handle);
141 static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
142 ThreadSafeSender* thread_safe_sender,
143 base::SingleThreadTaskRunner* main_thread_task_runner);
145 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
146 // instance if thread-local instance doesn't exist.
147 static ServiceWorkerDispatcher* GetThreadSpecificInstance();
149 base::SingleThreadTaskRunner* main_thread_task_runner() {
150 return main_thread_task_runner_.get();
153 private:
154 typedef IDMap<WebServiceWorkerRegistrationCallbacks,
155 IDMapOwnPointer> RegistrationCallbackMap;
156 typedef IDMap<WebServiceWorkerUnregistrationCallbacks,
157 IDMapOwnPointer> UnregistrationCallbackMap;
158 typedef IDMap<WebServiceWorkerGetRegistrationCallbacks,
159 IDMapOwnPointer> GetRegistrationCallbackMap;
160 typedef IDMap<WebServiceWorkerGetRegistrationsCallbacks,
161 IDMapOwnPointer> GetRegistrationsCallbackMap;
162 typedef IDMap<WebServiceWorkerGetRegistrationForReadyCallbacks,
163 IDMapOwnPointer> GetRegistrationForReadyCallbackMap;
165 typedef std::map<int, blink::WebServiceWorkerProviderClient*>
166 ProviderClientMap;
167 typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
168 typedef std::map<int, ServiceWorkerProviderContext*> WorkerToProviderMap;
169 typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
170 typedef std::map<int, WebServiceWorkerRegistrationImpl*>
171 RegistrationObjectMap;
173 friend class ServiceWorkerDispatcherTest;
174 friend class WebServiceWorkerImpl;
175 friend class WebServiceWorkerRegistrationImpl;
177 // WorkerTaskRunner::Observer implementation.
178 void OnWorkerRunLoopStopped() override;
180 void OnAssociateRegistrationWithServiceWorker(
181 int thread_id,
182 int provider_id,
183 const ServiceWorkerRegistrationObjectInfo& info,
184 const ServiceWorkerVersionAttributes& attrs);
185 void OnAssociateRegistration(int thread_id,
186 int provider_id,
187 const ServiceWorkerRegistrationObjectInfo& info,
188 const ServiceWorkerVersionAttributes& attrs);
189 void OnDisassociateRegistration(int thread_id,
190 int provider_id);
191 void OnRegistered(int thread_id,
192 int request_id,
193 const ServiceWorkerRegistrationObjectInfo& info,
194 const ServiceWorkerVersionAttributes& attrs);
195 void OnUnregistered(int thread_id,
196 int request_id,
197 bool is_success);
198 void OnDidGetRegistration(int thread_id,
199 int request_id,
200 const ServiceWorkerRegistrationObjectInfo& info,
201 const ServiceWorkerVersionAttributes& attrs);
202 void OnDidGetRegistrations(
203 int thread_id,
204 int request_id,
205 const std::vector<ServiceWorkerRegistrationObjectInfo>& infos,
206 const std::vector<ServiceWorkerVersionAttributes>& attrs);
207 void OnDidGetRegistrationForReady(
208 int thread_id,
209 int request_id,
210 const ServiceWorkerRegistrationObjectInfo& info,
211 const ServiceWorkerVersionAttributes& attrs);
212 void OnRegistrationError(int thread_id,
213 int request_id,
214 blink::WebServiceWorkerError::ErrorType error_type,
215 const base::string16& message);
216 void OnUnregistrationError(int thread_id,
217 int request_id,
218 blink::WebServiceWorkerError::ErrorType error_type,
219 const base::string16& message);
220 void OnGetRegistrationError(
221 int thread_id,
222 int request_id,
223 blink::WebServiceWorkerError::ErrorType error_type,
224 const base::string16& message);
225 void OnGetRegistrationsError(
226 int thread_id,
227 int request_id,
228 blink::WebServiceWorkerError::ErrorType error_type,
229 const base::string16& message);
230 void OnServiceWorkerStateChanged(int thread_id,
231 int handle_id,
232 blink::WebServiceWorkerState state);
233 void OnSetVersionAttributes(int thread_id,
234 int provider_id,
235 int registration_handle_id,
236 int changed_mask,
237 const ServiceWorkerVersionAttributes& attributes);
238 void OnUpdateFound(int thread_id,
239 int registration_handle_id);
240 void OnSetControllerServiceWorker(int thread_id,
241 int provider_id,
242 const ServiceWorkerObjectInfo& info,
243 bool should_notify_controllerchange);
244 void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params& params);
246 // Keeps map from handle_id to ServiceWorker object.
247 void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
248 void RemoveServiceWorker(int handle_id);
250 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
251 void AddServiceWorkerRegistration(
252 int registration_handle_id,
253 WebServiceWorkerRegistrationImpl* registration);
254 void RemoveServiceWorkerRegistration(
255 int registration_handle_id);
257 // Returns an existing registration or new one filled in with version
258 // attributes. This function assumes given |info| and |attrs| retain handle
259 // references and always adopts them.
260 // TODO(nhiroki): This assumption seems to impair readability. We could
261 // explictly pass ServiceWorker(Registration)HandleReference instead.
262 WebServiceWorkerRegistrationImpl* FindOrCreateRegistration(
263 const ServiceWorkerRegistrationObjectInfo& info,
264 const ServiceWorkerVersionAttributes& attrs);
266 RegistrationCallbackMap pending_registration_callbacks_;
267 UnregistrationCallbackMap pending_unregistration_callbacks_;
268 GetRegistrationCallbackMap pending_get_registration_callbacks_;
269 GetRegistrationsCallbackMap pending_get_registrations_callbacks_;
270 GetRegistrationForReadyCallbackMap get_for_ready_callbacks_;
272 ProviderClientMap provider_clients_;
273 ProviderContextMap provider_contexts_;
275 WorkerObjectMap service_workers_;
276 RegistrationObjectMap registrations_;
278 // A map for ServiceWorkers that are associated to a particular document
279 // (e.g. as .current).
280 WorkerToProviderMap worker_to_provider_;
282 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
283 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
285 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
288 } // namespace content
290 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_