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. 4 5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_ 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_ 7 8 #include "native_client/src/include/nacl_macros.h" 9 #include "native_client/src/include/nacl_string.h" 10 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" 11 12 #include "ppapi/c/private/pp_file_handle.h" 13 #include "ppapi/cpp/completion_callback.h" 14 15 namespace plugin { 16 17 class Plugin; 18 19 // Translation creates two temporary files. The first temporary file holds 20 // the object file created by llc. The second holds the nexe produced by 21 // the linker. Both of these temporary files are used to both write and 22 // read according to the following matrix: 23 // 24 // PnaclCoordinator::obj_file_: 25 // written by: llc (passed in explicitly through SRPC) 26 // read by: ld (returned via lookup service from SRPC) 27 // PnaclCoordinator::nexe_file_: 28 // written by: ld (passed in explicitly through SRPC) 29 // read by: sel_ldr (passed in explicitly to command channel) 30 // 31 32 // TempFile represents a file used as a temporary between stages in 33 // translation. It is automatically deleted when all handles are closed 34 // (or earlier -- immediately unlinked on POSIX systems). The file is only 35 // opened, once, but because both reading and writing are necessary (and in 36 // different processes), the user should reset / seek back to the beginning 37 // of the file between sessions. 38 class TempFile { 39 public: 40 // Create a TempFile. 41 explicit TempFile(Plugin* plugin); 42 ~TempFile(); 43 44 // Set an existing Fd instead of getting one from the nacl interface on open. 45 // Must be called before Open. 46 bool SetExistingFd(PP_FileHandle handle); 47 // Opens a temporary file object and descriptor wrapper referring to the file. 48 // If |writeable| is true, the descriptor will be opened for writing, and 49 // write_wrapper will return a valid pointer, otherwise it will return NULL. 50 void Open(const pp::CompletionCallback& cb, bool writeable); 51 // Resets file position of the handle, for reuse. 52 bool Reset(); 53 54 // Accessors. 55 // The nacl::DescWrapper* for the writeable version of the file. 56 nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); } 57 nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); } 58 nacl::DescWrapper* release_read_wrapper() { 59 return read_wrapper_.release(); 60 } 61 62 // For quota management. 63 const nacl::string identifier() const { 64 return nacl::string(reinterpret_cast<const char*>(identifier_)); 65 } 66 67 private: 68 NACL_DISALLOW_COPY_AND_ASSIGN(TempFile); 69 70 Plugin* plugin_; 71 nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_; 72 nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_; 73 PP_FileHandle existing_handle_; 74 75 // An identifier string used for quota request processing. The quota 76 // interface needs a string that is unique per sel_ldr instance only, so 77 // the identifiers can be reused between runs of the translator, start-ups of 78 // the browser, etc. 79 uint8_t identifier_[16]; 80 // A counter to dole out unique identifiers. 81 static uint32_t next_identifier; 82 }; 83 84 } // namespace plugin 85 86 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_ 87