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 #include "content/browser/service_worker/embedded_worker_registry.h"
7 #include "base/bind_helpers.h"
8 #include "base/stl_util.h"
9 #include "content/browser/renderer_host/render_widget_helper.h"
10 #include "content/browser/service_worker/embedded_worker_instance.h"
11 #include "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/common/service_worker/embedded_worker_messages.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "ipc/ipc_message.h"
16 #include "ipc/ipc_sender.h"
21 scoped_refptr
<EmbeddedWorkerRegistry
> EmbeddedWorkerRegistry::Create(
22 const base::WeakPtr
<ServiceWorkerContextCore
>& context
) {
23 return make_scoped_refptr(new EmbeddedWorkerRegistry(context
, 0));
27 scoped_refptr
<EmbeddedWorkerRegistry
> EmbeddedWorkerRegistry::Create(
28 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
29 EmbeddedWorkerRegistry
* old_registry
) {
30 scoped_refptr
<EmbeddedWorkerRegistry
> registry
=
31 new EmbeddedWorkerRegistry(
33 old_registry
->next_embedded_worker_id_
);
34 registry
->process_sender_map_
.swap(old_registry
->process_sender_map_
);
38 scoped_ptr
<EmbeddedWorkerInstance
> EmbeddedWorkerRegistry::CreateWorker() {
39 scoped_ptr
<EmbeddedWorkerInstance
> worker(
40 new EmbeddedWorkerInstance(context_
, next_embedded_worker_id_
));
41 worker_map_
[next_embedded_worker_id_
++] = worker
.get();
45 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::StopWorker(
46 int process_id
, int embedded_worker_id
) {
47 return Send(process_id
,
48 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id
));
51 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message
& message
,
53 // TODO(kinuko): Move all EmbeddedWorker message handling from
54 // ServiceWorkerDispatcherHost.
56 WorkerInstanceMap::iterator found
= worker_map_
.find(message
.routing_id());
57 DCHECK(found
!= worker_map_
.end());
58 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
60 return found
->second
->OnMessageReceived(message
);
63 void EmbeddedWorkerRegistry::Shutdown() {
64 for (WorkerInstanceMap::iterator it
= worker_map_
.begin();
65 it
!= worker_map_
.end();
71 void EmbeddedWorkerRegistry::OnWorkerReadyForInspection(
73 int embedded_worker_id
) {
74 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
75 DCHECK(found
!= worker_map_
.end());
76 DCHECK_EQ(found
->second
->process_id(), process_id
);
77 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
79 found
->second
->OnReadyForInspection();
82 void EmbeddedWorkerRegistry::OnWorkerScriptLoaded(
85 int embedded_worker_id
) {
86 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
87 DCHECK(found
!= worker_map_
.end());
88 DCHECK_EQ(found
->second
->process_id(), process_id
);
89 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
91 found
->second
->OnScriptLoaded(thread_id
);
94 void EmbeddedWorkerRegistry::OnWorkerScriptLoadFailed(int process_id
,
95 int embedded_worker_id
) {
96 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
97 DCHECK(found
!= worker_map_
.end());
98 DCHECK_EQ(found
->second
->process_id(), process_id
);
99 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
101 found
->second
->OnScriptLoadFailed();
104 void EmbeddedWorkerRegistry::OnWorkerScriptEvaluated(int process_id
,
105 int embedded_worker_id
,
107 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
108 DCHECK(found
!= worker_map_
.end());
109 DCHECK_EQ(found
->second
->process_id(), process_id
);
110 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
112 found
->second
->OnScriptEvaluated(success
);
115 void EmbeddedWorkerRegistry::OnWorkerStarted(
116 int process_id
, int embedded_worker_id
) {
117 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
118 // TODO(falken): Instead of DCHECK, we should terminate the process on
119 // unexpected message. Same with most of the DCHECKs in this file.
120 DCHECK(found
!= worker_map_
.end());
121 DCHECK_EQ(found
->second
->process_id(), process_id
);
122 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
125 DCHECK(ContainsKey(worker_process_map_
, process_id
) &&
126 worker_process_map_
[process_id
].count(embedded_worker_id
) == 1);
127 if (!ContainsKey(worker_process_map_
, process_id
) ||
128 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0) {
132 found
->second
->OnStarted();
135 void EmbeddedWorkerRegistry::OnWorkerStopped(
136 int process_id
, int embedded_worker_id
) {
137 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
138 DCHECK(found
!= worker_map_
.end());
139 DCHECK_EQ(found
->second
->process_id(), process_id
);
140 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
142 worker_process_map_
[process_id
].erase(embedded_worker_id
);
143 found
->second
->OnStopped();
146 void EmbeddedWorkerRegistry::OnPausedAfterDownload(
147 int process_id
, int embedded_worker_id
) {
148 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
149 DCHECK(found
!= worker_map_
.end());
150 DCHECK_EQ(found
->second
->process_id(), process_id
);
151 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
153 found
->second
->OnPausedAfterDownload();
156 void EmbeddedWorkerRegistry::OnReportException(
157 int embedded_worker_id
,
158 const base::string16
& error_message
,
161 const GURL
& source_url
) {
162 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
163 DCHECK(found
!= worker_map_
.end());
164 if (found
== worker_map_
.end())
166 found
->second
->OnReportException(
167 error_message
, line_number
, column_number
, source_url
);
170 void EmbeddedWorkerRegistry::OnReportConsoleMessage(
171 int embedded_worker_id
,
172 int source_identifier
,
174 const base::string16
& message
,
176 const GURL
& source_url
) {
177 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
178 DCHECK(found
!= worker_map_
.end());
179 if (found
== worker_map_
.end())
181 found
->second
->OnReportConsoleMessage(
182 source_identifier
, message_level
, message
, line_number
, source_url
);
185 void EmbeddedWorkerRegistry::AddChildProcessSender(
188 MessagePortMessageFilter
* message_port_message_filter
) {
189 process_sender_map_
[process_id
] = sender
;
190 process_message_port_message_filter_map_
[process_id
] =
191 message_port_message_filter
;
192 DCHECK(!ContainsKey(worker_process_map_
, process_id
));
195 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id
) {
196 process_sender_map_
.erase(process_id
);
197 process_message_port_message_filter_map_
.erase(process_id
);
198 std::map
<int, std::set
<int> >::iterator found
=
199 worker_process_map_
.find(process_id
);
200 if (found
!= worker_process_map_
.end()) {
201 const std::set
<int>& worker_set
= worker_process_map_
[process_id
];
202 for (std::set
<int>::const_iterator it
= worker_set
.begin();
203 it
!= worker_set
.end();
205 int embedded_worker_id
= *it
;
206 DCHECK(ContainsKey(worker_map_
, embedded_worker_id
));
207 worker_map_
[embedded_worker_id
]->OnStopped();
209 worker_process_map_
.erase(found
);
213 EmbeddedWorkerInstance
* EmbeddedWorkerRegistry::GetWorker(
214 int embedded_worker_id
) {
215 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
216 if (found
== worker_map_
.end())
218 return found
->second
;
221 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id
) const {
222 if (embedded_worker_id
< initial_embedded_worker_id_
||
223 next_embedded_worker_id_
<= embedded_worker_id
) {
229 MessagePortMessageFilter
*
230 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id
) {
231 return process_message_port_message_filter_map_
[process_id
];
234 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
235 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
236 int initial_embedded_worker_id
)
238 next_embedded_worker_id_(initial_embedded_worker_id
),
239 initial_embedded_worker_id_(initial_embedded_worker_id
) {
242 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
246 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::SendStartWorker(
247 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
250 return SERVICE_WORKER_ERROR_ABORT
;
252 // The ServiceWorkerDispatcherHost is supposed to be created when the process
253 // is created, and keep an entry in process_sender_map_ for its whole
255 DCHECK(ContainsKey(process_sender_map_
, process_id
));
257 int embedded_worker_id
= params
->embedded_worker_id
;
258 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
259 DCHECK(found
!= worker_map_
.end());
260 DCHECK_EQ(found
->second
->process_id(), process_id
);
262 DCHECK(!ContainsKey(worker_process_map_
, process_id
) ||
263 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0);
265 ServiceWorkerStatusCode status
=
266 Send(process_id
, new EmbeddedWorkerMsg_StartWorker(*params
));
267 if (status
== SERVICE_WORKER_OK
)
268 worker_process_map_
[process_id
].insert(embedded_worker_id
);
272 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::Send(
273 int process_id
, IPC::Message
* message_ptr
) {
274 scoped_ptr
<IPC::Message
> message(message_ptr
);
276 return SERVICE_WORKER_ERROR_ABORT
;
277 ProcessToSenderMap::iterator found
= process_sender_map_
.find(process_id
);
278 if (found
== process_sender_map_
.end())
279 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
;
280 if (!found
->second
->Send(message
.release()))
281 return SERVICE_WORKER_ERROR_IPC_FAILED
;
282 return SERVICE_WORKER_OK
;
285 void EmbeddedWorkerRegistry::RemoveWorker(int process_id
,
286 int embedded_worker_id
) {
287 DCHECK(ContainsKey(worker_map_
, embedded_worker_id
));
288 worker_map_
.erase(embedded_worker_id
);
289 worker_process_map_
.erase(process_id
);
292 } // namespace content