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.
6 // The portable representation of an instance and root scriptable object.
7 // The PPAPI version of the plugin instantiates a subclass of this class.
9 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_
10 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_
16 #include "native_client/src/include/nacl_macros.h"
17 #include "native_client/src/include/nacl_scoped_ptr.h"
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"
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"
31 #include "ppapi/utility/completion_callback_factory.h"
35 class DescWrapperFactory;
39 class CompletionCallback;
49 int32_t ConvertFileDescriptor(PP_FileHandle handle);
51 const PP_NaClFileInfo kInvalidNaClFileInfo = {
52 PP_kInvalidFileHandle,
57 class Plugin : public pp::Instance {
59 explicit Plugin(PP_Instance instance);
61 // ----- Methods inherited from pp::Instance:
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[]);
68 // Handles document load, when the plugin is a MIME type handler.
69 virtual bool HandleDocumentLoad(const pp::URLLoader& url_loader);
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
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);
91 // Finish hooking interfaces up, after low-level initialization is
93 bool LoadNaClModuleContinuationIntern();
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);
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);
112 // Report an error that was encountered while loading a module.
113 void ReportLoadError(const ErrorInfo& error_info);
115 nacl::DescWrapperFactory* wrapper_factory() const { return wrapper_factory_; }
117 const PPB_NaCl_Private* nacl_interface() const { return nacl_interface_; }
118 pp::UMAPrivate& uma_interface() { return uma_interface_; }
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().
126 // Shuts down socket connection, service runtime, and receive thread,
127 // in this order, for the main nacl subprocess.
128 void ShutDownSubprocesses();
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);
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
138 bool LoadHelperNaClModuleInternal(NaClSubprocess* subprocess,
139 const SelLdrStartParams& params);
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);
148 // Signals that StartSelLdr has finished.
149 // This is invoked on the main thread.
150 void SignalStartSelLdrDone(int32_t pp_error,
152 ServiceRuntime* service_runtime);
154 // This is invoked on the main thread.
155 void StartNexe(int32_t pp_error, ServiceRuntime* service_runtime);
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);
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);
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.
175 // Callback used when getting the manifest file as a local file descriptor.
176 void NaClManifestFileDidOpen(int32_t pp_error);
178 // Processes the JSON manifest string and starts loading the nexe.
179 void ProcessNaClManifest(const std::string& manifest_json);
181 // Keep track of the NaCl module subprocess that was spun up in the plugin.
182 NaClSubprocess main_subprocess_;
184 bool uses_nonsfi_mode_;
186 nacl::DescWrapperFactory* wrapper_factory_;
188 pp::CompletionCallbackFactory<Plugin> callback_factory_;
190 nacl::scoped_ptr<PnaclCoordinator> pnacl_coordinator_;
194 PP_NaClFileInfo nexe_file_info_;
196 const PPB_NaCl_Private* nacl_interface_;
197 pp::UMAPrivate uma_interface_;
200 } // namespace plugin
202 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_