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 "native_client/src/public/nacl_file_info.h"
23 #include "net/socket/socket_descriptor.h"
24 #include "ppapi/shared_impl/ppapi_permissions.h"
28 class BrowserChildProcessHost
;
29 class BrowserPpapiHost
;
38 class NaClHostMessageFilter
;
39 void* AllocateAddressSpaceASLR(base::ProcessHandle process
, size_t size
);
41 // Represents the browser side of the browser <--> NaCl communication
42 // channel. There will be one NaClProcessHost per NaCl process
43 // The browser is responsible for starting the NaCl process
44 // when requested by the renderer.
45 // After that, most of the communication is directly between NaCl plugin
46 // running in the renderer and NaCl processes.
47 class NaClProcessHost
: public content::BrowserChildProcessHostDelegate
{
49 // manifest_url: the URL of the manifest of the Native Client plugin being
51 // nexe_file: A file that corresponds to the nexe module to be loaded.
52 // nexe_token: A cache validation token for nexe_file.
53 // permissions: PPAPI permissions, to control access to private APIs.
54 // render_view_id: RenderView routing id, to control access to private APIs.
55 // permission_bits: controls which interfaces the NaCl plugin can use.
56 // uses_irt: whether the launched process should use the IRT.
57 // uses_nonsfi_mode: whether the program should be loaded under non-SFI mode.
58 // enable_dyncode_syscalls: whether the launched process should allow dyncode
59 // and mmap with PROT_EXEC.
60 // enable_exception_handling: whether the launched process should allow
61 // hardware exception handling.
62 // enable_crash_throttling: whether a crash of this process contributes
63 // to the crash throttling statistics, and also
64 // whether this process should not start when too
65 // many crashes have been observed.
66 // off_the_record: was the process launched from an incognito renderer?
67 // profile_directory: is the path of current profile directory.
68 NaClProcessHost(const GURL
& manifest_url
,
70 const NaClFileToken
& nexe_token
,
71 ppapi::PpapiPermissions permissions
,
73 uint32 permission_bits
,
75 bool uses_nonsfi_mode
,
76 bool enable_dyncode_syscalls
,
77 bool enable_exception_handling
,
78 bool enable_crash_throttling
,
80 const base::FilePath
& profile_directory
);
81 virtual ~NaClProcessHost();
83 virtual void OnProcessCrashed(int exit_status
) OVERRIDE
;
85 // Do any minimal work that must be done at browser startup.
86 static void EarlyStartup();
88 // Specifies throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
89 static void SetPpapiKeepAliveThrottleForTesting(unsigned milliseconds
);
91 // Initialize the new NaCl process. Result is returned by sending ipc
93 void Launch(NaClHostMessageFilter
* nacl_host_message_filter
,
94 IPC::Message
* reply_msg
,
95 const base::FilePath
& manifest_path
);
97 virtual void OnChannelConnected(int32 peer_pid
) OVERRIDE
;
100 void OnProcessLaunchedByBroker(base::ProcessHandle handle
);
101 void OnDebugExceptionHandlerLaunchedByBroker(bool success
);
104 bool Send(IPC::Message
* msg
);
106 content::BrowserChildProcessHost
* process() { return process_
.get(); }
107 content::BrowserPpapiHost
* browser_ppapi_host() { return ppapi_host_
.get(); }
110 // Internal class that holds the NaClHandle objecs so that
111 // nacl_process_host.h doesn't include NaCl headers. Needed since it's
112 // included by src\content, which can't depend on the NaCl gyp file because it
113 // depends on chrome.gyp (circular dependency).
116 bool 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 virtual bool OnMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
138 virtual 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 bool ReplyToRenderer(
148 const IPC::ChannelHandle
& ppapi_channel_handle
,
149 const IPC::ChannelHandle
& trusted_channel_handle
,
150 const IPC::ChannelHandle
& 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 // Does post-process-launching tasks for starting the NaCl process once
165 // we have a connection.
167 // Returns false on failure.
168 bool StartWithLaunchedProcess();
170 // Message handlers for validation caching.
171 void OnQueryKnownToValidate(const std::string
& signature
, bool* result
);
172 void OnSetKnownToValidate(const std::string
& signature
);
173 void OnResolveFileToken(uint64 file_token_lo
, uint64 file_token_hi
,
174 IPC::Message
* reply_msg
);
175 void FileResolved(const base::FilePath
& file_path
,
176 IPC::Message
* reply_msg
,
180 // Message handler for Windows hardware exception handling.
181 void OnAttachDebugExceptionHandler(const std::string
& info
,
182 IPC::Message
* reply_msg
);
183 bool AttachDebugExceptionHandler(const std::string
& info
,
184 IPC::Message
* reply_msg
);
187 // Called when the PPAPI IPC channels to the browser/renderer have been
189 void OnPpapiChannelsCreated(
190 const IPC::ChannelHandle
& browser_channel_handle
,
191 const IPC::ChannelHandle
& ppapi_renderer_channel_handle
,
192 const IPC::ChannelHandle
& trusted_renderer_channel_handle
,
193 const IPC::ChannelHandle
& manifest_service_channel_handle
);
196 base::File nexe_file_
;
197 NaClFileToken nexe_token_
;
199 ppapi::PpapiPermissions permissions_
;
202 // This field becomes true when the broker successfully launched
204 bool process_launched_by_broker_
;
206 // The NaClHostMessageFilter that requested this NaCl process. We use
207 // this for sending the reply once the process has started.
208 scoped_refptr
<NaClHostMessageFilter
> nacl_host_message_filter_
;
210 // The reply message to send. We must always send this message when the
211 // sub-process either succeeds or fails to unblock the renderer waiting for
212 // the reply. NULL when there is no reply to send.
213 IPC::Message
* reply_msg_
;
215 bool debug_exception_handler_requested_
;
216 scoped_ptr
<IPC::Message
> attach_debug_exception_handler_reply_msg_
;
219 // The file path to the manifest is passed to nacl-gdb when it is used to
220 // debug the NaCl loader.
221 base::FilePath manifest_path_
;
223 // Socket pairs for the NaCl process and renderer.
224 scoped_ptr
<NaClInternal
> internal_
;
226 base::WeakPtrFactory
<NaClProcessHost
> weak_factory_
;
228 scoped_ptr
<content::BrowserChildProcessHost
> process_
;
231 bool uses_nonsfi_mode_
;
233 bool enable_debug_stub_
;
234 bool enable_dyncode_syscalls_
;
235 bool enable_exception_handling_
;
236 bool enable_crash_throttling_
;
238 bool off_the_record_
;
240 const base::FilePath profile_directory_
;
242 // Channel proxy to terminate the NaCl-Browser PPAPI channel.
243 scoped_ptr
<IPC::ChannelProxy
> ipc_proxy_channel_
;
244 // Browser host for plugin process.
245 scoped_ptr
<content::BrowserPpapiHost
> ppapi_host_
;
249 // Throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
250 static unsigned keepalive_throttle_interval_milliseconds_
;
252 // Shared memory provided to the plugin and renderer for
253 // reporting crash information.
254 base::SharedMemory crash_info_shmem_
;
256 DISALLOW_COPY_AND_ASSIGN(NaClProcessHost
);
261 #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_