| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 | 38 |
| 39 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( | 39 ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker( |
| 40 int process_id, int embedded_worker_id) { | 40 int process_id, int embedded_worker_id) { |
| 41 return Send(process_id, | 41 return Send(process_id, |
| 42 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); | 42 new EmbeddedWorkerMsg_StopWorker(embedded_worker_id)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void EmbeddedWorkerRegistry::OnWorkerStarted( | 45 void EmbeddedWorkerRegistry::OnWorkerStarted( |
| 46 int process_id, int thread_id, int embedded_worker_id) { | 46 int process_id, int thread_id, int embedded_worker_id) { |
| 47 DCHECK(!ContainsKey(worker_process_map_, process_id) || | 47 DCHECK(!ContainsKey(worker_process_map_, process_id)); |
| 48 worker_process_map_[process_id].count(embedded_worker_id) == 0); | |
| 49 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 48 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 50 if (found == worker_map_.end()) { | 49 if (found == worker_map_.end()) { |
| 51 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 50 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
| 52 return; | 51 return; |
| 53 } | 52 } |
| 54 worker_process_map_[process_id].insert(embedded_worker_id); | 53 worker_process_map_[process_id] = embedded_worker_id; |
| 55 DCHECK_EQ(found->second->process_id(), process_id); | 54 DCHECK_EQ(found->second->process_id(), process_id); |
| 56 found->second->OnStarted(thread_id); | 55 found->second->OnStarted(thread_id); |
| 57 } | 56 } |
| 58 | 57 |
| 59 void EmbeddedWorkerRegistry::OnWorkerStopped( | 58 void EmbeddedWorkerRegistry::OnWorkerStopped( |
| 60 int process_id, int embedded_worker_id) { | 59 int process_id, int embedded_worker_id) { |
| 61 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 60 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 62 if (found == worker_map_.end()) { | 61 if (found == worker_map_.end()) { |
| 63 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 62 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
| 64 return; | 63 return; |
| 65 } | 64 } |
| 66 DCHECK_EQ(found->second->process_id(), process_id); | 65 DCHECK_EQ(found->second->process_id(), process_id); |
| 67 worker_process_map_[process_id].erase(embedded_worker_id); | 66 worker_process_map_.erase(process_id); |
| 68 found->second->OnStopped(); | 67 found->second->OnStopped(); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void EmbeddedWorkerRegistry::OnSendMessageToBrowser( | 70 void EmbeddedWorkerRegistry::OnSendMessageToBrowser( |
| 72 int embedded_worker_id, int request_id, const IPC::Message& message) { | 71 int embedded_worker_id, int request_id, const IPC::Message& message) { |
| 73 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 72 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 74 if (found == worker_map_.end()) { | 73 if (found == worker_map_.end()) { |
| 75 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; | 74 LOG(ERROR) << "Worker " << embedded_worker_id << " not registered"; |
| 76 return; | 75 return; |
| 77 } | 76 } |
| 78 // TODO(kinuko): Filter out unexpected messages here and uncomment below | 77 // TODO(kinuko): Filter out unexpected messages here and uncomment below |
| 79 // when we actually define messages that are to be sent from child process | 78 // when we actually define messages that are to be sent from child process |
| 80 // to the browser via this channel. (We don't have any yet) | 79 // to the browser via this channel. (We don't have any yet) |
| 81 found->second->OnMessageReceived(request_id, message); | 80 found->second->OnMessageReceived(request_id, message); |
| 82 } | 81 } |
| 83 | 82 |
| 84 void EmbeddedWorkerRegistry::AddChildProcessSender( | 83 void EmbeddedWorkerRegistry::AddChildProcessSender( |
| 85 int process_id, IPC::Sender* sender) { | 84 int process_id, IPC::Sender* sender) { |
| 86 process_sender_map_[process_id] = sender; | 85 process_sender_map_[process_id] = sender; |
| 87 DCHECK(!ContainsKey(worker_process_map_, process_id)); | 86 DCHECK(!ContainsKey(worker_process_map_, process_id)); |
| 88 } | 87 } |
| 89 | 88 |
| 90 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) { | 89 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) { |
| 91 process_sender_map_.erase(process_id); | 90 process_sender_map_.erase(process_id); |
| 92 std::map<int, std::set<int> >::iterator found = | 91 std::map<int, int>::iterator found = worker_process_map_.find(process_id); |
| 93 worker_process_map_.find(process_id); | |
| 94 if (found != worker_process_map_.end()) { | 92 if (found != worker_process_map_.end()) { |
| 95 const std::set<int>& worker_set = worker_process_map_[process_id]; | 93 int embedded_worker_id = found->second; |
| 96 for (std::set<int>::const_iterator it = worker_set.begin(); | 94 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 97 it != worker_set.end(); | 95 worker_map_[embedded_worker_id]->OnStopped(); |
| 98 ++it) { | |
| 99 int embedded_worker_id = *it; | |
| 100 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | |
| 101 worker_map_[embedded_worker_id]->OnStopped(); | |
| 102 } | |
| 103 worker_process_map_.erase(found); | 96 worker_process_map_.erase(found); |
| 104 } | 97 } |
| 105 } | 98 } |
| 106 | 99 |
| 107 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( | 100 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( |
| 108 int embedded_worker_id) { | 101 int embedded_worker_id) { |
| 109 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 102 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 110 if (found == worker_map_.end()) | 103 if (found == worker_map_.end()) |
| 111 return NULL; | 104 return NULL; |
| 112 return found->second; | 105 return found->second; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 127 } | 120 } |
| 128 | 121 |
| 129 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 122 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 130 int embedded_worker_id) { | 123 int embedded_worker_id) { |
| 131 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); | 124 DCHECK(ContainsKey(worker_map_, embedded_worker_id)); |
| 132 worker_map_.erase(embedded_worker_id); | 125 worker_map_.erase(embedded_worker_id); |
| 133 worker_process_map_.erase(process_id); | 126 worker_process_map_.erase(process_id); |
| 134 } | 127 } |
| 135 | 128 |
| 136 } // namespace content | 129 } // namespace content |
| OLD | NEW |