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

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

Issue 238043002: Teach EmbeddedWorkerInstance to create a process when it needs one. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Handle 2 places where context_ could be NULL. Created 6 years, 7 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 | Annotate | Revision Log
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/stl_util.h" 8 #include "base/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_instance.h" 9 #include "content/browser/service_worker/embedded_worker_instance.h"
9 #include "content/browser/service_worker/service_worker_context_core.h" 10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/common/service_worker/embedded_worker_messages.h" 12 #include "content/common/service_worker/embedded_worker_messages.h"
13 #include "content/public/browser/browser_thread.h"
11 #include "ipc/ipc_message.h" 14 #include "ipc/ipc_message.h"
12 #include "ipc/ipc_sender.h" 15 #include "ipc/ipc_sender.h"
13 16
14 namespace content { 17 namespace content {
15 18
16 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( 19 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
17 base::WeakPtr<ServiceWorkerContextCore> context) 20 base::WeakPtr<ServiceWorkerContextCore> context)
18 : context_(context), 21 : context_(context),
19 next_embedded_worker_id_(0) {} 22 next_embedded_worker_id_(0) {}
20 23
21 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { 24 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() {
22 scoped_ptr<EmbeddedWorkerInstance> worker( 25 scoped_ptr<EmbeddedWorkerInstance> worker(
23 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); 26 new EmbeddedWorkerInstance(this, next_embedded_worker_id_));
24 worker_map_[next_embedded_worker_id_++] = worker.get(); 27 worker_map_[next_embedded_worker_id_++] = worker.get();
25 return worker.Pass(); 28 return worker.Pass();
26 } 29 }
27 30
28 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StartWorker( 31 void EmbeddedWorkerRegistry::StartWorker(const std::vector<int>& process_ids,
29 int process_id, 32 int embedded_worker_id,
30 int embedded_worker_id, 33 int64 service_worker_version_id,
31 int64 service_worker_version_id, 34 const GURL& scope,
32 const GURL& scope, 35 const GURL& script_url,
33 const GURL& script_url) { 36 const StatusCallback& callback) {
34 return Send( 37 if (!context_) {
35 process_id, 38 callback.Run(SERVICE_WORKER_ERROR_ABORT);
36 new EmbeddedWorkerMsg_StartWorker( 39 return;
37 embedded_worker_id, service_worker_version_id, scope, script_url)); 40 }
41 context_->process_manager()->AllocateWorkerProcess(
42 process_ids,
43 script_url,
44 base::Bind(&EmbeddedWorkerRegistry::StartWorkerWithProcessId,
45 this,
46 embedded_worker_id,
47 base::Passed(make_scoped_ptr(new EmbeddedWorkerMsg_StartWorker(
48 embedded_worker_id,
49 service_worker_version_id,
50 scope,
51 script_url))),
52 callback));
38 } 53 }
39 54
40 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( 55 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker(
41 int process_id, int embedded_worker_id) { 56 int process_id, int embedded_worker_id) {
57 if (context_)
58 context_->process_manager()->ReleaseWorkerProcess(process_id);
42 return Send(process_id, 59 return Send(process_id,
43 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); 60 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id));
44 } 61 }
45 62
46 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) { 63 bool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) {
47 // TODO(kinuko): Move all EmbeddedWorker message handling from 64 // TODO(kinuko): Move all EmbeddedWorker message handling from
48 // ServiceWorkerDispatcherHost. 65 // ServiceWorkerDispatcherHost.
49 66
50 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id()); 67 WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id());
51 if (found == worker_map_.end()) { 68 if (found == worker_map_.end()) {
52 LOG(ERROR) << "Worker " << message.routing_id() << " not registered"; 69 LOG(ERROR) << "Worker " << message.routing_id() << " not registered";
53 return false; 70 return false;
54 } 71 }
55 return found->second->OnMessageReceived(message); 72 return found->second->OnMessageReceived(message);
56 } 73 }
57 74
75 void EmbeddedWorkerRegistry::Shutdown() {
76 for (WorkerInstanceMap::iterator it = worker_map_.begin();
77 it != worker_map_.end();
78 ++it) {
79 it->second->Stop();
80 }
81 }
82
58 void EmbeddedWorkerRegistry::OnWorkerStarted( 83 void EmbeddedWorkerRegistry::OnWorkerStarted(
59 int process_id, int thread_id, int embedded_worker_id) { 84 int process_id, int thread_id, int embedded_worker_id) {
60 DCHECK(!ContainsKey(worker_process_map_, process_id) || 85 DCHECK(!ContainsKey(worker_process_map_, process_id) ||
61 worker_process_map_[process_id].count(embedded_worker_id) == 0); 86 worker_process_map_[process_id].count(embedded_worker_id) == 0);
62 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); 87 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
63 if (found == worker_map_.end()) { 88 if (found == worker_map_.end()) {
64 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; 89 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered";
65 return; 90 return;
66 } 91 }
67 worker_process_map_[process_id].insert(embedded_worker_id); 92 worker_process_map_[process_id].insert(embedded_worker_id);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 } 161 }
137 162
138 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( 163 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
139 int embedded_worker_id) { 164 int embedded_worker_id) {
140 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); 165 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
141 if (found == worker_map_.end()) 166 if (found == worker_map_.end())
142 return NULL; 167 return NULL;
143 return found->second; 168 return found->second;
144 } 169 }
145 170
146 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} 171 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
172 Shutdown();
173 }
174
175 void EmbeddedWorkerRegistry::StartWorkerWithProcessId(
176 int embedded_worker_id,
177 scoped_ptr<EmbeddedWorkerMsg_StartWorker> message,
178 const StatusCallback& callback,
179 ServiceWorkerStatusCode status,
180 int process_id) {
181 WorkerInstanceMap::const_iterator worker =
182 worker_map_.find(embedded_worker_id);
183 if (worker == worker_map_.end()) {
184 // The Instance was destroyed before it could finish starting. Undo what
185 // we've done so far.
186 if (context_)
187 context_->process_manager()->ReleaseWorkerProcess(process_id);
188 callback.Run(SERVICE_WORKER_ERROR_ABORT);
189 return;
190 }
191 worker->second->RecordProcessId(process_id, status);
192
193 if (status != SERVICE_WORKER_OK) {
194 callback.Run(status);
195 return;
196 }
197 // The ServiceWorkerDispatcherHost is supposed to be created when the process
198 // is created, and keep an entry in process_sender_map_ for its whole
199 // lifetime.
200 DCHECK(ContainsKey(process_sender_map_, process_id));
201 callback.Run(Send(process_id, message.release()));
202 }
147 203
148 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( 204 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
149 int process_id, IPC::Message* message) { 205 int process_id, IPC::Message* message) {
150 if (!context_) 206 if (!context_)
151 return SERVICE_WORKER_ERROR_ABORT; 207 return SERVICE_WORKER_ERROR_ABORT;
152 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); 208 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id);
153 if (found == process_sender_map_.end()) 209 if (found == process_sender_map_.end())
154 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; 210 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
155 if (!found->second->Send(message)) 211 if (!found->second->Send(message))
156 return SERVICE_WORKER_ERROR_IPC_FAILED; 212 return SERVICE_WORKER_ERROR_IPC_FAILED;
157 return SERVICE_WORKER_OK; 213 return SERVICE_WORKER_OK;
158 } 214 }
159 215
160 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, 216 void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
161 int embedded_worker_id) { 217 int embedded_worker_id) {
162 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); 218 DCHECK(ContainsKey(worker_map_, embedded_worker_id));
163 worker_map_.erase(embedded_worker_id); 219 worker_map_.erase(embedded_worker_id);
164 worker_process_map_.erase(process_id); 220 worker_process_map_.erase(process_id);
165 } 221 }
166 222
167 } // namespace content 223 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698