OLD | NEW |
(Empty) | |
| 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_ELEMENT_READER_H_ |
| 6 #define NET_BASE_UPLOAD_ELEMENT_READER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/file_path.h" |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/time.h" |
| 14 #include "net/base/net_export.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 class FileStream; |
| 19 |
| 20 // An interface to read an upload data element. |
| 21 class NET_EXPORT UploadElementReader { |
| 22 public: |
| 23 UploadElementReader() {} |
| 24 virtual ~UploadElementReader() {} |
| 25 |
| 26 // Initializes the instance synchronously. |
| 27 virtual int InitSync() = 0; |
| 28 |
| 29 // Returns the byte-length of the element. For files that do not exist, 0 |
| 30 // is returned. This is done for consistency with Mozilla. |
| 31 virtual uint64 GetContentLength() const = 0; |
| 32 |
| 33 // Returns the number of bytes remaining to read. |
| 34 virtual uint64 BytesRemaining() const = 0; |
| 35 |
| 36 // Returns true if the upload element is entirely in memory. |
| 37 // The default implementation returns false. |
| 38 virtual bool IsInMemory() const; |
| 39 |
| 40 // Reads up to |buf_length| bytes synchronously. Returns the number of bytes |
| 41 // read. This function never fails. If there's less data to read than we |
| 42 // initially observed, then pad with zero (this can happen with files). |
| 43 // |buf_length| must be greater than 0. |
| 44 virtual int ReadSync(char* buf, int buf_length) = 0; |
| 45 |
| 46 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(UploadElementReader); |
| 48 }; |
| 49 |
| 50 // An UploadElementReader implementation for bytes. |
| 51 class NET_EXPORT UploadBytesElementReader : public UploadElementReader { |
| 52 public: |
| 53 UploadBytesElementReader(const char* bytes, int bytes_length); |
| 54 virtual ~UploadBytesElementReader(); |
| 55 |
| 56 // UploadElementReader overrides: |
| 57 virtual int InitSync() OVERRIDE; |
| 58 virtual uint64 GetContentLength() const OVERRIDE; |
| 59 virtual uint64 BytesRemaining() const OVERRIDE; |
| 60 virtual int ReadSync(char* buf, int buf_length) OVERRIDE; |
| 61 virtual bool IsInMemory() const OVERRIDE; |
| 62 |
| 63 private: |
| 64 const char* bytes_; |
| 65 int bytes_length_; |
| 66 int offset_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(UploadBytesElementReader); |
| 69 }; |
| 70 |
| 71 // An UploadElementReader implementation for file. |
| 72 class NET_EXPORT UploadFileElementReader : public UploadElementReader { |
| 73 public: |
| 74 UploadFileElementReader(const FilePath& path, |
| 75 uint64 range_offset, |
| 76 uint64 range_length, |
| 77 const base::Time& expected_modification_time); |
| 78 virtual ~UploadFileElementReader(); |
| 79 |
| 80 // UploadElementReader overrides: |
| 81 virtual int InitSync() OVERRIDE; |
| 82 virtual uint64 GetContentLength() const OVERRIDE; |
| 83 virtual uint64 BytesRemaining() const OVERRIDE; |
| 84 virtual int ReadSync(char* buf, int buf_length) OVERRIDE; |
| 85 |
| 86 private: |
| 87 FilePath path_; |
| 88 uint64 range_offset_; |
| 89 uint64 range_length_; |
| 90 base::Time expected_modification_time_; |
| 91 scoped_ptr<FileStream> file_stream_; |
| 92 uint64 content_length_; |
| 93 uint64 bytes_remaining_; |
| 94 |
| 95 // Sets an value to override the result for GetContentLength(). |
| 96 // Used for tests. |
| 97 struct ScopedOverridingContentLengthForTests { |
| 98 ScopedOverridingContentLengthForTests(uint64 value); |
| 99 ~ScopedOverridingContentLengthForTests(); |
| 100 }; |
| 101 |
| 102 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
| 103 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
| 104 UploadFileSmallerThanLength); |
| 105 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, |
| 106 UploadFileSmallerThanLength); |
| 107 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, |
| 108 UploadFileSmallerThanLength); |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(UploadFileElementReader); |
| 111 }; |
| 112 |
| 113 } // namespace net |
| 114 |
| 115 #endif // NET_BASE_UPLOAD_ELEMENT_READER_H_ |
OLD | NEW |