OLD | NEW |
---|---|
(Empty) | |
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 { | |
jvoung - send to chromium...
2012/05/15 23:51:05
Maybe prefix the name with "Pnacl", but... up to y
| |
46 public: | |
47 // Create a LocalTempFile with a random name. | |
48 LocalTempFile(Plugin* plugin, | |
49 pp::FileSystem* file_system); | |
50 // Create a LocalTempFile with a specific filename. | |
51 LocalTempFile(Plugin* plugin, | |
52 pp::FileSystem* file_system, | |
53 const nacl::string& filename); | |
54 ~LocalTempFile(); | |
55 // Opens a writeable file IO object and descriptor referring to the file. | |
56 void OpenWrite(const pp::CompletionCallback& cb); | |
57 // Opens a read only file IO object and descriptor referring to the file. | |
58 void OpenRead(const pp::CompletionCallback& cb); | |
59 // Closes the open descriptors. | |
60 void Close(const pp::CompletionCallback& cb); | |
61 // Deletes the temporary file. | |
62 void Delete(const pp::CompletionCallback& cb); | |
63 // Renames the temporary file. | |
64 void Rename(const nacl::string& new_name, | |
65 const pp::CompletionCallback& cb); | |
66 void FinishRename(); | |
67 | |
68 // Accessors. | |
69 // The nacl::DescWrapper* for the writeable version of the file. | |
70 nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); } | |
71 nacl::DescWrapper* release_write_wrapper() { | |
72 return write_wrapper_.release(); | |
73 } | |
74 // The nacl::DescWrapper* for the read-only version of the file. | |
75 nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); } | |
76 nacl::DescWrapper* release_read_wrapper() { | |
77 return read_wrapper_.release(); | |
78 } | |
79 // For quota management. | |
80 const nacl::string identifier() const { | |
81 return nacl::string(reinterpret_cast<const char*>(identifier_)); | |
82 } | |
83 const pp::FileIO& write_file_io() const { return *write_io_; } | |
84 | |
85 const static nacl::string& GetPnaclTempDir() { return kPnaclTempDir; } | |
Derek Schuff
2012/05/15 23:17:20
this replaces kPnaclTempDir in pnacl_coordinator.c
| |
86 | |
87 private: | |
88 NACL_DISALLOW_COPY_AND_ASSIGN(LocalTempFile); | |
89 | |
90 const static nacl::string kPnaclTempDir; | |
91 | |
92 void Initialize(); | |
93 | |
94 // Gets the POSIX file descriptor for a resource. | |
95 int32_t GetFD(int32_t pp_error, | |
96 const pp::Resource& resource, | |
97 bool is_writable); | |
98 // Called when the writable file IO was opened. | |
99 void WriteFileDidOpen(int32_t pp_error); | |
100 // Called when the readable file IO was opened. | |
101 void ReadFileDidOpen(int32_t pp_error); | |
102 // Completes the close operation after quota update. | |
103 void CloseContinuation(int32_t pp_error); | |
104 | |
105 Plugin* plugin_; | |
106 pp::FileSystem* file_system_; | |
107 const PPB_FileIOTrusted* file_io_trusted_; | |
108 pp::CompletionCallbackFactory<LocalTempFile> callback_factory_; | |
109 nacl::string filename_; | |
110 nacl::scoped_ptr<pp::FileRef> file_ref_; | |
111 // Temporarily holds the previous file ref during a rename operation. | |
112 nacl::scoped_ptr<pp::FileRef> old_ref_; | |
113 // The PPAPI and wrapper state for the writeable file. | |
114 nacl::scoped_ptr<pp::FileIO> write_io_; | |
115 nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_; | |
116 // The PPAPI and wrapper state for the read-only file. | |
117 nacl::scoped_ptr<pp::FileIO> read_io_; | |
118 nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_; | |
119 // The callback invoked when both file I/O objects are created. | |
120 pp::CompletionCallback done_callback_; | |
121 // Random number generator used to create filenames. | |
122 struct NaClDescRng *rng_desc_; | |
123 // An identifier string used for quota request processing. The quota | |
124 // interface needs a string that is unique per sel_ldr instance only, so | |
125 // the identifiers can be reused between runs of the translator, start-ups of | |
126 // the browser, etc. | |
127 uint8_t identifier_[16]; | |
128 // A counter to dole out unique identifiers. | |
129 static uint32_t next_identifier; | |
130 }; | |
131 | |
132 } // namespace plugin | |
133 | |
134 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_LOCAL_TEMP_FILE_H_ | |
OLD | NEW |