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/public/browser/browser_message_filter.h"
17 struct EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
;
21 class MessagePortMessageFilter
;
22 class ResourceContext
;
23 class ServiceWorkerContextCore
;
24 class ServiceWorkerContextWrapper
;
25 class ServiceWorkerHandle
;
26 class ServiceWorkerProviderHost
;
27 class ServiceWorkerRegistration
;
28 class ServiceWorkerRegistrationHandle
;
29 struct ServiceWorkerRegistrationObjectInfo
;
30 struct ServiceWorkerVersionAttributes
;
32 class CONTENT_EXPORT ServiceWorkerDispatcherHost
: public BrowserMessageFilter
{
34 ServiceWorkerDispatcherHost(
35 int render_process_id
,
36 MessagePortMessageFilter
* message_port_message_filter
,
37 ResourceContext
* resource_context
);
39 void Init(ServiceWorkerContextWrapper
* context_wrapper
);
41 // BrowserMessageFilter implementation
42 void OnFilterAdded(IPC::Sender
* sender
) override
;
43 void OnDestruct() const override
;
44 bool OnMessageReceived(const IPC::Message
& message
) override
;
46 // IPC::Sender implementation
48 // Send() queues the message until the underlying sender is ready. This
49 // class assumes that Send() can only fail after that when the renderer
50 // process has terminated, at which point the whole instance will eventually
52 bool Send(IPC::Message
* message
) override
;
54 // Returns the existing registration handle whose reference count is
55 // incremented or newly created one if it doesn't exist.
56 ServiceWorkerRegistrationHandle
* GetOrCreateRegistrationHandle(
58 ServiceWorkerRegistration
* registration
);
60 void RegisterServiceWorkerHandle(scoped_ptr
<ServiceWorkerHandle
> handle
);
61 void RegisterServiceWorkerRegistrationHandle(
62 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle
);
64 MessagePortMessageFilter
* message_port_message_filter() {
65 return message_port_message_filter_
;
69 ~ServiceWorkerDispatcherHost() override
;
72 friend class BrowserThread
;
73 friend class base::DeleteHelper
<ServiceWorkerDispatcherHost
>;
74 friend class TestingServiceWorkerDispatcherHost
;
76 // IPC Message handlers
77 void OnRegisterServiceWorker(int thread_id
,
81 const GURL
& script_url
);
82 void OnUnregisterServiceWorker(int thread_id
,
86 void OnGetRegistration(int thread_id
,
89 const GURL
& document_url
);
90 void OnProviderCreated(int provider_id
);
91 void OnProviderDestroyed(int provider_id
);
92 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
93 void OnWorkerReadyForInspection(int embedded_worker_id
);
94 void OnWorkerScriptLoaded(int embedded_worker_id
, int thread_id
);
95 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
96 void OnWorkerStarted(int embedded_worker_id
);
97 void OnWorkerStopped(int embedded_worker_id
);
98 void OnPausedAfterDownload(int embedded_worker_id
);
99 void OnReportException(int embedded_worker_id
,
100 const base::string16
& error_message
,
103 const GURL
& source_url
);
104 void OnReportConsoleMessage(
105 int embedded_worker_id
,
106 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
107 void OnPostMessage(int handle_id
,
108 const base::string16
& message
,
109 const std::vector
<int>& sent_message_port_ids
);
110 void OnIncrementServiceWorkerRefCount(int handle_id
);
111 void OnDecrementServiceWorkerRefCount(int handle_id
);
112 void OnIncrementRegistrationRefCount(int registration_handle_id
);
113 void OnDecrementRegistrationRefCount(int registration_handle_id
);
114 void OnPostMessageToWorker(int handle_id
,
115 const base::string16
& message
,
116 const std::vector
<int>& sent_message_port_ids
);
117 void OnServiceWorkerObjectDestroyed(int handle_id
);
119 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
121 int64 registration_id
);
123 void GetRegistrationObjectInfoAndVersionAttributes(
125 ServiceWorkerRegistration
* registration
,
126 ServiceWorkerRegistrationObjectInfo
* info
,
127 ServiceWorkerVersionAttributes
* attrs
);
129 // Callbacks from ServiceWorkerContextCore
130 void RegistrationComplete(int thread_id
,
133 ServiceWorkerStatusCode status
,
134 int64 registration_id
);
136 void UnregistrationComplete(int thread_id
,
138 ServiceWorkerStatusCode status
);
140 void GetRegistrationComplete(
144 ServiceWorkerStatusCode status
,
145 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
147 void SendRegistrationError(int thread_id
,
149 ServiceWorkerStatusCode status
);
151 void SendUnregistrationError(int thread_id
,
153 ServiceWorkerStatusCode status
);
155 void SendGetRegistrationError(int thread_id
,
157 ServiceWorkerStatusCode status
);
159 ServiceWorkerContextCore
* GetContext();
161 int render_process_id_
;
162 MessagePortMessageFilter
* const message_port_message_filter_
;
163 ResourceContext
* resource_context_
;
164 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
166 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
167 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
169 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
170 ScopedVector
<IPC::Message
> pending_messages_
;
172 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
175 } // namespace content
177 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_