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"
12 #include "base/files/file.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util_proxy.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/shared_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/process/process.h"
20 #include "components/nacl/common/nacl_types.h"
21 #include "content/public/browser/browser_child_process_host_delegate.h"
22 #include "content/public/browser/browser_child_process_host_iterator.h"
23 #include "ipc/ipc_channel_handle.h"
24 #include "net/socket/socket_descriptor.h"
25 #include "ppapi/shared_impl/ppapi_permissions.h"
29 class BrowserChildProcessHost
;
30 class BrowserPpapiHost
;
39 // NaClFileToken is a single-use nonce that the NaCl loader process can use
40 // to query the browser process for trusted information about a file. This
41 // helps establish that the file is known by the browser to be immutable
42 // and suitable for file-identity-based validation caching. lo == 0 && hi
43 // == 0 indicates the token is invalid and no additional information is
45 struct NaClFileToken
{
50 class NaClHostMessageFilter
;
51 void* AllocateAddressSpaceASLR(base::ProcessHandle process
, size_t size
);
53 // Represents the browser side of the browser <--> NaCl communication
54 // channel. There will be one NaClProcessHost per NaCl process
55 // The browser is responsible for starting the NaCl process
56 // when requested by the renderer.
57 // After that, most of the communication is directly between NaCl plugin
58 // running in the renderer and NaCl processes.
59 class NaClProcessHost
: public content::BrowserChildProcessHostDelegate
{
61 // manifest_url: the URL of the manifest of the Native Client plugin being
63 // nexe_file: A file that corresponds to the nexe module to be loaded.
64 // nexe_token: A cache validation token for nexe_file.
65 // prefetched_resource_files_info: An array of resource files prefetched.
66 // permissions: PPAPI permissions, to control access to private APIs.
67 // render_view_id: RenderView routing id, to control access to private APIs.
68 // permission_bits: controls which interfaces the NaCl plugin can use.
69 // uses_nonsfi_mode: whether the program should be loaded under non-SFI mode.
70 // off_the_record: was the process launched from an incognito renderer?
71 // process_type: the type of NaCl process.
72 // profile_directory: is the path of current profile directory.
74 const GURL
& manifest_url
,
76 const NaClFileToken
& nexe_token
,
77 const std::vector
<NaClResourcePrefetchResult
>& prefetched_resource_files
,
78 ppapi::PpapiPermissions permissions
,
80 uint32 permission_bits
,
81 bool uses_nonsfi_mode
,
83 NaClAppProcessType process_type
,
84 const base::FilePath
& profile_directory
);
85 ~NaClProcessHost() override
;
87 void OnProcessCrashed(int exit_status
) override
;
89 // Do any minimal work that must be done at browser startup.
90 static void EarlyStartup();
92 // Specifies throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
93 static void SetPpapiKeepAliveThrottleForTesting(unsigned milliseconds
);
95 // Initialize the new NaCl process. Result is returned by sending ipc
97 void Launch(NaClHostMessageFilter
* nacl_host_message_filter
,
98 IPC::Message
* reply_msg
,
99 const base::FilePath
& manifest_path
);
101 void OnChannelConnected(int32 peer_pid
) override
;
104 void OnProcessLaunchedByBroker(base::ProcessHandle handle
);
105 void OnDebugExceptionHandlerLaunchedByBroker(bool success
);
108 bool Send(IPC::Message
* msg
);
110 content::BrowserChildProcessHost
* process() { return process_
.get(); }
111 content::BrowserPpapiHost
* browser_ppapi_host() { return ppapi_host_
.get(); }
114 class ScopedChannelHandle
;
116 void LaunchNaClGdb();
118 // Mark the process as using a particular GDB debug stub port and notify
119 // listeners (if the port is not kGdbDebugStubPortUnknown).
120 void SetDebugStubPort(int port
);
122 #if defined(OS_POSIX)
123 // Create bound TCP socket in the browser process so that the NaCl GDB debug
124 // stub can use it to accept incoming connections even when the Chrome sandbox
126 net::SocketDescriptor
GetDebugStubSocketHandle();
130 // Called when the debug stub port has been selected.
131 void OnDebugStubPortSelected(uint16_t debug_stub_port
);
136 // BrowserChildProcessHostDelegate implementation:
137 bool OnMessageReceived(const IPC::Message
& msg
) override
;
138 void OnProcessLaunched() override
;
140 void OnResourcesReady();
142 // Enable the PPAPI proxy only for NaCl processes corresponding to a renderer.
143 bool enable_ppapi_proxy() { return render_view_id_
!= 0; }
145 // Sends the reply message to the renderer who is waiting for the plugin
146 // to load. Returns true on success.
147 void ReplyToRenderer(
148 ScopedChannelHandle ppapi_channel_handle
,
149 ScopedChannelHandle trusted_channel_handle
,
150 ScopedChannelHandle manifest_service_channel_handle
);
152 // Sends the reply with error message to the renderer.
153 void SendErrorToRenderer(const std::string
& error_message
);
155 // Sends the reply message to the renderer. Either result or
156 // error message must be empty.
157 void SendMessageToRenderer(const NaClLaunchResult
& result
,
158 const std::string
& error_message
);
160 // Sends the message to the NaCl process to load the plugin. Returns true
162 bool StartNaClExecution();
164 void StartNaClFileResolved(
165 NaClStartParams params
,
166 const base::FilePath
& file_path
,
167 base::File nexe_file
);
169 #if defined(OS_LINUX)
170 // Creates a pair of IPC::ChannelHandle. Returns true on success.
171 static bool CreateChannelHandlePair(ScopedChannelHandle
* channel_handle1
,
172 ScopedChannelHandle
* channel_handle2
);
175 // Starts browser PPAPI proxy. Returns true on success.
176 bool StartPPAPIProxy(ScopedChannelHandle channel_handle
);
178 // Does post-process-launching tasks for starting the NaCl process once
179 // we have a connection.
181 // Returns false on failure.
182 bool StartWithLaunchedProcess();
184 // Message handlers for validation caching.
185 void OnQueryKnownToValidate(const std::string
& signature
, bool* result
);
186 void OnSetKnownToValidate(const std::string
& signature
);
187 void OnResolveFileToken(uint64 file_token_lo
, uint64 file_token_hi
);
188 void FileResolved(uint64_t file_token_lo
,
189 uint64_t file_token_hi
,
190 const base::FilePath
& file_path
,
193 // Message handler for Windows hardware exception handling.
194 void OnAttachDebugExceptionHandler(const std::string
& info
,
195 IPC::Message
* reply_msg
);
196 bool AttachDebugExceptionHandler(const std::string
& info
,
197 IPC::Message
* reply_msg
);
200 // Called when the PPAPI IPC channels to the browser/renderer have been
202 void OnPpapiChannelsCreated(
203 const IPC::ChannelHandle
& ppapi_browser_channel_handle
,
204 const IPC::ChannelHandle
& ppapi_renderer_channel_handle
,
205 const IPC::ChannelHandle
& trusted_renderer_channel_handle
,
206 const IPC::ChannelHandle
& manifest_service_channel_handle
);
209 base::File nexe_file_
;
210 NaClFileToken nexe_token_
;
211 std::vector
<NaClResourcePrefetchResult
> prefetched_resource_files_
;
213 ppapi::PpapiPermissions permissions_
;
216 // This field becomes true when the broker successfully launched
218 bool process_launched_by_broker_
;
220 // The NaClHostMessageFilter that requested this NaCl process. We use
221 // this for sending the reply once the process has started.
222 scoped_refptr
<NaClHostMessageFilter
> nacl_host_message_filter_
;
224 // The reply message to send. We must always send this message when the
225 // sub-process either succeeds or fails to unblock the renderer waiting for
226 // the reply. NULL when there is no reply to send.
227 IPC::Message
* reply_msg_
;
229 bool debug_exception_handler_requested_
;
230 scoped_ptr
<IPC::Message
> attach_debug_exception_handler_reply_msg_
;
233 // The file path to the manifest is passed to nacl-gdb when it is used to
234 // debug the NaCl loader.
235 base::FilePath manifest_path_
;
237 scoped_ptr
<content::BrowserChildProcessHost
> process_
;
239 bool uses_nonsfi_mode_
;
241 bool enable_debug_stub_
;
242 bool enable_crash_throttling_
;
243 bool off_the_record_
;
244 NaClAppProcessType process_type_
;
246 const base::FilePath profile_directory_
;
248 // Channel proxy to terminate the NaCl-Browser PPAPI channel.
249 scoped_ptr
<IPC::ChannelProxy
> ipc_proxy_channel_
;
250 // Browser host for plugin process.
251 scoped_ptr
<content::BrowserPpapiHost
> ppapi_host_
;
255 // Throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
256 static unsigned keepalive_throttle_interval_milliseconds_
;
258 // Shared memory provided to the plugin and renderer for
259 // reporting crash information.
260 base::SharedMemory crash_info_shmem_
;
262 base::File socket_for_renderer_
;
263 base::File socket_for_sel_ldr_
;
265 base::WeakPtrFactory
<NaClProcessHost
> weak_factory_
;
267 DISALLOW_COPY_AND_ASSIGN(NaClProcessHost
);
272 #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_