Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / content / child / service_worker / service_worker_dispatcher.h
blob5ad6e35ace5f236a3679b5fd3ec0967a85290377
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/WebServiceWorkerState.h"
19 class GURL;
21 namespace blink {
22 class WebURL;
25 namespace IPC {
26 class Message;
29 namespace content {
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 {
44 public:
45 typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
46 WebServiceWorkerRegistrationCallbacks;
47 typedef
48 blink::WebServiceWorkerProvider::WebServiceWorkerUnregistrationCallbacks
49 WebServiceWorkerUnregistrationCallbacks;
50 typedef
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(
62 int provider_id,
63 const GURL& pattern,
64 const GURL& script_url,
65 WebServiceWorkerRegistrationCallbacks* callbacks);
66 // Corresponds to navigator.serviceWorker.unregister()
67 void UnregisterServiceWorker(
68 int provider_id,
69 const GURL& pattern,
70 WebServiceWorkerUnregistrationCallbacks* callbacks);
71 // Corresponds to navigator.serviceWorker.getRegistration()
72 void GetRegistration(
73 int provider_id,
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,
105 bool adopt_handle);
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
110 // registration.
111 WebServiceWorkerRegistrationImpl* FindServiceWorkerRegistration(
112 const ServiceWorkerRegistrationObjectInfo& info,
113 bool adopt_handle);
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
118 // registration.
119 WebServiceWorkerRegistrationImpl* CreateServiceWorkerRegistration(
120 const ServiceWorkerRegistrationObjectInfo& info,
121 bool adopt_handle);
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();
132 private:
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*>
141 ProviderClientMap;
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,
155 int provider_id,
156 const ServiceWorkerRegistrationObjectInfo& info,
157 const ServiceWorkerVersionAttributes& attrs);
158 void OnDisassociateRegistration(int thread_id,
159 int provider_id);
160 void OnRegistered(int thread_id,
161 int request_id,
162 const ServiceWorkerRegistrationObjectInfo& info,
163 const ServiceWorkerVersionAttributes& attrs);
164 void OnUnregistered(int thread_id,
165 int request_id,
166 bool is_success);
167 void OnDidGetRegistration(int thread_id,
168 int request_id,
169 const ServiceWorkerRegistrationObjectInfo& info,
170 const ServiceWorkerVersionAttributes& attrs);
171 void OnRegistrationError(int thread_id,
172 int request_id,
173 blink::WebServiceWorkerError::ErrorType error_type,
174 const base::string16& message);
175 void OnUnregistrationError(int thread_id,
176 int request_id,
177 blink::WebServiceWorkerError::ErrorType error_type,
178 const base::string16& message);
179 void OnGetRegistrationError(
180 int thread_id,
181 int request_id,
182 blink::WebServiceWorkerError::ErrorType error_type,
183 const base::string16& message);
184 void OnServiceWorkerStateChanged(int thread_id,
185 int handle_id,
186 blink::WebServiceWorkerState state);
187 void OnSetVersionAttributes(int thread_id,
188 int provider_id,
189 int registration_handle_id,
190 int changed_mask,
191 const ServiceWorkerVersionAttributes& attributes);
192 void OnUpdateFound(int thread_id,
193 const ServiceWorkerRegistrationObjectInfo& info);
194 void OnSetControllerServiceWorker(int thread_id,
195 int provider_id,
196 const ServiceWorkerObjectInfo& info,
197 bool should_notify_controllerchange);
198 void OnPostMessage(int thread_id,
199 int provider_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,
205 int request_id,
206 int provider_id);
208 void SetReadyRegistration(
209 int provider_id,
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_