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 struct ServiceWorkerObjectInfo
;
33 class ServiceWorkerProviderContext
;
34 class ThreadSafeSender
;
35 class WebServiceWorkerImpl
;
37 // This class manages communication with the browser process about
38 // registration of the service worker, exposed to renderer and worker
39 // scripts through methods like navigator.registerServiceWorker().
40 class ServiceWorkerDispatcher
: public WorkerTaskRunner::Observer
{
42 explicit ServiceWorkerDispatcher(ThreadSafeSender
* thread_safe_sender
);
43 virtual ~ServiceWorkerDispatcher();
45 void OnMessageReceived(const IPC::Message
& msg
);
46 bool Send(IPC::Message
* msg
);
48 // Corresponds to navigator.serviceWorker.register()
49 void RegisterServiceWorker(
52 const GURL
& script_url
,
53 blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks
* callbacks
);
54 // Corresponds to navigator.serviceWorker.unregister()
55 void UnregisterServiceWorker(
58 blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks
* callbacks
);
60 // Called when a new provider context for a document is created. Usually
61 // this happens when a new document is being loaded, and is called much
62 // earlier than AddScriptClient.
63 // (This is attached only to the document thread's ServiceWorkerDispatcher)
64 void AddProviderContext(ServiceWorkerProviderContext
* provider_context
);
65 void RemoveProviderContext(ServiceWorkerProviderContext
* provider_context
);
67 // Called when navigator.serviceWorker is instantiated or detached
68 // for a document whose provider can be identified by |provider_id|.
69 void AddScriptClient(int provider_id
,
70 blink::WebServiceWorkerProviderClient
* client
);
71 void RemoveScriptClient(int provider_id
);
73 // If an existing WebServiceWorkerImpl exists for the Service
74 // Worker, it is returned; otherwise a WebServiceWorkerImpl is
75 // created and its ownership is transferred to the caller. If
76 // |adopt_handle| is true, a ServiceWorkerHandleReference will be
77 // adopted for the specified Service Worker.
79 // TODO(dominicc): The lifetime of WebServiceWorkerImpl is too tricky; this
80 // method can return an existing WebServiceWorkerImpl, in which case
81 // it is owned by a WebCore::ServiceWorker and the lifetime is not
82 // being transferred to the owner; or it can create a
83 // WebServiceWorkerImpl, in which case ownership is transferred to
84 // the caller who must bounce it to a method that will associate it
85 // with a WebCore::ServiceWorker.
86 WebServiceWorkerImpl
* GetServiceWorker(const ServiceWorkerObjectInfo
&,
89 // |thread_safe_sender| needs to be passed in because if the call leads to
90 // construction it will be needed.
91 static ServiceWorkerDispatcher
* GetOrCreateThreadSpecificInstance(
92 ThreadSafeSender
* thread_safe_sender
);
94 // Unlike GetOrCreateThreadSpecificInstance() this doesn't create a new
95 // instance if thread-local instance doesn't exist.
96 static ServiceWorkerDispatcher
* GetThreadSpecificInstance();
99 typedef IDMap
<blink::WebServiceWorkerProvider::WebServiceWorkerCallbacks
,
100 IDMapOwnPointer
> CallbackMap
;
101 typedef std::map
<int, blink::WebServiceWorkerProviderClient
*> ScriptClientMap
;
102 typedef std::map
<int, ServiceWorkerProviderContext
*> ProviderContextMap
;
103 typedef std::map
<int, WebServiceWorkerImpl
*> WorkerObjectMap
;
104 typedef std::map
<int, ServiceWorkerProviderContext
*> WorkerToProviderMap
;
106 friend class WebServiceWorkerImpl
;
108 // WorkerTaskRunner::Observer implementation.
109 virtual void OnWorkerRunLoopStopped() OVERRIDE
;
111 void OnRegistered(int thread_id
,
113 const ServiceWorkerObjectInfo
& info
);
114 void OnUnregistered(int thread_id
,
116 void OnRegistrationError(int thread_id
,
118 blink::WebServiceWorkerError::ErrorType error_type
,
119 const base::string16
& message
);
120 void OnServiceWorkerStateChanged(int thread_id
,
122 blink::WebServiceWorkerState state
);
123 void OnSetInstallingServiceWorker(int thread_id
,
125 const ServiceWorkerObjectInfo
& info
);
126 void OnSetWaitingServiceWorker(int thread_id
,
128 const ServiceWorkerObjectInfo
& info
);
129 void OnSetActiveServiceWorker(int thread_id
,
131 const ServiceWorkerObjectInfo
& info
);
132 void OnSetControllerServiceWorker(int thread_id
,
134 const ServiceWorkerObjectInfo
& info
);
135 void OnPostMessage(int thread_id
,
137 const base::string16
& message
,
138 const std::vector
<int>& sent_message_port_ids
,
139 const std::vector
<int>& new_routing_ids
);
141 // Keeps map from handle_id to ServiceWorker object.
142 void AddServiceWorker(int handle_id
, WebServiceWorkerImpl
* worker
);
143 void RemoveServiceWorker(int handle_id
);
145 CallbackMap pending_callbacks_
;
146 ScriptClientMap script_clients_
;
147 ProviderContextMap provider_contexts_
;
148 WorkerObjectMap service_workers_
;
150 // A map for ServiceWorkers that are associated to a particular document
151 // (e.g. as .current).
152 WorkerToProviderMap worker_to_provider_
;
154 scoped_refptr
<ThreadSafeSender
> thread_safe_sender_
;
156 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcher
);
159 } // namespace content
161 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_H_