OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/nacl/broker_thread.h" | 5 #include "chrome/nacl/nacl_broker_listener.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop.h" |
9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
10 #include "base/process_util.h" | 11 #include "base/process_util.h" |
11 #include "chrome/common/chrome_switches.h" | |
12 #include "chrome/common/nacl_cmd_line.h" | 12 #include "chrome/common/nacl_cmd_line.h" |
13 #include "chrome/common/nacl_messages.h" | 13 #include "chrome/common/nacl_messages.h" |
14 #include "content/common/child_process.h" | 14 #include "content/common/content_switches.h" |
15 #include "content/common/sandbox_policy.h" | 15 #include "content/common/sandbox_policy.h" |
16 #include "ipc/ipc_switches.h" | 16 #include "ipc/ipc_switches.h" |
17 | 17 |
18 NaClBrokerThread::NaClBrokerThread() | 18 NaClBrokerListener::NaClBrokerListener() |
19 : browser_handle_(0), | 19 : browser_handle_(base::kNullProcessHandle) { |
20 broker_services_(NULL) { | |
21 } | 20 } |
22 | 21 |
23 NaClBrokerThread::~NaClBrokerThread() { | 22 NaClBrokerListener::~NaClBrokerListener() { |
24 base::CloseProcessHandle(browser_handle_); | 23 base::CloseProcessHandle(browser_handle_); |
25 } | 24 } |
26 | 25 |
27 NaClBrokerThread* NaClBrokerThread::current() { | 26 void NaClBrokerListener::Listen() { |
28 return static_cast<NaClBrokerThread*>(ChildThread::current()); | 27 std::string channel_name = |
| 28 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 29 switches::kProcessChannelID); |
| 30 channel_.reset(new IPC::Channel( |
| 31 channel_name, IPC::Channel::MODE_CLIENT, this)); |
| 32 CHECK(channel_->Connect()); |
| 33 MessageLoop::current()->Run(); |
29 } | 34 } |
30 | 35 |
31 bool NaClBrokerThread::OnControlMessageReceived(const IPC::Message& msg) { | 36 void NaClBrokerListener::OnChannelConnected(int32 peer_pid) { |
| 37 bool res = base::OpenProcessHandle(peer_pid, &browser_handle_); |
| 38 CHECK(res); |
| 39 } |
| 40 |
| 41 bool NaClBrokerListener::OnMessageReceived(const IPC::Message& msg) { |
32 bool handled = true; | 42 bool handled = true; |
33 IPC_BEGIN_MESSAGE_MAP(NaClBrokerThread, msg) | 43 IPC_BEGIN_MESSAGE_MAP(NaClBrokerListener, msg) |
34 IPC_MESSAGE_HANDLER(NaClProcessMsg_LaunchLoaderThroughBroker, | 44 IPC_MESSAGE_HANDLER(NaClProcessMsg_LaunchLoaderThroughBroker, |
35 OnLaunchLoaderThroughBroker) | 45 OnLaunchLoaderThroughBroker) |
36 IPC_MESSAGE_HANDLER(NaClProcessMsg_StopBroker, OnStopBroker) | 46 IPC_MESSAGE_HANDLER(NaClProcessMsg_StopBroker, OnStopBroker) |
37 IPC_MESSAGE_UNHANDLED(handled = false) | 47 IPC_MESSAGE_UNHANDLED(handled = false) |
38 IPC_END_MESSAGE_MAP() | 48 IPC_END_MESSAGE_MAP() |
39 return handled; | 49 return handled; |
40 } | 50 } |
41 | 51 |
42 void NaClBrokerThread::OnLaunchLoaderThroughBroker( | 52 void NaClBrokerListener::OnLaunchLoaderThroughBroker( |
43 const std::wstring& loader_channel_id) { | 53 const std::wstring& loader_channel_id) { |
44 base::ProcessHandle loader_process = 0; | 54 base::ProcessHandle loader_process = 0; |
45 base::ProcessHandle loader_handle_in_browser = 0; | 55 base::ProcessHandle loader_handle_in_browser = 0; |
46 | 56 |
47 // Create the path to the nacl broker/loader executable - it's the executable | 57 // Create the path to the nacl broker/loader executable - it's the executable |
48 // this code is running in. | 58 // this code is running in. |
49 FilePath exe_path; | 59 FilePath exe_path; |
50 PathService::Get(base::FILE_EXE, &exe_path); | 60 PathService::Get(base::FILE_EXE, &exe_path); |
51 if (!exe_path.empty()) { | 61 if (!exe_path.empty()) { |
52 CommandLine* cmd_line = new CommandLine(exe_path); | 62 CommandLine* cmd_line = new CommandLine(exe_path); |
53 nacl::CopyNaClCommandLineArguments(cmd_line); | 63 nacl::CopyNaClCommandLineArguments(cmd_line); |
54 | 64 |
55 cmd_line->AppendSwitchASCII(switches::kProcessType, | 65 cmd_line->AppendSwitchASCII(switches::kProcessType, |
56 switches::kNaClLoaderProcess); | 66 switches::kNaClLoaderProcess); |
57 | 67 |
58 // TODO(evanm): remove needless usage of wstring for channel id. | 68 // TODO(evanm): remove needless usage of wstring for channel id. |
59 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, | 69 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, |
60 WideToASCII(loader_channel_id)); | 70 WideToASCII(loader_channel_id)); |
61 | 71 |
62 loader_process = sandbox::StartProcessWithAccess(cmd_line, FilePath()); | 72 loader_process = sandbox::StartProcessWithAccess(cmd_line, FilePath()); |
63 if (loader_process) { | 73 if (loader_process) { |
64 DuplicateHandle(::GetCurrentProcess(), loader_process, | 74 DuplicateHandle(::GetCurrentProcess(), loader_process, |
65 browser_handle_, &loader_handle_in_browser, | 75 browser_handle_, &loader_handle_in_browser, |
66 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION , FALSE, 0); | 76 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION , FALSE, 0); |
67 } | 77 } |
68 } | 78 } |
69 Send(new NaClProcessMsg_LoaderLaunched(loader_channel_id, | 79 channel_->Send(new NaClProcessMsg_LoaderLaunched(loader_channel_id, |
70 loader_handle_in_browser)); | 80 loader_handle_in_browser)); |
71 } | 81 } |
72 | 82 |
73 void NaClBrokerThread::OnStopBroker() { | 83 void NaClBrokerListener::OnStopBroker() { |
74 ChildProcess::current()->ReleaseProcess(); | 84 MessageLoop::current()->Quit(); |
75 } | 85 } |
76 | |
77 void NaClBrokerThread::OnChannelConnected(int32 peer_pid) { | |
78 bool res = base::OpenProcessHandle(peer_pid, &browser_handle_); | |
79 DCHECK(res); | |
80 ChildProcess::current()->AddRefProcess(); | |
81 } | |
82 | |
OLD | NEW |