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 OnFilterRemoved() override
;
44 void OnDestruct() const override
;
45 bool OnMessageReceived(const IPC::Message
& message
) override
;
47 // IPC::Sender implementation
49 // Send() queues the message until the underlying sender is ready. This
50 // class assumes that Send() can only fail after that when the renderer
51 // process has terminated, at which point the whole instance will eventually
53 bool Send(IPC::Message
* message
) override
;
55 // Returns the existing registration handle whose reference count is
56 // incremented or newly created one if it doesn't exist.
57 ServiceWorkerRegistrationHandle
* GetOrCreateRegistrationHandle(
59 ServiceWorkerRegistration
* registration
);
61 void RegisterServiceWorkerHandle(scoped_ptr
<ServiceWorkerHandle
> handle
);
62 void RegisterServiceWorkerRegistrationHandle(
63 scoped_ptr
<ServiceWorkerRegistrationHandle
> handle
);
65 MessagePortMessageFilter
* message_port_message_filter() {
66 return message_port_message_filter_
;
70 ~ServiceWorkerDispatcherHost() override
;
73 friend class BrowserThread
;
74 friend class base::DeleteHelper
<ServiceWorkerDispatcherHost
>;
75 friend class TestingServiceWorkerDispatcherHost
;
77 // IPC Message handlers
78 void OnRegisterServiceWorker(int thread_id
,
82 const GURL
& script_url
);
83 void OnUnregisterServiceWorker(int thread_id
,
87 void OnGetRegistration(int thread_id
,
90 const GURL
& document_url
);
91 void OnProviderCreated(int provider_id
);
92 void OnProviderDestroyed(int provider_id
);
93 void OnSetHostedVersionId(int provider_id
, int64 version_id
);
94 void OnWorkerReadyForInspection(int embedded_worker_id
);
95 void OnWorkerScriptLoaded(int embedded_worker_id
, int thread_id
);
96 void OnWorkerScriptLoadFailed(int embedded_worker_id
);
97 void OnWorkerScriptEvaluated(int embedded_worker_id
, bool success
);
98 void OnWorkerStarted(int embedded_worker_id
);
99 void OnWorkerStopped(int embedded_worker_id
);
100 void OnPausedAfterDownload(int embedded_worker_id
);
101 void OnReportException(int embedded_worker_id
,
102 const base::string16
& error_message
,
105 const GURL
& source_url
);
106 void OnReportConsoleMessage(
107 int embedded_worker_id
,
108 const EmbeddedWorkerHostMsg_ReportConsoleMessage_Params
& params
);
109 void OnPostMessage(int handle_id
,
110 const base::string16
& message
,
111 const std::vector
<int>& sent_message_port_ids
);
112 void OnIncrementServiceWorkerRefCount(int handle_id
);
113 void OnDecrementServiceWorkerRefCount(int handle_id
);
114 void OnIncrementRegistrationRefCount(int registration_handle_id
);
115 void OnDecrementRegistrationRefCount(int registration_handle_id
);
116 void OnPostMessageToWorker(int handle_id
,
117 const base::string16
& message
,
118 const std::vector
<int>& sent_message_port_ids
);
119 void OnServiceWorkerObjectDestroyed(int handle_id
);
121 ServiceWorkerRegistrationHandle
* FindRegistrationHandle(
123 int64 registration_id
);
125 void GetRegistrationObjectInfoAndVersionAttributes(
127 ServiceWorkerRegistration
* registration
,
128 ServiceWorkerRegistrationObjectInfo
* info
,
129 ServiceWorkerVersionAttributes
* attrs
);
131 // Callbacks from ServiceWorkerContextCore
132 void RegistrationComplete(int thread_id
,
135 ServiceWorkerStatusCode status
,
136 int64 registration_id
);
138 void UnregistrationComplete(int thread_id
,
140 ServiceWorkerStatusCode status
);
142 void GetRegistrationComplete(
146 ServiceWorkerStatusCode status
,
147 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
149 void SendRegistrationError(int thread_id
,
151 ServiceWorkerStatusCode status
);
153 void SendUnregistrationError(int thread_id
,
155 ServiceWorkerStatusCode status
);
157 void SendGetRegistrationError(int thread_id
,
159 ServiceWorkerStatusCode status
);
161 ServiceWorkerContextCore
* GetContext();
163 int render_process_id_
;
164 MessagePortMessageFilter
* const message_port_message_filter_
;
165 ResourceContext
* resource_context_
;
166 scoped_refptr
<ServiceWorkerContextWrapper
> context_wrapper_
;
168 IDMap
<ServiceWorkerHandle
, IDMapOwnPointer
> handles_
;
169 IDMap
<ServiceWorkerRegistrationHandle
, IDMapOwnPointer
> registration_handles_
;
171 bool channel_ready_
; // True after BrowserMessageFilter::sender_ != NULL.
172 ScopedVector
<IPC::Message
> pending_messages_
;
174 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerDispatcherHost
);
177 } // namespace content
179 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_DISPATCHER_HOST_H_