OLD | NEW |
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_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include "base/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
8 #include "content/browser/devtools/embedded_worker_devtools_manager.h" | 8 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
9 #include "content/browser/service_worker/embedded_worker_registry.h" | 9 #include "content/browser/service_worker/embedded_worker_registry.h" |
10 #include "content/browser/service_worker/service_worker_context_core.h" | 10 #include "content/browser/service_worker/service_worker_context_core.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 86 } |
87 | 87 |
88 } // namespace | 88 } // namespace |
89 | 89 |
90 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { | 90 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
91 if (status_ == STARTING || status_ == RUNNING) | 91 if (status_ == STARTING || status_ == RUNNING) |
92 Stop(); | 92 Stop(); |
93 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 93 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
94 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 94 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
95 if (context_ && process_id_ != -1) | 95 if (context_ && process_id_ != -1) |
96 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 96 context_->process_manager()->ReleaseWorkerProcess(process_id_); |
97 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 97 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
98 } | 98 } |
99 | 99 |
100 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 100 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
101 const GURL& scope, | 101 const GURL& scope, |
102 const GURL& script_url, | 102 const GURL& script_url, |
103 const std::vector<int>& possible_process_ids, | 103 const std::vector<int>& possible_process_ids, |
104 const StatusCallback& callback) { | 104 const StatusCallback& callback) { |
105 if (!context_) { | 105 if (!context_) { |
106 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 106 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
107 return; | 107 return; |
108 } | 108 } |
109 DCHECK(status_ == STOPPED); | 109 DCHECK(status_ == STOPPED); |
110 status_ = STARTING; | 110 status_ = STARTING; |
111 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 111 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
112 new EmbeddedWorkerMsg_StartWorker_Params()); | 112 new EmbeddedWorkerMsg_StartWorker_Params()); |
113 params->embedded_worker_id = embedded_worker_id_; | 113 params->embedded_worker_id = embedded_worker_id_; |
114 params->service_worker_version_id = service_worker_version_id; | 114 params->service_worker_version_id = service_worker_version_id; |
115 params->scope = scope; | 115 params->scope = scope; |
116 params->script_url = script_url; | 116 params->script_url = script_url; |
117 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 117 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
118 params->pause_on_start = false; | 118 params->pause_on_start = false; |
119 context_->process_manager()->AllocateWorkerProcess( | 119 context_->process_manager()->AllocateWorkerProcess( |
120 embedded_worker_id_, | |
121 SortProcesses(possible_process_ids), | 120 SortProcesses(possible_process_ids), |
122 script_url, | 121 script_url, |
123 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, | 122 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
124 weak_factory_.GetWeakPtr(), | 123 weak_factory_.GetWeakPtr(), |
125 context_, | 124 context_, |
126 base::Passed(¶ms), | 125 base::Passed(¶ms), |
127 callback)); | 126 callback)); |
128 } | 127 } |
129 | 128 |
130 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 129 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 base::WeakPtr<ServiceWorkerContextCore> context, | 179 base::WeakPtr<ServiceWorkerContextCore> context, |
181 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 180 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
182 const EmbeddedWorkerInstance::StatusCallback& callback, | 181 const EmbeddedWorkerInstance::StatusCallback& callback, |
183 ServiceWorkerStatusCode status, | 182 ServiceWorkerStatusCode status, |
184 int process_id) { | 183 int process_id) { |
185 if (!context) { | 184 if (!context) { |
186 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 185 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
187 return; | 186 return; |
188 } | 187 } |
189 if (!instance) { | 188 if (!instance) { |
190 if (status == SERVICE_WORKER_OK) { | 189 context->process_manager()->ReleaseWorkerProcess(process_id); |
191 // We only have a process allocated if the status is OK. | |
192 context->process_manager()->ReleaseWorkerProcess( | |
193 params->embedded_worker_id); | |
194 } | |
195 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 190 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
196 return; | 191 return; |
197 } | 192 } |
198 instance->ProcessAllocated(params.Pass(), callback, process_id, status); | 193 instance->ProcessAllocated(params.Pass(), callback, process_id, status); |
199 } | 194 } |
200 | 195 |
201 void EmbeddedWorkerInstance::ProcessAllocated( | 196 void EmbeddedWorkerInstance::ProcessAllocated( |
202 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 197 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
203 const StatusCallback& callback, | 198 const StatusCallback& callback, |
204 int process_id, | 199 int process_id, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 DCHECK(status_ == STARTING); | 242 DCHECK(status_ == STARTING); |
248 status_ = RUNNING; | 243 status_ = RUNNING; |
249 thread_id_ = thread_id; | 244 thread_id_ = thread_id; |
250 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 245 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
251 } | 246 } |
252 | 247 |
253 void EmbeddedWorkerInstance::OnStopped() { | 248 void EmbeddedWorkerInstance::OnStopped() { |
254 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 249 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
255 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 250 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
256 if (context_) | 251 if (context_) |
257 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 252 context_->process_manager()->ReleaseWorkerProcess(process_id_); |
258 status_ = STOPPED; | 253 status_ = STOPPED; |
259 process_id_ = -1; | 254 process_id_ = -1; |
260 thread_id_ = -1; | 255 thread_id_ = -1; |
261 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; | 256 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
262 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); | 257 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); |
263 } | 258 } |
264 | 259 |
265 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 260 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
266 ListenerList::Iterator it(listener_list_); | 261 ListenerList::Iterator it(listener_list_); |
267 while (Listener* listener = it.GetNext()) { | 262 while (Listener* listener = it.GetNext()) { |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 // Sort descending by the reference count. | 314 // Sort descending by the reference count. |
320 std::sort(counted.begin(), counted.end(), SecondGreater()); | 315 std::sort(counted.begin(), counted.end(), SecondGreater()); |
321 | 316 |
322 std::vector<int> result(counted.size()); | 317 std::vector<int> result(counted.size()); |
323 for (size_t i = 0; i < counted.size(); ++i) | 318 for (size_t i = 0; i < counted.size(); ++i) |
324 result[i] = counted[i].first; | 319 result[i] = counted[i].first; |
325 return result; | 320 return result; |
326 } | 321 } |
327 | 322 |
328 } // namespace content | 323 } // namespace content |
OLD | NEW |