blob: 3f3faaa7bf9c2127ea1ccf34598af88851967d67 [file] [log] [blame]
[email protected]acbb8b12012-01-13 18:52:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]126f4fb2011-06-27 20:18:592// 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
avie029c4132015-12-23 06:45:228#include <stdint.h>
9
[email protected]126f4fb2011-06-27 20:18:5910#include "ppapi/c/pp_time.h"
[email protected]bcc801f2012-11-16 07:41:0911#include "ppapi/cpp/completion_callback.h"
[email protected]126f4fb2011-06-27 20:18:5912#include "ppapi/cpp/resource.h"
13
[email protected]c7c002082011-07-08 18:00:0514/// @file
15/// This file defines the API to create a file i/o object.
16
[email protected]126f4fb2011-06-27 20:18:5917struct PP_FileInfo;
18
19namespace pp {
20
[email protected]126f4fb2011-06-27 20:18:5921class FileRef;
[email protected]09af0f72012-02-27 20:23:1922class InstanceHandle;
[email protected]126f4fb2011-06-27 20:18:5923
[email protected]c7c002082011-07-08 18:00:0524/// The <code>FileIO</code> class represents a regular file.
[email protected]126f4fb2011-06-27 20:18:5925class FileIO : public Resource {
26 public:
[email protected]c7c002082011-07-08 18:00:0527 /// Default constructor for creating an is_null() <code>FileIO</code>
28 /// object.
[email protected]126f4fb2011-06-27 20:18:5929 FileIO();
30
[email protected]c7c002082011-07-08 18:00:0531 /// A constructor used to create a <code>FileIO</code> and associate it with
32 /// the provided <code>Instance</code>.
33 ///
[email protected]09af0f72012-02-27 20:23:1934 /// @param[in] instance The instance with which this resource will be
35 /// associated.
36 explicit FileIO(const InstanceHandle& instance);
[email protected]c7c002082011-07-08 18:00:0537
38 /// The copy constructor for <code>FileIO</code>.
39 ///
[email protected]deac5e52013-05-09 05:29:3640 /// @param[in] other A reference to a <code>FileIO</code>.
[email protected]126f4fb2011-06-27 20:18:5941 FileIO(const FileIO& other);
42
[email protected]c7c002082011-07-08 18:00:0543 /// Open() opens the specified regular file for I/O according to the given
44 /// open flags, which is a bit-mask of the PP_FileOpenFlags values. Upon
45 /// success, the corresponding file is classified as "in use" by this FileIO
46 /// object until such time as the FileIO object is closed or destroyed.
47 ///
48 /// @param[in] file_ref A <code>PP_Resource</code> corresponding to a file
49 /// reference.
[email protected]844fecb2012-11-16 20:11:0650 ///
[email protected]c7c002082011-07-08 18:00:0551 /// @param[in] open_flags A bit-mask of the <code>PP_FileOpenFlags</code>
[email protected]844fecb2012-11-16 20:11:0652 /// values. Valid values are:
53 /// - PP_FILEOPENFLAG_READ
54 /// - PP_FILEOPENFLAG_WRITE
55 /// - PP_FILEOPENFLAG_CREATE
56 /// - PP_FILEOPENFLAG_TRUNCATE
57 /// - PP_FILEOPENFLAG_EXCLUSIVE
58 /// See <code>PP_FileOpenFlags</code> in <code>ppb_file_io.h</code> for more
59 /// details on these flags.
60 ///
[email protected]ea8063f2011-08-09 22:17:3161 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]c7c002082011-07-08 18:00:0562 /// completion of Open().
63 ///
[email protected]ea8063f2011-08-09 22:17:3164 /// @return An int32_t containing an error code from
65 /// <code>pp_errors.h</code>.
[email protected]126f4fb2011-06-27 20:18:5966 int32_t Open(const FileRef& file_ref,
67 int32_t open_flags,
68 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:0569
70 /// Query() queries info about the file opened by this FileIO object. This
71 /// function will fail if the FileIO object has not been opened.
72 ///
[email protected]ea8063f2011-08-09 22:17:3173 /// @param[in] result_buf The <code>PP_FileInfo</code> structure representing
74 /// all information about the file.
75 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]452fb142013-10-23 01:49:4376 /// completion of Query(). <code>result_buf</code> must remain valid until
77 /// after the callback runs. If you pass a blocking callback,
78 /// <code>result_buf</code> must remain valid until after Query() returns.
[email protected]c7c002082011-07-08 18:00:0579 ///
[email protected]ea8063f2011-08-09 22:17:3180 /// @return An int32_t containing an error code from
81 /// <code>pp_errors.h</code>.
[email protected]126f4fb2011-06-27 20:18:5982 int32_t Query(PP_FileInfo* result_buf,
83 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:0584
85 /// Touch() Updates time stamps for the file opened by this FileIO object.
86 /// This function will fail if the FileIO object has not been opened.
87 ///
88 /// @param[in] last_access_time The last time the FileIO was accessed.
89 /// @param[in] last_modified_time The last time the FileIO was modified.
[email protected]ea8063f2011-08-09 22:17:3190 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]c7c002082011-07-08 18:00:0591 /// completion of Touch().
92 ///
[email protected]ea8063f2011-08-09 22:17:3193 /// @return An int32_t containing an error code from
94 /// <code>pp_errors.h</code>.
[email protected]126f4fb2011-06-27 20:18:5995 int32_t Touch(PP_Time last_access_time,
96 PP_Time last_modified_time,
97 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:0598
[email protected]844fecb2012-11-16 20:11:0699 /// Reads from an offset in the file.
100 ///
101 /// The size of the buffer must be large enough to hold the specified number
102 /// of bytes to read. This function might perform a partial read, meaning
103 /// that all the requested bytes might not be returned, even if the end of the
104 /// file has not been reached.
105 ///
106 /// This function reads into a buffer that the caller supplies. This buffer
107 /// must remain valid as long as the FileIO resource is alive. If you use
108 /// a completion callback factory and it goes out of scope, it will not issue
109 /// the callback on your class, BUT the callback factory can NOT cancel
110 /// the request from the browser's perspective. This means that the browser
111 /// will still try to write to your buffer even if the callback factory is
112 /// destroyed!
113 ///
114 /// So you must ensure that your buffer outlives the FileIO resource. If you
115 /// have one class and use the FileIO resource exclusively from that class
116 /// and never make any copies, this will be fine: the resource will be
117 /// destroyed when your class is. But keep in mind that copying a pp::FileIO
118 /// object just creates a second reference to the original resource. For
119 /// example, if you have a function like this:
120 /// pp::FileIO MyClass::GetFileIO();
121 /// where a copy of your FileIO resource could outlive your class, the
122 /// callback will still be pending when your class goes out of scope, creating
123 /// the possibility of writing into invalid memory. So it's recommended to
[email protected]935d00fd2013-03-29 22:26:15124 /// keep your FileIO resource and any output buffers tightly controlled in
[email protected]844fecb2012-11-16 20:11:06125 /// the same scope.
[email protected]c7c002082011-07-08 18:00:05126 ///
[email protected]bcc801f2012-11-16 07:41:09127 /// <strong>Caveat:</strong> This Read() is potentially unsafe if you're using
128 /// a CompletionCallbackFactory to scope callbacks to the lifetime of your
129 /// class. When your class goes out of scope, the callback factory will not
130 /// actually cancel the callback, but will rather just skip issuing the
131 /// callback on your class. This means that if the FileIO object outlives
132 /// your class (if you made a copy saved somewhere else, for example), then
133 /// the browser will still try to write into your buffer when the
134 /// asynchronous read completes, potentially causing a crash.
135 ///
136 /// See the other version of Read() which avoids this problem by writing into
137 /// CompletionCallbackWithOutput, where the output buffer is automatically
138 /// managed by the callback.
139 ///
[email protected]c7c002082011-07-08 18:00:05140 /// @param[in] offset The offset into the file.
141 /// @param[in] buffer The buffer to hold the specified number of bytes read.
142 /// @param[in] bytes_to_read The number of bytes to read from
143 /// <code>offset</code>.
[email protected]ea8063f2011-08-09 22:17:31144 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]452fb142013-10-23 01:49:43145 /// completion of Read(). <code>buffer</code> must remain valid until after
146 /// the callback runs. If you pass a blocking callback, <code>buffer</code>
147 /// must remain valid until after Read() returns.
[email protected]c7c002082011-07-08 18:00:05148 ///
149 /// @return An The number of bytes read an error code from
150 /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
151 /// reached. It is valid to call Read() multiple times with a completion
152 /// callback to queue up parallel reads from the file at different offsets.
[email protected]126f4fb2011-06-27 20:18:59153 int32_t Read(int64_t offset,
154 char* buffer,
155 int32_t bytes_to_read,
156 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:05157
[email protected]bcc801f2012-11-16 07:41:09158 /// Read() reads from an offset in the file. A PP_ArrayOutput must be
159 /// provided so that output will be stored in its allocated buffer. This
160 /// function might perform a partial read.
161 ///
162 /// @param[in] file_io A <code>PP_Resource</code> corresponding to a file
163 /// FileIO.
164 /// @param[in] offset The offset into the file.
165 /// @param[in] max_read_length The maximum number of bytes to read from
166 /// <code>offset</code>.
167 /// @param[in] output A <code>PP_ArrayOutput</code> to hold the output data.
168 /// @param[in] callback A <code>PP_CompletionCallback</code> to be called upon
169 /// completion of Read().
170 ///
171 /// @return The number of bytes read or an error code from
172 /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
173 /// reached. It is valid to call Read() multiple times with a completion
174 /// callback to queue up parallel reads from the file, but pending reads
175 /// cannot be interleaved with other operations.
176 int32_t Read(int32_t offset,
177 int32_t max_read_length,
178 const CompletionCallbackWithOutput< std::vector<char> >& cc);
179
[email protected]c7c002082011-07-08 18:00:05180 /// Write() writes to an offset in the file. This function might perform a
181 /// partial write. The FileIO object must have been opened with write access.
182 ///
183 /// @param[in] offset The offset into the file.
184 /// @param[in] buffer The buffer to hold the specified number of bytes read.
185 /// @param[in] bytes_to_write The number of bytes to write to
186 /// <code>offset</code>.
[email protected]ea8063f2011-08-09 22:17:31187 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]c7c002082011-07-08 18:00:05188 /// completion of Write().
189 ///
190 /// @return An The number of bytes written or an error code from
191 /// <code>pp_errors.h</code>. If the return value is 0, then end-of-file was
192 /// reached. It is valid to call Write() multiple times with a completion
193 /// callback to queue up parallel writes to the file at different offsets.
[email protected]126f4fb2011-06-27 20:18:59194 int32_t Write(int64_t offset,
195 const char* buffer,
196 int32_t bytes_to_write,
197 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:05198
199 /// SetLength() sets the length of the file. If the file size is extended,
200 /// then the extended area of the file is zero-filled. The FileIO object must
201 /// have been opened with write access.
202 ///
203 /// @param[in] length The length of the file to be set.
[email protected]ea8063f2011-08-09 22:17:31204 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]c7c002082011-07-08 18:00:05205 /// completion of SetLength().
206 ///
[email protected]ea8063f2011-08-09 22:17:31207 /// @return An int32_t containing an error code from
208 /// <code>pp_errors.h</code>.
[email protected]126f4fb2011-06-27 20:18:59209 int32_t SetLength(int64_t length,
210 const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:05211
212 /// Flush() flushes changes to disk. This call can be very expensive!
213 ///
[email protected]ea8063f2011-08-09 22:17:31214 /// @param[in] cc A <code>CompletionCallback</code> to be called upon
[email protected]c7c002082011-07-08 18:00:05215 /// completion of Flush().
216 ///
[email protected]ea8063f2011-08-09 22:17:31217 /// @return An int32_t containing an error code from
218 /// <code>pp_errors.h</code>.
[email protected]126f4fb2011-06-27 20:18:59219 int32_t Flush(const CompletionCallback& cc);
[email protected]c7c002082011-07-08 18:00:05220
221 /// Close() cancels any IO that may be pending, and closes the FileIO object.
222 /// Any pending callbacks will still run, reporting
[email protected]acbb8b12012-01-13 18:52:22223 /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is not
[email protected]c7c002082011-07-08 18:00:05224 /// valid to call Open() again after a call to this method.
[email protected]ea8063f2011-08-09 22:17:31225 ///
[email protected]c7c002082011-07-08 18:00:05226 /// <strong>Note:</strong> If the FileIO object is destroyed, and it is still
227 /// open, then it will be implicitly closed, so you are not required to call
228 /// Close().
[email protected]126f4fb2011-06-27 20:18:59229 void Close();
[email protected]bcc801f2012-11-16 07:41:09230
231 private:
232 struct CallbackData1_0 {
233 PP_ArrayOutput output;
234 char* temp_buffer;
235 PP_CompletionCallback original_callback;
236 };
237
[email protected]935d00fd2013-03-29 22:26:15238 // Provide backwards-compatibility for older Read versions. Converts the
[email protected]bcc801f2012-11-16 07:41:09239 // old-style "char*" output buffer of 1.0 to the new "PP_ArrayOutput"
240 // interface in 1.1.
241 //
242 // This takes a heap-allocated CallbackData1_0 struct passed as the user data
243 // and deletes it when the call completes.
244 static void CallbackConverter(void* user_data, int32_t result);
[email protected]126f4fb2011-06-27 20:18:59245};
246
247} // namespace pp
248
249#endif // PPAPI_CPP_FILE_IO_H_