Pepper_31_C++_interfaces
file_io.h
Go to the documentation of this file.
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 PPAPI_CPP_FILE_IO_H_
6 #define PPAPI_CPP_FILE_IO_H_
7 
8 #include "ppapi/c/pp_time.h"
9 #include "ppapi/cpp/completion_callback.h"
10 #include "ppapi/cpp/resource.h"
11 
12 /// @file
13 /// This file defines the API to create a file i/o object.
14 
15 struct PP_FileInfo;
16 
17 namespace pp {
18 
19 class FileRef;
20 class InstanceHandle;
21 
22 /// The <code>FileIO</code> class represents a regular file.
23 class FileIO : public Resource {
24  public:
25  /// Default constructor for creating an is_null() <code>FileIO</code>
26  /// object.
27  FileIO();
28 
29  /// A constructor used to create a <code>FileIO</code> and associate it with
30  /// the provided <code>Instance</code>.
31  ///
32  /// @param[in] instance The instance with which this resource will be
33  /// associated.
34  explicit FileIO(const InstanceHandle& instance);
35 
36  /// The copy constructor for <code>FileIO</code>.
37  ///
38  /// @param[in] other A reference to a <code>FileIO</code>.
39  FileIO(const FileIO& other);
40 
41  /// Open() opens the specified regular file for I/O according to the given
42  /// open flags, which is a bit-mask of the PP_FileOpenFlags values. Upon
43  /// success, the corresponding file is classified as "in use" by this FileIO
44  /// object until such time as the FileIO object is closed or destroyed.
45  ///
46  /// @param[in] file_ref A <code>PP_Resource</code> corresponding to a file
47  /// reference.
48  ///
49  /// @param[in] open_flags A bit-mask of the <code>PP_FileOpenFlags</code>
50  /// values. Valid values are:
51  /// - PP_FILEOPENFLAG_READ
52  /// - PP_FILEOPENFLAG_WRITE
53  /// - PP_FILEOPENFLAG_CREATE
54  /// - PP_FILEOPENFLAG_TRUNCATE
55  /// - PP_FILEOPENFLAG_EXCLUSIVE
56  /// See <code>PP_FileOpenFlags</code> in <code>ppb_file_io.h</code> for more
57  /// details on these flags.
58  ///
59  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
60  /// completion of Open().
61  ///
62  /// @return An int32_t containing an error code from
63  /// <code>pp_errors.h</code>.
64  int32_t Open(const FileRef& file_ref,
65  int32_t open_flags,
66  const CompletionCallback& cc);
67 
68  /// Query() queries info about the file opened by this FileIO object. This
69  /// function will fail if the FileIO object has not been opened.
70  ///
71  /// @param[in] result_buf The <code>PP_FileInfo</code> structure representing
72  /// all information about the file.
73  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
74  /// completion of Query().
75  ///
76  /// @return An int32_t containing an error code from
77  /// <code>pp_errors.h</code>.
78  int32_t Query(PP_FileInfo* result_buf,
79  const CompletionCallback& cc);
80 
81  /// Touch() Updates time stamps for the file opened by this FileIO object.
82  /// This function will fail if the FileIO object has not been opened.
83  ///
84  /// @param[in] last_access_time The last time the FileIO was accessed.
85  /// @param[in] last_modified_time The last time the FileIO was modified.
86  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
87  /// completion of Touch().
88  ///
89  /// @return An int32_t containing an error code from
90  /// <code>pp_errors.h</code>.
91  int32_t Touch(PP_Time last_access_time,
92  PP_Time last_modified_time,
93  const CompletionCallback& cc);
94 
95  /// Reads from an offset in the file.
96  ///
97  /// The size of the buffer must be large enough to hold the specified number
98  /// of bytes to read. This function might perform a partial read, meaning
99  /// that all the requested bytes might not be returned, even if the end of the
100  /// file has not been reached.
101  ///
102  /// This function reads into a buffer that the caller supplies. This buffer
103  /// must remain valid as long as the FileIO resource is alive. If you use
104  /// a completion callback factory and it goes out of scope, it will not issue
105  /// the callback on your class, BUT the callback factory can NOT cancel
106  /// the request from the browser's perspective. This means that the browser
107  /// will still try to write to your buffer even if the callback factory is
108  /// destroyed!
109  ///
110  /// So you must ensure that your buffer outlives the FileIO resource. If you
111  /// have one class and use the FileIO resource exclusively from that class
112  /// and never make any copies, this will be fine: the resource will be
113  /// destroyed when your class is. But keep in mind that copying a pp::FileIO
114  /// object just creates a second reference to the original resource. For
115  /// example, if you have a function like this:
116  /// pp::FileIO MyClass::GetFileIO();
117  /// where a copy of your FileIO resource could outlive your class, the
118  /// callback will still be pending when your class goes out of scope, creating
119  /// the possibility of writing into invalid memory. So it's recommended to
120  /// keep your FileIO resource and any output buffers tightly controlled in
121  /// the same scope.
122  ///
123  /// <strong>Caveat:</strong> This Read() is potentially unsafe if you're using
124  /// a CompletionCallbackFactory to scope callbacks to the lifetime of your
125  /// class. When your class goes out of scope, the callback factory will not
126  /// actually cancel the callback, but will rather just skip issuing the
127  /// callback on your class. This means that if the FileIO object outlives
128  /// your class (if you made a copy saved somewhere else, for example), then
129  /// the browser will still try to write into your buffer when the
130  /// asynchronous read completes, potentially causing a crash.
131  ///
132  /// See the other version of Read() which avoids this problem by writing into
133  /// CompletionCallbackWithOutput, where the output buffer is automatically
134  /// managed by the callback.
135  ///
136  /// @param[in] offset The offset into the file.
137  /// @param[in] buffer The buffer to hold the specified number of bytes read.
138  /// @param[in] bytes_to_read The number of bytes to read from
139  /// <code>offset</code>.
140  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
141  /// completion of Read().
142  ///
143  /// @return An The number of bytes read an error code from
144  /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
145  /// reached. It is valid to call Read() multiple times with a completion
146  /// callback to queue up parallel reads from the file at different offsets.
147  int32_t Read(int64_t offset,
148  char* buffer,
149  int32_t bytes_to_read,
150  const CompletionCallback& cc);
151 
152  /// Read() reads from an offset in the file. A PP_ArrayOutput must be
153  /// provided so that output will be stored in its allocated buffer. This
154  /// function might perform a partial read.
155  ///
156  /// @param[in] file_io A <code>PP_Resource</code> corresponding to a file
157  /// FileIO.
158  /// @param[in] offset The offset into the file.
159  /// @param[in] max_read_length The maximum number of bytes to read from
160  /// <code>offset</code>.
161  /// @param[in] output A <code>PP_ArrayOutput</code> to hold the output data.
162  /// @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
163  /// completion of Read().
164  ///
165  /// @return The number of bytes read or an error code from
166  /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
167  /// reached. It is valid to call Read() multiple times with a completion
168  /// callback to queue up parallel reads from the file, but pending reads
169  /// cannot be interleaved with other operations.
170  int32_t Read(int32_t offset,
171  int32_t max_read_length,
172  const CompletionCallbackWithOutput< std::vector<char> >& cc);
173 
174  /// Write() writes to an offset in the file. This function might perform a
175  /// partial write. The FileIO object must have been opened with write access.
176  ///
177  /// @param[in] offset The offset into the file.
178  /// @param[in] buffer The buffer to hold the specified number of bytes read.
179  /// @param[in] bytes_to_write The number of bytes to write to
180  /// <code>offset</code>.
181  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
182  /// completion of Write().
183  ///
184  /// @return An The number of bytes written or an error code from
185  /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
186  /// reached. It is valid to call Write() multiple times with a completion
187  /// callback to queue up parallel writes to the file at different offsets.
188  int32_t Write(int64_t offset,
189  const char* buffer,
190  int32_t bytes_to_write,
191  const CompletionCallback& cc);
192 
193  /// SetLength() sets the length of the file. If the file size is extended,
194  /// then the extended area of the file is zero-filled. The FileIO object must
195  /// have been opened with write access.
196  ///
197  /// @param[in] length The length of the file to be set.
198  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
199  /// completion of SetLength().
200  ///
201  /// @return An int32_t containing an error code from
202  /// <code>pp_errors.h</code>.
203  int32_t SetLength(int64_t length,
204  const CompletionCallback& cc);
205 
206  /// Flush() flushes changes to disk. This call can be very expensive!
207  ///
208  /// @param[in] cc A <code>CompletionCallback</code> to be called upon
209  /// completion of Flush().
210  ///
211  /// @return An int32_t containing an error code from
212  /// <code>pp_errors.h</code>.
213  int32_t Flush(const CompletionCallback& cc);
214 
215  /// Close() cancels any IO that may be pending, and closes the FileIO object.
216  /// Any pending callbacks will still run, reporting
217  /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is not
218  /// valid to call Open() again after a call to this method.
219  ///
220  /// <strong>Note:</strong> If the FileIO object is destroyed, and it is still
221  /// open, then it will be implicitly closed, so you are not required to call
222  /// Close().
223  void Close();
224 
225  private:
226  struct CallbackData1_0 {
227  PP_ArrayOutput output;
228  char* temp_buffer;
229  PP_CompletionCallback original_callback;
230  };
231 
232  // Provide backwards-compatibility for older Read versions. Converts the
233  // old-style "char*" output buffer of 1.0 to the new "PP_ArrayOutput"
234  // interface in 1.1.
235  //
236  // This takes a heap-allocated CallbackData1_0 struct passed as the user data
237  // and deletes it when the call completes.
238  static void CallbackConverter(void* user_data, int32_t result);
239 };
240 
241 } // namespace pp
242 
243 #endif // PPAPI_CPP_FILE_IO_H_
The FileIO class represents a regular file.
Definition: file_io.h:23
int32_t Query(PP_FileInfo *result_buf, const CompletionCallback &cc)
int32_t Touch(PP_Time last_access_time, PP_Time last_modified_time, const CompletionCallback &cc)
void Close()
int32_t Flush(const CompletionCallback &cc)
int32_t Write(int64_t offset, const char *buffer, int32_t bytes_to_write, const CompletionCallback &cc)
int32_t Read(int64_t offset, char *buffer, int32_t bytes_to_read, const CompletionCallback &cc)
int32_t SetLength(int64_t length, const CompletionCallback &cc)
int32_t Open(const FileRef &file_ref, int32_t open_flags, const CompletionCallback &cc)
A reference counted module resource.
Definition: resource.h:18