Componentize component_updater: Copy over test data with executable bit.
[chromium-blink-merge.git] / ppapi / native_client / src / trusted / plugin / pnacl_coordinator.h
blobe802c8fbdf4f1ddb7202d37e4f3e7f8378bbedc2
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_
8 #include <set>
9 #include <map>
10 #include <vector>
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/nacl_subprocess.h"
21 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
22 #include "ppapi/native_client/src/trusted/plugin/pnacl_resources.h"
24 #include "ppapi/utility/completion_callback_factory.h"
26 struct PP_PNaClOptions;
28 namespace plugin {
30 class Plugin;
31 class PnaclCoordinator;
32 class PnaclTranslateThread;
33 class TempFile;
35 // A class invoked by Plugin to handle PNaCl client-side translation.
36 // Usage:
37 // (1) Invoke the factory method, e.g.,
38 // PnaclCoordinator* coord = BitcodeToNative(plugin,
39 // "http://foo.com/my.pexe",
40 // pnacl_options,
41 // TranslateNotifyCallback);
42 // (2) TranslateNotifyCallback gets invoked when translation is complete.
43 // If the translation was successful, the pp_error argument is PP_OK.
44 // Other values indicate errors.
45 // (3) After finish_callback runs, get the file descriptor of the translated
46 // nexe, e.g.,
47 // fd = coord->ReleaseTranslatedFD();
48 // (4) Load the nexe from "fd".
49 // (5) delete coord.
51 // Translation proceeds in two steps:
52 // (1) llc translates the bitcode in pexe_url_ to an object in obj_file_.
53 // (2) ld links the object code in obj_file_ and produces a nexe in nexe_file_.
54 class PnaclCoordinator {
55 public:
56 // Maximum number of object files passable to the translator. Cannot be
57 // changed without changing the RPC signatures.
58 const static size_t kMaxTranslatorObjectFiles = 16;
59 virtual ~PnaclCoordinator();
61 // The factory method for translations.
62 static PnaclCoordinator* BitcodeToNative(
63 Plugin* plugin,
64 const nacl::string& pexe_url,
65 const PP_PNaClOptions& pnacl_options,
66 const pp::CompletionCallback& translate_notify_callback);
68 // Call this to take ownership of the FD of the translated nexe after
69 // BitcodeToNative has completed (and the finish_callback called).
70 PP_FileHandle TakeTranslatedFileHandle();
72 // Return a callback that should be notified when |bytes_compiled| bytes
73 // have been compiled.
74 pp::CompletionCallback GetCompileProgressCallback(int64_t bytes_compiled);
76 // Get the last known load progress.
77 void GetCurrentProgress(int64_t* bytes_loaded, int64_t* bytes_total);
79 // Return true if we should delay the progress event reporting.
80 // This delay approximates:
81 // - the size of the buffer of bytes sent but not-yet-compiled by LLC.
82 // - the linking time.
83 bool ShouldDelayProgressEvent() {
84 const uint32_t kProgressEventSlopPct = 5;
85 return ((expected_pexe_size_ - pexe_bytes_compiled_) * 100 /
86 expected_pexe_size_) < kProgressEventSlopPct;
90 void BitcodeStreamCacheHit(PP_FileHandle handle);
91 void BitcodeStreamCacheMiss(int64_t expected_pexe_size,
92 PP_FileHandle handle);
94 // Invoked when a pexe data chunk arrives (when using streaming translation)
95 void BitcodeStreamGotData(const void* data, int32_t length);
97 // Invoked when the pexe download finishes (using streaming translation)
98 void BitcodeStreamDidFinish(int32_t pp_error);
100 private:
101 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclCoordinator);
103 // BitcodeToNative is the factory method for PnaclCoordinators.
104 // Therefore the constructor is private.
105 PnaclCoordinator(Plugin* plugin,
106 const nacl::string& pexe_url,
107 const PP_PNaClOptions& pnacl_options,
108 const pp::CompletionCallback& translate_notify_callback);
110 // Invoke to issue a GET request for bitcode.
111 void OpenBitcodeStream();
113 // Invoked when a pexe data chunk is compiled.
114 void BitcodeGotCompiled(int32_t pp_error, int64_t bytes_compiled);
115 // Once llc and ld nexes have been loaded and the two temporary files have
116 // been created, this starts the translation. Translation starts two
117 // subprocesses, one for llc and one for ld.
118 void RunTranslate(int32_t pp_error);
120 // Invoked when translation is finished.
121 void TranslateFinished(int32_t pp_error);
123 // Invoked when the read descriptor for nexe_file_ is created.
124 void NexeReadDidOpen(int32_t pp_error);
126 // Bring control back to the plugin by invoking the
127 // |translate_notify_callback_|. This does not set the ErrorInfo report,
128 // it is assumed that it was already set.
129 void ExitWithError();
130 // Run |translate_notify_callback_| with an error condition that is not
131 // PPAPI specific. Also set ErrorInfo report.
132 void ReportNonPpapiError(PP_NaClError err, const nacl::string& message);
133 // Run when faced with a PPAPI error condition. Bring control back to the
134 // plugin by invoking the |translate_notify_callback_|.
135 // Also set ErrorInfo report.
136 void ReportPpapiError(PP_NaClError err,
137 int32_t pp_error, const nacl::string& message);
140 // Keeps track of the pp_error upon entry to TranslateFinished,
141 // for inspection after cleanup.
142 int32_t translate_finish_error_;
144 // The plugin owning the nexe for which we are doing translation.
145 Plugin* plugin_;
147 pp::CompletionCallback translate_notify_callback_;
148 // Set to true when the translation (if applicable) is finished and the nexe
149 // file is loaded, (or when there was an error), and the browser has been
150 // notified via ReportTranslationFinished. If it is not set before
151 // plugin/coordinator destruction, the destructor will call
152 // ReportTranslationFinished.
153 bool translation_finished_reported_;
154 // Threadsafety is required to support file lookups.
155 pp::CompletionCallbackFactory<PnaclCoordinator,
156 pp::ThreadSafeThreadTraits> callback_factory_;
158 // An auxiliary class that manages downloaded resources (llc and ld nexes).
159 nacl::scoped_ptr<PnaclResources> resources_;
161 // The URL for the pexe file.
162 nacl::string pexe_url_;
163 // Options for translation.
164 PP_PNaClOptions pnacl_options_;
165 // Architecture-specific attributes used for translation. These are
166 // supplied by Chrome, not the developer, and are therefore different
167 // from PNaCl options.
168 nacl::string architecture_attributes_;
170 // Object file, produced by the translator and consumed by the linker.
171 std::vector<TempFile*> obj_files_;
172 nacl::scoped_ptr<nacl::DescWrapper> invalid_desc_wrapper_;
173 // Number of split modules (threads) for llc
174 int split_module_count_;
176 // Translated nexe file, produced by the linker.
177 nacl::scoped_ptr<TempFile> temp_nexe_file_;
179 // Used to report information when errors (PPAPI or otherwise) are reported.
180 ErrorInfo error_info_;
182 // True if an error was already reported, and translate_notify_callback_
183 // was already run/consumed.
184 bool error_already_reported_;
186 // State for timing and size information for UMA stats.
187 int64_t pexe_size_; // Count as we stream -- will converge to pexe size.
188 int64_t pexe_bytes_compiled_; // Count as we compile.
189 int64_t expected_pexe_size_; // Expected download total (-1 if unknown).
191 // The helper thread used to do translations via SRPC.
192 // It accesses fields of PnaclCoordinator so it must have a
193 // shorter lifetime.
194 nacl::scoped_ptr<PnaclTranslateThread> translate_thread_;
197 //----------------------------------------------------------------------
199 } // namespace plugin;
200 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_