| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_BLOB_LOCAL_FILE_READER_H_ | 5 #ifndef WEBKIT_BLOB_LOCAL_FILE_READER_H_ |
| 6 #define WEBKIT_BLOB_LOCAL_FILE_READER_H_ | 6 #define WEBKIT_BLOB_LOCAL_FILE_READER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "webkit/blob/blob_export.h" | 15 #include "webkit/blob/blob_export.h" |
| 16 #include "webkit/blob/file_reader.h" | 16 #include "webkit/blob/file_reader.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class MessageLoopProxy; | 19 class TaskRunner; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace webkit_blob { | 22 namespace webkit_blob { |
| 23 | 23 |
| 24 // A thin wrapper of net::FileStream with range support for sliced file | 24 // A thin wrapper of net::FileStream with range support for sliced file |
| 25 // handling. | 25 // handling. |
| 26 class BLOB_EXPORT LocalFileReader : public FileReader { | 26 class BLOB_EXPORT LocalFileReader : public FileReader { |
| 27 public: | 27 public: |
| 28 typedef base::Callback<void(int error, scoped_ptr<net::FileStream> stream)> | 28 typedef base::Callback<void(int error, scoped_ptr<net::FileStream> stream)> |
| 29 OpenFileStreamCallback; | 29 OpenFileStreamCallback; |
| 30 | 30 |
| 31 // A convenient method to translate platform file error to net error code. | 31 // A convenient method to translate platform file error to net error code. |
| 32 static int PlatformFileErrorToNetError(base::PlatformFileError file_error); | 32 static int PlatformFileErrorToNetError(base::PlatformFileError file_error); |
| 33 | 33 |
| 34 // Creates a new FileReader for a local file |file_path|. | 34 // Creates a new FileReader for a local file |file_path|. |
| 35 // |initial_offset| specifies the offset in the file where the first read | 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 | 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. | 37 // read operation may error out with net::ERR_REQUEST_RANGE_NOT_SATISFIABLE. |
| 38 // | 38 // |
| 39 // |expected_modification_time| specifies the expected last modification | 39 // |expected_modification_time| specifies the expected last modification |
| 40 // If the value is non-null, the reader will check the underlying file's | 40 // If the value is non-null, the reader will check the underlying file's |
| 41 // actual modification time to see if the file has been modified, and if | 41 // actual modification time to see if the file has been modified, and if |
| 42 // it does any succeeding read operations should fail with | 42 // it does any succeeding read operations should fail with |
| 43 // ERR_UPLOAD_FILE_CHANGED error. | 43 // ERR_UPLOAD_FILE_CHANGED error. |
| 44 // TODO(kinuko): Consider using SequencedWorkerPool. | 44 // TODO(kinuko): Consider using SequencedWorkerPool. |
| 45 LocalFileReader(base::MessageLoopProxy* file_thread_proxy, | 45 LocalFileReader(base::TaskRunner* task_runner, |
| 46 const FilePath& file_path, | 46 const FilePath& file_path, |
| 47 int64 initial_offset, | 47 int64 initial_offset, |
| 48 const base::Time& expected_modification_time); | 48 const base::Time& expected_modification_time); |
| 49 virtual ~LocalFileReader(); | 49 virtual ~LocalFileReader(); |
| 50 | 50 |
| 51 // FileReader overrides. | 51 // FileReader overrides. |
| 52 virtual int Read(net::IOBuffer* buf, int buf_len, | 52 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 53 const net::CompletionCallback& callback) OVERRIDE; | 53 const net::CompletionCallback& callback) OVERRIDE; |
| 54 | 54 |
| 55 // Returns the length of the file if it could successfully retrieve the | 55 // Returns the length of the file if it could successfully retrieve the |
| 56 // file info *and* its last modification time equals to | 56 // file info *and* its last modification time equals to |
| 57 // expected_modification_time_ (rv >= 0 cases). | 57 // expected_modification_time_ (rv >= 0 cases). |
| 58 // Otherwise, a negative error code is returned (rv < 0 cases). | 58 // Otherwise, a negative error code is returned (rv < 0 cases). |
| 59 int GetLength(const net::Int64CompletionCallback& callback); | 59 int GetLength(const net::Int64CompletionCallback& callback); |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 class OpenFileStreamHelper; | 62 class OpenFileStreamHelper; |
| 63 | 63 |
| 64 int Open(const OpenFileStreamCallback& callback); | 64 int Open(const OpenFileStreamCallback& callback); |
| 65 void DidOpen(net::IOBuffer* buf, | 65 void DidOpen(net::IOBuffer* buf, |
| 66 int buf_len, | 66 int buf_len, |
| 67 const net::CompletionCallback& callback, | 67 const net::CompletionCallback& callback, |
| 68 int open_error, | 68 int open_error, |
| 69 scoped_ptr<net::FileStream> stream); | 69 scoped_ptr<net::FileStream> stream); |
| 70 | 70 |
| 71 scoped_refptr<base::MessageLoopProxy> file_thread_proxy_; | 71 scoped_refptr<base::TaskRunner> task_runner_; |
| 72 scoped_ptr<net::FileStream> stream_impl_; | 72 scoped_ptr<net::FileStream> stream_impl_; |
| 73 const FilePath file_path_; | 73 const FilePath file_path_; |
| 74 const int64 initial_offset_; | 74 const int64 initial_offset_; |
| 75 const base::Time expected_modification_time_; | 75 const base::Time expected_modification_time_; |
| 76 bool has_pending_open_; | 76 bool has_pending_open_; |
| 77 base::WeakPtrFactory<LocalFileReader> weak_factory_; | 77 base::WeakPtrFactory<LocalFileReader> weak_factory_; |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 } // namespace webkit_blob | 80 } // namespace webkit_blob |
| 81 | 81 |
| 82 #endif // WEBKIT_BLOB_LOCAL_FILE_READER_H_ | 82 #endif // WEBKIT_BLOB_LOCAL_FILE_READER_H_ |
| OLD | NEW |