Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: content/browser/service_worker/embedded_worker_registry.cc

Issue 2638313002: Manage ServiceWorkerDispatcherHost in ServiceWorkerContextCore (Closed)
Patch Set: Fix an include guard Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/embedded_worker_registry.h" 5 #include "content/browser/service_worker/embedded_worker_registry.h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "content/browser/renderer_host/render_widget_helper.h" 10 #include "content/browser/renderer_host/render_widget_helper.h"
11 #include "content/browser/service_worker/embedded_worker_instance.h" 11 #include "content/browser/service_worker/embedded_worker_instance.h"
12 #include "content/browser/service_worker/service_worker_context_core.h" 12 #include "content/browser/service_worker/service_worker_context_core.h"
13 #include "content/browser/service_worker/service_worker_context_wrapper.h" 13 #include "content/browser/service_worker/service_worker_context_wrapper.h"
14 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
14 #include "content/common/service_worker/embedded_worker_messages.h" 15 #include "content/common/service_worker/embedded_worker_messages.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "ipc/ipc_message.h" 17 #include "ipc/ipc_message.h"
17 #include "ipc/ipc_sender.h" 18 #include "ipc/ipc_sender.h"
18 19
19 namespace content { 20 namespace content {
20 21
21 // static 22 // static
22 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( 23 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
23 const base::WeakPtr<ServiceWorkerContextCore>& context) { 24 const base::WeakPtr<ServiceWorkerContextCore>& context) {
24 return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0)); 25 return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0));
25 } 26 }
26 27
27 // static 28 // static
28 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( 29 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
29 const base::WeakPtr<ServiceWorkerContextCore>& context, 30 const base::WeakPtr<ServiceWorkerContextCore>& context,
30 EmbeddedWorkerRegistry* old_registry) { 31 EmbeddedWorkerRegistry* old_registry) {
31 scoped_refptr<EmbeddedWorkerRegistry> registry = 32 scoped_refptr<EmbeddedWorkerRegistry> registry =
32 new EmbeddedWorkerRegistry( 33 new EmbeddedWorkerRegistry(
33 context, 34 context,
34 old_registry->next_embedded_worker_id_); 35 old_registry->next_embedded_worker_id_);
35 registry->process_sender_map_.swap(old_registry->process_sender_map_);
36 return registry; 36 return registry;
37 } 37 }
38 38
39 std::unique_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { 39 std::unique_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() {
40 std::unique_ptr<EmbeddedWorkerInstance> worker( 40 std::unique_ptr<EmbeddedWorkerInstance> worker(
41 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_)); 41 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_));
42 worker_map_[next_embedded_worker_id_++] = worker.get(); 42 worker_map_[next_embedded_worker_id_++] = worker.get();
43 return worker; 43 return worker;
44 } 44 }
45 45
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 const base::string16& message, 172 const base::string16& message,
173 int line_number, 173 int line_number,
174 const GURL& source_url) { 174 const GURL& source_url) {
175 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); 175 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id);
176 if (!worker) 176 if (!worker)
177 return; 177 return;
178 worker->OnReportConsoleMessage(source_identifier, message_level, message, 178 worker->OnReportConsoleMessage(source_identifier, message_level, message,
179 line_number, source_url); 179 line_number, source_url);
180 } 180 }
181 181
182 void EmbeddedWorkerRegistry::AddChildProcessSender( 182 void EmbeddedWorkerRegistry::RemoveProcess(int process_id) {
183 int process_id,
184 IPC::Sender* sender,
185 MessagePortMessageFilter* message_port_message_filter) {
186 process_sender_map_[process_id] = sender;
187 process_message_port_message_filter_map_[process_id] =
188 message_port_message_filter;
189 DCHECK(!base::ContainsKey(worker_process_map_, process_id));
190 }
191
192 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) {
193 process_sender_map_.erase(process_id);
194 process_message_port_message_filter_map_.erase(process_id);
195 std::map<int, std::set<int> >::iterator found = 183 std::map<int, std::set<int> >::iterator found =
196 worker_process_map_.find(process_id); 184 worker_process_map_.find(process_id);
197 if (found != worker_process_map_.end()) { 185 if (found != worker_process_map_.end()) {
198 const std::set<int>& worker_set = worker_process_map_[process_id]; 186 const std::set<int>& worker_set = worker_process_map_[process_id];
199 for (std::set<int>::const_iterator it = worker_set.begin(); 187 for (std::set<int>::const_iterator it = worker_set.begin();
200 it != worker_set.end(); 188 it != worker_set.end();
201 ++it) { 189 ++it) {
202 int embedded_worker_id = *it; 190 int embedded_worker_id = *it;
203 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); 191 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id));
204 // Somehow the worker thread has lost contact with the browser process. 192 // RemoveProcess is typically called after the running workers on the
205 // The renderer may have been killed. Set the worker's status to STOPPED 193 // process have been stopped, so if there is a running worker at this
206 // so a new thread can be created for this version. Use OnDetached rather 194 // point somehow the worker thread has lost contact with the browser
207 // than OnStopped so UMA doesn't record it as a normal stoppage. 195 // process.
196 // Set the worker's status to STOPPED so a new thread can be created for
197 // this version. Use OnDetached rather than OnStopped so UMA doesn't
198 // record it as a normal stoppage.
208 worker_map_[embedded_worker_id]->OnDetached(); 199 worker_map_[embedded_worker_id]->OnDetached();
209 } 200 }
210 worker_process_map_.erase(found); 201 worker_process_map_.erase(found);
211 } 202 }
212 } 203 }
213 204
214 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( 205 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
215 int embedded_worker_id) { 206 int embedded_worker_id) {
216 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); 207 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
217 if (found == worker_map_.end()) 208 if (found == worker_map_.end())
218 return nullptr; 209 return nullptr;
219 return found->second; 210 return found->second;
220 } 211 }
221 212
222 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const { 213 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const {
223 if (embedded_worker_id < initial_embedded_worker_id_ || 214 if (embedded_worker_id < initial_embedded_worker_id_ ||
224 next_embedded_worker_id_ <= embedded_worker_id) { 215 next_embedded_worker_id_ <= embedded_worker_id) {
225 return false; 216 return false;
226 } 217 }
227 return true; 218 return true;
228 } 219 }
229 220
230 MessagePortMessageFilter* 221 MessagePortMessageFilter*
231 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) { 222 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) {
232 return process_message_port_message_filter_map_[process_id]; 223 ServiceWorkerDispatcherHost* dispatcher_host =
224 context_->GetDispatcherHost(process_id);
225 DCHECK(dispatcher_host);
226 return dispatcher_host->message_port_message_filter();
233 } 227 }
234 228
235 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( 229 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
236 const base::WeakPtr<ServiceWorkerContextCore>& context, 230 const base::WeakPtr<ServiceWorkerContextCore>& context,
237 int initial_embedded_worker_id) 231 int initial_embedded_worker_id)
238 : context_(context), 232 : context_(context),
239 next_embedded_worker_id_(initial_embedded_worker_id), 233 next_embedded_worker_id_(initial_embedded_worker_id),
240 initial_embedded_worker_id_(initial_embedded_worker_id) { 234 initial_embedded_worker_id_(initial_embedded_worker_id) {
241 } 235 }
242 236
243 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { 237 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
244 Shutdown(); 238 Shutdown();
245 } 239 }
246 240
247 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id, 241 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id,
248 int embedded_worker_id) { 242 int embedded_worker_id) {
249 // The ServiceWorkerDispatcherHost is supposed to be created when the process
250 // is created, and keep an entry in process_sender_map_ for its whole
251 // lifetime.
252 DCHECK(base::ContainsKey(process_sender_map_, process_id));
253 DCHECK(GetWorker(embedded_worker_id)); 243 DCHECK(GetWorker(embedded_worker_id));
254 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id); 244 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id);
255 DCHECK( 245 DCHECK(
256 !base::ContainsKey(worker_process_map_, process_id) || 246 !base::ContainsKey(worker_process_map_, process_id) ||
257 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id)); 247 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id));
258 248
259 worker_process_map_[process_id].insert(embedded_worker_id); 249 worker_process_map_[process_id].insert(embedded_worker_id);
260 } 250 }
261 251
262 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( 252 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
263 int process_id, IPC::Message* message_ptr) { 253 int process_id, IPC::Message* message_ptr) {
264 std::unique_ptr<IPC::Message> message(message_ptr); 254 std::unique_ptr<IPC::Message> message(message_ptr);
265 if (!context_) 255 if (!context_)
266 return SERVICE_WORKER_ERROR_ABORT; 256 return SERVICE_WORKER_ERROR_ABORT;
267 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); 257 IPC::Sender* sender = context_->GetDispatcherHost(process_id);
268 if (found == process_sender_map_.end()) 258 if (!sender)
269 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; 259 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
270 if (!found->second->Send(message.release())) 260 if (!sender->Send(message.release()))
271 return SERVICE_WORKER_ERROR_IPC_FAILED; 261 return SERVICE_WORKER_ERROR_IPC_FAILED;
272 return SERVICE_WORKER_OK; 262 return SERVICE_WORKER_OK;
273 } 263 }
274 264
275 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, 265 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
276 int embedded_worker_id) { 266 int embedded_worker_id) {
277 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); 267 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id));
278 DetachWorker(process_id, embedded_worker_id); 268 DetachWorker(process_id, embedded_worker_id);
279 worker_map_.erase(embedded_worker_id); 269 worker_map_.erase(embedded_worker_id);
280 } 270 }
(...skipping 14 matching lines...) Expand all
295 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); 285 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id);
296 if (!worker || worker->process_id() != process_id) { 286 if (!worker || worker->process_id() != process_id) {
297 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false); 287 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false);
298 return nullptr; 288 return nullptr;
299 } 289 }
300 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true); 290 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true);
301 return worker; 291 return worker;
302 } 292 }
303 293
304 } // namespace content 294 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698