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

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

Issue 2638313002: Manage ServiceWorkerDispatcherHost in ServiceWorkerContextCore (Closed)
Patch Set: Rebase 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(int process_id, 182 void EmbeddedWorkerRegistry::RemoveProcess(int process_id) {
183 IPC::Sender* sender) {
184 process_sender_map_[process_id] = sender;
185 DCHECK(!base::ContainsKey(worker_process_map_, process_id));
186 }
187
188 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) {
189 process_sender_map_.erase(process_id);
190 std::map<int, std::set<int> >::iterator found = 183 std::map<int, std::set<int> >::iterator found =
191 worker_process_map_.find(process_id); 184 worker_process_map_.find(process_id);
192 if (found != worker_process_map_.end()) { 185 if (found != worker_process_map_.end()) {
193 const std::set<int>& worker_set = worker_process_map_[process_id]; 186 const std::set<int>& worker_set = worker_process_map_[process_id];
194 for (std::set<int>::const_iterator it = worker_set.begin(); 187 for (std::set<int>::const_iterator it = worker_set.begin();
195 it != worker_set.end(); 188 it != worker_set.end();
196 ++it) { 189 ++it) {
197 int embedded_worker_id = *it; 190 int embedded_worker_id = *it;
198 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); 191 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id));
199 // Somehow the worker thread has lost contact with the browser process. 192 // RemoveProcess is typically called after the running workers on the
200 // 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
201 // 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
202 // 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.
203 worker_map_[embedded_worker_id]->OnDetached(); 199 worker_map_[embedded_worker_id]->OnDetached();
204 } 200 }
205 worker_process_map_.erase(found); 201 worker_process_map_.erase(found);
206 } 202 }
207 } 203 }
208 204
209 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( 205 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
210 int embedded_worker_id) { 206 int embedded_worker_id) {
211 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); 207 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
212 if (found == worker_map_.end()) 208 if (found == worker_map_.end())
(...skipping 16 matching lines...) Expand all
229 next_embedded_worker_id_(initial_embedded_worker_id), 225 next_embedded_worker_id_(initial_embedded_worker_id),
230 initial_embedded_worker_id_(initial_embedded_worker_id) { 226 initial_embedded_worker_id_(initial_embedded_worker_id) {
231 } 227 }
232 228
233 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { 229 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
234 Shutdown(); 230 Shutdown();
235 } 231 }
236 232
237 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id, 233 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id,
238 int embedded_worker_id) { 234 int embedded_worker_id) {
239 // The ServiceWorkerDispatcherHost is supposed to be created when the process
240 // is created, and keep an entry in process_sender_map_ for its whole
241 // lifetime.
242 DCHECK(base::ContainsKey(process_sender_map_, process_id));
243 DCHECK(GetWorker(embedded_worker_id)); 235 DCHECK(GetWorker(embedded_worker_id));
244 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id); 236 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id);
245 DCHECK( 237 DCHECK(
246 !base::ContainsKey(worker_process_map_, process_id) || 238 !base::ContainsKey(worker_process_map_, process_id) ||
247 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id)); 239 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id));
248 240
249 worker_process_map_[process_id].insert(embedded_worker_id); 241 worker_process_map_[process_id].insert(embedded_worker_id);
250 } 242 }
251 243
252 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( 244 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
253 int process_id, IPC::Message* message_ptr) { 245 int process_id, IPC::Message* message_ptr) {
254 std::unique_ptr<IPC::Message> message(message_ptr); 246 std::unique_ptr<IPC::Message> message(message_ptr);
255 if (!context_) 247 if (!context_)
256 return SERVICE_WORKER_ERROR_ABORT; 248 return SERVICE_WORKER_ERROR_ABORT;
257 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); 249 IPC::Sender* sender = context_->GetDispatcherHost(process_id);
258 if (found == process_sender_map_.end()) 250 if (!sender)
259 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; 251 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
260 if (!found->second->Send(message.release())) 252 if (!sender->Send(message.release()))
261 return SERVICE_WORKER_ERROR_IPC_FAILED; 253 return SERVICE_WORKER_ERROR_IPC_FAILED;
262 return SERVICE_WORKER_OK; 254 return SERVICE_WORKER_OK;
263 } 255 }
264 256
265 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, 257 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
266 int embedded_worker_id) { 258 int embedded_worker_id) {
267 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); 259 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id));
268 DetachWorker(process_id, embedded_worker_id); 260 DetachWorker(process_id, embedded_worker_id);
269 worker_map_.erase(embedded_worker_id); 261 worker_map_.erase(embedded_worker_id);
270 } 262 }
(...skipping 14 matching lines...) Expand all
285 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); 277 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id);
286 if (!worker || worker->process_id() != process_id) { 278 if (!worker || worker->process_id() != process_id) {
287 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false); 279 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false);
288 return nullptr; 280 return nullptr;
289 } 281 }
290 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true); 282 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true);
291 return worker; 283 return worker;
292 } 284 }
293 285
294 } // namespace content 286 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698