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