• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/nacl_host/nacl_broker_service_win.h"
6 
7 #include "chrome/browser/nacl_host/nacl_process_host.h"
8 
GetInstance()9 NaClBrokerService* NaClBrokerService::GetInstance() {
10   return Singleton<NaClBrokerService>::get();
11 }
12 
NaClBrokerService()13 NaClBrokerService::NaClBrokerService()
14     : loaders_running_(0) {
15 }
16 
StartBroker()17 bool NaClBrokerService::StartBroker() {
18   NaClBrokerHost* broker_host = new NaClBrokerHost;
19   if (!broker_host->Init()) {
20     delete broker_host;
21     return false;
22   }
23   return true;
24 }
25 
LaunchLoader(NaClProcessHost * nacl_process_host,const std::wstring & loader_channel_id)26 bool NaClBrokerService::LaunchLoader(NaClProcessHost* nacl_process_host,
27                                      const std::wstring& loader_channel_id) {
28   // Add task to the list
29   pending_launches_[loader_channel_id] = nacl_process_host;
30   NaClBrokerHost* broker_host = GetBrokerHost();
31 
32   if (!broker_host) {
33     if (!StartBroker())
34       return false;
35     broker_host = GetBrokerHost();
36   }
37   broker_host->LaunchLoader(loader_channel_id);
38 
39   return true;
40 }
41 
OnLoaderLaunched(const std::wstring & channel_id,base::ProcessHandle handle)42 void NaClBrokerService::OnLoaderLaunched(const std::wstring& channel_id,
43                                          base::ProcessHandle handle) {
44   NaClProcessHost* client;
45   PendingLaunchesMap::iterator it = pending_launches_.find(channel_id);
46   if (pending_launches_.end() == it)
47     NOTREACHED();
48 
49   client = it->second;
50   client->OnProcessLaunchedByBroker(handle);
51   pending_launches_.erase(it);
52   ++loaders_running_;
53 }
54 
OnLoaderDied()55 void NaClBrokerService::OnLoaderDied() {
56   --loaders_running_;
57   // Stop the broker only if there are no loaders running or being launched.
58   NaClBrokerHost* broker_host = GetBrokerHost();
59   if (loaders_running_ + pending_launches_.size() == 0 && broker_host != NULL) {
60     broker_host->StopBroker();
61   }
62 }
63 
GetBrokerHost()64 NaClBrokerHost* NaClBrokerService::GetBrokerHost() {
65   for (BrowserChildProcessHost::Iterator iter(
66            ChildProcessInfo::NACL_BROKER_PROCESS);
67        !iter.Done();
68        ++iter) {
69     NaClBrokerHost* broker_host = static_cast<NaClBrokerHost*>(*iter);
70     return broker_host;
71   }
72   return NULL;
73 }
74