OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ | 5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ |
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ | 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <map> | 9 #include <map> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "native_client/src/include/nacl_macros.h" | 12 #include "native_client/src/include/nacl_macros.h" |
13 #include "native_client/src/include/nacl_string.h" | 13 #include "native_client/src/include/nacl_string.h" |
14 #include "native_client/src/shared/platform/nacl_sync_checked.h" | |
15 #include "native_client/src/shared/platform/nacl_sync_raii.h" | 14 #include "native_client/src/shared/platform/nacl_sync_raii.h" |
16 #include "native_client/src/shared/platform/nacl_threads.h" | 15 |
17 #include "native_client/src/shared/srpc/nacl_srpc.h" | 16 #include "native_client/src/shared/srpc/nacl_srpc.h" |
18 #include "native_client/src/trusted/desc/nacl_desc_rng.h" | 17 |
19 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" | 18 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
20 #include "native_client/src/trusted/plugin/delayed_callback.h" | 19 #include "native_client/src/trusted/plugin/delayed_callback.h" |
20 #include "native_client/src/trusted/plugin/local_temp_file.h" | |
21 #include "native_client/src/trusted/plugin/nacl_subprocess.h" | 21 #include "native_client/src/trusted/plugin/nacl_subprocess.h" |
22 #include "native_client/src/trusted/plugin/plugin_error.h" | 22 #include "native_client/src/trusted/plugin/plugin_error.h" |
23 #include "native_client/src/trusted/plugin/pnacl_resources.h" | 23 #include "native_client/src/trusted/plugin/pnacl_resources.h" |
24 | 24 |
25 #include "ppapi/c/pp_file_info.h" | 25 #include "ppapi/c/pp_file_info.h" |
26 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 26 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
27 #include "ppapi/cpp/completion_callback.h" | 27 #include "ppapi/cpp/completion_callback.h" |
28 #include "ppapi/cpp/file_io.h" | 28 #include "ppapi/cpp/file_io.h" |
29 #include "ppapi/cpp/file_ref.h" | 29 #include "ppapi/cpp/file_ref.h" |
30 #include "ppapi/cpp/file_system.h" | 30 #include "ppapi/cpp/file_system.h" |
31 | 31 |
32 struct NaClMutex; | |
33 | 32 |
34 namespace plugin { | 33 namespace plugin { |
35 | 34 |
36 class Manifest; | 35 class Manifest; |
37 class Plugin; | 36 class Plugin; |
38 class PnaclCoordinator; | 37 class PnaclCoordinator; |
39 | 38 class PnaclTranslateThread; |
40 // Translation creates two temporary files. The first temporary file holds | |
41 // the object file created by llc. The second holds the nexe produced by | |
42 // the linker. Both of these temporary files are used to both write and | |
43 // read according to the following matrix: | |
44 // | |
45 // PnaclCoordinator::obj_file_: | |
46 // written by: llc (passed in explicitly through SRPC) | |
47 // read by: ld (returned via lookup service from SRPC) | |
48 // PnaclCoordinator::nexe_file_: | |
49 // written by: ld (passed in explicitly through SRPC) | |
50 // read by: sel_ldr (passed in explicitly to command channel) | |
51 // | |
52 | |
53 // LocalTempFile represents a file used as a temporary between stages in | |
54 // translation. It is created in the local temporary file system of the page | |
55 // being processed. The name of the temporary file is a random 32-character | |
56 // hex string. Because both reading and writing are necessary, two I/O objects | |
57 // for the file are opened. | |
58 class LocalTempFile { | |
59 public: | |
60 // Create a LocalTempFile with a random name. | |
61 LocalTempFile(Plugin* plugin, | |
62 pp::FileSystem* file_system); | |
63 // Create a LocalTempFile with a specific filename. | |
64 LocalTempFile(Plugin* plugin, | |
65 pp::FileSystem* file_system, | |
66 const nacl::string& filename); | |
67 ~LocalTempFile(); | |
68 // Opens a writeable file IO object and descriptor referring to the file. | |
69 void OpenWrite(const pp::CompletionCallback& cb); | |
70 // Opens a read only file IO object and descriptor referring to the file. | |
71 void OpenRead(const pp::CompletionCallback& cb); | |
72 // Closes the open descriptors. | |
73 void Close(const pp::CompletionCallback& cb); | |
74 // Deletes the temporary file. | |
75 void Delete(const pp::CompletionCallback& cb); | |
76 // Renames the temporary file. | |
77 void Rename(const nacl::string& new_name, | |
78 const pp::CompletionCallback& cb); | |
79 void FinishRename(); | |
80 | |
81 // Accessors. | |
82 // The nacl::DescWrapper* for the writeable version of the file. | |
83 nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); } | |
84 nacl::DescWrapper* release_write_wrapper() { | |
85 return write_wrapper_.release(); | |
86 } | |
87 // The nacl::DescWrapper* for the read-only version of the file. | |
88 nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); } | |
89 nacl::DescWrapper* release_read_wrapper() { | |
90 return read_wrapper_.release(); | |
91 } | |
92 // For quota management. | |
93 const nacl::string identifier() const { | |
94 return nacl::string(reinterpret_cast<const char*>(identifier_)); | |
95 } | |
96 const pp::FileIO& write_file_io() const { return *write_io_; } | |
97 | |
98 private: | |
99 NACL_DISALLOW_COPY_AND_ASSIGN(LocalTempFile); | |
100 | |
101 void Initialize(); | |
102 | |
103 // Gets the POSIX file descriptor for a resource. | |
104 int32_t GetFD(int32_t pp_error, | |
105 const pp::Resource& resource, | |
106 bool is_writable); | |
107 // Called when the writable file IO was opened. | |
108 void WriteFileDidOpen(int32_t pp_error); | |
109 // Called when the readable file IO was opened. | |
110 void ReadFileDidOpen(int32_t pp_error); | |
111 // Completes the close operation after quota update. | |
112 void CloseContinuation(int32_t pp_error); | |
113 | |
114 Plugin* plugin_; | |
115 pp::FileSystem* file_system_; | |
116 const PPB_FileIOTrusted* file_io_trusted_; | |
117 pp::CompletionCallbackFactory<LocalTempFile> callback_factory_; | |
118 nacl::string filename_; | |
119 nacl::scoped_ptr<pp::FileRef> file_ref_; | |
120 // Temporarily holds the previous file ref during a rename operation. | |
121 nacl::scoped_ptr<pp::FileRef> old_ref_; | |
122 // The PPAPI and wrapper state for the writeable file. | |
123 nacl::scoped_ptr<pp::FileIO> write_io_; | |
124 nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_; | |
125 // The PPAPI and wrapper state for the read-only file. | |
126 nacl::scoped_ptr<pp::FileIO> read_io_; | |
127 nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_; | |
128 // The callback invoked when both file I/O objects are created. | |
129 pp::CompletionCallback done_callback_; | |
130 // Random number generator used to create filenames. | |
131 struct NaClDescRng *rng_desc_; | |
132 // An identifier string used for quota request processing. The quota | |
133 // interface needs a string that is unique per sel_ldr instance only, so | |
134 // the identifiers can be reused between runs of the translator, start-ups of | |
135 // the browser, etc. | |
136 uint8_t identifier_[16]; | |
137 // A counter to dole out unique identifiers. | |
138 static uint32_t next_identifier; | |
139 }; | |
140 | 39 |
141 // A thread safe reference counting class Needed for CompletionCallbackFactory | 40 // A thread safe reference counting class Needed for CompletionCallbackFactory |
142 // in PnaclCoordinator. | 41 // in PnaclCoordinator. |
143 class PnaclRefCount { | 42 class PnaclRefCount { |
144 public: | 43 public: |
145 PnaclRefCount() : ref_(0) { NaClXMutexCtor(&mu_); } | 44 PnaclRefCount() : ref_(0) { NaClXMutexCtor(&mu_); } |
146 ~PnaclRefCount() { NaClMutexDtor(&mu_); } | 45 ~PnaclRefCount() { NaClMutexDtor(&mu_); } |
147 int32_t AddRef() { | 46 int32_t AddRef() { |
148 nacl::MutexLocker ml(&mu_); | 47 nacl::MutexLocker ml(&mu_); |
149 return ++ref_; | 48 return ++ref_; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 void NexeFileWasRenamed(int32_t pp_error); | 176 void NexeFileWasRenamed(int32_t pp_error); |
278 // Invoked when the read descriptor for nexe_file_ is created. | 177 // Invoked when the read descriptor for nexe_file_ is created. |
279 void NexeReadDidOpen(int32_t pp_error); | 178 void NexeReadDidOpen(int32_t pp_error); |
280 // Invoked if there was an error and we've cleaned up the nexe_file_ temp. | 179 // Invoked if there was an error and we've cleaned up the nexe_file_ temp. |
281 void NexeFileWasDeleted(int32_t pp_error); | 180 void NexeFileWasDeleted(int32_t pp_error); |
282 | 181 |
283 // Once llc and ld nexes have been loaded and the two temporary files have | 182 // Once llc and ld nexes have been loaded and the two temporary files have |
284 // been created, this starts the translation. Translation starts two | 183 // been created, this starts the translation. Translation starts two |
285 // subprocesses, one for llc and one for ld. | 184 // subprocesses, one for llc and one for ld. |
286 void RunTranslate(int32_t pp_error); | 185 void RunTranslate(int32_t pp_error); |
287 // Starts an individual llc or ld subprocess used for translation. | 186 |
288 NaClSubprocess* StartSubprocess(const nacl::string& url, | |
289 const Manifest* manifest); | |
290 // PnaclCoordinator creates a helper thread to allow translations to be | |
291 // invoked via SRPC. This is the helper thread function for translation. | |
292 static void WINAPI DoTranslateThread(void* arg); | |
293 // Returns true if a the translate thread and subprocesses should stop. | |
294 bool SubprocessesShouldDie(); | |
295 // Signal the translate thread and subprocesses that they should stop. | |
296 void SetSubprocessesShouldDie(bool subprocesses_should_die); | |
297 // Signal that Pnacl translation completed normally. | |
jvoung - send to chromium...
2012/05/15 23:51:05
Leave the comment for TranslateFinished?
Derek Schuff
2012/05/16 16:47:46
Done.
| |
298 void TranslateFinished(int32_t pp_error); | 187 void TranslateFinished(int32_t pp_error); |
299 // Keeps track of the pp_error upon entry to TranslateFinished, | 188 // Keeps track of the pp_error upon entry to TranslateFinished, |
300 // for inspection after cleanup. | 189 // for inspection after cleanup. |
301 int32_t translate_finish_error_; | 190 int32_t translate_finish_error_; |
302 | 191 |
303 // Signal that Pnacl translation failed, from the translation thread only. | |
304 void TranslateFailed(const nacl::string& error_string); | |
305 | |
306 // The plugin owning the nexe for which we are doing translation. | 192 // The plugin owning the nexe for which we are doing translation. |
307 Plugin* plugin_; | 193 Plugin* plugin_; |
308 | 194 |
309 pp::CompletionCallback translate_notify_callback_; | 195 pp::CompletionCallback translate_notify_callback_; |
310 // PnaclRefCount is only needed to support file lookups. | 196 // PnaclRefCount is only needed to support file lookups. |
311 // TODO(sehr): remove this when file lookup is through ReverseService. | 197 // TODO(sehr): remove this when file lookup is through ReverseService. |
312 pp::CompletionCallbackFactory<PnaclCoordinator, | 198 pp::CompletionCallbackFactory<PnaclCoordinator, |
313 PnaclRefCount> callback_factory_; | 199 PnaclRefCount> callback_factory_; |
314 | 200 |
315 // True if the translation thread and related subprocesses should exit. | |
316 bool subprocesses_should_die_; | |
317 // Used to guard and publish subprocesses_should_die_. | |
318 struct NaClMutex subprocess_mu_; | |
319 | |
320 // Nexe from the final native Link. | 201 // Nexe from the final native Link. |
321 nacl::scoped_ptr<nacl::DescWrapper> translated_fd_; | 202 nacl::scoped_ptr<nacl::DescWrapper> translated_fd_; |
322 | 203 |
323 // The helper thread used to do translations via SRPC. | 204 // The helper thread used to do translations via SRPC. |
324 nacl::scoped_ptr<NaClThread> translate_thread_; | 205 nacl::scoped_ptr<PnaclTranslateThread> translate_thread_; |
325 // Translation creates local temporary files. | 206 // Translation creates local temporary files. |
326 nacl::scoped_ptr<pp::FileSystem> file_system_; | 207 nacl::scoped_ptr<pp::FileSystem> file_system_; |
327 // The manifest used by resource loading and llc's reverse service to look up | 208 // The manifest used by resource loading and llc's reverse service to look up |
328 // objects and libraries. | 209 // objects and libraries. |
329 nacl::scoped_ptr<const Manifest> manifest_; | 210 nacl::scoped_ptr<const Manifest> manifest_; |
330 // TEMPORARY: ld needs to look up dynamic libraries in the nexe's manifest | 211 // TEMPORARY: ld needs to look up dynamic libraries in the nexe's manifest |
331 // until metadata is complete in pexes. This manifest lookup allows looking | 212 // until metadata is complete in pexes. This manifest lookup allows looking |
332 // for whether a resource requested by ld is in the nexe manifest first, and | 213 // for whether a resource requested by ld is in the nexe manifest first, and |
333 // if not, then consults the extension manifest. | 214 // if not, then consults the extension manifest. |
334 // TODO(sehr,jvoung,pdox): remove this when metadata is correct. | 215 // TODO(sehr,jvoung,pdox): remove this when metadata is correct. |
(...skipping 10 matching lines...) Expand all Loading... | |
345 // The URL for the pexe file. | 226 // The URL for the pexe file. |
346 nacl::string pexe_url_; | 227 nacl::string pexe_url_; |
347 // Optional cache identity for translation caching. | 228 // Optional cache identity for translation caching. |
348 nacl::string cache_identity_; | 229 nacl::string cache_identity_; |
349 // Borrowed reference which must outlive the thread. | 230 // Borrowed reference which must outlive the thread. |
350 nacl::scoped_ptr<nacl::DescWrapper> pexe_wrapper_; | 231 nacl::scoped_ptr<nacl::DescWrapper> pexe_wrapper_; |
351 // Object file, produced by the translator and consumed by the linker. | 232 // Object file, produced by the translator and consumed by the linker. |
352 nacl::scoped_ptr<LocalTempFile> obj_file_; | 233 nacl::scoped_ptr<LocalTempFile> obj_file_; |
353 // Translated nexe file, produced by the linker and consumed by sel_ldr. | 234 // Translated nexe file, produced by the linker and consumed by sel_ldr. |
354 nacl::scoped_ptr<LocalTempFile> nexe_file_; | 235 nacl::scoped_ptr<LocalTempFile> nexe_file_; |
355 // Callback to run when tasks are completed or an error has occurred. | |
356 pp::CompletionCallback report_translate_finished_; | |
357 | 236 |
358 // Used to report information when errors (PPAPI or otherwise) are reported. | 237 // Used to report information when errors (PPAPI or otherwise) are reported. |
359 ErrorInfo error_info_; | 238 ErrorInfo error_info_; |
360 // True if an error was already reported, and translate_notify_callback_ | 239 // True if an error was already reported, and translate_notify_callback_ |
361 // was already run/consumed. | 240 // was already run/consumed. |
362 bool error_already_reported_; | 241 bool error_already_reported_; |
363 }; | 242 }; |
364 | 243 |
365 //---------------------------------------------------------------------- | 244 //---------------------------------------------------------------------- |
366 | 245 |
367 } // namespace plugin; | 246 } // namespace plugin; |
368 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ | 247 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_COORDINATOR_H_ |
OLD | NEW |