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_BROWSER_BLOB_LOCAL_FILE_STREAM_READER_H_ 6 #define WEBKIT_BROWSER_BLOB_LOCAL_FILE_STREAM_READER_H_ 7 8 #include "base/basictypes.h" 9 #include "base/compiler_specific.h" 10 #include "base/files/file_path.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/platform_file.h" 13 #include "base/time/time.h" 14 #include "webkit/browser/blob/file_stream_reader.h" 15 #include "webkit/browser/webkit_storage_browser_export.h" 16 17 namespace base { 18 class TaskRunner; 19 } 20 21 namespace net { 22 class FileStream; 23 } 24 25 namespace webkit_blob { 26 27 // A thin wrapper of net::FileStream with range support for sliced file 28 // handling. 29 class WEBKIT_STORAGE_BROWSER_EXPORT LocalFileStreamReader 30 : public FileStreamReader { 31 public: 32 // Creates a new FileReader for a local file |file_path|. 33 // |initial_offset| specifies the offset in the file where the first read 34 // should start. If the given offset is out of the file range any 35 // read operation may error out with net::ERR_REQUEST_RANGE_NOT_SATISFIABLE. 36 // 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 LocalFileStreamReader(base::TaskRunner* task_runner, 43 const base::FilePath& file_path, 44 int64 initial_offset, 45 const base::Time& expected_modification_time); 46 virtual ~LocalFileStreamReader(); 47 48 // FileStreamReader overrides. 49 virtual int Read(net::IOBuffer* buf, int buf_len, 50 const net::CompletionCallback& callback) OVERRIDE; 51 virtual int64 GetLength( 52 const net::Int64CompletionCallback& callback) OVERRIDE; 53 54 private: 55 int Open(const net::CompletionCallback& callback); 56 57 // Callbacks that are chained from Open for Read. 58 void DidVerifyForOpen(const net::CompletionCallback& callback, 59 int64 get_length_result); 60 void DidOpenFileStream(const net::CompletionCallback& callback, 61 int result); 62 void DidSeekFileStream(const net::CompletionCallback& callback, 63 int64 seek_result); 64 void DidOpenForRead(net::IOBuffer* buf, 65 int buf_len, 66 const net::CompletionCallback& callback, 67 int open_result); 68 69 void DidGetFileInfoForGetLength(const net::Int64CompletionCallback& callback, 70 base::PlatformFileError error, 71 const base::PlatformFileInfo& file_info); 72 73 scoped_refptr<base::TaskRunner> task_runner_; 74 scoped_ptr<net::FileStream> stream_impl_; 75 const base::FilePath file_path_; 76 const int64 initial_offset_; 77 const base::Time expected_modification_time_; 78 bool has_pending_open_; 79 base::WeakPtrFactory<LocalFileStreamReader> weak_factory_; 80 }; 81 82 } // namespace webkit_blob 83 84 #endif // WEBKIT_BROWSER_BLOB_LOCAL_FILE_STREAM_READER_H_ 85