1 // Copyright (c) 2012 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.
5 #include "components/nacl/browser/nacl_broker_service_win.h"
7 #include "components/nacl/browser/nacl_process_host.h"
8 #include "components/nacl/common/nacl_process_type.h"
9 #include "content/public/browser/browser_child_process_host_iterator.h"
11 using content::BrowserChildProcessHostIterator
;
15 NaClBrokerService
* NaClBrokerService::GetInstance() {
16 return base::Singleton
<NaClBrokerService
>::get();
19 NaClBrokerService::NaClBrokerService()
20 : loaders_running_(0) {
23 NaClBrokerService::~NaClBrokerService() {
26 bool NaClBrokerService::StartBroker() {
27 NaClBrokerHost
* broker_host
= new NaClBrokerHost
;
28 if (!broker_host
->Init()) {
35 bool NaClBrokerService::LaunchLoader(
36 base::WeakPtr
<nacl::NaClProcessHost
> nacl_process_host
,
37 const std::string
& loader_channel_id
) {
38 // Add task to the list
39 pending_launches_
[loader_channel_id
] = nacl_process_host
;
40 NaClBrokerHost
* broker_host
= GetBrokerHost();
45 broker_host
= GetBrokerHost();
47 broker_host
->LaunchLoader(loader_channel_id
);
52 void NaClBrokerService::OnLoaderLaunched(const std::string
& channel_id
,
53 base::ProcessHandle handle
) {
54 PendingLaunchesMap::iterator it
= pending_launches_
.find(channel_id
);
55 if (pending_launches_
.end() == it
)
58 NaClProcessHost
* client
= it
->second
.get();
60 client
->OnProcessLaunchedByBroker(handle
);
61 pending_launches_
.erase(it
);
65 void NaClBrokerService::OnLoaderDied() {
66 DCHECK(loaders_running_
> 0);
68 // Stop the broker only if there are no loaders running or being launched.
69 NaClBrokerHost
* broker_host
= GetBrokerHost();
70 if (loaders_running_
+ pending_launches_
.size() == 0 && broker_host
!= NULL
) {
71 broker_host
->StopBroker();
75 bool NaClBrokerService::LaunchDebugExceptionHandler(
76 base::WeakPtr
<NaClProcessHost
> nacl_process_host
, int32 pid
,
77 base::ProcessHandle process_handle
, const std::string
& startup_info
) {
78 pending_debuggers_
[pid
] = nacl_process_host
;
79 NaClBrokerHost
* broker_host
= GetBrokerHost();
82 return broker_host
->LaunchDebugExceptionHandler(pid
, process_handle
,
86 void NaClBrokerService::OnDebugExceptionHandlerLaunched(int32 pid
,
88 PendingDebugExceptionHandlersMap::iterator it
= pending_debuggers_
.find(pid
);
89 if (pending_debuggers_
.end() == it
)
92 NaClProcessHost
* client
= it
->second
.get();
94 client
->OnDebugExceptionHandlerLaunchedByBroker(success
);
95 pending_debuggers_
.erase(it
);
98 NaClBrokerHost
* NaClBrokerService::GetBrokerHost() {
99 BrowserChildProcessHostIterator
iter(PROCESS_TYPE_NACL_BROKER
);
100 while (!iter
.Done()) {
101 NaClBrokerHost
* host
= static_cast<NaClBrokerHost
*>(iter
.GetDelegate());
102 if (!host
->IsTerminating())