[email protected] | acbb8b1 | 2012-01-13 18:52:22 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 10 | #include "ppapi/c/pp_time.h" |
[email protected] | bcc801f | 2012-11-16 07:41:09 | [diff] [blame] | 11 | #include "ppapi/cpp/completion_callback.h" |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 12 | #include "ppapi/cpp/resource.h" |
| 13 | |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 14 | /// @file |
| 15 | /// This file defines the API to create a file i/o object. |
| 16 | |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 17 | struct PP_FileInfo; |
| 18 | |
| 19 | namespace pp { |
| 20 | |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 21 | class FileRef; |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 22 | class InstanceHandle; |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 23 | |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 24 | /// The <code>FileIO</code> class represents a regular file. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 25 | class FileIO : public Resource { |
| 26 | public: |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 27 | /// Default constructor for creating an is_null() <code>FileIO</code> |
| 28 | /// object. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 29 | FileIO(); |
| 30 | |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 31 | /// A constructor used to create a <code>FileIO</code> and associate it with |
| 32 | /// the provided <code>Instance</code>. |
| 33 | /// |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 34 | /// @param[in] instance The instance with which this resource will be |
| 35 | /// associated. |
| 36 | explicit FileIO(const InstanceHandle& instance); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 37 | |
| 38 | /// The copy constructor for <code>FileIO</code>. |
| 39 | /// |
[email protected] | deac5e5 | 2013-05-09 05:29:36 | [diff] [blame] | 40 | /// @param[in] other A reference to a <code>FileIO</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 41 | FileIO(const FileIO& other); |
| 42 | |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 43 | /// 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] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 50 | /// |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 51 | /// @param[in] open_flags A bit-mask of the <code>PP_FileOpenFlags</code> |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 52 | /// 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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 61 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 62 | /// completion of Open(). |
| 63 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 64 | /// @return An int32_t containing an error code from |
| 65 | /// <code>pp_errors.h</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 66 | int32_t Open(const FileRef& file_ref, |
| 67 | int32_t open_flags, |
| 68 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 69 | |
| 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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 73 | /// @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] | 452fb14 | 2013-10-23 01:49:43 | [diff] [blame] | 76 | /// 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] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 79 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 80 | /// @return An int32_t containing an error code from |
| 81 | /// <code>pp_errors.h</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 82 | int32_t Query(PP_FileInfo* result_buf, |
| 83 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 84 | |
| 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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 90 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 91 | /// completion of Touch(). |
| 92 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 93 | /// @return An int32_t containing an error code from |
| 94 | /// <code>pp_errors.h</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 95 | int32_t Touch(PP_Time last_access_time, |
| 96 | PP_Time last_modified_time, |
| 97 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 98 | |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 99 | /// 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] | 935d00fd | 2013-03-29 22:26:15 | [diff] [blame] | 124 | /// keep your FileIO resource and any output buffers tightly controlled in |
[email protected] | 844fecb | 2012-11-16 20:11:06 | [diff] [blame] | 125 | /// the same scope. |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 126 | /// |
[email protected] | bcc801f | 2012-11-16 07:41:09 | [diff] [blame] | 127 | /// <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] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 140 | /// @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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 144 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | 452fb14 | 2013-10-23 01:49:43 | [diff] [blame] | 145 | /// 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] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 148 | /// |
| 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] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 153 | int32_t Read(int64_t offset, |
| 154 | char* buffer, |
| 155 | int32_t bytes_to_read, |
| 156 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 157 | |
[email protected] | bcc801f | 2012-11-16 07:41:09 | [diff] [blame] | 158 | /// 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] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 180 | /// 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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 187 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 188 | /// 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] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 194 | int32_t Write(int64_t offset, |
| 195 | const char* buffer, |
| 196 | int32_t bytes_to_write, |
| 197 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 198 | |
| 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] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 204 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 205 | /// completion of SetLength(). |
| 206 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 207 | /// @return An int32_t containing an error code from |
| 208 | /// <code>pp_errors.h</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 209 | int32_t SetLength(int64_t length, |
| 210 | const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 211 | |
| 212 | /// Flush() flushes changes to disk. This call can be very expensive! |
| 213 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 214 | /// @param[in] cc A <code>CompletionCallback</code> to be called upon |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 215 | /// completion of Flush(). |
| 216 | /// |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 217 | /// @return An int32_t containing an error code from |
| 218 | /// <code>pp_errors.h</code>. |
[email protected] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 219 | int32_t Flush(const CompletionCallback& cc); |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 220 | |
| 221 | /// Close() cancels any IO that may be pending, and closes the FileIO object. |
| 222 | /// Any pending callbacks will still run, reporting |
[email protected] | acbb8b1 | 2012-01-13 18:52:22 | [diff] [blame] | 223 | /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is not |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 224 | /// valid to call Open() again after a call to this method. |
[email protected] | ea8063f | 2011-08-09 22:17:31 | [diff] [blame] | 225 | /// |
[email protected] | c7c00208 | 2011-07-08 18:00:05 | [diff] [blame] | 226 | /// <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] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 229 | void Close(); |
[email protected] | bcc801f | 2012-11-16 07:41:09 | [diff] [blame] | 230 | |
| 231 | private: |
| 232 | struct CallbackData1_0 { |
| 233 | PP_ArrayOutput output; |
| 234 | char* temp_buffer; |
| 235 | PP_CompletionCallback original_callback; |
| 236 | }; |
| 237 | |
[email protected] | 935d00fd | 2013-03-29 22:26:15 | [diff] [blame] | 238 | // Provide backwards-compatibility for older Read versions. Converts the |
[email protected] | bcc801f | 2012-11-16 07:41:09 | [diff] [blame] | 239 | // 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] | 126f4fb | 2011-06-27 20:18:59 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | } // namespace pp |
| 248 | |
| 249 | #endif // PPAPI_CPP_FILE_IO_H_ |