summaryrefslogtreecommitdiffstats
path: root/content/child/service_worker/service_worker_dispatcher.h
blob: 32fc1334e361be6ad443ce9b4da0f7b9ac919619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_
#define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_

#include <map>
#include <vector>

#include "base/id_map.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string16.h"
#include "content/public/child/worker_thread.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerError.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerProvider.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
#include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerState.h"

class GURL;

namespace base {
class SingleThreadTaskRunner;
}

namespace blink {
class WebURL;
}

namespace IPC {
class Message;
}

struct ServiceWorkerMsg_MessageToDocument_Params;

namespace content {

class ServiceWorkerMessageFilter;
class ServiceWorkerProviderContext;
class ServiceWorkerRegistrationHandleReference;
class ThreadSafeSender;
class WebServiceWorkerImpl;
class WebServiceWorkerRegistrationImpl;
struct ServiceWorkerObjectInfo;
struct ServiceWorkerRegistrationObjectInfo;
struct ServiceWorkerVersionAttributes;

// This class manages communication with the browser process about
// registration of the service worker, exposed to renderer and worker
// scripts through methods like navigator.registerServiceWorker().
class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
 public:
  typedef blink::WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks
      WebServiceWorkerRegistrationCallbacks;
  typedef blink::WebServiceWorkerRegistration::WebServiceWorkerUpdateCallbacks
      WebServiceWorkerUpdateCallbacks;
  typedef blink::WebServiceWorkerRegistration::
      WebServiceWorkerUnregistrationCallbacks
          WebServiceWorkerUnregistrationCallbacks;
  typedef
      blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationCallbacks
      WebServiceWorkerGetRegistrationCallbacks;
  typedef
      blink::WebServiceWorkerProvider::WebServiceWorkerGetRegistrationsCallbacks
      WebServiceWorkerGetRegistrationsCallbacks;
  typedef blink::WebServiceWorkerProvider::
      WebServiceWorkerGetRegistrationForReadyCallbacks
          WebServiceWorkerGetRegistrationForReadyCallbacks;

  ServiceWorkerDispatcher(
      ThreadSafeSender* thread_safe_sender,
      base::SingleThreadTaskRunner* main_thread_task_runner);
  ~ServiceWorkerDispatcher() override;

  void OnMessageReceived(const IPC::Message& msg);
  bool Send(IPC::Message* msg);

  // Corresponds to navigator.serviceWorker.register().
  void RegisterServiceWorker(
      int provider_id,
      const GURL& pattern,
      const GURL& script_url,
      WebServiceWorkerRegistrationCallbacks* callbacks);
  // Corresponds to ServiceWorkerRegistration.update().
  void UpdateServiceWorker(int provider_id,
                           int64 registration_id,
                           WebServiceWorkerUpdateCallbacks* callbacks);
  // Corresponds to ServiceWorkerRegistration.unregister().
  void UnregisterServiceWorker(
      int provider_id,
      int64 registration_id,
      WebServiceWorkerUnregistrationCallbacks* callbacks);
  // Corresponds to navigator.serviceWorker.getRegistration().
  void GetRegistration(int provider_id,
                       const GURL& document_url,
                       WebServiceWorkerGetRegistrationCallbacks* callbacks);
  // Corresponds to navigator.serviceWorker.getRegistrations().
  void GetRegistrations(
      int provider_id,
      WebServiceWorkerGetRegistrationsCallbacks* callbacks);

  void GetRegistrationForReady(
      int provider_id,
      WebServiceWorkerGetRegistrationForReadyCallbacks* callbacks);

  // Called when a new provider context for a document is created. Usually
  // this happens when a new document is being loaded, and is called much
  // earlier than AddScriptClient.
  // (This is attached only to the document thread's ServiceWorkerDispatcher)
  void AddProviderContext(ServiceWorkerProviderContext* provider_context);
  void RemoveProviderContext(ServiceWorkerProviderContext* provider_context);

