Chromium Code Reviews| Index: content/browser/service_worker/embedded_worker_instance.cc |
| diff --git a/content/browser/service_worker/embedded_worker_instance.cc b/content/browser/service_worker/embedded_worker_instance.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b2c22565645cff8bf4f1374cf4ecec9ada6e75e6 |
| --- /dev/null |
| +++ b/content/browser/service_worker/embedded_worker_instance.cc |
| @@ -0,0 +1,90 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/service_worker/embedded_worker_instance.h" |
| + |
| +#include "content/browser/service_worker/embedded_worker_registry.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| + |
| +EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
| + EmbeddedWorkerRegistry* registry, |
| + int embedded_worker_id) |
| + : registry_(registry), |
| + embedded_worker_id_(embedded_worker_id), |
| + status_(STOPPED), |
| + process_id_(-1), |
| + thread_id_(-1) { |
| +} |
| + |
| +EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
| + DCHECK(status_ == STOPPED); |
| + registry_->RemoveWorker(embedded_worker_id_); |
| +} |
| + |
| +void EmbeddedWorkerInstance::Start( |
| + int64 service_worker_version_id, |
| + const GURL& script_url) { |
| + DCHECK(status_ == STOPPED); |
| + process_id_ = ChooseProcess(); |
| + status_ = STARTING; |
| + bool success = registry_->StartWorker( |
| + process_id_, |
| + embedded_worker_id_, |
| + service_worker_version_id, |
| + script_url); |
| + if (!success) |
| + OnStopped(); |
| +} |
| + |
| +void EmbeddedWorkerInstance::Stop() { |
| + DCHECK(status_ == STARTING || status_ == RUNNING); |
| + registry_->StopWorker(process_id_, embedded_worker_id_); |
|
alecflett
2013/12/05 05:18:42
set status to STOPPING?
kinuko
2013/12/09 14:07:42
Yup, done.
|
| +} |
| + |
| +void EmbeddedWorkerInstance::OnStarted(int thread_id) { |
| + DCHECK(status_ == STARTING); |
| + status_ = RUNNING; |
| + thread_id_ = thread_id; |
| +} |
| + |
| +void EmbeddedWorkerInstance::OnStopped() { |
| + status_ = STOPPED; |
|
alecflett
2013/12/05 05:18:42
DCHECK(status_ == STOPPING)?
kinuko
2013/12/09 14:07:42
This could also happen in STARTING or RUNNING (i.e
|
| + process_id_ = -1; |
| + thread_id_ = -1; |
| +} |
| + |
| +void EmbeddedWorkerInstance::AddProcessReference(int process_id) { |
| + ProcessRefMap::iterator found = process_refs_.find(process_id); |
| + if (found == process_refs_.end()) |
| + found = process_refs_.insert(std::make_pair(process_id, 0)).first; |
| + ++found->second; |
| +} |
| + |
| +void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { |
| + ProcessRefMap::iterator found = process_refs_.find(process_id); |
| + if (found == process_refs_.end()) { |
| + NOTREACHED() << "Releasing unknown process ref " << process_id; |
| + return; |
| + } |
| + if (--found->second == 0) |
| + process_refs_.erase(found); |
| +} |
| + |
| +int EmbeddedWorkerInstance::ChooseProcess() { |
| + // Naive implementation; chooses a process which has the biggest number of |
| + // associated providers (so that hopefully likely live longer). |
| + ProcessRefMap::iterator max_ref_iter = process_refs_.end(); |
| + for (ProcessRefMap::iterator iter = process_refs_.begin(); |
| + iter != process_refs_.end(); ++iter) { |
| + if (max_ref_iter == process_refs_.end() || |
| + max_ref_iter->second < iter->second) |
| + max_ref_iter = iter; |
| + } |
| + DCHECK(max_ref_iter != process_refs_.end()); |
|
alecflett
2013/12/05 05:18:42
Won't this be true if there are no entries in proc
kinuko
2013/12/09 14:07:42
Currently this instance code has some assumptions
|
| + return max_ref_iter->first; |
| +} |
| + |
| +} // namespace content |