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
) {
52 // TODO(kinuko): Move all EmbeddedWorker message handling from
53 // ServiceWorkerDispatcherHost.
55 WorkerInstanceMap::iterator found
= worker_map_
.find(message
.routing_id());
56 DCHECK(found
!= worker_map_
.end());
57 if (found
== worker_map_
.end())
59 return found
->second
->OnMessageReceived(message
);
62 void EmbeddedWorkerRegistry::Shutdown() {
63 for (WorkerInstanceMap::iterator it
= worker_map_
.begin();
64 it
!= worker_map_
.end();
70 void EmbeddedWorkerRegistry::OnWorkerReadyForInspection(
72 int embedded_worker_id
) {
73 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
74 DCHECK(found
!= worker_map_
.end());
75 DCHECK_EQ(found
->second
->process_id(), process_id
);
76 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
78 found
->second
->OnReadyForInspection();
81 void EmbeddedWorkerRegistry::OnWorkerScriptLoaded(
84 int embedded_worker_id
) {
85 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
86 DCHECK(found
!= worker_map_
.end());
87 DCHECK_EQ(found
->second
->process_id(), process_id
);
88 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
90 found
->second
->OnScriptLoaded(thread_id
);
93 void EmbeddedWorkerRegistry::OnWorkerScriptLoadFailed(int process_id
,
94 int embedded_worker_id
) {
95 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
96 DCHECK(found
!= worker_map_
.end());
97 DCHECK_EQ(found
->second
->process_id(), process_id
);
98 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
100 found
->second
->OnScriptLoadFailed();
103 void EmbeddedWorkerRegistry::OnWorkerScriptEvaluated(int process_id
,
104 int embedded_worker_id
,
106 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
107 DCHECK(found
!= worker_map_
.end());
108 DCHECK_EQ(found
->second
->process_id(), process_id
);
109 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
111 found
->second
->OnScriptEvaluated(success
);
114 void EmbeddedWorkerRegistry::OnWorkerStarted(
115 int process_id
, int embedded_worker_id
) {
116 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
117 // TODO(falken): Instead of DCHECK, we should terminate the process on
118 // unexpected message. Same with most of the DCHECKs in this file.
119 DCHECK(found
!= worker_map_
.end());
120 DCHECK_EQ(found
->second
->process_id(), process_id
);
121 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
124 DCHECK(ContainsKey(worker_process_map_
, process_id
) &&
125 worker_process_map_
[process_id
].count(embedded_worker_id
) == 1);
126 if (!ContainsKey(worker_process_map_
, process_id
) ||
127 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0) {
131 found
->second
->OnStarted();
134 void EmbeddedWorkerRegistry::OnWorkerStopped(
135 int process_id
, int embedded_worker_id
) {
136 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
137 DCHECK(found
!= worker_map_
.end());
138 DCHECK_EQ(found
->second
->process_id(), process_id
);
139 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
141 worker_process_map_
[process_id
].erase(embedded_worker_id
);
142 found
->second
->OnStopped();
145 void EmbeddedWorkerRegistry::OnPausedAfterDownload(
146 int process_id
, int embedded_worker_id
) {
147 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
148 DCHECK(found
!= worker_map_
.end());
149 DCHECK_EQ(found
->second
->process_id(), process_id
);
150 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
152 found
->second
->OnPausedAfterDownload();
155 void EmbeddedWorkerRegistry::OnReportException(
156 int embedded_worker_id
,
157 const base::string16
& error_message
,
160 const GURL
& source_url
) {
161 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
162 DCHECK(found
!= worker_map_
.end());
163 if (found
== worker_map_
.end())
165 found
->second
->OnReportException(
166 error_message
, line_number
, column_number
, source_url
);
169 void EmbeddedWorkerRegistry::OnReportConsoleMessage(
170 int embedded_worker_id
,
171 int source_identifier
,
173 const base::string16
& message
,
175 const GURL
& source_url
) {
176 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
177 DCHECK(found
!= worker_map_
.end());
178 if (found
== worker_map_
.end())
180 found
->second
->OnReportConsoleMessage(
181 source_identifier
, message_level
, message
, line_number
, source_url
);
184 void EmbeddedWorkerRegistry::AddChildProcessSender(
187 MessagePortMessageFilter
* message_port_message_filter
) {
188 process_sender_map_
[process_id
] = sender
;
189 process_message_port_message_filter_map_
[process_id
] =
190 message_port_message_filter
;
191 DCHECK(!ContainsKey(worker_process_map_
, process_id
));
194 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id
) {
195 process_sender_map_
.erase(process_id
);
196 process_message_port_message_filter_map_
.erase(process_id
);
197 std::map
<int, std::set
<int> >::iterator found
=
198 worker_process_map_
.find(process_id
);
199 if (found
!= worker_process_map_
.end()) {
200 const std::set
<int>& worker_set
= worker_process_map_
[process_id
];
201 for (std::set
<int>::const_iterator it
= worker_set
.begin();
202 it
!= worker_set
.end();
204 int embedded_worker_id
= *it
;
205 DCHECK(ContainsKey(worker_map_
, embedded_worker_id
));
206 worker_map_
[embedded_worker_id
]->OnStopped();
208 worker_process_map_
.erase(found
);
212 EmbeddedWorkerInstance
* EmbeddedWorkerRegistry::GetWorker(
213 int embedded_worker_id
) {
214 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
215 if (found
== worker_map_
.end())
217 return found
->second
;
220 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id
) const {
221 if (embedded_worker_id
< initial_embedded_worker_id_
||
222 next_embedded_worker_id_
<= embedded_worker_id
) {
228 MessagePortMessageFilter
*
229 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id
) {
230 return process_message_port_message_filter_map_
[process_id
];
233 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
234 const base::WeakPtr
<ServiceWorkerContextCore
>& context
,
235 int initial_embedded_worker_id
)
237 next_embedded_worker_id_(initial_embedded_worker_id
),
238 initial_embedded_worker_id_(initial_embedded_worker_id
) {
241 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
245 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::SendStartWorker(
246 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
248 // The ServiceWorkerDispatcherHost is supposed to be created when the process
249 // is created, and keep an entry in process_sender_map_ for its whole
251 DCHECK(ContainsKey(process_sender_map_
, process_id
));
253 int embedded_worker_id
= params
->embedded_worker_id
;
254 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
255 DCHECK(found
!= worker_map_
.end());
256 DCHECK_EQ(found
->second
->process_id(), process_id
);
258 DCHECK(!ContainsKey(worker_process_map_
, process_id
) ||
259 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0);
261 ServiceWorkerStatusCode status
=
262 Send(process_id
, new EmbeddedWorkerMsg_StartWorker(*params
));
263 if (status
== SERVICE_WORKER_OK
)
264 worker_process_map_
[process_id
].insert(embedded_worker_id
);
268 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::Send(
269 int process_id
, IPC::Message
* message_ptr
) {
270 scoped_ptr
<IPC::Message
> message(message_ptr
);
272 return SERVICE_WORKER_ERROR_ABORT
;
273 ProcessToSenderMap::iterator found
= process_sender_map_
.find(process_id
);
274 if (found
== process_sender_map_
.end())
275 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
;
276 if (!found
->second
->Send(message
.release()))
277 return SERVICE_WORKER_ERROR_IPC_FAILED
;
278 return SERVICE_WORKER_OK
;
281 void EmbeddedWorkerRegistry::RemoveWorker(int process_id
,
282 int embedded_worker_id
) {
283 DCHECK(ContainsKey(worker_map_
, embedded_worker_id
));
284 worker_map_
.erase(embedded_worker_id
);
285 worker_process_map_
.erase(process_id
);
288 } // namespace content