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 #ifndef COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
6 #define COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
8 #include "build/build_config.h"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util_proxy.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/process/process.h"
18 #include "components/nacl/common/nacl_types.h"
19 #include "content/public/browser/browser_child_process_host_delegate.h"
20 #include "content/public/browser/browser_child_process_host_iterator.h"
21 #include "ipc/ipc_channel_handle.h"
22 #include "net/socket/socket_descriptor.h"
23 #include "ppapi/shared_impl/ppapi_permissions.h"
27 class BrowserChildProcessHost
;
28 class BrowserPpapiHost
;
37 // NaClFileToken is a single-use nonce that the NaCl loader process can use
38 // to query the browser process for trusted information about a file. This
39 // helps establish that the file is known by the browser to be immutable
40 // and suitable for file-identity-based validation caching. lo == 0 && hi
41 // == 0 indicates the token is invalid and no additional information is
43 struct NaClFileToken
{
48 class NaClHostMessageFilter
;
49 void* AllocateAddressSpaceASLR(base::ProcessHandle process
, size_t size
);
51 // Represents the browser side of the browser <--> NaCl communication
52 // channel. There will be one NaClProcessHost per NaCl process
53 // The browser is responsible for starting the NaCl process
54 // when requested by the renderer.
55 // After that, most of the communication is directly between NaCl plugin
56 // running in the renderer and NaCl processes.
57 class NaClProcessHost
: public content::BrowserChildProcessHostDelegate
{
59 // manifest_url: the URL of the manifest of the Native Client plugin being
61 // nexe_file: A file that corresponds to the nexe module to be loaded.
62 // nexe_token: A cache validation token for nexe_file.
63 // permissions: PPAPI permissions, to control access to private APIs.
64 // render_view_id: RenderView routing id, to control access to private APIs.
65 // permission_bits: controls which interfaces the NaCl plugin can use.
66 // uses_nonsfi_mode: whether the program should be loaded under non-SFI mode.
67 // off_the_record: was the process launched from an incognito renderer?
68 // process_type: the type of NaCl process.
69 // profile_directory: is the path of current profile directory.
70 NaClProcessHost(const GURL
& manifest_url
,
72 const NaClFileToken
& nexe_token
,
73 ppapi::PpapiPermissions permissions
,
75 uint32 permission_bits
,
76 bool uses_nonsfi_mode
,
78 NaClAppProcessType process_type
,
79 const base::FilePath
& profile_directory
);
80 ~NaClProcessHost() override
;
82 void OnProcessCrashed(int exit_status
) override
;
84 // Do any minimal work that must be done at browser startup.
85 static void EarlyStartup();
87 // Specifies throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
88 static void SetPpapiKeepAliveThrottleForTesting(unsigned milliseconds
);
90 // Initialize the new NaCl process. Result is returned by sending ipc
92 void Launch(NaClHostMessageFilter
* nacl_host_message_filter
,
93 IPC::Message
* reply_msg
,
94 const base::FilePath
& manifest_path
);
96 void OnChannelConnected(int32 peer_pid
) override
;
99 void OnProcessLaunchedByBroker(base::ProcessHandle handle
);
100 void OnDebugExceptionHandlerLaunchedByBroker(bool success
);
103 bool Send(IPC::Message
* msg
);
105 content::BrowserChildProcessHost
* process() { return process_
.get(); }
106 content::BrowserPpapiHost
* browser_ppapi_host() { return ppapi_host_
.get(); }
109 void LaunchNaClGdb();
111 // Mark the process as using a particular GDB debug stub port and notify
112 // listeners (if the port is not kGdbDebugStubPortUnknown).
113 void SetDebugStubPort(int port
);
115 #if defined(OS_POSIX)
116 // Create bound TCP socket in the browser process so that the NaCl GDB debug
117 // stub can use it to accept incoming connections even when the Chrome sandbox
119 net::SocketDescriptor
GetDebugStubSocketHandle();
123 // Called when the debug stub port has been selected.
124 void OnDebugStubPortSelected(uint16_t debug_stub_port
);
129 // BrowserChildProcessHostDelegate implementation:
130 bool OnMessageReceived(const IPC::Message
& msg
) override
;
131 void OnProcessLaunched() override
;
133 void OnResourcesReady();
135 // Enable the PPAPI proxy only for NaCl processes corresponding to a renderer.
136 bool enable_ppapi_proxy() { return render_view_id_
!= 0; }
138 // Sends the reply message to the renderer who is waiting for the plugin
139 // to load. Returns true on success.
140 bool ReplyToRenderer(
141 const IPC::ChannelHandle
& ppapi_channel_handle
,
142 const IPC::ChannelHandle
& trusted_channel_handle
,
143 const IPC::ChannelHandle
& manifest_service_channel_handle
);
145 // Sends the reply with error message to the renderer.
146 void SendErrorToRenderer(const std::string
& error_message
);
148 // Sends the reply message to the renderer. Either result or
149 // error message must be empty.
150 void SendMessageToRenderer(const NaClLaunchResult
& result
,
151 const std::string
& error_message
);
153 // Sends the message to the NaCl process to load the plugin. Returns true
155 bool StartNaClExecution();
157 void StartNaClFileResolved(
158 NaClStartParams params
,
159 const base::FilePath
& file_path
,
160 base::File nexe_file
);
162 // Does post-process-launching tasks for starting the NaCl process once
163 // we have a connection.
165 // Returns false on failure.
166 bool StartWithLaunchedProcess();
168 // Message handlers for validation caching.
169 void OnQueryKnownToValidate(const std::string
& signature
, bool* result
);
170 void OnSetKnownToValidate(const std::string
& signature
);
171 void OnResolveFileToken(uint64 file_token_lo
, uint64 file_token_hi
);
172 void FileResolved(uint64_t file_token_lo
,
173 uint64_t file_token_hi
,
174 const base::FilePath
& file_path
,
177 // Message handler for Windows hardware exception handling.
178 void OnAttachDebugExceptionHandler(const std::string
& info
,
179 IPC::Message
* reply_msg
);
180 bool AttachDebugExceptionHandler(const std::string
& info
,
181 IPC::Message
* reply_msg
);
184 // Called when the PPAPI IPC channels to the browser/renderer have been
186 void OnPpapiChannelsCreated(
187 const IPC::ChannelHandle
& browser_channel_handle
,
188 const IPC::ChannelHandle
& ppapi_renderer_channel_handle
,
189 const IPC::ChannelHandle
& trusted_renderer_channel_handle
,
190 const IPC::ChannelHandle
& manifest_service_channel_handle
);
193 base::File nexe_file_
;
194 NaClFileToken nexe_token_
;
196 ppapi::PpapiPermissions permissions_
;
199 // This field becomes true when the broker successfully launched
201 bool process_launched_by_broker_
;
203 // The NaClHostMessageFilter that requested this NaCl process. We use
204 // this for sending the reply once the process has started.
205 scoped_refptr
<NaClHostMessageFilter
> nacl_host_message_filter_
;
207 // The reply message to send. We must always send this message when the
208 // sub-process either succeeds or fails to unblock the renderer waiting for
209 // the reply. NULL when there is no reply to send.
210 IPC::Message
* reply_msg_
;
212 bool debug_exception_handler_requested_
;
213 scoped_ptr
<IPC::Message
> attach_debug_exception_handler_reply_msg_
;
216 // The file path to the manifest is passed to nacl-gdb when it is used to
217 // debug the NaCl loader.
218 base::FilePath manifest_path_
;
220 scoped_ptr
<content::BrowserChildProcessHost
> process_
;
222 bool uses_nonsfi_mode_
;
224 bool enable_debug_stub_
;
225 bool enable_crash_throttling_
;
226 bool off_the_record_
;
227 NaClAppProcessType process_type_
;
229 const base::FilePath profile_directory_
;
231 // Channel proxy to terminate the NaCl-Browser PPAPI channel.
232 scoped_ptr
<IPC::ChannelProxy
> ipc_proxy_channel_
;
233 // Browser host for plugin process.
234 scoped_ptr
<content::BrowserPpapiHost
> ppapi_host_
;
238 // Throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
239 static unsigned keepalive_throttle_interval_milliseconds_
;
241 // Shared memory provided to the plugin and renderer for
242 // reporting crash information.
243 base::SharedMemory crash_info_shmem_
;
245 base::File socket_for_renderer_
;
246 base::File socket_for_sel_ldr_
;
248 base::WeakPtrFactory
<NaClProcessHost
> weak_factory_
;
250 DISALLOW_COPY_AND_ASSIGN(NaClProcessHost
);
255 #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_