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 provider_id
, int64 registration_id
);
92 void OnUnregisterServiceWorker(int thread_id
,
95 int64 registration_id
);
96 void OnGetRegistration(int thread_id
,
99 const GURL
& document_url
);
100 void OnGetRegistrations(int thread_id
, int request_id
, int provider_id
);
101 void OnGetRegistrationForReady(int thread_id
,
104 void OnProviderCreated(int provider_id
,
106 ServiceWorkerProviderType provider_type
);
107 void OnProviderDestroyed(int provider_id
);
108 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
109 void OnWorkerReadyForInspection(int embedded_worker_id
);
110 void OnWorkerScriptLoaded(int embedded_worker_id
,
113 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
114 void OnWorkerScriptEvaluated(int embedded_worker_id
, bool success
);
115 void OnWorkerStarted(int embedded_worker_id
);
116 void OnWorkerStopped(int embedded_worker_id
);
117 void OnReportException(int embedded_worker_id
,
118 const base::string16
& error_message
,
121 const GURL
& source_url
);
122 void OnReportConsoleMessage(
123 int embedded_worker_id
,
124 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
125 void OnIncrementServiceWorkerRefCount(int handle_id
);
126 void OnDecrementServiceWorkerRefCount(int handle_id
);
127 void OnIncrementRegistrationRefCount(int registration_handle_id
);
128 void OnDecrementRegistrationRefCount(int registration_handle_id
);
129 void OnPostMessageToWorker(
131 const base::string16
& message
,
132 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
133 void OnServiceWorkerObjectDestroyed(int handle_id
);
134 void OnTerminateWorker(int handle_id
);
136 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
138 int64 registration_id
);
140 void GetRegistrationObjectInfoAndVersionAttributes(
141 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
142 ServiceWorkerRegistration
* registration
,
143 ServiceWorkerRegistrationObjectInfo
* info
,
144 ServiceWorkerVersionAttributes
* attrs
);
146 // Callbacks from ServiceWorkerContextCore
147 void RegistrationComplete(int thread_id
,
150 ServiceWorkerStatusCode status
,
151 const std::string
& status_message
,
152 int64 registration_id
);
154 void UnregistrationComplete(int thread_id
,
156 ServiceWorkerStatusCode status
);
158 void GetRegistrationComplete(
162 ServiceWorkerStatusCode status
,
163 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
165 void GetRegistrationsComplete(
169 const std::vector
<scoped_refptr
<ServiceWorkerRegistration
>>&
172 void GetRegistrationForReadyComplete(
175 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
176 ServiceWorkerRegistration
* registration
);
178 void SendRegistrationError(int thread_id
,
180 ServiceWorkerStatusCode status
,
181 const std::string
& status_message
);
183 void SendUnregistrationError(int thread_id
,
185 ServiceWorkerStatusCode status
);
187 void SendGetRegistrationError(int thread_id
,
189 ServiceWorkerStatusCode status
);
191 void SendGetRegistrationsError(int thread_id
,
193 ServiceWorkerStatusCode status
);
195 ServiceWorkerContextCore
* GetContext();
197 int render_process_id_
;
198 MessagePortMessageFilter
* const message_port_message_filter_
;
199 ResourceContext
* resource_context_
;
200 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
202 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
203 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
205 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
206 ScopedVector
<IPC::Message
> pending_messages_
;
208 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
211 } // namespace content
213 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_