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 // Returns the existing registration handle whose reference count is
64 // incremented or newly created one if it doesn't exist.
65 ServiceWorkerRegistrationHandle
* GetOrCreateRegistrationHandle(
66 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
67 ServiceWorkerRegistration
* registration
);
69 MessagePortMessageFilter
* message_port_message_filter() {
70 return message_port_message_filter_
;
74 ~ServiceWorkerDispatcherHost() override
;
77 friend class BrowserThread
;
78 friend class base::DeleteHelper
<ServiceWorkerDispatcherHost
>;
79 friend class TestingServiceWorkerDispatcherHost
;
81 // IPC Message handlers
82 void OnRegisterServiceWorker(int thread_id
,
86 const GURL
& script_url
);
87 void OnUnregisterServiceWorker(int thread_id
,
91 void OnGetRegistration(int thread_id
,
94 const GURL
& document_url
);
95 void OnGetRegistrationForReady(int thread_id
,
98 void OnProviderCreated(int provider_id
,
100 ServiceWorkerProviderType provider_type
);
101 void OnProviderDestroyed(int provider_id
);
102 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
103 void OnWorkerReadyForInspection(int embedded_worker_id
);
104 void OnWorkerScriptLoaded(int embedded_worker_id
,
107 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
108 void OnWorkerScriptEvaluated(int embedded_worker_id
, bool success
);
109 void OnWorkerStarted(int embedded_worker_id
);
110 void OnWorkerStopped(int embedded_worker_id
);
111 void OnPausedAfterDownload(int embedded_worker_id
);
112 void OnReportException(int embedded_worker_id
,
113 const base::string16
& error_message
,
116 const GURL
& source_url
);
117 void OnReportConsoleMessage(
118 int embedded_worker_id
,
119 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
120 void OnIncrementServiceWorkerRefCount(int handle_id
);
121 void OnDecrementServiceWorkerRefCount(int handle_id
);
122 void OnIncrementRegistrationRefCount(int registration_handle_id
);
123 void OnDecrementRegistrationRefCount(int registration_handle_id
);
124 void OnPostMessageToWorker(
126 const base::string16
& message
,
127 const std::vector
<TransferredMessagePort
>& sent_message_ports
);
128 void OnServiceWorkerObjectDestroyed(int handle_id
);
129 void OnTerminateWorker(int handle_id
);
131 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
133 int64 registration_id
);
135 void GetRegistrationObjectInfoAndVersionAttributes(
136 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
137 ServiceWorkerRegistration
* registration
,
138 ServiceWorkerRegistrationObjectInfo
* info
,
139 ServiceWorkerVersionAttributes
* attrs
);
141 // Callbacks from ServiceWorkerContextCore
142 void RegistrationComplete(int thread_id
,
145 ServiceWorkerStatusCode status
,
146 const std::string
& status_message
,
147 int64 registration_id
);
149 void UnregistrationComplete(int thread_id
,
151 ServiceWorkerStatusCode status
);
153 void GetRegistrationComplete(
157 ServiceWorkerStatusCode status
,
158 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
160 void GetRegistrationForReadyComplete(
163 base::WeakPtr
<ServiceWorkerProviderHost
> provider_host
,
164 ServiceWorkerRegistration
* registration
);
166 void SendRegistrationError(int thread_id
,
168 ServiceWorkerStatusCode status
,
169 const std::string
& status_message
);
171 void SendUnregistrationError(int thread_id
,
173 ServiceWorkerStatusCode status
);
175 void SendGetRegistrationError(int thread_id
,
177 ServiceWorkerStatusCode status
);
179 ServiceWorkerContextCore
* GetContext();
181 int render_process_id_
;
182 MessagePortMessageFilter
* const message_port_message_filter_
;
183 ResourceContext
* resource_context_
;
184 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
186 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
187 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
189 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
190 ScopedVector
<IPC::Message
> pending_messages_
;
192 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
195 } // namespace content
197 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_