Chromium Code Reviews| 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/bind_helpers.h" | 7 #include "base/bind_helpers.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "content/browser/renderer_host/render_widget_helper.h" | 10 #include "content/browser/renderer_host/render_widget_helper.h" |
| 11 #include "content/browser/service_worker/embedded_worker_instance.h" | 11 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 12 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | |
| 14 #include "content/common/service_worker/embedded_worker_messages.h" | 15 #include "content/common/service_worker/embedded_worker_messages.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
| 17 #include "ipc/ipc_sender.h" | 18 #include "ipc/ipc_sender.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 // static | 22 // static |
| 22 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( | 23 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( |
| 23 const base::WeakPtr<ServiceWorkerContextCore>& context) { | 24 const base::WeakPtr<ServiceWorkerContextCore>& context) { |
| 24 return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0)); | 25 return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0)); |
| 25 } | 26 } |
| 26 | 27 |
| 27 // static | 28 // static |
| 28 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( | 29 scoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create( |
| 29 const base::WeakPtr<ServiceWorkerContextCore>& context, | 30 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 30 EmbeddedWorkerRegistry* old_registry) { | 31 EmbeddedWorkerRegistry* old_registry) { |
| 31 scoped_refptr<EmbeddedWorkerRegistry> registry = | 32 scoped_refptr<EmbeddedWorkerRegistry> registry = |
| 32 new EmbeddedWorkerRegistry( | 33 new EmbeddedWorkerRegistry( |
| 33 context, | 34 context, |
| 34 old_registry->next_embedded_worker_id_); | 35 old_registry->next_embedded_worker_id_); |
| 35 registry->process_sender_map_.swap(old_registry->process_sender_map_); | |
| 36 return registry; | 36 return registry; |
| 37 } | 37 } |
| 38 | 38 |
| 39 std::unique_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { | 39 std::unique_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() { |
| 40 std::unique_ptr<EmbeddedWorkerInstance> worker( | 40 std::unique_ptr<EmbeddedWorkerInstance> worker( |
| 41 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_)); | 41 new EmbeddedWorkerInstance(context_, next_embedded_worker_id_)); |
| 42 worker_map_[next_embedded_worker_id_++] = worker.get(); | 42 worker_map_[next_embedded_worker_id_++] = worker.get(); |
| 43 return worker; | 43 return worker; |
| 44 } | 44 } |
| 45 | 45 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 const base::string16& message, | 172 const base::string16& message, |
| 173 int line_number, | 173 int line_number, |
| 174 const GURL& source_url) { | 174 const GURL& source_url) { |
| 175 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); | 175 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); |
| 176 if (!worker) | 176 if (!worker) |
| 177 return; | 177 return; |
| 178 worker->OnReportConsoleMessage(source_identifier, message_level, message, | 178 worker->OnReportConsoleMessage(source_identifier, message_level, message, |
| 179 line_number, source_url); | 179 line_number, source_url); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void EmbeddedWorkerRegistry::AddChildProcessSender( | 182 void EmbeddedWorkerRegistry::RemoveProcess(int process_id) { |
| 183 int process_id, | |
| 184 IPC::Sender* sender, | |
| 185 MessagePortMessageFilter* message_port_message_filter) { | |
| 186 process_sender_map_[process_id] = sender; | |
| 187 process_message_port_message_filter_map_[process_id] = | |
| 188 message_port_message_filter; | |
| 189 DCHECK(!base::ContainsKey(worker_process_map_, process_id)); | |
| 190 } | |
| 191 | |
| 192 void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) { | |
| 193 process_sender_map_.erase(process_id); | |
| 194 process_message_port_message_filter_map_.erase(process_id); | |
| 195 std::map<int, std::set<int> >::iterator found = | 183 std::map<int, std::set<int> >::iterator found = |
| 196 worker_process_map_.find(process_id); | 184 worker_process_map_.find(process_id); |
| 197 if (found != worker_process_map_.end()) { | 185 if (found != worker_process_map_.end()) { |
| 198 const std::set<int>& worker_set = worker_process_map_[process_id]; | 186 const std::set<int>& worker_set = worker_process_map_[process_id]; |
| 199 for (std::set<int>::const_iterator it = worker_set.begin(); | 187 for (std::set<int>::const_iterator it = worker_set.begin(); |
| 200 it != worker_set.end(); | 188 it != worker_set.end(); |
| 201 ++it) { | 189 ++it) { |
| 202 int embedded_worker_id = *it; | 190 int embedded_worker_id = *it; |
| 203 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); | 191 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); |
| 204 // Somehow the worker thread has lost contact with the browser process. | 192 // |worker_map_| doesn't have workers when it's stopped normally, and |
| 205 // The renderer may have been killed. Set the worker's status to STOPPED | 193 // this is for resetting the workers which has lost contact with the |
| 206 // so a new thread can be created for this version. Use OnDetached rather | 194 // browser process somehow. |
|
falken
2017/02/13 05:59:24
I like how the existing comment explains why OnDet
shimazu
2017/02/13 08:20:19
Done.
| |
| 207 // than OnStopped so UMA doesn't record it as a normal stoppage. | |
| 208 worker_map_[embedded_worker_id]->OnDetached(); | 195 worker_map_[embedded_worker_id]->OnDetached(); |
| 209 } | 196 } |
| 210 worker_process_map_.erase(found); | 197 worker_process_map_.erase(found); |
| 211 } | 198 } |
| 212 } | 199 } |
| 213 | 200 |
| 214 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( | 201 EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker( |
| 215 int embedded_worker_id) { | 202 int embedded_worker_id) { |
| 216 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); | 203 WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id); |
| 217 if (found == worker_map_.end()) | 204 if (found == worker_map_.end()) |
| 218 return nullptr; | 205 return nullptr; |
| 219 return found->second; | 206 return found->second; |
| 220 } | 207 } |
| 221 | 208 |
| 222 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const { | 209 bool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const { |
| 223 if (embedded_worker_id < initial_embedded_worker_id_ || | 210 if (embedded_worker_id < initial_embedded_worker_id_ || |
| 224 next_embedded_worker_id_ <= embedded_worker_id) { | 211 next_embedded_worker_id_ <= embedded_worker_id) { |
| 225 return false; | 212 return false; |
| 226 } | 213 } |
| 227 return true; | 214 return true; |
| 228 } | 215 } |
| 229 | 216 |
| 230 MessagePortMessageFilter* | 217 MessagePortMessageFilter* |
| 231 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) { | 218 EmbeddedWorkerRegistry::MessagePortMessageFilterForProcess(int process_id) { |
| 232 return process_message_port_message_filter_map_[process_id]; | 219 ServiceWorkerDispatcherHost* dispatcher_host = |
| 220 context_->GetDispatcherHost(process_id); | |
| 221 DCHECK(dispatcher_host); | |
| 222 return dispatcher_host->message_port_message_filter(); | |
| 233 } | 223 } |
| 234 | 224 |
| 235 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( | 225 EmbeddedWorkerRegistry::EmbeddedWorkerRegistry( |
| 236 const base::WeakPtr<ServiceWorkerContextCore>& context, | 226 const base::WeakPtr<ServiceWorkerContextCore>& context, |
| 237 int initial_embedded_worker_id) | 227 int initial_embedded_worker_id) |
| 238 : context_(context), | 228 : context_(context), |
| 239 next_embedded_worker_id_(initial_embedded_worker_id), | 229 next_embedded_worker_id_(initial_embedded_worker_id), |
| 240 initial_embedded_worker_id_(initial_embedded_worker_id) { | 230 initial_embedded_worker_id_(initial_embedded_worker_id) { |
| 241 } | 231 } |
| 242 | 232 |
| 243 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { | 233 EmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() { |
| 244 Shutdown(); | 234 Shutdown(); |
| 245 } | 235 } |
| 246 | 236 |
| 247 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id, | 237 void EmbeddedWorkerRegistry::BindWorkerToProcess(int process_id, |
| 248 int embedded_worker_id) { | 238 int embedded_worker_id) { |
| 249 // The ServiceWorkerDispatcherHost is supposed to be created when the process | |
| 250 // is created, and keep an entry in process_sender_map_ for its whole | |
| 251 // lifetime. | |
| 252 DCHECK(base::ContainsKey(process_sender_map_, process_id)); | |
| 253 DCHECK(GetWorker(embedded_worker_id)); | 239 DCHECK(GetWorker(embedded_worker_id)); |
| 254 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id); | 240 DCHECK_EQ(GetWorker(embedded_worker_id)->process_id(), process_id); |
| 255 DCHECK( | 241 DCHECK( |
| 256 !base::ContainsKey(worker_process_map_, process_id) || | 242 !base::ContainsKey(worker_process_map_, process_id) || |
| 257 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id)); | 243 !base::ContainsKey(worker_process_map_[process_id], embedded_worker_id)); |
| 258 | 244 |
| 259 worker_process_map_[process_id].insert(embedded_worker_id); | 245 worker_process_map_[process_id].insert(embedded_worker_id); |
| 260 } | 246 } |
| 261 | 247 |
| 262 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( | 248 ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send( |
| 263 int process_id, IPC::Message* message_ptr) { | 249 int process_id, IPC::Message* message_ptr) { |
| 264 std::unique_ptr<IPC::Message> message(message_ptr); | 250 std::unique_ptr<IPC::Message> message(message_ptr); |
| 265 if (!context_) | 251 if (!context_) |
| 266 return SERVICE_WORKER_ERROR_ABORT; | 252 return SERVICE_WORKER_ERROR_ABORT; |
| 267 ProcessToSenderMap::iterator found = process_sender_map_.find(process_id); | 253 IPC::Sender* sender = context_->GetDispatcherHost(process_id); |
| 268 if (found == process_sender_map_.end()) | 254 if (!sender) |
| 269 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; | 255 return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND; |
| 270 if (!found->second->Send(message.release())) | 256 if (!sender->Send(message.release())) |
| 271 return SERVICE_WORKER_ERROR_IPC_FAILED; | 257 return SERVICE_WORKER_ERROR_IPC_FAILED; |
| 272 return SERVICE_WORKER_OK; | 258 return SERVICE_WORKER_OK; |
| 273 } | 259 } |
| 274 | 260 |
| 275 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, | 261 void EmbeddedWorkerRegistry::RemoveWorker(int process_id, |
| 276 int embedded_worker_id) { | 262 int embedded_worker_id) { |
| 277 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); | 263 DCHECK(base::ContainsKey(worker_map_, embedded_worker_id)); |
| 278 DetachWorker(process_id, embedded_worker_id); | 264 DetachWorker(process_id, embedded_worker_id); |
| 279 worker_map_.erase(embedded_worker_id); | 265 worker_map_.erase(embedded_worker_id); |
| 280 } | 266 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 295 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); | 281 EmbeddedWorkerInstance* worker = GetWorker(embedded_worker_id); |
| 296 if (!worker || worker->process_id() != process_id) { | 282 if (!worker || worker->process_id() != process_id) { |
| 297 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false); | 283 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", false); |
| 298 return nullptr; | 284 return nullptr; |
| 299 } | 285 } |
| 300 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true); | 286 UMA_HISTOGRAM_BOOLEAN("ServiceWorker.WorkerForMessageFound", true); |
| 301 return worker; | 287 return worker; |
| 302 } | 288 } |
| 303 | 289 |
| 304 } // namespace content | 290 } // namespace content |
| OLD | NEW |