| 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_registry.h" | 5 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/browser/service_worker/embedded_worker_instance.h" | 8 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/common/service_worker/embedded_worker_messages.h" | 10 #include "content/common/service_worker/embedded_worker_messages.h" |
| 11 #include "ipc/ipc_message.h" | 11 #include "ipc/ipc_message.h" |
| 12 #include "ipc/ipc_sender.h" | 12 #include "ipc/ipc_sender.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( | 16 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( |
| 17 base::WeakPtr<ServiceWorkerContextCore> context) | 17 base::WeakPtr<ServiceWorkerContextCore> context) |
| 18 : context_(context), | 18 : context_(context), |
| 19 next_embedded_worker_id_(0) {} | 19 next_embedded_worker_id_(0) {} |
| 20 | 20 |
| 21 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { | 21 scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { |
| 22 scoped_ptr<EmbeddedWorkerInstance> worker( | 22 scoped_ptr<EmbeddedWorkerInstance> worker( |
| 23 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); | 23 new EmbeddedWorkerInstance(this, next_embedded_worker_id_)); |
| 24 LOG(ERROR) << "Adding EmbeddedWorkerInstance with id " |
| 25 << next_embedded_worker_id_; |
| 24 worker_map_[next_embedded_worker_id_++] = worker.get(); | 26 worker_map_[next_embedded_worker_id_++] = worker.get(); |
| 25 return worker.Pass(); | 27 return worker.Pass(); |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool EmbeddedWorkerRegistry::StartWorker( | 30 bool EmbeddedWorkerRegistry::StartWorker( |
| 29 int process_id, | 31 int process_id, |
| 30 int embedded_worker_id, | 32 int embedded_worker_id, |
| 31 int64 service_worker_version_id, | 33 int64 service_worker_version_id, |
| 32 const GURL& script_url, | 34 const GURL& script_url, |
| 33 const IPC::Message& initialize_message) { | 35 const IPC::Message& initialize_message) { |
| 34 return Send(process_id, | 36 return Send(process_id, |
| 35 new EmbeddedWorkerMsg_StartWorker(embedded_worker_id, | 37 new EmbeddedWorkerMsg_StartWorker(embedded_worker_id, |
| 36 service_worker_version_id, | 38 service_worker_version_id, |
| 37 script_url, | 39 script_url, |
| 38 initialize_message)); | 40 initialize_message)); |
| 39 } | 41 } |
| 40 | 42 |
| 41 bool EmbeddedWorkerRegistry::StopWorker(int process_id, | 43 bool EmbeddedWorkerRegistry::StopWorker(int process_id, |
| 42 int embedded_worker_id) { | 44 int embedded_worker_id) { |
| 43 return Send(process_id, | 45 return Send(process_id, |
| 44 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 46 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
| 45 } | 47 } |
| 46 | 48 |
| 47 void EmbeddedWorkerRegistry::OnWorkerStarted( | 49 void EmbeddedWorkerRegistry::OnWorkerStarted( |
| 48 int process_id, int thread_id, int embedded_worker_id) { | 50 int process_id, int thread_id, int embedded_worker_id) { |
| 51 LOG(ERROR) << "OnWorkerStarted(" << process_id << ", " << thread_id << ", " |
| 52 << embedded_worker_id << ")"; |
| 49 DCHECK(!ContainsKey(worker_process_map_, process_id)); | 53 DCHECK(!ContainsKey(worker_process_map_, process_id)); |
| 50 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 54 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 51 if (found == worker_map_.end()) { | 55 if (found == worker_map_.end()) { |
| 52 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 56 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
| 53 return; | 57 return; |
| 54 } | 58 } |
| 55 worker_process_map_[process_id] = embedded_worker_id; | 59 worker_process_map_[process_id] = embedded_worker_id; |
| 56 DCHECK_EQ(found->second->process_id(), process_id); | 60 DCHECK_EQ(found->second->process_id(), process_id); |
| 57 found->second->OnStarted(thread_id); | 61 found->second->OnStarted(thread_id); |
| 58 } | 62 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 int embedded_worker_id = found->second; | 96 int embedded_worker_id = found->second; |
| 93 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 97 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 94 worker_map_[embedded_worker_id]->OnStopped(); | 98 worker_map_[embedded_worker_id]->OnStopped(); |
| 95 worker_process_map_.erase(found); | 99 worker_process_map_.erase(found); |
| 96 } | 100 } |
| 97 } | 101 } |
| 98 | 102 |
| 99 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} | 103 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {} |
| 100 | 104 |
| 101 bool EmbeddedWorkerRegistry::Send(int process_id, IPC::Message* message) { | 105 bool EmbeddedWorkerRegistry::Send(int process_id, IPC::Message* message) { |
| 102 if (!context_) | 106 if (!context_) { |
| 107 LOG(ERROR) << "No context, bailing on the send"; |
| 103 return false; | 108 return false; |
| 109 } |
| 104 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 110 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); |
| 105 if (found == process_sender_map_.end()) | 111 if (found == process_sender_map_.end()) { |
| 112 LOG(ERROR) << "sender not found for pid " << process_id; |
| 106 return false; | 113 return false; |
| 114 } |
| 115 LOG(ERROR) << "Sending message to " << process_id; |
| 107 return found->second->Send(message); | 116 return found->second->Send(message); |
| 108 } | 117 } |
| 109 | 118 |
| 110 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 119 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 111 int embedded_worker_id) { | 120 int embedded_worker_id) { |
| 112 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 121 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 113 worker_map_.erase(embedded_worker_id); | 122 worker_map_.erase(embedded_worker_id); |
| 114 worker_process_map_.erase(process_id); | 123 worker_process_map_.erase(process_id); |
| 115 } | 124 } |
| 116 | 125 |
| 117 } // namespace content | 126 } // namespace content |
| OLD | NEW |