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 WEBKIT_BLOB_FILE_STREAM_READER_H_ 6 #define WEBKIT_BLOB_FILE_STREAM_READER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "net/base/completion_callback.h" 11 #include "webkit/browser/webkit_storage_browser_export.h" 12 13 namespace base { 14 class FilePath; 15 class TaskRunner; 16 class Time; 17 } 18 19 namespace net { 20 class IOBuffer; 21 } 22 23 namespace fileapi { 24 class FileSystemContext; 25 class FileSystemURL; 26 } 27 28 namespace webkit_blob { 29 30 // A generic interface for reading a file-like object. 31 class FileStreamReader { 32 public: 33 // Creates a new FileReader for a local file |file_path|. 34 // |initial_offset| specifies the offset in the file where the first read 35 // should start. If the given offset is out of the file range any 36 // read operation may error out with net::ERR_REQUEST_RANGE_NOT_SATISFIABLE. 37 // |expected_modification_time| specifies the expected last modification 38 // If the value is non-null, the reader will check the underlying file's 39 // actual modification time to see if the file has been modified, and if 40 // it does any succeeding read operations should fail with 41 // ERR_UPLOAD_FILE_CHANGED error. 42 WEBKIT_STORAGE_BROWSER_EXPORT static FileStreamReader* 43 CreateForLocalFile(base::TaskRunner* task_runner, 44 const base::FilePath& file_path, 45 int64 initial_offset, 46 const base::Time& expected_modification_time); 47 48 // Creates a new reader for a filesystem URL |url| form |initial_offset|. 49 // |expected_modification_time| specifies the expected last modification if 50 // the value is non-null, the reader will check the underlying file's actual 51 // modification time to see if the file has been modified, and if it does any 52 // succeeding read operations should fail with ERR_UPLOAD_FILE_CHANGED error. 53 WEBKIT_STORAGE_BROWSER_EXPORT static FileStreamReader* 54 CreateForFileSystemFile(fileapi::FileSystemContext* context, 55 const fileapi::FileSystemURL& url, 56 int64 initial_offset, 57 const base::Time& expected_modification_time); 58 59 // It is valid to delete the reader at any time. If the stream is deleted 60 // while it has a pending read, its callback will not be called. 61 virtual ~FileStreamReader() {} 62 63 // Reads from the current cursor position asynchronously. 64 // 65 // Up to buf_len bytes will be copied into buf. (In other words, partial 66 // reads are allowed.) Returns the number of bytes copied, 0 if at 67 // end-of-file, or an error code if the operation could not be performed. 68 // If the read could not complete synchronously, then ERR_IO_PENDING is 69 // returned, and the callback will be run on the thread where Read() 70 // was called, when the read has completed. 71 // 72 // It is invalid to call Read while there is an in-flight Read operation. 73 // 74 // If the stream is deleted while it has an in-flight Read operation 75 // |callback| will not be called. 76 virtual int Read(net::IOBuffer* buf, int buf_len, 77 const net::CompletionCallback& callback) = 0; 78 79 // Returns the length of the file if it could successfully retrieve the 80 // file info *and* its last modification time equals to 81 // expected modification time (rv >= 0 cases). 82 // Otherwise, a negative error code is returned (rv < 0 cases). 83 // If the stream is deleted while it has an in-flight GetLength operation 84 // |callback| will not be called. 85 // Note that the return type is int64 to return a larger file's size (a file 86 // larger than 2G) but an error code should fit in the int range (may be 87 // smaller than int64 range). 88 virtual int64 GetLength(const net::Int64CompletionCallback& callback) = 0; 89 }; 90 91 } // namespace webkit_blob 92 93 #endif // WEBKIT_BLOB_FILE_STREAM_READER_H_ 94