ServiceWorker: Update the install sequence as per the latest spec
[chromium-blink-merge.git] / content / child / service_worker / service_worker_dispatcher.h
blob3f136c28730f3f42b7056c24f46d830243b681cb
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;
48 explicit ServiceWorkerDispatcher(ThreadSafeSender* thread_safe_sender);
49 virtual ~ServiceWorkerDispatcher();
51 void OnMessageReceived(const IPC::Message& msg);
52 bool Send(IPC::Message* msg);
54 // Corresponds to navigator.serviceWorker.register()
55 void RegisterServiceWorker(
56 int provider_id,
57 const GURL& pattern,
58 const GURL& script_url,
59 WebServiceWorkerRegistrationCallbacks* callbacks);
60 // Corresponds to navigator.serviceWorker.unregister()
61 void UnregisterServiceWorker(
62 int provider_id,
63 const GURL& pattern,
64 WebServiceWorkerRegistrationCallbacks* callbacks);
66 // Called when a new provider context for a document is created. Usually
67 // this happens when a new document is being loaded, and is called much
68 // earlier than AddScriptClient.
69 // (This is attached only to the document thread's ServiceWorkerDispatcher)
70 void AddProviderContext(ServiceWorkerProviderContext* provider_context);
71 void RemoveProviderContext(ServiceWorkerProviderContext* provider_context);
73 // Called when navigator.serviceWorker is instantiated or detached
74 // for a document whose provider can be identified by |provider_id|.
75 void AddScriptClient(int provider_id,
76 blink::WebServiceWorkerProviderClient* client);
77 void RemoveScriptClient(int provider_id);
79 // If an existing WebServiceWorkerImpl exists for the Service
80 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
81 // created and its ownership is transferred to the caller. If
82 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
83 // adopted for the specified Service Worker.
85 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
86 // method can return an existing WebServiceWorkerImpl, in which case
87 // it is owned by a WebCore::ServiceWorker and the lifetime is not
88 // being transferred to the owner; or it can create a
89 // WebServiceWorkerImpl, in which case ownership is transferred to
90 // the caller who must bounce it to a method that will associate it
91 // with a WebCore::ServiceWorker.
92 WebServiceWorkerImpl* GetServiceWorker(
93 const ServiceWorkerObjectInfo& info,
94 bool adopt_handle);
96 // If an existing WebServiceWorkerRegistrationImpl exists for the
97 // registration, it is returned; otherwise a WebServiceWorkerRegistrationImpl
98 // is created and its ownership is transferred to the caller. If
99 // |adopt_handle| is true, a ServiceWorkerRegistrationHandleReference will be
100 // adopted for the specified registration.
101 WebServiceWorkerRegistrationImpl* GetServiceWorkerRegistration(
102 const ServiceWorkerRegistrationObjectInfo& info,
103 bool adopt_handle);
105 // |thread_safe_sender| needs to be passed in because if the call leads to
106 // construction it will be needed.
107 static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
108 ThreadSafeSender* thread_safe_sender);
110 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
111 // instance if thread-local instance doesn't exist.
112 static ServiceWorkerDispatcher* GetThreadSpecificInstance();
114 private:
115 typedef IDMap<WebServiceWorkerRegistrationCallbacks,
116 IDMapOwnPointer> CallbackMap;
117 typedef std::map<int, blink::WebServiceWorkerProviderClient*> ScriptClientMap;
118 typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
119 typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
120 typedef std::map<int, ServiceWorkerProviderContext*> WorkerToProviderMap;
121 typedef std::map<int, WebServiceWorkerRegistrationImpl*>
122 RegistrationObjectMap;
124 friend class WebServiceWorkerImpl;
125 friend class WebServiceWorkerRegistrationImpl;
127 // WorkerTaskRunner::Observer implementation.
128 virtual void OnWorkerRunLoopStopped() OVERRIDE;
130 void OnRegistered(int thread_id,
131 int request_id,
132 const ServiceWorkerRegistrationObjectInfo& info,
133 const ServiceWorkerVersionAttributes& attrs);
134 void OnUnregistered(int thread_id,
135 int request_id);
136 void OnRegistrationError(int thread_id,
137 int request_id,
138 blink::WebServiceWorkerError::ErrorType error_type,
139 const base::string16& message);
140 void OnServiceWorkerStateChanged(int thread_id,
141 int handle_id,
142 blink::WebServiceWorkerState state);
143 void OnSetVersionAttributes(int thread_id,
144 int provider_id,
145 int registration_handle_id,
146 int changed_mask,
147 const ServiceWorkerVersionAttributes& attributes);
148 void OnUpdateFound(int thread_id,
149 const ServiceWorkerRegistrationObjectInfo& info);
150 void OnSetControllerServiceWorker(int thread_id,
151 int provider_id,
152 const ServiceWorkerObjectInfo& info);
153 void OnPostMessage(int thread_id,
154 int provider_id,
155 const base::string16& message,
156 const std::vector<int>& sent_message_port_ids,
157 const std::vector<int>& new_routing_ids);
159 void SetInstallingServiceWorker(
160 int provider_id,
161 int registration_handle_id,
162 const ServiceWorkerObjectInfo& info);
163 void SetWaitingServiceWorker(
164 int provider_id,
165 int registration_handle_id,
166 const ServiceWorkerObjectInfo& info);
167 void SetActiveServiceWorker(
168 int provider_id,
169 int registration_handle_id,
170 const ServiceWorkerObjectInfo& info);
172 // Keeps map from handle_id to ServiceWorker object.
173 void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
174 void RemoveServiceWorker(int handle_id);
176 // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
177 void AddServiceWorkerRegistration(
178 int registration_handle_id,
179 WebServiceWorkerRegistrationImpl* registration);
180 void RemoveServiceWorkerRegistration(
181 int registration_handle_id);
183 CallbackMap pending_callbacks_;
184 ScriptClientMap script_clients_;
185 ProviderContextMap provider_contexts_;
186 WorkerObjectMap service_workers_;
187 RegistrationObjectMap registrations_;
189 // A map for ServiceWorkers that are associated to a particular document
190 // (e.g. as .current).
191 WorkerToProviderMap worker_to_provider_;
193 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
195 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
198 } // namespace content
200 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_