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_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_
10 #include "base/id_map.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string16.h"
13 #include "content/browser/service_worker/service_worker_registration_status.h"
14 #include "content/common/service_worker/service_worker_types.h"
15 #include "content/public/browser/browser_message_filter.h"
18 struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
;
22 class MessagePortMessageFilter
;
23 class ResourceContext
;
24 class ServiceWorkerContextCore
;
25 class ServiceWorkerContextWrapper
;
26 class ServiceWorkerHandle
;
27 class ServiceWorkerProviderHost
;
28 class ServiceWorkerRegistration
;
29 class ServiceWorkerRegistrationHandle
;
30 class ServiceWorkerVersion
;
31 struct ServiceWorkerObjectInfo
;
32 struct ServiceWorkerRegistrationInfo
;
33 struct ServiceWorkerRegistrationObjectInfo
;
34 struct ServiceWorkerVersionAttributes
;
35 struct TransferredMessagePort
;
37 class CONTENT_EXPORT ServiceWorkerDispatcherHost
: public BrowserMessageFilter
{
39 ServiceWorkerDispatcherHost(
40 int render_process_id
,
41 MessagePortMessageFilter
* message_port_message_filter
,
42 ResourceContext
* resource_context
);
44 void Init(ServiceWorkerContextWrapper
* context_wrapper
);
46 // BrowserMessageFilter implementation
47 void OnFilterAdded(IPC::Sender
* sender
) override
;
48 void OnFilterRemoved() override
;
49 void OnDestruct() const override
;
50 bool OnMessageReceived(const IPC::Message
& message
) override
;
52 // IPC::Sender implementation
54 // Send() queues the message until the underlying sender is ready. This
55 // class assumes that Send() can only fail after that when the renderer
56 // process has terminated, at which point the whole instance will eventually
58 bool Send(IPC::Message
* message
) override
;
60 void RegisterServiceWorkerHandle(scoped_ptr
<ServiceWorkerHandle
> handle
);
61 void RegisterServiceWorkerRegistrationHandle(
62 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle
);
64 ServiceWorkerHandle
* FindServiceWorkerHandle(int provider_id
,
67 // Returns the existing registration handle whose reference count is
68 // incremented or newly created one if it doesn't exist.
69 ServiceWorkerRegistrationHandle
* GetOrCreateRegistrationHandle(
70 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
71 ServiceWorkerRegistration
* registration
);
73 MessagePortMessageFilter
* message_port_message_filter() {
74 return message_port_message_filter_
;
78 ~ServiceWorkerDispatcherHost() override
;
81 friend class BrowserThread
;
82 friend class base::DeleteHelper
<ServiceWorkerDispatcherHost
>;
83 friend class TestingServiceWorkerDispatcherHost
;
85 // IPC Message handlers
86 void OnRegisterServiceWorker(int thread_id
,
90 const GURL
& script_url
);
91 void OnUpdateServiceWorker(int thread_id
,
94 int64 registration_id
);
95 void OnUnregisterServiceWorker(int thread_id
,
98 int64 registration_id
);
99 void OnGetRegistration(int thread_id
,
102 const GURL
& document_url
);
103 void OnGetRegistrations(int thread_id
, int request_id
, int provider_id
);
104 void OnGetRegistrationForReady(int thread_id
,
107 void OnProviderCreated(int provider_id
,
109 ServiceWorkerProviderType provider_type
);
110 void OnProviderDestroyed(int provider_id
);
111 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
112 void OnWorkerReadyForInspection(int embedded_worker_id
);
113 void OnWorkerScriptLoaded(int embedded_worker_id
,
116 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
117 void OnWorkerScriptEvaluated(int embedded_worker_id
, bool success
);
118 void OnWorkerStarted(int embedded_worker_id
);
119 void OnWorkerStopped(int embedded_worker_id
);
120 void OnReportException(int embedded_worker_id
,
121 const base::string16
& error_message
,
124 const GURL
& source_url
);
125 void OnReportConsoleMessage(
126 int embedded_worker_id
,
127 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
128 void OnIncrementServiceWorkerRefCount(int handle_id
);
129 void OnDecrementServiceWorkerRefCount(int handle_id
);
130 void OnIncrementRegistrationRefCount(int registration_handle_id
);
131 void OnDecrementRegistrationRefCount(int registration_handle_id
);
132 void OnPostMessageToWorker(
134 const base::string16
& message
,
135 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
136 void OnServiceWorkerObjectDestroyed(int handle_id
);
137 void OnTerminateWorker(int handle_id
);
139 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
141 int64 registration_id
);
143 void GetRegistrationObjectInfoAndVersionAttributes(
144 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
145 ServiceWorkerRegistration
* registration
,
146 ServiceWorkerRegistrationObjectInfo
* info
,
147 ServiceWorkerVersionAttributes
* attrs
);
149 // Callbacks from ServiceWorkerContextCore
150 void RegistrationComplete(int thread_id
,
153 ServiceWorkerStatusCode status
,
154 const std::string
& status_message
,
155 int64 registration_id
);
157 void UpdateComplete(int thread_id
,
160 ServiceWorkerStatusCode status
,
161 const std::string
& status_message
,
162 int64 registration_id
);
164 void UnregistrationComplete(int thread_id
,
166 ServiceWorkerStatusCode status
);
168 void GetRegistrationComplete(
172 ServiceWorkerStatusCode status
,
173 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
175 void GetRegistrationsComplete(
179 const std::vector
<scoped_refptr
<ServiceWorkerRegistration
>>&
182 void GetRegistrationForReadyComplete(
185 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
186 ServiceWorkerRegistration
* registration
);
188 void SendRegistrationError(int thread_id
,
190 ServiceWorkerStatusCode status
,
191 const std::string
& status_message
);
193 void SendUpdateError(int thread_id
,
195 ServiceWorkerStatusCode status
,
196 const std::string
& status_message
);
198 void SendUnregistrationError(int thread_id
,
200 ServiceWorkerStatusCode status
);
202 void SendGetRegistrationError(int thread_id
,
204 ServiceWorkerStatusCode status
);
206 void SendGetRegistrationsError(int thread_id
,
208 ServiceWorkerStatusCode status
);
210 ServiceWorkerContextCore
* GetContext();
212 int render_process_id_
;
213 MessagePortMessageFilter
* const message_port_message_filter_
;
214 ResourceContext
* resource_context_
;
215 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
217 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
218 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
220 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
221 ScopedVector
<IPC::Message
> pending_messages_
;
223 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
226 } // namespace content
228 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_