OLD | NEW |
| (Empty) |
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 bool Init(uint32_t argc, const char* argn[], const char* argv[]) override; | |
67 | |
68 // Handles document load, when the plugin is a MIME type handler. | |
69 bool HandleDocumentLoad(const pp::URLLoader& url_loader) override; | |
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 | |
90 // Load support. | |
91 // A helper SRPC NaCl module can be loaded given a PP_NaClFileInfo. | |
92 // Blocks until the helper module signals initialization is done. | |
93 // Does not update nacl_module_origin(). | |
94 // Returns NULL or the NaClSubprocess of the new helper NaCl module. | |
95 NaClSubprocess* LoadHelperNaClModule(const std::string& helper_url, | |
96 PP_NaClFileInfo file_info, | |
97 ErrorInfo* error_info); | |
98 | |
99 // Report an error that was encountered while loading a module. | |
100 void ReportLoadError(const ErrorInfo& error_info); | |
101 | |
102 nacl::DescWrapperFactory* wrapper_factory() const { return wrapper_factory_; } | |
103 | |
104 const PPB_NaCl_Private* nacl_interface() const { return nacl_interface_; } | |
105 pp::UMAPrivate& uma_interface() { return uma_interface_; } | |
106 | |
107 private: | |
108 NACL_DISALLOW_COPY_AND_ASSIGN(Plugin); | |
109 // The browser will invoke the destructor via the pp::Instance | |
110 // pointer to this object, not from base's Delete(). | |
111 ~Plugin() override; | |
112 | |
113 // Shuts down socket connection, service runtime, and receive thread, | |
114 // in this order, for the main nacl subprocess. | |
115 void ShutDownSubprocesses(); | |
116 | |
117 // Histogram helper functions, internal to Plugin so they can use | |
118 // uma_interface_ normally. | |
119 void HistogramTimeSmall(const std::string& name, int64_t ms); | |
120 | |
121 // Loads and starts a helper (e.g. llc, ld) NaCl module. | |
122 // Only to be used from a background (non-main) thread for the PNaCl | |
123 // translator. This will fully initialize the |subprocess| if the load was | |
124 // successful. | |
125 bool LoadHelperNaClModuleInternal(NaClSubprocess* subprocess, | |
126 const SelLdrStartParams& params); | |
127 | |
128 // Start sel_ldr from the main thread, given the start params. | |
129 // |pp_error| is set by CallOnMainThread (should be PP_OK). | |
130 void StartSelLdrOnMainThread(int32_t pp_error, | |
131 ServiceRuntime* service_runtime, | |
132 const SelLdrStartParams& params, | |
133 pp::CompletionCallback callback); | |
134 | |
135 // Signals that StartSelLdr has finished. | |
136 // This is invoked on the main thread. | |
137 void SignalStartSelLdrDone(int32_t pp_error, | |
138 bool* started, | |
139 ServiceRuntime* service_runtime); | |
140 | |
141 // This is invoked on the main thread. | |
142 void StartNexe(int32_t pp_error, ServiceRuntime* service_runtime); | |
143 | |
144 // Callback used when getting the URL for the .nexe file. If the URL loading | |
145 // is successful, the file descriptor is opened and can be passed to sel_ldr | |
146 // with the sandbox on. | |
147 void NexeFileDidOpen(int32_t pp_error); | |
148 | |
149 // Callback used when a .nexe is translated from bitcode. If the translation | |
150 // is successful, the file descriptor is opened and can be passed to sel_ldr | |
151 // with the sandbox on. | |
152 void BitcodeDidTranslate(int32_t pp_error); | |
153 | |
154 // NaCl ISA selection manifest file support. The manifest file is specified | |
155 // using the "nacl" attribute in the <embed> tag. First, the manifest URL (or | |
156 // data: URI) is fetched, then the JSON is parsed. Once a valid .nexe is | |
157 // chosen for the sandbox ISA, any current service runtime is shut down, the | |
158 // .nexe is loaded and run. | |
159 | |
160 // Callback used when getting the manifest file as a local file descriptor. | |
161 void NaClManifestFileDidOpen(int32_t pp_error); | |
162 | |
163 // Processes the JSON manifest string and starts loading the nexe. | |
164 void ProcessNaClManifest(const std::string& manifest_json); | |
165 | |
166 // Keep track of the NaCl module subprocess that was spun up in the plugin. | |
167 NaClSubprocess main_subprocess_; | |
168 | |
169 bool uses_nonsfi_mode_; | |
170 | |
171 nacl::DescWrapperFactory* wrapper_factory_; | |
172 | |
173 pp::CompletionCallbackFactory<Plugin> callback_factory_; | |
174 | |
175 nacl::scoped_ptr<PnaclCoordinator> pnacl_coordinator_; | |
176 | |
177 int exit_status_; | |
178 | |
179 PP_NaClFileInfo nexe_file_info_; | |
180 | |
181 const PPB_NaCl_Private* nacl_interface_; | |
182 pp::UMAPrivate uma_interface_; | |
183 }; | |
184 | |
185 } // namespace plugin | |
186 | |
187 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PLUGIN_H_ | |
OLD | NEW |