Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / nacl / browser / nacl_broker_host_win.cc
blob7c14f7c56996d031afa7dcadce684e4b4c7cbce2
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_host_win.h"
7 #include "base/base_switches.h"
8 #include "base/command_line.h"
9 #include "components/nacl/browser/nacl_broker_service_win.h"
10 #include "components/nacl/browser/nacl_browser.h"
11 #include "components/nacl/common/nacl_cmd_line.h"
12 #include "components/nacl/common/nacl_messages.h"
13 #include "components/nacl/common/nacl_process_type.h"
14 #include "components/nacl/common/nacl_switches.h"
15 #include "content/public/browser/browser_child_process_host.h"
16 #include "content/public/browser/child_process_data.h"
17 #include "content/public/common/child_process_host.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/sandboxed_process_launcher_delegate.h"
20 #include "ipc/ipc_switches.h"
22 namespace {
23 // NOTE: changes to this class need to be reviewed by the security team.
24 class NaClBrokerSandboxedProcessLauncherDelegate
25 : public content::SandboxedProcessLauncherDelegate {
26 public:
27 NaClBrokerSandboxedProcessLauncherDelegate() {}
28 ~NaClBrokerSandboxedProcessLauncherDelegate() override {}
30 bool ShouldSandbox() override {
31 return false;
34 private:
35 DISALLOW_COPY_AND_ASSIGN(NaClBrokerSandboxedProcessLauncherDelegate);
37 } // namespace
39 namespace nacl {
41 NaClBrokerHost::NaClBrokerHost() : is_terminating_(false) {
42 process_.reset(content::BrowserChildProcessHost::Create(
43 static_cast<content::ProcessType>(PROCESS_TYPE_NACL_BROKER), this));
46 NaClBrokerHost::~NaClBrokerHost() {
49 bool NaClBrokerHost::Init() {
50 // Create the channel that will be used for communicating with the broker.
51 std::string channel_id = process_->GetHost()->CreateChannel();
52 if (channel_id.empty())
53 return false;
55 // Create the path to the nacl broker/loader executable.
56 base::FilePath nacl_path;
57 if (!NaClBrowser::GetInstance()->GetNaCl64ExePath(&nacl_path))
58 return false;
60 base::CommandLine* cmd_line = new base::CommandLine(nacl_path);
61 CopyNaClCommandLineArguments(cmd_line);
63 cmd_line->AppendSwitchASCII(switches::kProcessType,
64 switches::kNaClBrokerProcess);
65 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
66 if (NaClBrowser::GetDelegate()->DialogsAreSuppressed())
67 cmd_line->AppendSwitch(switches::kNoErrorDialogs);
69 process_->Launch(new NaClBrokerSandboxedProcessLauncherDelegate,
70 cmd_line,
71 true);
72 return true;
75 bool NaClBrokerHost::OnMessageReceived(const IPC::Message& msg) {
76 bool handled = true;
77 IPC_BEGIN_MESSAGE_MAP(NaClBrokerHost, msg)
78 IPC_MESSAGE_HANDLER(NaClProcessMsg_LoaderLaunched, OnLoaderLaunched)
79 IPC_MESSAGE_HANDLER(NaClProcessMsg_DebugExceptionHandlerLaunched,
80 OnDebugExceptionHandlerLaunched)
81 IPC_MESSAGE_UNHANDLED(handled = false)
82 IPC_END_MESSAGE_MAP()
83 return handled;
86 bool NaClBrokerHost::LaunchLoader(const std::string& loader_channel_id) {
87 return process_->Send(
88 new NaClProcessMsg_LaunchLoaderThroughBroker(loader_channel_id));
91 void NaClBrokerHost::OnLoaderLaunched(const std::string& loader_channel_id,
92 base::ProcessHandle handle) {
93 NaClBrokerService::GetInstance()->OnLoaderLaunched(loader_channel_id, handle);
96 bool NaClBrokerHost::LaunchDebugExceptionHandler(
97 int32 pid, base::ProcessHandle process_handle,
98 const std::string& startup_info) {
99 base::ProcessHandle broker_process = process_->GetData().handle;
100 base::ProcessHandle handle_in_broker_process;
101 if (!DuplicateHandle(::GetCurrentProcess(), process_handle,
102 broker_process, &handle_in_broker_process,
103 0, /* bInheritHandle= */ FALSE, DUPLICATE_SAME_ACCESS))
104 return false;
105 return process_->Send(new NaClProcessMsg_LaunchDebugExceptionHandler(
106 pid, handle_in_broker_process, startup_info));
109 void NaClBrokerHost::OnDebugExceptionHandlerLaunched(int32 pid, bool success) {
110 NaClBrokerService::GetInstance()->OnDebugExceptionHandlerLaunched(pid,
111 success);
114 void NaClBrokerHost::StopBroker() {
115 is_terminating_ = true;
116 process_->Send(new NaClProcessMsg_StopBroker());
119 } // namespace nacl