| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 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 | 
|  | 3 // found in the LICENSE file. | 
|  | 4 | 
|  | 5 #include "content/browser/service_worker/embedded_worker_instance.h" | 
|  | 6 | 
|  | 7 #include "content/browser/service_worker/embedded_worker_registry.h" | 
|  | 8 #include "url/gurl.h" | 
|  | 9 | 
|  | 10 namespace content { | 
|  | 11 | 
|  | 12 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { | 
|  | 13   registry_->RemoveWorker(embedded_worker_id_); | 
|  | 14 } | 
|  | 15 | 
|  | 16 bool EmbeddedWorkerInstance::Start( | 
|  | 17     int64 service_worker_version_id, | 
|  | 18     const GURL& script_url) { | 
|  | 19   DCHECK(status_ == STOPPED); | 
|  | 20   if (!ChooseProcess()) | 
|  | 21     return false; | 
|  | 22   status_ = STARTING; | 
|  | 23   bool success = registry_->StartWorker( | 
|  | 24       process_id_, | 
|  | 25       embedded_worker_id_, | 
|  | 26       service_worker_version_id, | 
|  | 27       script_url); | 
|  | 28   if (!success) { | 
|  | 29     status_ = STOPPED; | 
|  | 30     process_id_ = -1; | 
|  | 31   } | 
|  | 32   return success; | 
|  | 33 } | 
|  | 34 | 
|  | 35 bool EmbeddedWorkerInstance::Stop() { | 
|  | 36   DCHECK(status_ == STARTING || status_ == RUNNING); | 
|  | 37   const bool success = registry_->StopWorker(process_id_, embedded_worker_id_); | 
|  | 38   if (success) | 
|  | 39     status_ = STOPPING; | 
|  | 40   return success; | 
|  | 41 } | 
|  | 42 | 
|  | 43 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | 
|  | 44   ProcessRefMap::iterator found = process_refs_.find(process_id); | 
|  | 45   if (found == process_refs_.end()) | 
|  | 46     found = process_refs_.insert(std::make_pair(process_id, 0)).first; | 
|  | 47   ++found->second; | 
|  | 48 } | 
|  | 49 | 
|  | 50 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | 
|  | 51   ProcessRefMap::iterator found = process_refs_.find(process_id); | 
|  | 52   if (found == process_refs_.end()) { | 
|  | 53     NOTREACHED() << "Releasing unknown process ref " << process_id; | 
|  | 54     return; | 
|  | 55   } | 
|  | 56   if (--found->second == 0) | 
|  | 57     process_refs_.erase(found); | 
|  | 58 } | 
|  | 59 | 
|  | 60 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 
|  | 61     EmbeddedWorkerRegistry* registry, | 
|  | 62     int embedded_worker_id) | 
|  | 63     : registry_(registry), | 
|  | 64       embedded_worker_id_(embedded_worker_id), | 
|  | 65       status_(STOPPED), | 
|  | 66       process_id_(-1), | 
|  | 67       thread_id_(-1) { | 
|  | 68 } | 
|  | 69 | 
|  | 70 void EmbeddedWorkerInstance::OnStarted(int thread_id) { | 
|  | 71   DCHECK(status_ == STARTING); | 
|  | 72   status_ = RUNNING; | 
|  | 73   thread_id_ = thread_id; | 
|  | 74 } | 
|  | 75 | 
|  | 76 void EmbeddedWorkerInstance::OnStopped() { | 
|  | 77   status_ = STOPPED; | 
|  | 78   process_id_ = -1; | 
|  | 79   thread_id_ = -1; | 
|  | 80 } | 
|  | 81 | 
|  | 82 bool EmbeddedWorkerInstance::ChooseProcess() { | 
|  | 83   DCHECK_EQ(-1, process_id_); | 
|  | 84   // Naive implementation; chooses a process which has the biggest number of | 
|  | 85   // associated providers (so that hopefully likely live longer). | 
|  | 86   ProcessRefMap::iterator max_ref_iter = process_refs_.end(); | 
|  | 87   for (ProcessRefMap::iterator iter = process_refs_.begin(); | 
|  | 88        iter != process_refs_.end(); ++iter) { | 
|  | 89     if (max_ref_iter == process_refs_.end() || | 
|  | 90         max_ref_iter->second < iter->second) | 
|  | 91       max_ref_iter = iter; | 
|  | 92   } | 
|  | 93   if (max_ref_iter == process_refs_.end()) | 
|  | 94     return false; | 
|  | 95   process_id_ = max_ref_iter->first; | 
|  | 96   return true; | 
|  | 97 } | 
|  | 98 | 
|  | 99 }  // namespace content | 
| OLD | NEW | 
|---|