| 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/embedded_worker_messages.h" | 8 #include "content/common/service_worker/embedded_worker_messages.h" |
| 9 #include "ipc/ipc_message.h" | 9 #include "ipc/ipc_message.h" |
| 10 #include "url/gurl.h" | 10 #include "url/gurl.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { | 14 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
| 15 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 15 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
| 16 } | 16 } |
| 17 | 17 |
| 18 bool EmbeddedWorkerInstance::Start( | 18 ServiceWorkerStatusCode EmbeddedWorkerInstance::Start( |
| 19 int64 service_worker_version_id, | 19 int64 service_worker_version_id, |
| 20 const GURL& script_url) { | 20 const GURL& script_url) { |
| 21 DCHECK(status_ == STOPPED); | 21 DCHECK(status_ == STOPPED); |
| 22 if (!ChooseProcess()) | 22 if (!ChooseProcess()) |
| 23 return false; | 23 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
| 24 status_ = STARTING; | 24 status_ = STARTING; |
| 25 bool success = registry_->StartWorker( | 25 ServiceWorkerStatusCode status = registry_->StartWorker( |
| 26 process_id_, | 26 process_id_, |
| 27 embedded_worker_id_, | 27 embedded_worker_id_, |
| 28 service_worker_version_id, | 28 service_worker_version_id, |
| 29 script_url); | 29 script_url); |
| 30 if (!success) { | 30 if (status != SERVICE_WORKER_OK) { |
| 31 status_ = STOPPED; | 31 status_ = STOPPED; |
| 32 process_id_ = -1; | 32 process_id_ = -1; |
| 33 } | 33 } |
| 34 return success; | 34 return status; |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool EmbeddedWorkerInstance::Stop() { | 37 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
| 38 DCHECK(status_ == STARTING || status_ == RUNNING); | 38 DCHECK(status_ == STARTING || status_ == RUNNING); |
| 39 const bool success = registry_->StopWorker(process_id_, embedded_worker_id_); | 39 ServiceWorkerStatusCode status = |
| 40 if (success) | 40 registry_->StopWorker(process_id_, embedded_worker_id_); |
| 41 if (status == SERVICE_WORKER_OK) |
| 41 status_ = STOPPING; | 42 status_ = STOPPING; |
| 42 return success; | 43 return status; |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool EmbeddedWorkerInstance::SendMessage(const IPC::Message& message) { | 46 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
| 47 const IPC::Message& message) { |
| 46 DCHECK(status_ == RUNNING); | 48 DCHECK(status_ == RUNNING); |
| 47 return registry_->Send(process_id_, | 49 return registry_->Send(process_id_, |
| 48 new EmbeddedWorkerContextMsg_SendMessageToWorker( | 50 new EmbeddedWorkerContextMsg_SendMessageToWorker( |
| 49 thread_id_, embedded_worker_id_, message)); | 51 thread_id_, embedded_worker_id_, message)); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | 54 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { |
| 53 ProcessRefMap::iterator found = process_refs_.find(process_id); | 55 ProcessRefMap::iterator found = process_refs_.find(process_id); |
| 54 if (found == process_refs_.end()) | 56 if (found == process_refs_.end()) |
| 55 found = process_refs_.insert(std::make_pair(process_id, 0)).first; | 57 found = process_refs_.insert(std::make_pair(process_id, 0)).first; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 max_ref_iter->second < iter->second) | 118 max_ref_iter->second < iter->second) |
| 117 max_ref_iter = iter; | 119 max_ref_iter = iter; |
| 118 } | 120 } |
| 119 if (max_ref_iter == process_refs_.end()) | 121 if (max_ref_iter == process_refs_.end()) |
| 120 return false; | 122 return false; |
| 121 process_id_ = max_ref_iter->first; | 123 process_id_ = max_ref_iter->first; |
| 122 return true; | 124 return true; |
| 123 } | 125 } |
| 124 | 126 |
| 125 } // namespace content | 127 } // namespace content |
| OLD | NEW |