Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / ppapi / native_client / src / trusted / plugin / plugin.h
1 // -*- c++ -*-
2 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 // The portable representation of an instance and root scriptable object.
7 // The PPAPI version of the plugin instantiates a subclass of this class.
8
9 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_
10 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_
11
12 #include <stdio.h>
13
14 #include <string>
15
16 #include "native_client/src/include/nacl_macros.h"
17 #include "native_client/src/include/nacl_scoped_ptr.h"
18
19 #include "ppapi/c/private/ppb_nacl_private.h"
20 #include "ppapi/cpp/instance.h"
21 #include "ppapi/cpp/private/uma_private.h"
22 #include "ppapi/cpp/url_loader.h"
23 #include "ppapi/cpp/var.h"
24 #include "ppapi/cpp/view.h"
25
26 #include "ppapi/native_client/src/trusted/plugin/nacl_subprocess.h"
27 #include "ppapi/native_client/src/trusted/plugin/pnacl_coordinator.h"
28 #include "ppapi/native_client/src/trusted/plugin/service_runtime.h"
29 #include "ppapi/native_client/src/trusted/plugin/utility.h"
30
31 #include "ppapi/utility/completion_callback_factory.h"
32
33 namespace nacl {
34 class DescWrapper;
35 class DescWrapperFactory;
36 }  // namespace nacl
37
38 namespace pp {
39 class CompletionCallback;
40 class URLLoader;
41 class URLUtil_Dev;
42 }
43
44 namespace plugin {
45
46 class ErrorInfo;
47 class Manifest;
48
49 int32_t ConvertFileDescriptor(PP_FileHandle handle);
50
51 const PP_NaClFileInfo kInvalidNaClFileInfo = {
52   PP_kInvalidFileHandle,
53   0,  // token_lo
54   0,  // token_hi
55 };
56
57 class Plugin : public pp::Instance {
58  public:
59   explicit Plugin(PP_Instance instance);
60
61   // ----- Methods inherited from pp::Instance:
62
63   // Initializes this plugin with <embed/object ...> tag attribute count |argc|,
64   // names |argn| and values |argn|. Returns false on failure.
65   // Gets called by the browser right after New().
66   virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]);
67
68   // Handles document load, when the plugin is a MIME type handler.
69   virtual bool HandleDocumentLoad(const pp::URLLoader& url_loader);
70
71   // Load support.
72   //
73   // Starts NaCl module but does not wait until low-level
74   // initialization (e.g. ld.so dynamic loading of manifest files) is
75   // done.  The module will become ready later, asynchronously.  Other
76   // event handlers should block until the module is ready before
77   // trying to communicate with it, i.e., until nacl_ready_state is
78   // DONE.
79   //
80   // NB: currently we do not time out, so if the untrusted code
81   // does not signal that it is ready, then we will deadlock the main
82   // thread of the renderer on this subsequent event delivery.  We
83   // should include a time-out at which point we declare the
84   // nacl_ready_state to be done, and let the normal crash detection
85   // mechanism(s) take over.
86   void LoadNaClModule(PP_NaClFileInfo file_info,
87                       bool uses_nonsfi_mode,
88                       PP_NaClAppProcessType process_type,
89                       const pp::CompletionCallback& init_done_cb);
90
91   // Finish hooking interfaces up, after low-level initialization is
92   // complete.
93   bool LoadNaClModuleContinuationIntern();
94
95   // Continuation for starting SRPC/JSProxy services as appropriate.
96   // This is invoked as a callback when the NaCl module makes the
97   // init_done reverse RPC to tell us that low-level initialization
98   // such as ld.so processing is done.  That initialization requires
99   // that the main thread be free in order to do Pepper
100   // main-thread-only operations such as file processing.
101   bool LoadNaClModuleContinuation(int32_t pp_error);
102
103   // Load support.
104   // A helper SRPC NaCl module can be loaded given a PP_NaClFileInfo.
105   // Blocks until the helper module signals initialization is done.
106   // Does not update nacl_module_origin().
107   // Returns NULL or the NaClSubprocess of the new helper NaCl module.
108   NaClSubprocess* LoadHelperNaClModule(const std::string& helper_url,
109                                        PP_NaClFileInfo file_info,
110                                        ErrorInfo* error_info);
111
112   // Report an error that was encountered while loading a module.
113   void ReportLoadError(const ErrorInfo& error_info);
114
115   nacl::DescWrapperFactory* wrapper_factory() const { return wrapper_factory_; }
116
117   const PPB_NaCl_Private* nacl_interface() const { return nacl_interface_; }
118   pp::UMAPrivate& uma_interface() { return uma_interface_; }
119
120  private:
121   NACL_DISALLOW_COPY_AND_ASSIGN(Plugin);
122   // The browser will invoke the destructor via the pp::Instance
123   // pointer to this object, not from base's Delete().
124   ~Plugin();
125
126   // Shuts down socket connection, service runtime, and receive thread,
127   // in this order, for the main nacl subprocess.
128   void ShutDownSubprocesses();
129
130   // Histogram helper functions, internal to Plugin so they can use
131   // uma_interface_ normally.
132   void HistogramTimeSmall(const std::string& name, int64_t ms);
133
134   // Loads and starts a helper (e.g. llc, ld) NaCl module.
135   // Only to be used from a background (non-main) thread for the PNaCl
136   // translator. This will fully initialize the |subprocess| if the load was
137   // successful.
138   bool LoadHelperNaClModuleInternal(NaClSubprocess* subprocess,
139                                     const SelLdrStartParams& params);
140
141   // Start sel_ldr from the main thread, given the start params.
142   // |pp_error| is set by CallOnMainThread (should be PP_OK).
143   void StartSelLdrOnMainThread(int32_t pp_error,
144                                ServiceRuntime* service_runtime,
145                                const SelLdrStartParams& params,
146                                pp::CompletionCallback callback);
147
148   // Signals that StartSelLdr has finished.
149   // This is invoked on the main thread.
150   void SignalStartSelLdrDone(int32_t pp_error,
151                              bool* started,
152                              ServiceRuntime* service_runtime);
153
154   // This is invoked on the main thread.
155   void StartNexe(int32_t pp_error, ServiceRuntime* service_runtime);
156
157   // Callback used when getting the URL for the .nexe file.  If the URL loading
158   // is successful, the file descriptor is opened and can be passed to sel_ldr
159   // with the sandbox on.
160   void NexeFileDidOpen(int32_t pp_error);
161   void NexeFileDidOpenContinuation(int32_t pp_error);
162
163   // Callback used when a .nexe is translated from bitcode.  If the translation
164   // is successful, the file descriptor is opened and can be passed to sel_ldr
165   // with the sandbox on.
166   void BitcodeDidTranslate(int32_t pp_error);
167   void BitcodeDidTranslateContinuation(int32_t pp_error);
168
169   // NaCl ISA selection manifest file support.  The manifest file is specified
170   // using the "nacl" attribute in the <embed> tag.  First, the manifest URL (or
171   // data: URI) is fetched, then the JSON is parsed.  Once a valid .nexe is
172   // chosen for the sandbox ISA, any current service runtime is shut down, the
173   // .nexe is loaded and run.
174
175   // Callback used when getting the manifest file as a local file descriptor.
176   void NaClManifestFileDidOpen(int32_t pp_error);
177
178   // Processes the JSON manifest string and starts loading the nexe.
179   void ProcessNaClManifest(const std::string& manifest_json);
180
181   // Keep track of the NaCl module subprocess that was spun up in the plugin.
182   NaClSubprocess main_subprocess_;
183
184   bool uses_nonsfi_mode_;
185
186   nacl::DescWrapperFactory* wrapper_factory_;
187
188   pp::CompletionCallbackFactory<Plugin> callback_factory_;
189
190   nacl::scoped_ptr<PnaclCoordinator> pnacl_coordinator_;
191
192   int exit_status_;
193
194   PP_NaClFileInfo nexe_file_info_;
195
196   const PPB_NaCl_Private* nacl_interface_;
197   pp::UMAPrivate uma_interface_;
198 };
199
200 }  // namespace plugin
201
202 #endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_