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(int process_id
,
83 int embedded_worker_id
) {
84 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
85 DCHECK(found
!= worker_map_
.end());
86 DCHECK_EQ(found
->second
->process_id(), process_id
);
87 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
89 found
->second
->OnScriptLoaded();
92 void EmbeddedWorkerRegistry::OnWorkerThreadStarted(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
->OnThreadStarted(thread_id
);
103 void EmbeddedWorkerRegistry::OnWorkerScriptLoadFailed(int process_id
,
104 int embedded_worker_id
) {
105 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
106 DCHECK(found
!= worker_map_
.end());
107 DCHECK_EQ(found
->second
->process_id(), process_id
);
108 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
110 found
->second
->OnScriptLoadFailed();
113 void EmbeddedWorkerRegistry::OnWorkerScriptEvaluated(int process_id
,
114 int embedded_worker_id
,
116 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
117 DCHECK(found
!= worker_map_
.end());
118 DCHECK_EQ(found
->second
->process_id(), process_id
);
119 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
121 found
->second
->OnScriptEvaluated(success
);
124 void EmbeddedWorkerRegistry::OnWorkerStarted(
125 int process_id
, int embedded_worker_id
) {
126 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
127 // TODO(falken): Instead of DCHECK, we should terminate the process on
128 // unexpected message. Same with most of the DCHECKs in this file.
129 DCHECK(found
!= worker_map_
.end());
130 DCHECK_EQ(found
->second
->process_id(), process_id
);
131 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
134 DCHECK(ContainsKey(worker_process_map_
, process_id
) &&
135 worker_process_map_
[process_id
].count(embedded_worker_id
) == 1);
136 if (!ContainsKey(worker_process_map_
, process_id
) ||
137 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0) {
141 found
->second
->OnStarted();
144 void EmbeddedWorkerRegistry::OnWorkerStopped(
145 int process_id
, int embedded_worker_id
) {
146 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
147 DCHECK(found
!= worker_map_
.end());
148 DCHECK_EQ(found
->second
->process_id(), process_id
);
149 if (found
== worker_map_
.end() || found
->second
->process_id() != process_id
)
151 worker_process_map_
[process_id
].erase(embedded_worker_id
);
152 found
->second
->OnStopped();
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
]->OnDetached();
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
,
249 return SERVICE_WORKER_ERROR_ABORT
;
251 // The ServiceWorkerDispatcherHost is supposed to be created when the process
252 // is created, and keep an entry in process_sender_map_ for its whole
254 DCHECK(ContainsKey(process_sender_map_
, process_id
));
256 int embedded_worker_id
= params
->embedded_worker_id
;
257 WorkerInstanceMap::iterator found
= worker_map_
.find(embedded_worker_id
);
258 DCHECK(found
!= worker_map_
.end());
259 DCHECK_EQ(found
->second
->process_id(), process_id
);
261 DCHECK(!ContainsKey(worker_process_map_
, process_id
) ||
262 worker_process_map_
[process_id
].count(embedded_worker_id
) == 0);
264 ServiceWorkerStatusCode status
=
265 Send(process_id
, new EmbeddedWorkerMsg_StartWorker(*params
));
266 if (status
== SERVICE_WORKER_OK
)
267 worker_process_map_
[process_id
].insert(embedded_worker_id
);
271 ServiceWorkerStatusCode
EmbeddedWorkerRegistry::Send(
272 int process_id
, IPC::Message
* message_ptr
) {
273 scoped_ptr
<IPC::Message
> message(message_ptr
);
275 return SERVICE_WORKER_ERROR_ABORT
;
276 ProcessToSenderMap::iterator found
= process_sender_map_
.find(process_id
);
277 if (found
== process_sender_map_
.end())
278 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
;
279 if (!found
->second
->Send(message
.release()))
280 return SERVICE_WORKER_ERROR_IPC_FAILED
;
281 return SERVICE_WORKER_OK
;
284 void EmbeddedWorkerRegistry::RemoveWorker(int process_id
,
285 int embedded_worker_id
) {
286 DCHECK(ContainsKey(worker_map_
, embedded_worker_id
));
287 worker_map_
.erase(embedded_worker_id
);
288 if (!ContainsKey(worker_process_map_
, process_id
))
290 worker_process_map_
[process_id
].erase(embedded_worker_id
);
291 if (worker_process_map_
[process_id
].empty())
292 worker_process_map_
.erase(process_id
);
295 } // namespace content