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