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 NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_ 6 #define NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/files/file.h" 10 #include "base/files/file_path.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "base/time/time.h" 16 #include "net/base/upload_element_reader.h" 17 18 namespace base { 19 class TaskRunner; 20 } 21 22 namespace net { 23 24 class FileStream; 25 26 // An UploadElementReader implementation for file. 27 class NET_EXPORT UploadFileElementReader : public UploadElementReader { 28 public: 29 // |task_runner| is used to perform file operations. It must not be NULL. 30 UploadFileElementReader(base::TaskRunner* task_runner, 31 const base::FilePath& path, 32 uint64 range_offset, 33 uint64 range_length, 34 const base::Time& expected_modification_time); 35 virtual ~UploadFileElementReader(); 36 path()37 const base::FilePath& path() const { return path_; } range_offset()38 uint64 range_offset() const { return range_offset_; } range_length()39 uint64 range_length() const { return range_length_; } expected_modification_time()40 const base::Time& expected_modification_time() const { 41 return expected_modification_time_; 42 } 43 44 // UploadElementReader overrides: 45 virtual const UploadFileElementReader* AsFileReader() const OVERRIDE; 46 virtual int Init(const CompletionCallback& callback) OVERRIDE; 47 virtual uint64 GetContentLength() const OVERRIDE; 48 virtual uint64 BytesRemaining() const OVERRIDE; 49 virtual int Read(IOBuffer* buf, 50 int buf_length, 51 const CompletionCallback& callback) OVERRIDE; 52 53 private: 54 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); 55 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, 56 UploadFileSmallerThanLength); 57 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, 58 UploadFileSmallerThanLength); 59 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, 60 UploadFileSmallerThanLength); 61 62 // Resets this instance to the uninitialized state. 63 void Reset(); 64 65 // These methods are used to implement Init(). 66 void OnOpenCompleted(const CompletionCallback& callback, int result); 67 void OnSeekCompleted(const CompletionCallback& callback, int64 result); 68 void OnGetFileInfoCompleted(const CompletionCallback& callback, 69 base::File::Info* file_info, 70 bool result); 71 72 // This method is used to implement Read(). 73 int OnReadCompleted(const CompletionCallback& callback, int result); 74 75 // Sets an value to override the result for GetContentLength(). 76 // Used for tests. 77 struct NET_EXPORT_PRIVATE ScopedOverridingContentLengthForTests { 78 ScopedOverridingContentLengthForTests(uint64 value); 79 ~ScopedOverridingContentLengthForTests(); 80 }; 81 82 scoped_refptr<base::TaskRunner> task_runner_; 83 const base::FilePath path_; 84 const uint64 range_offset_; 85 const uint64 range_length_; 86 const base::Time expected_modification_time_; 87 scoped_ptr<FileStream> file_stream_; 88 uint64 content_length_; 89 uint64 bytes_remaining_; 90 base::WeakPtrFactory<UploadFileElementReader> weak_ptr_factory_; 91 92 DISALLOW_COPY_AND_ASSIGN(UploadFileElementReader); 93 }; 94 95 } // namespace net 96 97 #endif // NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_ 98