| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 97 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 98 if (context_ && process_id_ != -1) | 98 if (context_ && process_id_ != -1) |
| 99 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 99 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
| 100 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 100 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 103 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
| 104 const GURL& scope, | 104 const GURL& scope, |
| 105 const GURL& script_url, | 105 const GURL& script_url, |
| 106 bool pause_after_download, | 106 bool pause_after_download, |
| 107 const std::vector<int>& possible_process_ids, | |
| 108 const StatusCallback& callback) { | 107 const StatusCallback& callback) { |
| 109 if (!context_) { | 108 if (!context_) { |
| 110 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 109 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 111 return; | 110 return; |
| 112 } | 111 } |
| 113 DCHECK(status_ == STOPPED); | 112 DCHECK(status_ == STOPPED); |
| 114 status_ = STARTING; | 113 status_ = STARTING; |
| 115 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 114 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 116 new EmbeddedWorkerMsg_StartWorker_Params()); | 115 new EmbeddedWorkerMsg_StartWorker_Params()); |
| 117 params->embedded_worker_id = embedded_worker_id_; | 116 params->embedded_worker_id = embedded_worker_id_; |
| 118 params->service_worker_version_id = service_worker_version_id; | 117 params->service_worker_version_id = service_worker_version_id; |
| 119 params->scope = scope; | 118 params->scope = scope; |
| 120 params->script_url = script_url; | 119 params->script_url = script_url; |
| 121 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 120 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
| 122 params->pause_after_download = pause_after_download; | 121 params->pause_after_download = pause_after_download; |
| 123 params->wait_for_debugger = false; | 122 params->wait_for_debugger = false; |
| 124 context_->process_manager()->AllocateWorkerProcess( | 123 context_->process_manager()->AllocateWorkerProcess( |
| 125 embedded_worker_id_, | 124 embedded_worker_id_, |
| 126 SortProcesses(possible_process_ids), | 125 scope, |
| 127 script_url, | 126 script_url, |
| 128 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, | 127 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
| 129 weak_factory_.GetWeakPtr(), | 128 weak_factory_.GetWeakPtr(), |
| 130 context_, | 129 context_, |
| 131 base::Passed(¶ms), | 130 base::Passed(¶ms), |
| 132 callback)); | 131 callback)); |
| 133 } | 132 } |
| 134 | 133 |
| 135 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 134 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
| 136 DCHECK(status_ == STARTING || status_ == RUNNING); | 135 DCHECK(status_ == STARTING || status_ == RUNNING); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 149 } | 148 } |
| 150 | 149 |
| 151 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 150 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
| 152 const IPC::Message& message) { | 151 const IPC::Message& message) { |
| 153 DCHECK(status_ == RUNNING); | 152 DCHECK(status_ == RUNNING); |
| 154 return registry_->Send(process_id_, | 153 return registry_->Send(process_id_, |
| 155 new EmbeddedWorkerContextMsg_MessageToWorker( | 154 new EmbeddedWorkerContextMsg_MessageToWorker( |
| 156 thread_id_, embedded_worker_id_, message)); | 155 thread_id_, embedded_worker_id_, message)); |
| 157 } | 156 } |
| 158 | 157 |
| 159 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | |
| 160 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
| 161 if (found == process_refs_.end()) | |
| 162 found = process_refs_.insert(std::make_pair(process_id, 0)).first; | |
| 163 ++found->second; | |
| 164 } | |
| 165 | |
| 166 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | |
| 167 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
| 168 if (found == process_refs_.end()) { | |
| 169 NOTREACHED() << "Releasing unknown process ref " << process_id; | |
| 170 return; | |
| 171 } | |
| 172 if (--found->second == 0) | |
| 173 process_refs_.erase(found); | |
| 174 } | |
| 175 | |
| 176 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 158 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
| 177 base::WeakPtr<ServiceWorkerContextCore> context, | 159 base::WeakPtr<ServiceWorkerContextCore> context, |
| 178 int embedded_worker_id) | 160 int embedded_worker_id) |
| 179 : context_(context), | 161 : context_(context), |
| 180 registry_(context->embedded_worker_registry()), | 162 registry_(context->embedded_worker_registry()), |
| 181 embedded_worker_id_(embedded_worker_id), | 163 embedded_worker_id_(embedded_worker_id), |
| 182 status_(STOPPED), | 164 status_(STOPPED), |
| 183 process_id_(-1), | 165 process_id_(-1), |
| 184 thread_id_(-1), | 166 thread_id_(-1), |
| 185 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), | 167 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 } | 298 } |
| 317 | 299 |
| 318 void EmbeddedWorkerInstance::AddListener(Listener* listener) { | 300 void EmbeddedWorkerInstance::AddListener(Listener* listener) { |
| 319 listener_list_.AddObserver(listener); | 301 listener_list_.AddObserver(listener); |
| 320 } | 302 } |
| 321 | 303 |
| 322 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { | 304 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { |
| 323 listener_list_.RemoveObserver(listener); | 305 listener_list_.RemoveObserver(listener); |
| 324 } | 306 } |
| 325 | 307 |
| 326 std::vector<int> EmbeddedWorkerInstance::SortProcesses( | |
| 327 const std::vector<int>& possible_process_ids) const { | |
| 328 // Add the |possible_process_ids| to the existing process_refs_ since each one | |
| 329 // is likely to take a reference once the SW starts up. | |
| 330 ProcessRefMap refs_with_new_ids = process_refs_; | |
| 331 for (std::vector<int>::const_iterator it = possible_process_ids.begin(); | |
| 332 it != possible_process_ids.end(); | |
| 333 ++it) { | |
| 334 refs_with_new_ids[*it]++; | |
| 335 } | |
| 336 | |
| 337 std::vector<std::pair<int, int> > counted(refs_with_new_ids.begin(), | |
| 338 refs_with_new_ids.end()); | |
| 339 // Sort descending by the reference count. | |
| 340 std::sort(counted.begin(), counted.end(), SecondGreater()); | |
| 341 | |
| 342 std::vector<int> result(counted.size()); | |
| 343 for (size_t i = 0; i < counted.size(); ++i) | |
| 344 result[i] = counted[i].first; | |
| 345 return result; | |
| 346 } | |
| 347 | |
| 348 } // namespace content | 308 } // namespace content |
| OLD | NEW |