Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / nacl / browser / nacl_process_host.h
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.
4
5 #ifndef COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
6 #define COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_
7
8 #include "build/build_config.h"
9
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"
22 #include "url/gurl.h"
23
24 class CommandLine;
25
26 namespace content {
27 class BrowserChildProcessHost;
28 class BrowserPpapiHost;
29 }
30
31 namespace IPC {
32 class ChannelProxy;
33 }
34
35 namespace nacl {
36
37 class NaClHostMessageFilter;
38 void* AllocateAddressSpaceASLR(base::ProcessHandle process, size_t size);
39
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 {
47  public:
48   // manifest_url: the URL of the manifest of the Native Client plugin being
49   // executed.
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,
64                   int render_view_id,
65                   uint32 permission_bits,
66                   bool uses_irt,
67                   bool enable_dyncode_syscalls,
68                   bool enable_exception_handling,
69                   bool enable_crash_throttling,
70                   bool off_the_record,
71                   const base::FilePath& profile_directory);
72   virtual ~NaClProcessHost();
73
74   virtual void OnProcessCrashed(int exit_status) OVERRIDE;
75
76   // Do any minimal work that must be done at browser startup.
77   static void EarlyStartup();
78
79   // Initialize the new NaCl process. Result is returned by sending ipc
80   // message reply_msg.
81   void Launch(NaClHostMessageFilter* nacl_host_message_filter,
82               IPC::Message* reply_msg,
83               const base::FilePath& manifest_path);
84
85   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
86
87 #if defined(OS_WIN)
88   void OnProcessLaunchedByBroker(base::ProcessHandle handle);
89   void OnDebugExceptionHandlerLaunchedByBroker(bool success);
90 #endif
91
92   bool Send(IPC::Message* msg);
93
94   content::BrowserChildProcessHost* process() { return process_.get(); }
95   content::BrowserPpapiHost* browser_ppapi_host() { return ppapi_host_.get(); }
96
97  private:
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).
102   struct NaClInternal;
103
104   bool LaunchNaClGdb();
105
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
109   // is enabled.
110   net::SocketDescriptor GetDebugStubSocketHandle();
111 #endif
112   bool LaunchSelLdr();
113
114   // BrowserChildProcessHostDelegate implementation:
115   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
116   virtual void OnProcessLaunched() OVERRIDE;
117
118   void OnResourcesReady();
119
120   // Enable the PPAPI proxy only for NaCl processes corresponding to a renderer.
121   bool enable_ppapi_proxy() { return render_view_id_ != 0; }
122
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);
126
127   // Sends the reply with error message to the renderer.
128   void SendErrorToRenderer(const std::string& error_message);
129
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);
134
135   // Sends the message to the NaCl process to load the plugin. Returns true
136   // on success.
137   bool StartNaClExecution();
138
139   // Called once all initialization is complete and the NaCl process is
140   // ready to go. Returns true on success.
141   bool SendStart();
142
143   // Does post-process-launching tasks for starting the NaCl process once
144   // we have a connection.
145   //
146   // Returns false on failure.
147   bool StartWithLaunchedProcess();
148
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);
157
158 #if defined(OS_WIN)
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);
164 #endif
165
166   // Called when the PPAPI IPC channels to the browser/renderer have been
167   // created.
168   void OnPpapiChannelsCreated(
169       const IPC::ChannelHandle& browser_channel_handle,
170       const IPC::ChannelHandle& renderer_channel_handle);
171
172   GURL manifest_url_;
173   ppapi::PpapiPermissions permissions_;
174
175 #if defined(OS_WIN)
176   // This field becomes true when the broker successfully launched
177   // the NaCl loader.
178   bool process_launched_by_broker_;
179 #endif
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_;
183
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_;
188 #if defined(OS_WIN)
189   bool debug_exception_handler_requested_;
190   scoped_ptr<IPC::Message> attach_debug_exception_handler_reply_msg_;
191 #endif
192
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_;
196
197   // Socket pairs for the NaCl process and renderer.
198   scoped_ptr<NaClInternal> internal_;
199
200   base::WeakPtrFactory<NaClProcessHost> weak_factory_;
201
202   scoped_ptr<content::BrowserChildProcessHost> process_;
203
204   bool uses_irt_;
205
206   bool enable_debug_stub_;
207   bool enable_dyncode_syscalls_;
208   bool enable_exception_handling_;
209   bool enable_crash_throttling_;
210
211   bool off_the_record_;
212
213   const base::FilePath profile_directory_;
214
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_;
219
220   int render_view_id_;
221
222   DISALLOW_COPY_AND_ASSIGN(NaClProcessHost);
223 };
224
225 }  // namespace nacl
226
227 #endif  // COMPONENTS_NACL_BROWSER_NACL_PROCESS_HOST_H_