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
,
78 nacl::NaClResourceFileInfo
>& prefetched_resource_files_info
,
79 ppapi::PpapiPermissions permissions
,
81 uint32 permission_bits
,
82 bool uses_nonsfi_mode
,
84 NaClAppProcessType process_type
,
85 const base::FilePath
& profile_directory
);
86 ~NaClProcessHost() override
;
88 void OnProcessCrashed(int exit_status
) override
;
90 // Do any minimal work that must be done at browser startup.
91 static void EarlyStartup();
93 // Specifies throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
94 static void SetPpapiKeepAliveThrottleForTesting(unsigned milliseconds
);
96 // Initialize the new NaCl process. Result is returned by sending ipc
98 void Launch(NaClHostMessageFilter
* nacl_host_message_filter
,
99 IPC::Message
* reply_msg
,
100 const base::FilePath
& manifest_path
);
102 void OnChannelConnected(int32 peer_pid
) override
;
105 void OnProcessLaunchedByBroker(base::ProcessHandle handle
);
106 void OnDebugExceptionHandlerLaunchedByBroker(bool success
);
109 bool Send(IPC::Message
* msg
);
111 content::BrowserChildProcessHost
* process() { return process_
.get(); }
112 content::BrowserPpapiHost
* browser_ppapi_host() { return ppapi_host_
.get(); }
115 void LaunchNaClGdb();
117 // Mark the process as using a particular GDB debug stub port and notify
118 // listeners (if the port is not kGdbDebugStubPortUnknown).
119 void SetDebugStubPort(int port
);
121 #if defined(OS_POSIX)
122 // Create bound TCP socket in the browser process so that the NaCl GDB debug
123 // stub can use it to accept incoming connections even when the Chrome sandbox
125 net::SocketDescriptor
GetDebugStubSocketHandle();
129 // Called when the debug stub port has been selected.
130 void OnDebugStubPortSelected(uint16_t debug_stub_port
);
135 // BrowserChildProcessHostDelegate implementation:
136 bool OnMessageReceived(const IPC::Message
& msg
) override
;
137 void OnProcessLaunched() override
;
139 void OnResourcesReady();
141 // Enable the PPAPI proxy only for NaCl processes corresponding to a renderer.
142 bool enable_ppapi_proxy() { return render_view_id_
!= 0; }
144 // Sends the reply message to the renderer who is waiting for the plugin
145 // to load. Returns true on success.
146 bool ReplyToRenderer(
147 const IPC::ChannelHandle
& ppapi_channel_handle
,
148 const IPC::ChannelHandle
& trusted_channel_handle
,
149 const IPC::ChannelHandle
& manifest_service_channel_handle
);
151 // Sends the reply with error message to the renderer.
152 void SendErrorToRenderer(const std::string
& error_message
);
154 // Sends the reply message to the renderer. Either result or
155 // error message must be empty.
156 void SendMessageToRenderer(const NaClLaunchResult
& result
,
157 const std::string
& error_message
);
159 // Sends the message to the NaCl process to load the plugin. Returns true
161 bool StartNaClExecution();
163 void StartNaClFileResolved(
164 NaClStartParams params
,
165 const base::FilePath
& file_path
,
166 base::File nexe_file
);
168 // Does post-process-launching tasks for starting the NaCl process once
169 // we have a connection.
171 // Returns false on failure.
172 bool StartWithLaunchedProcess();
174 // Message handlers for validation caching.
175 void OnQueryKnownToValidate(const std::string
& signature
, bool* result
);
176 void OnSetKnownToValidate(const std::string
& signature
);
177 void OnResolveFileToken(uint64 file_token_lo
, uint64 file_token_hi
);
178 void FileResolved(uint64_t file_token_lo
,
179 uint64_t file_token_hi
,
180 const base::FilePath
& file_path
,
183 // Message handler for Windows hardware exception handling.
184 void OnAttachDebugExceptionHandler(const std::string
& info
,
185 IPC::Message
* reply_msg
);
186 bool AttachDebugExceptionHandler(const std::string
& info
,
187 IPC::Message
* reply_msg
);
190 // Called when the PPAPI IPC channels to the browser/renderer have been
192 void OnPpapiChannelsCreated(
193 const IPC::ChannelHandle
& browser_channel_handle
,
194 const IPC::ChannelHandle
& ppapi_renderer_channel_handle
,
195 const IPC::ChannelHandle
& trusted_renderer_channel_handle
,
196 const IPC::ChannelHandle
& manifest_service_channel_handle
);
199 base::File nexe_file_
;
200 NaClFileToken nexe_token_
;
201 std::vector
<nacl::NaClResourceFileInfo
> prefetched_resource_files_info_
;
203 ppapi::PpapiPermissions permissions_
;
206 // This field becomes true when the broker successfully launched
208 bool process_launched_by_broker_
;
210 // The NaClHostMessageFilter that requested this NaCl process. We use
211 // this for sending the reply once the process has started.
212 scoped_refptr
<NaClHostMessageFilter
> nacl_host_message_filter_
;
214 // The reply message to send. We must always send this message when the
215 // sub-process either succeeds or fails to unblock the renderer waiting for
216 // the reply. NULL when there is no reply to send.
217 IPC::Message
* reply_msg_
;
219 bool debug_exception_handler_requested_
;
220 scoped_ptr
<IPC::Message
> attach_debug_exception_handler_reply_msg_
;
223 // The file path to the manifest is passed to nacl-gdb when it is used to
224 // debug the NaCl loader.
225 base::FilePath manifest_path_
;
227 scoped_ptr
<content::BrowserChildProcessHost
> process_
;
229 bool uses_nonsfi_mode_
;
231 bool enable_debug_stub_
;
232 bool enable_crash_throttling_
;
233 bool off_the_record_
;
234 NaClAppProcessType process_type_
;
236 const base::FilePath profile_directory_
;
238 // Channel proxy to terminate the NaCl-Browser PPAPI channel.
239 scoped_ptr
<IPC::ChannelProxy
> ipc_proxy_channel_
;
240 // Browser host for plugin process.
241 scoped_ptr
<content::BrowserPpapiHost
> ppapi_host_
;
245 // Throttling time in milliseconds for PpapiHostMsg_Keepalive IPCs.
246 static unsigned keepalive_throttle_interval_milliseconds_
;
248 // Shared memory provided to the plugin and renderer for
249 // reporting crash information.
250 base::SharedMemory crash_info_shmem_
;
252 base::File socket_for_renderer_
;
253 base::File socket_for_sel_ldr_
;
255 base::WeakPtrFactory
<NaClProcessHost
> weak_factory_
;
257 DISALLOW_COPY_AND_ASSIGN(NaClProcessHost
);
262 #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_