This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_registry.cc
blobf20dd2224859ef39f457d12a793d61b5d72c1d99
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(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)
88 return;
89 found->second->OnScriptLoaded();
92 void EmbeddedWorkerRegistry::OnWorkerThreadStarted(int process_id,
93 int thread_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)
99 return;
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)
109 return;
110 found->second->OnScriptLoadFailed();
113 void EmbeddedWorkerRegistry::OnWorkerScriptEvaluated(int process_id,
114 int embedded_worker_id,
115 bool success) {
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)
120 return;
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)
132 return;
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) {
138 return;
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)
150 return;
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,
158 int line_number,
159 int column_number,
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())
164 return;
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,
172 int message_level,
173 const base::string16& message,
174 int line_number,
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())
179 return;
180 found->second->OnReportConsoleMessage(
181 source_identifier, message_level, message, line_number, source_url);
184 void EmbeddedWorkerRegistry::AddChildProcessSender(
185 int process_id,
186 IPC::Sender* sender,
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();
203 ++it) {
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())
216 return NULL;
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) {
223 return false;
225 return true;
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)
236 : context_(context),
237 next_embedded_worker_id_(initial_embedded_worker_id),
238 initial_embedded_worker_id_(initial_embedded_worker_id) {
241 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
242 Shutdown();
245 ServiceWorkerStatusCode EmbeddedWorkerRegistry::SendStartWorker(
246 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
247 int process_id) {
248 if (!context_)
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
253 // lifetime.
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);
268 return status;
271 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
272 int process_id, IPC::Message* message_ptr) {
273 scoped_ptr<IPC::Message> message(message_ptr);
274 if (!context_)
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))
289 return;
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