1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file.
4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/service_worker/embedded_worker_registry.h"
6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
75c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/bind_helpers.h"
8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/stl_util.h"
95c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/renderer_host/render_widget_helper.h"
10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/service_worker/embedded_worker_instance.h"
11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "content/browser/service_worker/service_worker_context_core.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/browser/service_worker/service_worker_context_wrapper.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/common/service_worker/embedded_worker_messages.h"
145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "content/public/browser/browser_thread.h"
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ipc/ipc_message.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ipc/ipc_sender.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace content {
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochscoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const base::WeakPtr<ServiceWorkerContextCore>& context) {
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return make_scoped_refptr(new EmbeddedWorkerRegistry(context, 0));
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// static
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochscoped_refptr<EmbeddedWorkerRegistry> EmbeddedWorkerRegistry::Create(
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const base::WeakPtr<ServiceWorkerContextCore>& context,
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    EmbeddedWorkerRegistry* old_registry) {
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<EmbeddedWorkerRegistry> registry =
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new EmbeddedWorkerRegistry(
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          context,
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          old_registry->next_embedded_worker_id_);
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  registry->process_sender_map_.swap(old_registry->process_sender_map_);
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return registry;
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)scoped_ptr<EmbeddedWorkerInstance> EmbeddedWorkerRegistry::CreateWorker() {
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  scoped_ptr<EmbeddedWorkerInstance> worker(
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new EmbeddedWorkerInstance(context_, next_embedded_worker_id_));
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  worker_map_[next_embedded_worker_id_++] = worker.get();
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return worker.Pass();
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ServiceWorkerStatusCode EmbeddedWorkerRegistry::StopWorker(
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int process_id, int embedded_worker_id) {
47a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return Send(process_id,
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              new EmbeddedWorkerMsg_StopWorker(embedded_worker_id));
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochbool EmbeddedWorkerRegistry::OnMessageReceived(const IPC::Message& message) {
520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // TODO(kinuko): Move all EmbeddedWorker message handling from
530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // ServiceWorkerDispatcherHost.
540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  WorkerInstanceMap::iterator found = worker_map_.find(message.routing_id());
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end())
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    return false;
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  return found->second->OnMessageReceived(message);
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuvoid EmbeddedWorkerRegistry::Shutdown() {
635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  for (WorkerInstanceMap::iterator it = worker_map_.begin();
645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu       it != worker_map_.end();
655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu       ++it) {
665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    it->second->Stop();
675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
7003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)void EmbeddedWorkerRegistry::OnWorkerReadyForInspection(
7103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    int process_id,
7203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    int embedded_worker_id) {
7303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
7403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    return;
7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  found->second->OnReadyForInspection();
7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)}
8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid EmbeddedWorkerRegistry::OnWorkerScriptLoaded(
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int process_id,
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int thread_id,
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int embedded_worker_id ) {
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
8603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
8703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
8803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
89010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  found->second->OnScriptLoaded(thread_id);
91010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
92010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
93010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)void EmbeddedWorkerRegistry::OnWorkerScriptLoadFailed(int process_id,
94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                                                      int embedded_worker_id) {
95010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
9603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
9703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
9803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
99010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    return;
100010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  found->second->OnScriptLoadFailed();
101010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
102010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void EmbeddedWorkerRegistry::OnWorkerStarted(
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    int process_id, int embedded_worker_id) {
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!ContainsKey(worker_process_map_, process_id) ||
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         worker_process_map_[process_id].count(embedded_worker_id) == 0);
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
10803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
10903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
11003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  worker_process_map_[process_id].insert(embedded_worker_id);
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  found->second->OnStarted();
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void EmbeddedWorkerRegistry::OnWorkerStopped(
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int process_id, int embedded_worker_id) {
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
12003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
12103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  worker_process_map_[process_id].erase(embedded_worker_id);
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  found->second->OnStopped();
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid EmbeddedWorkerRegistry::OnPausedAfterDownload(
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int process_id, int embedded_worker_id) {
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
13003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK_EQ(found->second->process_id(), process_id);
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end() || found->second->process_id() != process_id)
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return;
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  found->second->OnPausedAfterDownload();
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid EmbeddedWorkerRegistry::OnReportException(
1380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int embedded_worker_id,
1390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const base::string16& error_message,
1400529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int line_number,
1410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int column_number,
1420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const GURL& source_url) {
1430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
14403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
14503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end())
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
1470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  found->second->OnReportException(
1480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      error_message, line_number, column_number, source_url);
1490529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
1500529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
1510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid EmbeddedWorkerRegistry::OnReportConsoleMessage(
1520529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int embedded_worker_id,
1530529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int source_identifier,
1540529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int message_level,
1550529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const base::string16& message,
1560529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    int line_number,
1570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const GURL& source_url) {
1580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
15903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  DCHECK(found != worker_map_.end());
16003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  if (found == worker_map_.end())
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
1620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  found->second->OnReportConsoleMessage(
1630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      source_identifier, message_level, message, line_number, source_url);
164a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
165a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
166a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void EmbeddedWorkerRegistry::AddChildProcessSender(
167a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    int process_id, IPC::Sender* sender) {
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  process_sender_map_[process_id] = sender;
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(!ContainsKey(worker_process_map_, process_id));
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
172a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void EmbeddedWorkerRegistry::RemoveChildProcessSender(int process_id) {
173a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  process_sender_map_.erase(process_id);
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<int, std::set<int> >::iterator found =
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      worker_process_map_.find(process_id);
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (found != worker_process_map_.end()) {
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::set<int>& worker_set = worker_process_map_[process_id];
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (std::set<int>::const_iterator it = worker_set.begin();
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         it != worker_set.end();
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         ++it) {
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int embedded_worker_id = *it;
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      DCHECK(ContainsKey(worker_map_, embedded_worker_id));
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      worker_map_[embedded_worker_id]->OnStopped();
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    worker_process_map_.erase(found);
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)EmbeddedWorkerInstance* EmbeddedWorkerRegistry::GetWorker(
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    int embedded_worker_id) {
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  WorkerInstanceMap::iterator found = worker_map_.find(embedded_worker_id);
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (found == worker_map_.end())
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return NULL;
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return found->second;
195a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
196a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
197116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool EmbeddedWorkerRegistry::CanHandle(int embedded_worker_id) const {
198116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (embedded_worker_id < initial_embedded_worker_id_ ||
199116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      next_embedded_worker_id_ <= embedded_worker_id) {
200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return false;
201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
202116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return true;
203116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
204116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
205116680a4aac90f2aa7413d9095a592090648e557Ben MurdochEmbeddedWorkerRegistry::EmbeddedWorkerRegistry(
206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const base::WeakPtr<ServiceWorkerContextCore>& context,
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int initial_embedded_worker_id)
208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : context_(context),
209116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      next_embedded_worker_id_(initial_embedded_worker_id),
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      initial_embedded_worker_id_(initial_embedded_worker_id) {
211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
212116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo LiuEmbeddedWorkerRegistry::~EmbeddedWorkerRegistry() {
2145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  Shutdown();
2155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
2165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
217cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void EmbeddedWorkerRegistry::SendStartWorker(
2185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
2195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const StatusCallback& callback,
2205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    int process_id) {
2215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // The ServiceWorkerDispatcherHost is supposed to be created when the process
2225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // is created, and keep an entry in process_sender_map_ for its whole
2235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // lifetime.
2245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DCHECK(ContainsKey(process_sender_map_, process_id));
2255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  callback.Run(Send(process_id, new EmbeddedWorkerMsg_StartWorker(*params)));
2265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)ServiceWorkerStatusCode EmbeddedWorkerRegistry::Send(
229cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    int process_id, IPC::Message* message_ptr) {
230cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<IPC::Message> message(message_ptr);
231a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (!context_)
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return SERVICE_WORKER_ERROR_ABORT;
233a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ProcessToSenderMap::iterator found = process_sender_map_.find(process_id);
234a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (found == process_sender_map_.end())
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND;
236cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!found->second->Send(message.release()))
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return SERVICE_WORKER_ERROR_IPC_FAILED;
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return SERVICE_WORKER_OK;
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void EmbeddedWorkerRegistry::RemoveWorker(int process_id,
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                          int embedded_worker_id) {
2435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(ContainsKey(worker_map_, embedded_worker_id));
2445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  worker_map_.erase(embedded_worker_id);
2455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  worker_process_map_.erase(process_id);
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
248a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace content
249