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 ServiceWorkerRegistrationObjectInfo
;
33 struct ServiceWorkerVersionAttributes
;
34 struct TransferredMessagePort
;
36 class CONTENT_EXPORT ServiceWorkerDispatcherHost
: public BrowserMessageFilter
{
38 ServiceWorkerDispatcherHost(
39 int render_process_id
,
40 MessagePortMessageFilter
* message_port_message_filter
,
41 ResourceContext
* resource_context
);
43 void Init(ServiceWorkerContextWrapper
* context_wrapper
);
45 // BrowserMessageFilter implementation
46 void OnFilterAdded(IPC::Sender
* sender
) override
;
47 void OnFilterRemoved() override
;
48 void OnDestruct() const override
;
49 bool OnMessageReceived(const IPC::Message
& message
) override
;
51 // IPC::Sender implementation
53 // Send() queues the message until the underlying sender is ready. This
54 // class assumes that Send() can only fail after that when the renderer
55 // process has terminated, at which point the whole instance will eventually
57 bool Send(IPC::Message
* message
) override
;
59 void RegisterServiceWorkerHandle(scoped_ptr
<ServiceWorkerHandle
> handle
);
60 void RegisterServiceWorkerRegistrationHandle(
61 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle
);
63 ServiceWorkerHandle
* FindServiceWorkerHandle(int provider_id
,
66 // Returns the existing registration handle whose reference count is
67 // incremented or newly created one if it doesn't exist.
68 ServiceWorkerRegistrationHandle
* GetOrCreateRegistrationHandle(
69 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
70 ServiceWorkerRegistration
* registration
);
72 MessagePortMessageFilter
* message_port_message_filter() {
73 return message_port_message_filter_
;
77 ~ServiceWorkerDispatcherHost() override
;
80 friend class BrowserThread
;
81 friend class base::DeleteHelper
<ServiceWorkerDispatcherHost
>;
82 friend class TestingServiceWorkerDispatcherHost
;
84 // IPC Message handlers
85 void OnRegisterServiceWorker(int thread_id
,
89 const GURL
& script_url
);
90 void OnUnregisterServiceWorker(int thread_id
,
94 void OnGetRegistration(int thread_id
,
97 const GURL
& document_url
);
98 void OnGetRegistrationForReady(int thread_id
,
101 void OnProviderCreated(int provider_id
,
103 ServiceWorkerProviderType provider_type
);
104 void OnProviderDestroyed(int provider_id
);
105 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
106 void OnWorkerReadyForInspection(int embedded_worker_id
);
107 void OnWorkerScriptLoaded(int embedded_worker_id
,
110 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
111 void OnWorkerScriptEvaluated(int embedded_worker_id
, bool success
);
112 void OnWorkerStarted(int embedded_worker_id
);
113 void OnWorkerStopped(int embedded_worker_id
);
114 void OnPausedAfterDownload(int embedded_worker_id
);
115 void OnReportException(int embedded_worker_id
,
116 const base::string16
& error_message
,
119 const GURL
& source_url
);
120 void OnReportConsoleMessage(
121 int embedded_worker_id
,
122 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
123 void OnIncrementServiceWorkerRefCount(int handle_id
);
124 void OnDecrementServiceWorkerRefCount(int handle_id
);
125 void OnIncrementRegistrationRefCount(int registration_handle_id
);
126 void OnDecrementRegistrationRefCount(int registration_handle_id
);
127 void OnPostMessageToWorker(
129 const base::string16
& message
,
130 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
131 void OnServiceWorkerObjectDestroyed(int handle_id
);
132 void OnTerminateWorker(int handle_id
);
134 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
136 int64 registration_id
);
138 void GetRegistrationObjectInfoAndVersionAttributes(
139 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
140 ServiceWorkerRegistration
* registration
,
141 ServiceWorkerRegistrationObjectInfo
* info
,
142 ServiceWorkerVersionAttributes
* attrs
);
144 // Callbacks from ServiceWorkerContextCore
145 void RegistrationComplete(int thread_id
,
148 ServiceWorkerStatusCode status
,
149 const std::string
& status_message
,
150 int64 registration_id
);
152 void UnregistrationComplete(int thread_id
,
154 ServiceWorkerStatusCode status
);
156 void GetRegistrationComplete(
160 ServiceWorkerStatusCode status
,
161 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
163 void GetRegistrationForReadyComplete(
166 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
167 ServiceWorkerRegistration
* registration
);
169 void SendRegistrationError(int thread_id
,
171 ServiceWorkerStatusCode status
,
172 const std::string
& status_message
);
174 void SendUnregistrationError(int thread_id
,
176 ServiceWorkerStatusCode status
);
178 void SendGetRegistrationError(int thread_id
,
180 ServiceWorkerStatusCode status
);
182 ServiceWorkerContextCore
* GetContext();
184 int render_process_id_
;
185 MessagePortMessageFilter
* const message_port_message_filter_
;
186 ResourceContext
* resource_context_
;
187 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
189 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
190 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
192 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
193 ScopedVector
<IPC::Message
> pending_messages_
;
195 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
198 } // namespace content
200 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_