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_LOCAL_TEMP_FILE_H_ 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_LOCAL_TEMP_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_rng.h" 11 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" 12 13 #include "ppapi/c/trusted/ppb_file_io_trusted.h" 14 #include "ppapi/utility/completion_callback_factory.h" 15 16 namespace pp { 17 class CompletionCallback; 18 class FileIO; 19 class FileRef; 20 class FileSystem; 21 } 22 23 namespace plugin { 24 25 class Plugin; 26 27 // Translation creates two temporary files. The first temporary file holds 28 // the object file created by llc. The second holds the nexe produced by 29 // the linker. Both of these temporary files are used to both write and 30 // read according to the following matrix: 31 // 32 // PnaclCoordinator::obj_file_: 33 // written by: llc (passed in explicitly through SRPC) 34 // read by: ld (returned via lookup service from SRPC) 35 // PnaclCoordinator::nexe_file_: 36 // written by: ld (passed in explicitly through SRPC) 37 // read by: sel_ldr (passed in explicitly to command channel) 38 // 39 40 // LocalTempFile represents a file used as a temporary between stages in 41 // translation. It is created in the local temporary file system of the page 42 // being processed. The name of the temporary file is a random 32-character 43 // hex string. Because both reading and writing are necessary, two I/O objects 44 // for the file are opened. 45 class LocalTempFile { 46 public: 47 // Create a LocalTempFile with a random name. 48 LocalTempFile(Plugin* plugin, 49 pp::FileSystem* file_system, 50 const nacl::string& base_dir); 51 // Create a LocalTempFile with a specific filename. 52 LocalTempFile(Plugin* plugin, 53 pp::FileSystem* file_system, 54 const nacl::string& base_dir, 55 const nacl::string& filename); 56 ~LocalTempFile(); 57 // Opens a writeable file IO object and descriptor referring to the file. 58 void OpenWrite(const pp::CompletionCallback& cb); 59 // Opens a read only file IO object and descriptor referring to the file. 60 void OpenRead(const pp::CompletionCallback& cb); 61 // Closes the open descriptors. 62 void Close(const pp::CompletionCallback& cb); 63 // Deletes the temporary file. 64 void Delete(const pp::CompletionCallback& cb); 65 // Renames the temporary file. 66 void Rename(const nacl::string& new_name, 67 const pp::CompletionCallback& cb); 68 void FinishRename(); 69 70 // Accessors. 71 // The nacl::DescWrapper* for the writeable version of the file. 72 nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); } 73 nacl::DescWrapper* release_write_wrapper() { 74 return write_wrapper_.release(); 75 } 76 // The nacl::DescWrapper* for the read-only version of the file. 77 nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); } 78 nacl::DescWrapper* release_read_wrapper() { 79 return read_wrapper_.release(); 80 } 81 // For quota management. 82 const nacl::string identifier() const { 83 return nacl::string(reinterpret_cast<const char*>(identifier_)); 84 } 85 pp::FileIO* write_file_io() const { return write_io_.get(); } 86 87 private: 88 NACL_DISALLOW_COPY_AND_ASSIGN(LocalTempFile); 89 90 void Initialize(); 91 92 // Gets the POSIX file descriptor for a resource. 93 int32_t GetFD(int32_t pp_error, 94 const pp::Resource& resource, 95 bool is_writable); 96 // Called when the writable file IO was opened. 97 void WriteFileDidOpen(int32_t pp_error); 98 // Called when the readable file IO was opened. 99 void ReadFileDidOpen(int32_t pp_error); 100 // Completes the close operation after quota update. 101 void CloseContinuation(int32_t pp_error); 102 103 Plugin* plugin_; 104 pp::FileSystem* file_system_; 105 const PPB_FileIOTrusted* file_io_trusted_; 106 pp::CompletionCallbackFactory<LocalTempFile> callback_factory_; 107 nacl::string base_dir_; 108 nacl::string filename_; 109 nacl::scoped_ptr<pp::FileRef> file_ref_; 110 // Temporarily holds the previous file ref during a rename operation. 111 nacl::scoped_ptr<pp::FileRef> old_ref_; 112 // The PPAPI and wrapper state for the writeable file. 113 nacl::scoped_ptr<pp::FileIO> write_io_; 114 nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_; 115 // The PPAPI and wrapper state for the read-only file. 116 nacl::scoped_ptr<pp::FileIO> read_io_; 117 nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_; 118 // The callback invoked when both file I/O objects are created. 119 pp::CompletionCallback done_callback_; 120 // Random number generator used to create filenames. 121 struct NaClDescRng *rng_desc_; 122 // An identifier string used for quota request processing. The quota 123 // interface needs a string that is unique per sel_ldr instance only, so 124 // the identifiers can be reused between runs of the translator, start-ups of 125 // the browser, etc. 126 uint8_t identifier_[16]; 127 // A counter to dole out unique identifiers. 128 static uint32_t next_identifier; 129 }; 130 131 } // namespace plugin 132 133 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_LOCAL_TEMP_FILE_H_ 134