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_path.h"
11 #include "base/files/file_util_proxy.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/process/process.h"
16 #include "components/nacl/common/nacl_types.h"
17 #include "content/public/browser/browser_child_process_host_delegate.h"
18 #include "content/public/browser/browser_child_process_host_iterator.h"
19 #include "ipc/ipc_channel_handle.h"
20 #include "net/socket/socket_descriptor.h"
21 #include "ppapi/shared_impl/ppapi_permissions.h"
27 class BrowserChildProcessHost;
28 class BrowserPpapiHost;
37 class NaClHostMessageFilter;
38 void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size);
40 // Represents the browser side of the browser <--> NaCl communication
41 // channel. There will be one NaClProcessHost per NaCl process
42 // The browser is responsible for starting the NaCl process
43 // when requested by the renderer.
44 // After that, most of the communication is directly between NaCl plugin
45 // running in the renderer and NaCl processes.
46 class NaClProcessHost : public content::BrowserChildProcessHostDelegate {
48 // manifest_url: the URL of the manifest of the Native Client plugin being
50 // render_view_id: RenderView routing id, to control access to private APIs.
51 // permission_bits: controls which interfaces the NaCl plugin can use.
52 // uses_irt: whether the launched process should use the IRT.
53 // enable_dyncode_syscalls: whether the launched process should allow dyncode
54 // and mmap with PROT_EXEC.
55 // enable_exception_handling: whether the launched process should allow
56 // hardware exception handling.
57 // enable_crash_throttling: whether a crash of this process contributes
58 // to the crash throttling statistics, and also
59 // whether this process should not start when too
60 // many crashes have been observed.
61 // off_the_record: was the process launched from an incognito renderer?
62 // profile_directory: is the path of current profile directory.
63 NaClProcessHost(const GURL& manifest_url,
65 uint32 permission_bits,
67 bool enable_dyncode_syscalls,
68 bool enable_exception_handling,
69 bool enable_crash_throttling,
71 const base::FilePath& profile_directory);
72 virtual ~NaClProcessHost();
74 virtual void OnProcessCrashed(int exit_status) OVERRIDE;
76 // Do any minimal work that must be done at browser startup.
77 static void EarlyStartup();
79 // Initialize the new NaCl process. Result is returned by sending ipc
81 void Launch(NaClHostMessageFilter* nacl_host_message_filter,
82 IPC::Message* reply_msg,
83 const base::FilePath& manifest_path);
85 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
88 void OnProcessLaunchedByBroker(base::ProcessHandle handle);
89 void OnDebugExceptionHandlerLaunchedByBroker(bool success);
92 bool Send(IPC::Message* msg);
94 content::BrowserChildProcessHost* process() { return process_.get(); }
95 content::BrowserPpapiHost* browser_ppapi_host() { return ppapi_host_.get(); }
98 // Internal class that holds the NaClHandle objecs so that
99 // nacl_process_host.h doesn't include NaCl headers. Needed since it's
100 // included by src\content, which can't depend on the NaCl gyp file because it
101 // depends on chrome.gyp (circular dependency).
104 bool LaunchNaClGdb();
106 #if defined(OS_POSIX)
107 // Create bound TCP socket in the browser process so that the NaCl GDB debug
108 // stub can use it to accept incoming connections even when the Chrome sandbox
110 net::SocketDescriptor GetDebugStubSocketHandle();
114 // BrowserChildProcessHostDelegate implementation:
115 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
116 virtual void OnProcessLaunched() OVERRIDE;
118 void OnResourcesReady();
120 // Enable the PPAPI proxy only for NaCl processes corresponding to a renderer.
121 bool enable_ppapi_proxy() { return render_view_id_ != 0; }
123 // Sends the reply message to the renderer who is waiting for the plugin
124 // to load. Returns true on success.
125 bool ReplyToRenderer(const IPC::ChannelHandle& channel_handle);
127 // Sends the reply with error message to the renderer.
128 void SendErrorToRenderer(const std::string& error_message);
130 // Sends the reply message to the renderer. Either result or
131 // error message must be empty.
132 void SendMessageToRenderer(const NaClLaunchResult& result,
133 const std::string& error_message);
135 // Sends the message to the NaCl process to load the plugin. Returns true
137 bool StartNaClExecution();
139 // Called once all initialization is complete and the NaCl process is
140 // ready to go. Returns true on success.
143 // Does post-process-launching tasks for starting the NaCl process once
144 // we have a connection.
146 // Returns false on failure.
147 bool StartWithLaunchedProcess();
149 // Message handlers for validation caching.
150 void OnQueryKnownToValidate(const std::string& signature, bool* result);
151 void OnSetKnownToValidate(const std::string& signature);
152 void OnResolveFileToken(uint64 file_token_lo, uint64 file_token_hi,
153 IPC::Message* reply_msg);
154 void FileResolved(const base::FilePath& file_path,
155 IPC::Message* reply_msg,
156 const base::PlatformFile& file);
159 // Message handler for Windows hardware exception handling.
160 void OnAttachDebugExceptionHandler(const std::string& info,
161 IPC::Message* reply_msg);
162 bool AttachDebugExceptionHandler(const std::string& info,
163 IPC::Message* reply_msg);
166 // Called when the PPAPI IPC channels to the browser/renderer have been
168 void OnPpapiChannelsCreated(
169 const IPC::ChannelHandle& browser_channel_handle,
170 const IPC::ChannelHandle& renderer_channel_handle);
173 ppapi::PpapiPermissions permissions_;
176 // This field becomes true when the broker successfully launched
178 bool process_launched_by_broker_;
180 // The NaClHostMessageFilter that requested this NaCl process. We use
181 // this for sending the reply once the process has started.
182 scoped_refptr<NaClHostMessageFilter> nacl_host_message_filter_;
184 // The reply message to send. We must always send this message when the
185 // sub-process either succeeds or fails to unblock the renderer waiting for
186 // the reply. NULL when there is no reply to send.
187 IPC::Message* reply_msg_;
189 bool debug_exception_handler_requested_;
190 scoped_ptr<IPC::Message> attach_debug_exception_handler_reply_msg_;
193 // The file path to the manifest is passed to nacl-gdb when it is used to
194 // debug the NaCl loader.
195 base::FilePath manifest_path_;
197 // Socket pairs for the NaCl process and renderer.
198 scoped_ptr<NaClInternal> internal_;
200 base::WeakPtrFactory<NaClProcessHost> weak_factory_;
202 scoped_ptr<content::BrowserChildProcessHost> process_;
206 bool enable_debug_stub_;
207 bool enable_dyncode_syscalls_;
208 bool enable_exception_handling_;
209 bool enable_crash_throttling_;
211 bool off_the_record_;
213 const base::FilePath profile_directory_;
215 // Channel proxy to terminate the NaCl-Browser PPAPI channel.
216 scoped_ptr<IPC::ChannelProxy> ipc_proxy_channel_;
217 // Browser host for plugin process.
218 scoped_ptr<content::BrowserPpapiHost> ppapi_host_;
222 DISALLOW_COPY_AND_ASSIGN(NaClProcessHost);
227 #endif // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_