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