| 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(process_id_); | 96 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_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_, |
| 120 SortProcesses(possible_process_ids), | 121 SortProcesses(possible_process_ids), |
| 121 script_url, | 122 script_url, |
| 122 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, | 123 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
| 123 weak_factory_.GetWeakPtr(), | 124 weak_factory_.GetWeakPtr(), |
| 124 context_, | 125 context_, |
| 125 base::Passed(¶ms), | 126 base::Passed(¶ms), |
| 126 callback)); | 127 callback)); |
| 127 } | 128 } |
| 128 | 129 |
| 129 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 130 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 base::WeakPtr<ServiceWorkerContextCore> context, | 180 base::WeakPtr<ServiceWorkerContextCore> context, |
| 180 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 181 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 181 const EmbeddedWorkerInstance::StatusCallback& callback, | 182 const EmbeddedWorkerInstance::StatusCallback& callback, |
| 182 ServiceWorkerStatusCode status, | 183 ServiceWorkerStatusCode status, |
| 183 int process_id) { | 184 int process_id) { |
| 184 if (!context) { | 185 if (!context) { |
| 185 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 186 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 186 return; | 187 return; |
| 187 } | 188 } |
| 188 if (!instance) { | 189 if (!instance) { |
| 189 context->process_manager()->ReleaseWorkerProcess(process_id); | 190 if (status == SERVICE_WORKER_OK) { |
| 191 // We only have a process allocated if the status is OK. |
| 192 context->process_manager()->ReleaseWorkerProcess( |
| 193 params->embedded_worker_id); |
| 194 } |
| 190 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 195 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 191 return; | 196 return; |
| 192 } | 197 } |
| 193 instance->ProcessAllocated(params.Pass(), callback, process_id, status); | 198 instance->ProcessAllocated(params.Pass(), callback, process_id, status); |
| 194 } | 199 } |
| 195 | 200 |
| 196 void EmbeddedWorkerInstance::ProcessAllocated( | 201 void EmbeddedWorkerInstance::ProcessAllocated( |
| 197 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | 202 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 198 const StatusCallback& callback, | 203 const StatusCallback& callback, |
| 199 int process_id, | 204 int process_id, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 DCHECK(status_ == STARTING); | 247 DCHECK(status_ == STARTING); |
| 243 status_ = RUNNING; | 248 status_ = RUNNING; |
| 244 thread_id_ = thread_id; | 249 thread_id_ = thread_id; |
| 245 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 250 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
| 246 } | 251 } |
| 247 | 252 |
| 248 void EmbeddedWorkerInstance::OnStopped() { | 253 void EmbeddedWorkerInstance::OnStopped() { |
| 249 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) | 254 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 250 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 255 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 251 if (context_) | 256 if (context_) |
| 252 context_->process_manager()->ReleaseWorkerProcess(process_id_); | 257 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
| 253 status_ = STOPPED; | 258 status_ = STOPPED; |
| 254 process_id_ = -1; | 259 process_id_ = -1; |
| 255 thread_id_ = -1; | 260 thread_id_ = -1; |
| 256 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; | 261 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
| 257 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); | 262 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); |
| 258 } | 263 } |
| 259 | 264 |
| 260 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 265 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
| 261 ListenerList::Iterator it(listener_list_); | 266 ListenerList::Iterator it(listener_list_); |
| 262 while (Listener* listener = it.GetNext()) { | 267 while (Listener* listener = it.GetNext()) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // Sort descending by the reference count. | 319 // Sort descending by the reference count. |
| 315 std::sort(counted.begin(), counted.end(), SecondGreater()); | 320 std::sort(counted.begin(), counted.end(), SecondGreater()); |
| 316 | 321 |
| 317 std::vector<int> result(counted.size()); | 322 std::vector<int> result(counted.size()); |
| 318 for (size_t i = 0; i < counted.size(); ++i) | 323 for (size_t i = 0; i < counted.size(); ++i) |
| 319 result[i] = counted[i].first; | 324 result[i] = counted[i].first; |
| 320 return result; | 325 return result; |
| 321 } | 326 } |
| 322 | 327 |
| 323 } // namespace content | 328 } // namespace content |
| OLD | NEW |