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

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

Issue 2638313002: Manage ServiceWorkerDispatcherHost in ServiceWorkerContextCore (Closed)
Patch Set: Remove a break line 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::DetachProcess(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 // Somehow the worker thread has lost contact with the browser process.
205 // The renderer may have been killed. Set the worker's status to STOPPED 193 // The renderer may have been killed. Set the worker's status to STOPPED
206 // so a new thread can be created for this version. Use OnDetached rather 194 // so a new thread can be created for this version. Use OnDetached rather
207 // than OnStopped so UMA doesn't record it as a normal stoppage. 195 // than OnStopped so UMA doesn't record it as a normal stoppage.
falken 2017/02/08 06:53:51 So we expect the workers to have been stopped norm
shimazu 2017/02/13 03:25:56 Yes, that's right. Tried updating the comment.
208 worker_map_[embedded_worker_id]->OnDetached(); 196 worker_map_[embedded_worker_id]->OnDetached();
209 } 197 }
210 worker_process_map_.erase(found); 198 worker_process_map_.erase(found);
211 } 199 }
212 } 200 }
213 201
214 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( 202 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
215 int embedded_worker_id) { 203 int embedded_worker_id) {
216 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); 204 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
217 if (found == worker_map_.end()) 205 if (found == worker_map_.end())
218 return nullptr; 206 return nullptr;
219 return found->second; 207 return found->second;
220 } 208 }
221 209
222 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const { 210 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const {
223 if (embedded_worker_id < initial_embedded_worker_id_ || 211 if (embedded_worker_id < initial_embedded_worker_id_ ||
224 next_embedded_worker_id_ <= embedded_worker_id) { 212 next_embedded_worker_id_ <= embedded_worker_id) {
225 return false; 213 return false;
226 } 214 }
227 return true; 215 return true;
228 } 216 }
229 217
230 MessagePortMessageFilter* 218 MessagePortMessageFilter*
231 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) { 219 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) {
232 return process_message_port_message_filter_map_[process_id]; 220 ServiceWorkerDispatcherHost* dispatcher_host =
221 context_->GetDispatcherHost(process_id);
222 DCHECK(dispatcher_host);
223 return dispatcher_host->message_port_message_filter();
233 } 224 }
234 225
235 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( 226 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
236 const base::WeakPtr<ServiceWorkerContextCore>& context, 227 const base::WeakPtr<ServiceWorkerContextCore>& context,
237 int initial_embedded_worker_id) 228 int initial_embedded_worker_id)
238 : context_(context), 229 : context_(context),
239 next_embedded_worker_id_(initial_embedded_worker_id), 230 next_embedded_worker_id_(initial_embedded_worker_id),
240 initial_embedded_worker_id_(initial_embedded_worker_id) { 231 initial_embedded_worker_id_(initial_embedded_worker_id) {
241 } 232 }
242 233
243 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { 234 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
244 Shutdown(); 235 Shutdown();
245 } 236 }
246 237
247 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id, 238 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id,
248 int embedded_worker_id) { 239 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)); 240 DCHECK(GetWorker(embedded_worker_id));
254 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id); 241 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id);
255 DCHECK( 242 DCHECK(
256 !base::ContainsKey(worker_process_map_, process_id) || 243 !base::ContainsKey(worker_process_map_, process_id) ||
257 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id)); 244 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id));
258 245
259 worker_process_map_[process_id].insert(embedded_worker_id); 246 worker_process_map_[process_id].insert(embedded_worker_id);
260 } 247 }
261 248
262 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( 249 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
263 int process_id, IPC::Message* message_ptr) { 250 int process_id, IPC::Message* message_ptr) {
264 std::unique_ptr<IPC::Message> message(message_ptr); 251 std::unique_ptr<IPC::Message> message(message_ptr);
265 if (!context_) 252 if (!context_)
266 return SERVICE_WORKER_ERROR_ABORT; 253 return SERVICE_WORKER_ERROR_ABORT;
267 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); 254 IPC::Sender* sender = context_->GetDispatcherHost(process_id);
268 if (found == process_sender_map_.end()) 255 if (!sender)
269 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; 256 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
270 if (!found->second->Send(message.release())) 257 if (!sender->Send(message.release()))
271 return SERVICE_WORKER_ERROR_IPC_FAILED; 258 return SERVICE_WORKER_ERROR_IPC_FAILED;
272 return SERVICE_WORKER_OK; 259 return SERVICE_WORKER_OK;
273 } 260 }
274 261
275 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, 262 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
276 int embedded_worker_id) { 263 int embedded_worker_id) {
277 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); 264 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id));
278 DetachWorker(process_id, embedded_worker_id); 265 DetachWorker(process_id, embedded_worker_id);
279 worker_map_.erase(embedded_worker_id); 266 worker_map_.erase(embedded_worker_id);
280 } 267 }
(...skipping 14 matching lines...) Expand all
295 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); 282 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id);
296 if (!worker || worker->process_id() != process_id) { 283 if (!worker || worker->process_id() != process_id) {
297 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false); 284 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false);
298 return nullptr; 285 return nullptr;
299 } 286 }
300 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true); 287 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true);
301 return worker; 288 return worker;
302 } 289 }
303 290
304 } // namespace content 291 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698