Add ICU message format support
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_registry.cc
blobbf268e8bedc8aa7e210a933d13b981604758d653
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"
18 namespace content {
20 // static
21 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
22 const base::WeakPtr<ServiceWorkerContextCore>& context) {
23 return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0));
26 // static
27 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
28 const base::WeakPtr<ServiceWorkerContextCore>& context,
29 EmbeddedWorkerRegistry* old_registry) {
30 scoped_refptr<EmbeddedWorkerRegistry> registry =
31 new EmbeddedWorkerRegistry(
32 context,
33 old_registry->next_embedded_worker_id_);
34 registry->process_sender_map_.swap(old_registry->process_sender_map_);
35 return registry;
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();
42 return worker.Pass();
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 int process_id) {
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)
59 return false;
60 return found->second->OnMessageReceived(message);
63 void EmbeddedWorkerRegistry::Shutdown() {
64 for (WorkerInstanceMap::iterator it = worker_map_.begin();
65 it != worker_map_.end();
66 ++it) {
67 it->second->Stop();
71 void EmbeddedWorkerRegistry::OnWorkerReadyForInspection(
72 int process_id,
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)
78 return;
79 found->second->OnReadyForInspection();
82 void EmbeddedWorkerRegistry::OnWorkerScriptLoaded(
83 int process_id,
84 int thread_id,
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)
90 return;
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)
100 return;
101 found->second->OnScriptLoadFailed();
104 void EmbeddedWorkerRegistry::OnWorkerScriptEvaluated(int process_id,
105 int embedded_worker_id,
106 bool success) {
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)
111 return;
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)
123 return;
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) {
129 return;
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)
141 return;
142 worker_process_map_[process_id].erase(embedded_worker_id);
143 found->second->OnStopped();
146 void EmbeddedWorkerRegistry::OnReportException(
147 int embedded_worker_id,
148 const base::string16& error_message,
149 int line_number,
150 int column_number,
151 const GURL& source_url) {
152 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
153 DCHECK(found != worker_map_.end());
154 if (found == worker_map_.end())
155 return;
156 found->second->OnReportException(
157 error_message, line_number, column_number, source_url);
160 void EmbeddedWorkerRegistry::OnReportConsoleMessage(
161 int embedded_worker_id,
162 int source_identifier,
163 int message_level,
164 const base::string16& message,
165 int line_number,
166 const GURL& source_url) {
167 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
168 DCHECK(found != worker_map_.end());
169 if (found == worker_map_.end())
170 return;
171 found->second->OnReportConsoleMessage(
172 source_identifier, message_level, message, line_number, source_url);
175 void EmbeddedWorkerRegistry::AddChildProcessSender(
176 int process_id,
177 IPC::Sender* sender,
178 MessagePortMessageFilter* message_port_message_filter) {
179 process_sender_map_[process_id] = sender;
180 process_message_port_message_filter_map_[process_id] =
181 message_port_message_filter;
182 DCHECK(!ContainsKey(worker_process_map_, process_id));
185 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) {
186 process_sender_map_.erase(process_id);
187 process_message_port_message_filter_map_.erase(process_id);
188 std::map<int, std::set<int> >::iterator found =
189 worker_process_map_.find(process_id);
190 if (found != worker_process_map_.end()) {
191 const std::set<int>& worker_set = worker_process_map_[process_id];
192 for (std::set<int>::const_iterator it = worker_set.begin();
193 it != worker_set.end();
194 ++it) {
195 int embedded_worker_id = *it;
196 DCHECK(ContainsKey(worker_map_, embedded_worker_id));
197 worker_map_[embedded_worker_id]->OnDetached();
199 worker_process_map_.erase(found);
203 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
204 int embedded_worker_id) {
205 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
206 if (found == worker_map_.end())
207 return NULL;
208 return found->second;
211 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const {
212 if (embedded_worker_id < initial_embedded_worker_id_ ||
213 next_embedded_worker_id_ <= embedded_worker_id) {
214 return false;
216 return true;
219 MessagePortMessageFilter*
220 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) {
221 return process_message_port_message_filter_map_[process_id];
224 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
225 const base::WeakPtr<ServiceWorkerContextCore>& context,
226 int initial_embedded_worker_id)
227 : context_(context),
228 next_embedded_worker_id_(initial_embedded_worker_id),
229 initial_embedded_worker_id_(initial_embedded_worker_id) {
232 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
233 Shutdown();
236 ServiceWorkerStatusCode EmbeddedWorkerRegistry::SendStartWorker(
237 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
238 int process_id) {
239 if (!context_)
240 return SERVICE_WORKER_ERROR_ABORT;
242 // The ServiceWorkerDispatcherHost is supposed to be created when the process
243 // is created, and keep an entry in process_sender_map_ for its whole
244 // lifetime.
245 DCHECK(ContainsKey(process_sender_map_, process_id));
247 int embedded_worker_id = params->embedded_worker_id;
248 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
249 DCHECK(found != worker_map_.end());
250 DCHECK_EQ(found->second->process_id(), process_id);
252 DCHECK(!ContainsKey(worker_process_map_, process_id) ||
253 worker_process_map_[process_id].count(embedded_worker_id) == 0);
255 ServiceWorkerStatusCode status =
256 Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params));
257 if (status == SERVICE_WORKER_OK)
258 worker_process_map_[process_id].insert(embedded_worker_id);
259 return status;
262 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
263 int process_id, IPC::Message* message_ptr) {
264 scoped_ptr<IPC::Message> message(message_ptr);
265 if (!context_)
266 return SERVICE_WORKER_ERROR_ABORT;
267 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id);
268 if (found == process_sender_map_.end())
269 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
270 if (!found->second->Send(message.release()))
271 return SERVICE_WORKER_ERROR_IPC_FAILED;
272 return SERVICE_WORKER_OK;
275 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
276 int embedded_worker_id) {
277 DCHECK(ContainsKey(worker_map_, embedded_worker_id));
278 worker_map_.erase(embedded_worker_id);
279 if (!ContainsKey(worker_process_map_, process_id))
280 return;
281 worker_process_map_[process_id].erase(embedded_worker_id);
282 if (worker_process_map_[process_id].empty())
283 worker_process_map_.erase(process_id);
286 } // namespace content