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/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
;
31 class PnaclCoordinator
;
32 class PnaclTranslateThread
;
35 // A class invoked by Plugin to handle PNaCl client-side translation.
37 // (1) Invoke the factory method, e.g.,
38 // PnaclCoordinator* coord = BitcodeToNative(plugin,
39 // "http://foo.com/my.pexe",
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
47 // fd = coord->ReleaseTranslatedFD();
48 // (4) Load the nexe from "fd".
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
{
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(
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
);
93 // Invoked when a pexe data chunk arrives (when using streaming translation)
94 void BitcodeStreamGotData(const void* data
, int32_t length
);
96 // Invoked when the pexe download finishes (using streaming translation)
97 void BitcodeStreamDidFinish(int32_t pp_error
);
100 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclCoordinator
);
102 // BitcodeToNative is the factory method for PnaclCoordinators.
103 // Therefore the constructor is private.
104 PnaclCoordinator(Plugin
* plugin
,
105 const nacl::string
& pexe_url
,
106 const PP_PNaClOptions
& pnacl_options
,
107 const pp::CompletionCallback
& translate_notify_callback
);
109 // Invoke to issue a GET request for bitcode.
110 void OpenBitcodeStream();
112 // Invoked when a pexe data chunk is compiled.
113 void BitcodeGotCompiled(int32_t pp_error
, int64_t bytes_compiled
);
114 // Once llc and ld nexes have been loaded and the two temporary files have
115 // been created, this starts the translation. Translation starts two
116 // subprocesses, one for llc and one for ld.
117 void RunTranslate(int32_t pp_error
);
119 // Invoked when translation is finished.
120 void TranslateFinished(int32_t pp_error
);
122 // Invoked when the read descriptor for nexe_file_ is created.
123 void NexeReadDidOpen(int32_t pp_error
);
125 // Bring control back to the plugin by invoking the
126 // |translate_notify_callback_|. This does not set the ErrorInfo report,
127 // it is assumed that it was already set.
128 void ExitWithError();
129 // Run |translate_notify_callback_| with an error condition that is not
130 // PPAPI specific. Also set ErrorInfo report.
131 void ReportNonPpapiError(PP_NaClError err
, const nacl::string
& message
);
132 // Run when faced with a PPAPI error condition. Bring control back to the
133 // plugin by invoking the |translate_notify_callback_|.
134 // Also set ErrorInfo report.
135 void ReportPpapiError(PP_NaClError err
,
136 int32_t pp_error
, const nacl::string
& message
);
139 // Keeps track of the pp_error upon entry to TranslateFinished,
140 // for inspection after cleanup.
141 int32_t translate_finish_error_
;
143 // The plugin owning the nexe for which we are doing translation.
146 pp::CompletionCallback translate_notify_callback_
;
147 // Set to true when the translation (if applicable) is finished and the nexe
148 // file is loaded, (or when there was an error), and the browser has been
149 // notified via ReportTranslationFinished. If it is not set before
150 // plugin/coordinator destruction, the destructor will call
151 // ReportTranslationFinished.
152 bool translation_finished_reported_
;
153 // Threadsafety is required to support file lookups.
154 pp::CompletionCallbackFactory
<PnaclCoordinator
,
155 pp::ThreadSafeThreadTraits
> callback_factory_
;
157 // An auxiliary class that manages downloaded resources (llc and ld nexes).
158 nacl::scoped_ptr
<PnaclResources
> resources_
;
160 // The URL for the pexe file.
161 nacl::string pexe_url_
;
162 // Options for translation.
163 PP_PNaClOptions pnacl_options_
;
164 // Architecture-specific attributes used for translation. These are
165 // supplied by Chrome, not the developer, and are therefore different
166 // from PNaCl options.
167 nacl::string architecture_attributes_
;
169 // Object file, produced by the translator and consumed by the linker.
170 std::vector
<TempFile
*> obj_files_
;
171 nacl::scoped_ptr
<nacl::DescWrapper
> invalid_desc_wrapper_
;
172 // Number of split modules (threads) for llc
173 int split_module_count_
;
175 // Translated nexe file, produced by the linker.
176 nacl::scoped_ptr
<TempFile
> temp_nexe_file_
;
178 // Used to report information when errors (PPAPI or otherwise) are reported.
179 ErrorInfo error_info_
;
181 // True if an error was already reported, and translate_notify_callback_
182 // was already run/consumed.
183 bool error_already_reported_
;
185 // State for timing and size information for UMA stats.
186 int64_t pexe_size_
; // Count as we stream -- will converge to pexe size.
187 int64_t pexe_bytes_compiled_
; // Count as we compile.
188 int64_t expected_pexe_size_
; // Expected download total (-1 if unknown).
190 // The helper thread used to do translations via SRPC.
191 // It accesses fields of PnaclCoordinator so it must have a
193 nacl::scoped_ptr
<PnaclTranslateThread
> translate_thread_
;
196 //----------------------------------------------------------------------
198 } // namespace plugin;
199 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_