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_host_win.h"
6
7#include "base/command_line.h"
8#include "base/path_service.h"
9#include "ipc/ipc_switches.h"
10#include "chrome/browser/nacl_host/nacl_broker_service_win.h"
11#include "chrome/browser/nacl_host/nacl_process_host.h"
12#include "chrome/common/chrome_constants.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/nacl_cmd_line.h"
15#include "chrome/common/nacl_messages.h"
16
17NaClBrokerHost::NaClBrokerHost()
18    : BrowserChildProcessHost(NACL_BROKER_PROCESS),
19      stopping_(false) {
20}
21
22NaClBrokerHost::~NaClBrokerHost() {
23}
24
25bool NaClBrokerHost::Init() {
26  // Create the channel that will be used for communicating with the broker.
27  if (!CreateChannel())
28    return false;
29
30  // Create the path to the nacl broker/loader executable.
31  FilePath module_path;
32  if (!PathService::Get(base::FILE_MODULE, &module_path))
33    return false;
34
35  FilePath nacl_path = module_path.DirName().Append(chrome::kNaClAppName);
36  CommandLine* cmd_line = new CommandLine(nacl_path);
37  nacl::CopyNaClCommandLineArguments(cmd_line);
38
39  cmd_line->AppendSwitchASCII(switches::kProcessType,
40                              switches::kNaClBrokerProcess);
41
42  cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id());
43
44  BrowserChildProcessHost::Launch(FilePath(), cmd_line);
45  return true;
46}
47
48bool NaClBrokerHost::OnMessageReceived(const IPC::Message& msg) {
49  bool handled = true;
50  IPC_BEGIN_MESSAGE_MAP(NaClBrokerHost, msg)
51    IPC_MESSAGE_HANDLER(NaClProcessMsg_LoaderLaunched, OnLoaderLaunched)
52    IPC_MESSAGE_UNHANDLED(handled = false)
53  IPC_END_MESSAGE_MAP()
54  return handled;
55}
56
57bool NaClBrokerHost::LaunchLoader(
58    const std::wstring& loader_channel_id) {
59  return Send(new NaClProcessMsg_LaunchLoaderThroughBroker(loader_channel_id));
60}
61
62void NaClBrokerHost::OnLoaderLaunched(const std::wstring& loader_channel_id,
63                                      base::ProcessHandle handle) {
64  NaClBrokerService::GetInstance()->OnLoaderLaunched(loader_channel_id, handle);
65}
66
67void NaClBrokerHost::StopBroker() {
68  stopping_ = true;
69  Send(new NaClProcessMsg_StopBroker());
70}
71