| 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_DATA_H_ | 5 #ifndef NET_BASE_UPLOAD_DATA_H_ |
| 6 #define NET_BASE_UPLOAD_DATA_H_ | 6 #define NET_BASE_UPLOAD_DATA_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 bool is_last_chunk() const { return is_last_chunk_; } | 103 bool is_last_chunk() const { return is_last_chunk_; } |
| 104 // Sets whether this is the last chunk. Used during IPC marshalling. | 104 // Sets whether this is the last chunk. Used during IPC marshalling. |
| 105 void set_is_last_chunk(bool is_last_chunk) { | 105 void set_is_last_chunk(bool is_last_chunk) { |
| 106 is_last_chunk_ = is_last_chunk; | 106 is_last_chunk_ = is_last_chunk; |
| 107 } | 107 } |
| 108 | 108 |
| 109 // Returns the byte-length of the element. For files that do not exist, 0 | 109 // Returns the byte-length of the element. For files that do not exist, 0 |
| 110 // is returned. This is done for consistency with Mozilla. | 110 // is returned. This is done for consistency with Mozilla. |
| 111 uint64 GetContentLength(); | 111 uint64 GetContentLength(); |
| 112 | 112 |
| 113 // Returns a FileStream opened for reading for this element, positioned at | 113 // Reads up to |buf_len| bytes synchronously. Returns the number of bytes |
| 114 // |file_range_offset_|. The caller gets ownership and is responsible | 114 // read. This function never fails. If there's less data to read than we |
| 115 // for cleaning up the FileStream. Returns NULL if this element is not of | 115 // initially observed, then pad with zero (this can happen with files). |
| 116 // type TYPE_FILE or if the file is not openable. | 116 // |buf_len| must be greater than 0. |
| 117 FileStream* NewFileStreamForReading(); | 117 int ReadSync(char* buf, int buf_len); |
| 118 |
| 119 // Returns the number of bytes remaining to read. |
| 120 uint64 BytesRemaining(); |
| 121 |
| 122 // Resets the offset to zero, so that the element can be reread. |
| 123 void ResetOffset() { offset_ = 0; } |
| 118 | 124 |
| 119 private: | 125 private: |
| 126 // Returns a FileStream opened for reading for this element, positioned |
| 127 // at |file_range_offset_|. Returns NULL if the file is not openable. |
| 128 FileStream* OpenFileStream(); |
| 129 |
| 130 // Reads up to |buf_len| bytes synchronously from memory (i.e. type_ is |
| 131 // TYPE_BYTES or TYPE_CHUNK). |
| 132 int ReadFromMemorySync(char* buf, int buf_len); |
| 133 |
| 134 // Reads up to |buf_len| bytes synchronously from a file (i.e. type_ is |
| 135 // TYPE_FILE). |
| 136 int ReadFromFileSync(char* buf, int buf_len); |
| 137 |
| 120 // Allows tests to override the result of GetContentLength. | 138 // Allows tests to override the result of GetContentLength. |
| 121 void SetContentLength(uint64 content_length) { | 139 void SetContentLength(uint64 content_length) { |
| 122 override_content_length_ = true; | 140 override_content_length_ = true; |
| 123 content_length_ = content_length; | 141 content_length_ = content_length; |
| 124 } | 142 } |
| 125 | 143 |
| 126 Type type_; | 144 Type type_; |
| 127 std::vector<char> bytes_; | 145 std::vector<char> bytes_; |
| 128 FilePath file_path_; | 146 FilePath file_path_; |
| 129 uint64 file_range_offset_; | 147 uint64 file_range_offset_; |
| 130 uint64 file_range_length_; | 148 uint64 file_range_length_; |
| 131 base::Time expected_file_modification_time_; | 149 base::Time expected_file_modification_time_; |
| 132 GURL blob_url_; | 150 GURL blob_url_; |
| 133 bool is_last_chunk_; | 151 bool is_last_chunk_; |
| 134 bool override_content_length_; | 152 bool override_content_length_; |
| 135 bool content_length_computed_; | 153 bool content_length_computed_; |
| 136 uint64 content_length_; | 154 uint64 content_length_; |
| 155 |
| 156 // The byte offset from the beginning of the element data. Used to track |
| 157 // the current position when reading data. |
| 158 size_t offset_; |
| 159 |
| 160 // The stream of the element data, if this element is of TYPE_FILE. |
| 137 FileStream* file_stream_; | 161 FileStream* file_stream_; |
| 138 | 162 |
| 139 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 163 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
| 140 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 164 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
| 141 UploadFileSmallerThanLength); | 165 UploadFileSmallerThanLength); |
| 142 }; | 166 }; |
| 143 | 167 |
| 144 UploadData(); | 168 UploadData(); |
| 145 | 169 |
| 146 void AppendBytes(const char* bytes, int bytes_len); | 170 void AppendBytes(const char* bytes, int bytes_len); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 173 // Returns the total size in bytes of the data to upload, for testing. | 197 // Returns the total size in bytes of the data to upload, for testing. |
| 174 // This version may perform file IO on the current thread. This function | 198 // This version may perform file IO on the current thread. This function |
| 175 // will fail if called on a thread where file IO is prohibited. Usually | 199 // will fail if called on a thread where file IO is prohibited. Usually |
| 176 // used for testing, but Chrome Frame also uses this version. | 200 // used for testing, but Chrome Frame also uses this version. |
| 177 uint64 GetContentLengthSync(); | 201 uint64 GetContentLengthSync(); |
| 178 | 202 |
| 179 // Returns true if the upload data is entirely in memory (i.e. the | 203 // Returns true if the upload data is entirely in memory (i.e. the |
| 180 // upload data is not chunked, and all elemnts are of TYPE_BYTES). | 204 // upload data is not chunked, and all elemnts are of TYPE_BYTES). |
| 181 bool IsInMemory() const; | 205 bool IsInMemory() const; |
| 182 | 206 |
| 207 // Resets the offset of each upload data element to zero, so that the |
| 208 // upload data can be reread. This can happen if the same upload data is |
| 209 // reused for a new UploadDataStream. |
| 210 void ResetOffset(); |
| 211 |
| 183 std::vector<Element>* elements() { | 212 std::vector<Element>* elements() { |
| 184 return &elements_; | 213 return &elements_; |
| 185 } | 214 } |
| 186 | 215 |
| 187 void SetElements(const std::vector<Element>& elements); | 216 void SetElements(const std::vector<Element>& elements); |
| 188 | 217 |
| 189 void swap_elements(std::vector<Element>* elements) { | 218 void swap_elements(std::vector<Element>* elements) { |
| 190 elements_.swap(*elements); | 219 elements_.swap(*elements); |
| 191 } | 220 } |
| 192 | 221 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 262 |
| 234 inline bool operator!=(const UploadData::Element& a, | 263 inline bool operator!=(const UploadData::Element& a, |
| 235 const UploadData::Element& b) { | 264 const UploadData::Element& b) { |
| 236 return !(a == b); | 265 return !(a == b); |
| 237 } | 266 } |
| 238 #endif // defined(UNIT_TEST) | 267 #endif // defined(UNIT_TEST) |
| 239 | 268 |
| 240 } // namespace net | 269 } // namespace net |
| 241 | 270 |
| 242 #endif // NET_BASE_UPLOAD_DATA_H_ | 271 #endif // NET_BASE_UPLOAD_DATA_H_ |
| OLD | NEW |