blob: af23be1c37bab40abf5ba951896b7fb02d97e939 [file] [log] [blame]
[email protected]54e158ef2012-03-11 02:10:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]103607e2010-02-01 18:57:092// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7c766e92013-11-22 20:44:025#include "components/nacl/browser/nacl_broker_service_win.h"
[email protected]103607e2010-02-01 18:57:096
[email protected]7c766e92013-11-22 20:44:027#include "components/nacl/browser/nacl_process_host.h"
[email protected]d5d383252013-07-04 14:44:328#include "components/nacl/common/nacl_process_type.h"
[email protected]4967f792012-01-20 22:14:409#include "content/public/browser/browser_child_process_host_iterator.h"
10
11using content::BrowserChildProcessHostIterator;
[email protected]103607e2010-02-01 18:57:0912
[email protected]7c766e92013-11-22 20:44:0213namespace nacl {
14
[email protected]103607e2010-02-01 18:57:0915NaClBrokerService* NaClBrokerService::GetInstance() {
16 return Singleton<NaClBrokerService>::get();
17}
18
19NaClBrokerService::NaClBrokerService()
[email protected]1dbaaa702011-04-06 17:43:4220 : loaders_running_(0) {
[email protected]103607e2010-02-01 18:57:0921}
22
23bool NaClBrokerService::StartBroker() {
[email protected]1dbaaa702011-04-06 17:43:4224 NaClBrokerHost* broker_host = new NaClBrokerHost;
[email protected]2a4d7542010-03-17 02:06:3325 if (!broker_host->Init()) {
26 delete broker_host;
27 return false;
[email protected]103607e2010-02-01 18:57:0928 }
[email protected]2a4d7542010-03-17 02:06:3329 return true;
[email protected]103607e2010-02-01 18:57:0930}
31
[email protected]26b3c002012-04-26 19:38:3432bool NaClBrokerService::LaunchLoader(
[email protected]7c766e92013-11-22 20:44:0233 base::WeakPtr<nacl::NaClProcessHost> nacl_process_host,
[email protected]26b3c002012-04-26 19:38:3434 const std::string& loader_channel_id) {
[email protected]c3f678fc2010-02-22 21:56:0035 // Add task to the list
36 pending_launches_[loader_channel_id] = nacl_process_host;
[email protected]2a4d7542010-03-17 02:06:3337 NaClBrokerHost* broker_host = GetBrokerHost();
38
39 if (!broker_host) {
40 if (!StartBroker())
41 return false;
42 broker_host = GetBrokerHost();
[email protected]103607e2010-02-01 18:57:0943 }
[email protected]2a4d7542010-03-17 02:06:3344 broker_host->LaunchLoader(loader_channel_id);
45
[email protected]103607e2010-02-01 18:57:0946 return true;
47}
48
[email protected]644b2e22012-03-25 01:41:0749void NaClBrokerService::OnLoaderLaunched(const std::string& channel_id,
[email protected]103607e2010-02-01 18:57:0950 base::ProcessHandle handle) {
[email protected]103607e2010-02-01 18:57:0951 PendingLaunchesMap::iterator it = pending_launches_.find(channel_id);
[email protected]16e70ae2010-03-08 21:41:2852 if (pending_launches_.end() == it)
[email protected]103607e2010-02-01 18:57:0953 NOTREACHED();
[email protected]16e70ae2010-03-08 21:41:2854
[email protected]f4605dc2013-06-05 13:07:1155 NaClProcessHost* client = it->second.get();
[email protected]26b3c002012-04-26 19:38:3456 if (client)
57 client->OnProcessLaunchedByBroker(handle);
[email protected]103607e2010-02-01 18:57:0958 pending_launches_.erase(it);
[email protected]16e70ae2010-03-08 21:41:2859 ++loaders_running_;
60}
61
62void NaClBrokerService::OnLoaderDied() {
[email protected]54e158ef2012-03-11 02:10:4863 DCHECK(loaders_running_ > 0);
[email protected]16e70ae2010-03-08 21:41:2864 --loaders_running_;
65 // Stop the broker only if there are no loaders running or being launched.
[email protected]2a4d7542010-03-17 02:06:3366 NaClBrokerHost* broker_host = GetBrokerHost();
67 if (loaders_running_ + pending_launches_.size() == 0 && broker_host != NULL) {
68 broker_host->StopBroker();
[email protected]16e70ae2010-03-08 21:41:2869 }
70}
71
[email protected]fb335fe2012-03-24 18:11:3872bool NaClBrokerService::LaunchDebugExceptionHandler(
[email protected]4c65fb632012-04-27 00:42:2573 base::WeakPtr<NaClProcessHost> nacl_process_host, int32 pid,
[email protected]fd37bf62012-05-04 16:46:4874 base::ProcessHandle process_handle, const std::string& startup_info) {
[email protected]fb335fe2012-03-24 18:11:3875 pending_debuggers_[pid] = nacl_process_host;
76 NaClBrokerHost* broker_host = GetBrokerHost();
77 if (!broker_host)
78 return false;
[email protected]fd37bf62012-05-04 16:46:4879 return broker_host->LaunchDebugExceptionHandler(pid, process_handle,
80 startup_info);
[email protected]fb335fe2012-03-24 18:11:3881}
82
[email protected]ea6588842012-05-03 05:39:3883void NaClBrokerService::OnDebugExceptionHandlerLaunched(int32 pid,
84 bool success) {
[email protected]fb335fe2012-03-24 18:11:3885 PendingDebugExceptionHandlersMap::iterator it = pending_debuggers_.find(pid);
86 if (pending_debuggers_.end() == it)
87 NOTREACHED();
88
[email protected]f4605dc2013-06-05 13:07:1189 NaClProcessHost* client = it->second.get();
[email protected]26b3c002012-04-26 19:38:3490 if (client)
[email protected]ea6588842012-05-03 05:39:3891 client->OnDebugExceptionHandlerLaunchedByBroker(success);
[email protected]fb335fe2012-03-24 18:11:3892 pending_debuggers_.erase(it);
93}
94
[email protected]2a4d7542010-03-17 02:06:3395NaClBrokerHost* NaClBrokerService::GetBrokerHost() {
[email protected]f3b357692013-03-22 05:16:1396 BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_BROKER);
[email protected]15c532e2012-06-15 00:39:5097 while (!iter.Done()) {
[email protected]7c766e92013-11-22 20:44:0298 NaClBrokerHost* host = static_cast<NaClBrokerHost*>(
99 iter.GetDelegate());
[email protected]15c532e2012-06-15 00:39:50100 if (!host->IsTerminating())
101 return host;
102 ++iter;
103 }
104 return NULL;
[email protected]103607e2010-02-01 18:57:09105}
[email protected]7c766e92013-11-22 20:44:02106
107} // namespace nacl