  // Called when navigator.serviceWorker is instantiated or detached
  // for a document whose provider can be identified by |provider_id|.
  void AddProviderClient(int provider_id,
                         blink::WebServiceWorkerProviderClient* client);
  void RemoveProviderClient(int provider_id);

  // If an existing WebServiceWorkerImpl exists for the Service
  // Worker, it is returned; otherwise a WebServiceWorkerImpl is
  // created and its ownership is transferred to the caller. If
  // |adopt_handle| is true, a ServiceWorkerHandleReference will be
  // adopted for the specified Service Worker.
  //
  // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
  // method can return an existing WebServiceWorkerImpl, in which case
  // it is owned by a WebCore::ServiceWorker and the lifetime is not
  // being transferred to the owner; or it can create a
  // WebServiceWorkerImpl, in which case ownership is transferred to
  // the caller who must bounce it to a method that will associate it
  // with a WebCore::ServiceWorker.
  WebServiceWorkerImpl* GetServiceWorker(
      const ServiceWorkerObjectInfo& info,
      bool adopt_handle);

  // Returns the existing registration or a newly created one. When a new one is
  // created, increments interprocess references to the registration and its
  // versions via ServiceWorker(Registration)HandleReference.
  scoped_refptr<WebServiceWorkerRegistrationImpl> GetOrCreateRegistration(
      const ServiceWorkerRegistrationObjectInfo& info,
      const ServiceWorkerVersionAttributes& attrs);

  // Returns the existing registration or a newly created one. Always adopts
  // interprocess references to the registration and its versions via
  // ServiceWorker(Registration)HandleReference.
  scoped_refptr<WebServiceWorkerRegistrationImpl> GetOrAdoptRegistration(
      const ServiceWorkerRegistrationObjectInfo& info,
      const ServiceWorkerVersionAttributes& attrs);

  static ServiceWorkerDispatcher* GetOrCreateThreadSpecificInstance(
      ThreadSafeSender* thread_safe_sender,
      base::SingleThreadTaskRunner* main_thread_task_runner);

  // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
  // instance if thread-local instance doesn't exist.
  static ServiceWorkerDispatcher* GetThreadSpecificInstance();

  base::SingleThreadTaskRunner* main_thread_task_runner() {
    return main_thread_task_runner_.get();
  }

 private:
  typedef IDMap<WebServiceWorkerRegistrationCallbacks,
      IDMapOwnPointer> RegistrationCallbackMap;
  typedef IDMap<WebServiceWorkerUpdateCallbacks, IDMapOwnPointer>
      UpdateCallbackMap;
  typedef IDMap<WebServiceWorkerUnregistrationCallbacks,
      IDMapOwnPointer> UnregistrationCallbackMap;
  typedef IDMap<WebServiceWorkerGetRegistrationCallbacks,
      IDMapOwnPointer> GetRegistrationCallbackMap;
  typedef IDMap<WebServiceWorkerGetRegistrationsCallbacks,
      IDMapOwnPointer> GetRegistrationsCallbackMap;
  typedef IDMap<WebServiceWorkerGetRegistrationForReadyCallbacks,
      IDMapOwnPointer> GetRegistrationForReadyCallbackMap;

  typedef std::map<int, blink::WebServiceWorkerProviderClient*>
      ProviderClientMap;
  typedef std::map<int, ServiceWorkerProviderContext*> ProviderContextMap;
  typedef std::map<int, ServiceWorkerProviderContext*> WorkerToProviderMap;
  typedef std::map<int, WebServiceWorkerImpl*> WorkerObjectMap;
  typedef std::map<int, WebServiceWorkerRegistrationImpl*>
      RegistrationObjectMap;

  friend class ServiceWorkerDispatcherTest;
  friend class WebServiceWorkerImpl;
  friend class WebServiceWorkerRegistrationImpl;

  // WorkerThread::Observer implementation.
  void WillStopCurrentWorkerThread() override;

