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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_
12 #include "native_client/src/include/nacl_macros.h"
13 #include "native_client/src/include/nacl_string.h"
14 #include "native_client/src/shared/platform/nacl_sync_raii.h"
15 #include "native_client/src/shared/srpc/nacl_srpc.h"
16 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
18 #include "ppapi/cpp/completion_callback.h"
20 #include "ppapi/native_client/src/trusted/plugin/callback_source.h"
21 #include "ppapi/native_client/src/trusted/plugin/file_downloader.h"
22 #include "ppapi/native_client/src/trusted/plugin/nacl_subprocess.h"
23 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
24 #include "ppapi/native_client/src/trusted/plugin/pnacl_options.h"
25 #include "ppapi/native_client/src/trusted/plugin/pnacl_resources.h"
32 class PnaclCoordinator;
33 class PnaclTranslateThread;
36 // A class invoked by Plugin to handle PNaCl client-side translation.
38 // (1) Invoke the factory method, e.g.,
39 // PnaclCoordinator* coord = BitcodeToNative(plugin,
40 // "http://foo.com/my.pexe",
42 // TranslateNotifyCallback);
43 // (2) TranslateNotifyCallback gets invoked when translation is complete.
44 // If the translation was successful, the pp_error argument is PP_OK.
45 // Other values indicate errors.
46 // (3) After finish_callback runs, get the file descriptor of the translated
48 // fd = coord->ReleaseTranslatedFD();
49 // (4) Load the nexe from "fd".
52 // Translation proceeds in two steps:
53 // (1) llc translates the bitcode in pexe_url_ to an object in obj_file_.
54 // (2) ld links the object code in obj_file_ and produces a nexe in nexe_file_.
56 // The coordinator proceeds through several states. They are
57 // OPEN_BITCODE_STREAM
58 // Complete when BitcodeStreamDidOpen is invoked
59 // LOAD_TRANSLATOR_BINARIES
60 // Complete when ResourcesDidLoad is invoked.
62 // Get an FD which contains the cached nexe, or is writeable for
63 // translation output. Complete when NexeFdDidOpen is called.
65 // If there was a cache hit, go to OPEN_NEXE_FOR_SEL_LDR, otherwise,
66 // continue streaming the bitcode, and:
67 // OPEN_TMP_FOR_LLC_TO_LD_COMMUNICATION
68 // Complete when ObjectFileDidOpen is invoked.
69 // OPEN_NEXE_FD_FOR_WRITING
70 // Complete when RunTranslate is invoked.
71 // START_LD_AND_LLC_SUBPROCESS_AND_INITIATE_TRANSLATION
72 // Complete when RunTranslate returns.
73 // TRANSLATION_COMPLETE
74 // Complete when TranslateFinished is invoked.
76 // OPEN_NEXE_FOR_SEL_LDR
77 // Complete when NexeReadDidOpen is invoked.
78 class PnaclCoordinator: public CallbackSource<FileStreamData> {
80 virtual ~PnaclCoordinator();
82 // The factory method for translations.
83 static PnaclCoordinator* BitcodeToNative(
85 const nacl::string& pexe_url,
86 const PnaclOptions& pnacl_options,
87 const pp::CompletionCallback& translate_notify_callback);
89 // Call this to take ownership of the FD of the translated nexe after
90 // BitcodeToNative has completed (and the finish_callback called).
91 nacl::DescWrapper* ReleaseTranslatedFD();
93 // Run |translate_notify_callback_| with an error condition that is not
94 // PPAPI specific. Also set ErrorInfo report.
95 void ReportNonPpapiError(PluginErrorCode err, const nacl::string& message);
96 // Run when faced with a PPAPI error condition. Bring control back to the
97 // plugin by invoking the |translate_notify_callback_|.
98 // Also set ErrorInfo report.
99 void ReportPpapiError(PluginErrorCode err,
100 int32_t pp_error, const nacl::string& message);
101 // Bring control back to the plugin by invoking the
102 // |translate_notify_callback_|. This does not set the ErrorInfo report,
103 // it is assumed that it was already set.
104 void ExitWithError();
106 // Implement FileDownloader's template of the CallbackSource interface.
107 // This method returns a callback which will be called by the FileDownloader
108 // to stream the bitcode data as it arrives. The callback
109 // (BitcodeStreamGotData) passes it to llc over SRPC.
110 StreamCallback GetCallback();
112 // Return a callback that should be notified when |bytes_compiled| bytes
113 // have been compiled.
114 pp::CompletionCallback GetCompileProgressCallback(int64_t bytes_compiled);
116 // Get the last known load progress.
117 void GetCurrentProgress(int64_t* bytes_loaded, int64_t* bytes_total);
119 // Return true if the total progress to report (w/ progress events) is known.
120 bool ExpectedProgressKnown() { return expected_pexe_size_ != -1; }
122 // Return true if we should delay the progress event reporting.
123 // This delay approximates:
124 // - the size of the buffer of bytes sent but not-yet-compiled by LLC.
125 // - the linking time.
126 bool ShouldDelayProgressEvent() {
127 const uint32_t kProgressEventSlopPct = 5;
128 return ((expected_pexe_size_ - pexe_bytes_compiled_) * 100 /
129 expected_pexe_size_) < kProgressEventSlopPct;
133 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclCoordinator);
135 // BitcodeToNative is the factory method for PnaclCoordinators.
136 // Therefore the constructor is private.
137 PnaclCoordinator(Plugin* plugin,
138 const nacl::string& pexe_url,
139 const PnaclOptions& pnacl_options,
140 const pp::CompletionCallback& translate_notify_callback);
142 // Invoke to issue a GET request for bitcode.
143 void OpenBitcodeStream();
144 // Invoked when we've started an URL fetch for the pexe to check for
146 void BitcodeStreamDidOpen(int32_t pp_error);
148 // Callback for when the resource info JSON file has been read.
149 void ResourceInfoWasRead(int32_t pp_error);
151 // Callback for when llc and ld have been downloaded.
152 void ResourcesDidLoad(int32_t pp_error);
153 // Invoked when we've gotten a temp FD for the nexe, either with the nexe
154 // data, or a writeable fd to save to.
155 void NexeFdDidOpen(int32_t pp_error);
156 // Invoked when a pexe data chunk arrives (when using streaming translation)
157 void BitcodeStreamGotData(int32_t pp_error, FileStreamData data);
158 // Invoked when a pexe data chunk is compiled.
159 void BitcodeGotCompiled(int32_t pp_error, int64_t bytes_compiled);
160 // Invoked when the pexe download finishes (using streaming translation)
161 void BitcodeStreamDidFinish(int32_t pp_error);
162 // Invoked when the write descriptor for obj_file_ is created.
163 void ObjectFileDidOpen(int32_t pp_error);
164 // Once llc and ld nexes have been loaded and the two temporary files have
165 // been created, this starts the translation. Translation starts two
166 // subprocesses, one for llc and one for ld.
167 void RunTranslate(int32_t pp_error);
169 // Invoked when translation is finished.
170 void TranslateFinished(int32_t pp_error);
172 // Invoked when the read descriptor for nexe_file_ is created.
173 void NexeReadDidOpen(int32_t pp_error);
175 // Keeps track of the pp_error upon entry to TranslateFinished,
176 // for inspection after cleanup.
177 int32_t translate_finish_error_;
179 // The plugin owning the nexe for which we are doing translation.
182 pp::CompletionCallback translate_notify_callback_;
183 // Set to true when the translation (if applicable) is finished and the nexe
184 // file is loaded, (or when there was an error), and the browser has been
185 // notified via ReportTranslationFinished. If it is not set before
186 // plugin/coordinator destruction, the destructor will call
187 // ReportTranslationFinished.
188 bool translation_finished_reported_;
189 // Threadsafety is required to support file lookups.
190 pp::CompletionCallbackFactory<PnaclCoordinator,
191 pp::ThreadSafeThreadTraits> callback_factory_;
193 // The manifest used by resource loading and ld + llc's reverse service
194 // to look up objects and libraries.
195 nacl::scoped_ptr<const Manifest> manifest_;
196 // An auxiliary class that manages downloaded resources (llc and ld nexes).
197 nacl::scoped_ptr<PnaclResources> resources_;
199 // The URL for the pexe file.
200 nacl::string pexe_url_;
201 // Options for translation.
202 PnaclOptions pnacl_options_;
204 // Object file, produced by the translator and consumed by the linker.
205 nacl::scoped_ptr<TempFile> obj_file_;
206 // Translated nexe file, produced by the linker.
207 nacl::scoped_ptr<TempFile> temp_nexe_file_;
208 // Passed to the browser, which sets it to true if there is a translation
210 PP_Bool is_cache_hit_;
212 // Downloader for streaming translation
213 nacl::scoped_ptr<FileDownloader> streaming_downloader_;
215 // Used to report information when errors (PPAPI or otherwise) are reported.
216 ErrorInfo error_info_;
218 // True if an error was already reported, and translate_notify_callback_
219 // was already run/consumed.
220 bool error_already_reported_;
222 // True if compilation is off_the_record.
223 bool off_the_record_;
225 // State for timing and size information for UMA stats.
226 int64_t pnacl_init_time_;
227 int64_t pexe_size_; // Count as we stream -- will converge to pexe size.
228 int64_t pexe_bytes_compiled_; // Count as we compile.
229 int64_t expected_pexe_size_; // Expected download total (-1 if unknown).
231 // The helper thread used to do translations via SRPC.
232 // Keep this last in declaration order to ensure the other variables
233 // haven't been destroyed yet when its destructor runs.
234 nacl::scoped_ptr<PnaclTranslateThread> translate_thread_;
237 //----------------------------------------------------------------------
239 } // namespace plugin;
240 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_