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