  void OnAssociateRegistrationWithServiceWorker(
      int thread_id,
      int provider_id,
      const ServiceWorkerRegistrationObjectInfo& info,
      const ServiceWorkerVersionAttributes& attrs);
  void OnAssociateRegistration(int thread_id,
                               int provider_id,
                               const ServiceWorkerRegistrationObjectInfo& info,
                               const ServiceWorkerVersionAttributes& attrs);
  void OnDisassociateRegistration(int thread_id,
                                  int provider_id);
  void OnRegistered(int thread_id,
                    int request_id,
                    const ServiceWorkerRegistrationObjectInfo& info,
                    const ServiceWorkerVersionAttributes& attrs);
  void OnUpdated(int thread_id, int request_id);
  void OnUnregistered(int thread_id,
                      int request_id,
                      bool is_success);
  void OnDidGetRegistration(int thread_id,
                            int request_id,
                            const ServiceWorkerRegistrationObjectInfo& info,
                            const ServiceWorkerVersionAttributes& attrs);
  void OnDidGetRegistrations(
      int thread_id,
      int request_id,
      const std::vector<ServiceWorkerRegistrationObjectInfo>& infos,
      const std::vector<ServiceWorkerVersionAttributes>& attrs);
  void OnDidGetRegistrationForReady(
      int thread_id,
      int request_id,
      const ServiceWorkerRegistrationObjectInfo& info,
      const ServiceWorkerVersionAttributes& attrs);
  void OnRegistrationError(int thread_id,
                           int request_id,
                           blink::WebServiceWorkerError::ErrorType error_type,
                           const base::string16& message);
  void OnUpdateError(int thread_id,
                     int request_id,
                     blink::WebServiceWorkerError::ErrorType error_type,
                     const base::string16& message);
  void OnUnregistrationError(int thread_id,
                             int request_id,
                             blink::WebServiceWorkerError::ErrorType error_type,
                             const base::string16& message);
  void OnGetRegistrationError(
      int thread_id,
      int request_id,
      blink::WebServiceWorkerError::ErrorType error_type,
      const base::string16& message);
  void OnGetRegistrationsError(
      int thread_id,
      int request_id,
      blink::WebServiceWorkerError::ErrorType error_type,
      const base::string16& message);
  void OnServiceWorkerStateChanged(int thread_id,
                                   int handle_id,
                                   blink::WebServiceWorkerState state);
  void OnSetVersionAttributes(int thread_id,
                              int provider_id,
                              int registration_handle_id,
                              int changed_mask,
                              const ServiceWorkerVersionAttributes& attributes);
  void OnUpdateFound(int thread_id,
                     int registration_handle_id);
  void OnSetControllerServiceWorker(int thread_id,
                                    int provider_id,
                                    const ServiceWorkerObjectInfo& info,
                                    bool should_notify_controllerchange);
  void OnPostMessage(const ServiceWorkerMsg_MessageToDocument_Params& params);

  // Keeps map from handle_id to ServiceWorker object.
  void AddServiceWorker(int handle_id, WebServiceWorkerImpl* worker);
  void RemoveServiceWorker(int handle_id);

  // Keeps map from registration_handle_id to ServiceWorkerRegistration object.
  void AddServiceWorkerRegistration(
      int registration_handle_id,
      WebServiceWorkerRegistrationImpl* registration);
  void RemoveServiceWorkerRegistration(
      int registration_handle_id);

  RegistrationCallbackMap pending_registration_callbacks_;
  UpdateCallbackMap pending_update_callbacks_;
  UnregistrationCallbackMap pending_unregistration_callbacks_;
  GetRegistrationCallbackMap pending_get_registration_callbacks_;
  GetRegistrationsCallbackMap pending_get_registrations_callbacks_;
  GetRegistrationForReadyCallbackMap get_for_ready_callbacks_;

  ProviderClientMap provider_clients_;
  ProviderContextMap provider_contexts_;

  WorkerObjectMap service_workers_;
  RegistrationObjectMap registrations_;

  scoped_refptr<ThreadSafeSender> thread_safe_sender_;
  scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;

  DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher);
};

}  // namespace content

#endif  // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_