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 NET_BASE_UPLOAD_ELEMENT_H_ | 5 #ifndef NET_BASE_UPLOAD_ELEMENT_H_ |
6 #define NET_BASE_UPLOAD_ELEMENT_H_ | 6 #define NET_BASE_UPLOAD_ELEMENT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/gtest_prod_util.h" | |
13 #include "base/time.h" | 12 #include "base/time.h" |
14 #include "googleurl/src/gurl.h" | |
15 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
16 | 14 |
17 namespace net { | 15 namespace net { |
18 | 16 |
19 class FileStream; | |
20 | |
21 // A class representing an element contained by UploadData. | 17 // A class representing an element contained by UploadData. |
22 class NET_EXPORT UploadElement { | 18 class NET_EXPORT UploadElement { |
23 public: | 19 public: |
24 enum Type { | 20 enum Type { |
25 TYPE_BYTES, | 21 TYPE_BYTES, |
26 TYPE_FILE, | 22 TYPE_FILE, |
27 }; | 23 }; |
28 | 24 |
29 UploadElement(); | 25 UploadElement(); |
30 ~UploadElement(); | 26 ~UploadElement(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 void SetToFilePathRange(const FilePath& path, | 61 void SetToFilePathRange(const FilePath& path, |
66 uint64 offset, uint64 length, | 62 uint64 offset, uint64 length, |
67 const base::Time& expected_modification_time) { | 63 const base::Time& expected_modification_time) { |
68 type_ = TYPE_FILE; | 64 type_ = TYPE_FILE; |
69 file_path_ = path; | 65 file_path_ = path; |
70 file_range_offset_ = offset; | 66 file_range_offset_ = offset; |
71 file_range_length_ = length; | 67 file_range_length_ = length; |
72 expected_file_modification_time_ = expected_modification_time; | 68 expected_file_modification_time_ = expected_modification_time; |
73 } | 69 } |
74 | 70 |
75 // Returns the byte-length of the element. For files that do not exist, 0 | |
76 // is returned. This is done for consistency with Mozilla. | |
77 uint64 GetContentLength(); | |
78 | |
79 // Reads up to |buf_len| bytes synchronously. Returns the number of bytes | |
80 // read. This function never fails. If there's less data to read than we | |
81 // initially observed, then pad with zero (this can happen with files). | |
82 // |buf_len| must be greater than 0. | |
83 int ReadSync(char* buf, int buf_len); | |
84 | |
85 // Returns the number of bytes remaining to read. | |
86 uint64 BytesRemaining(); | |
87 | |
88 // Resets the offset to zero and closes the file stream if opened, so | |
89 // that the element can be reread. | |
90 void ResetOffset(); | |
91 | |
92 private: | 71 private: |
93 // Returns a FileStream opened for reading for this element, positioned | |
94 // at |file_range_offset_|. Returns NULL if the file is not openable. | |
95 FileStream* OpenFileStream(); | |
96 | |
97 // Reads up to |buf_len| bytes synchronously from memory (i.e. type_ is | |
98 // TYPE_BYTES). | |
99 int ReadFromMemorySync(char* buf, int buf_len); | |
100 | |
101 // Reads up to |buf_len| bytes synchronously from a file (i.e. type_ is | |
102 // TYPE_FILE). | |
103 int ReadFromFileSync(char* buf, int buf_len); | |
104 | |
105 // Allows tests to override the result of GetContentLength. | |
106 void SetContentLength(uint64 content_length) { | |
107 override_content_length_ = true; | |
108 content_length_ = content_length; | |
109 } | |
110 | |
111 Type type_; | 72 Type type_; |
112 std::vector<char> buf_; | 73 std::vector<char> buf_; |
113 const char* bytes_start_; | 74 const char* bytes_start_; |
114 uint64 bytes_length_; | 75 uint64 bytes_length_; |
115 FilePath file_path_; | 76 FilePath file_path_; |
116 uint64 file_range_offset_; | 77 uint64 file_range_offset_; |
117 uint64 file_range_length_; | 78 uint64 file_range_length_; |
118 base::Time expected_file_modification_time_; | 79 base::Time expected_file_modification_time_; |
119 bool override_content_length_; | |
120 bool content_length_computed_; | |
121 uint64 content_length_; | |
122 | |
123 // The byte offset from the beginning of the element data. Used to track | |
124 // the current position when reading data. | |
125 uint64 offset_; | |
126 | |
127 // The stream of the element data, if this element is of TYPE_FILE. | |
128 FileStream* file_stream_; | |
129 | |
130 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | |
131 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | |
132 UploadFileSmallerThanLength); | |
133 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, | |
134 UploadFileSmallerThanLength); | |
135 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, | |
136 UploadFileSmallerThanLength); | |
137 }; | 80 }; |
138 | 81 |
139 #if defined(UNIT_TEST) | 82 #if defined(UNIT_TEST) |
140 inline bool operator==(const UploadElement& a, | 83 inline bool operator==(const UploadElement& a, |
141 const UploadElement& b) { | 84 const UploadElement& b) { |
142 if (a.type() != b.type()) | 85 if (a.type() != b.type()) |
143 return false; | 86 return false; |
144 if (a.type() == UploadElement::TYPE_BYTES) | 87 if (a.type() == UploadElement::TYPE_BYTES) |
145 return a.bytes_length() == b.bytes_length() && | 88 return a.bytes_length() == b.bytes_length() && |
146 memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0; | 89 memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0; |
147 if (a.type() == UploadElement::TYPE_FILE) { | 90 if (a.type() == UploadElement::TYPE_FILE) { |
148 return a.file_path() == b.file_path() && | 91 return a.file_path() == b.file_path() && |
149 a.file_range_offset() == b.file_range_offset() && | 92 a.file_range_offset() == b.file_range_offset() && |
150 a.file_range_length() == b.file_range_length() && | 93 a.file_range_length() == b.file_range_length() && |
151 a.expected_file_modification_time() == | 94 a.expected_file_modification_time() == |
152 b.expected_file_modification_time(); | 95 b.expected_file_modification_time(); |
153 } | 96 } |
154 return false; | 97 return false; |
155 } | 98 } |
156 | 99 |
157 inline bool operator!=(const UploadElement& a, | 100 inline bool operator!=(const UploadElement& a, |
158 const UploadElement& b) { | 101 const UploadElement& b) { |
159 return !(a == b); | 102 return !(a == b); |
160 } | 103 } |
161 #endif // defined(UNIT_TEST) | 104 #endif // defined(UNIT_TEST) |
162 | 105 |
163 } // namespace net | 106 } // namespace net |
164 | 107 |
165 #endif // NET_BASE_UPLOAD_ELEMENT_H_ | 108 #endif // NET_BASE_UPLOAD_ELEMENT_H_ |
OLD | NEW |