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 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 public base::SupportsUserData { | 44 public base::SupportsUserData { |
45 public: | 45 public: |
46 enum Type { | 46 enum Type { |
47 TYPE_BYTES, | 47 TYPE_BYTES, |
48 TYPE_FILE, | 48 TYPE_FILE, |
49 TYPE_BLOB, | 49 TYPE_BLOB, |
50 | 50 |
51 // A block of bytes to be sent in chunked encoding immediately, without | 51 // A block of bytes to be sent in chunked encoding immediately, without |
52 // waiting for rest of the data. | 52 // waiting for rest of the data. |
53 TYPE_CHUNK, | 53 TYPE_CHUNK, |
54 | |
55 // For a file specified as FileSystem URL. | |
56 TYPE_FILE_FILESYSTEM, | |
darin (slow to review)
2012/08/10 16:50:24
Can we avoid this? It seems unfortunate to make t
| |
54 }; | 57 }; |
55 | 58 |
56 class NET_EXPORT Element { | 59 class NET_EXPORT Element { |
57 public: | 60 public: |
58 Element(); | 61 Element(); |
59 ~Element(); | 62 ~Element(); |
60 | 63 |
61 Type type() const { return type_; } | 64 Type type() const { return type_; } |
62 // Explicitly sets the type of this Element. Used during IPC | 65 // Explicitly sets the type of this Element. Used during IPC |
63 // marshalling. | 66 // marshalling. |
64 void set_type(Type type) { | 67 void set_type(Type type) { |
65 type_ = type; | 68 type_ = type; |
66 } | 69 } |
67 | 70 |
68 const std::vector<char>& bytes() const { return bytes_; } | 71 const std::vector<char>& bytes() const { return bytes_; } |
69 const FilePath& file_path() const { return file_path_; } | 72 const FilePath& file_path() const { return file_path_; } |
70 uint64 file_range_offset() const { return file_range_offset_; } | 73 uint64 file_range_offset() const { return file_range_offset_; } |
71 uint64 file_range_length() const { return file_range_length_; } | 74 uint64 file_range_length() const { return file_range_length_; } |
72 // If NULL time is returned, we do not do the check. | 75 // If NULL time is returned, we do not do the check. |
73 const base::Time& expected_file_modification_time() const { | 76 const base::Time& expected_file_modification_time() const { |
74 return expected_file_modification_time_; | 77 return expected_file_modification_time_; |
75 } | 78 } |
76 const GURL& blob_url() const { return blob_url_; } | 79 const GURL& url() const { return url_; } |
77 | 80 |
78 void SetToBytes(const char* bytes, int bytes_len) { | 81 void SetToBytes(const char* bytes, int bytes_len) { |
79 type_ = TYPE_BYTES; | 82 type_ = TYPE_BYTES; |
80 bytes_.assign(bytes, bytes + bytes_len); | 83 bytes_.assign(bytes, bytes + bytes_len); |
81 } | 84 } |
82 | 85 |
83 void SetToFilePath(const FilePath& path) { | 86 void SetToFilePath(const FilePath& path) { |
84 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 87 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
85 } | 88 } |
86 | 89 |
87 // If expected_modification_time is NULL, we do not check for the file | 90 // If expected_modification_time is NULL, we do not check for the file |
88 // change. Also note that the granularity for comparison is time_t, not | 91 // change. Also note that the granularity for comparison is time_t, not |
89 // the full precision. | 92 // the full precision. |
90 void SetToFilePathRange(const FilePath& path, | 93 void SetToFilePathRange(const FilePath& path, |
91 uint64 offset, uint64 length, | 94 uint64 offset, uint64 length, |
92 const base::Time& expected_modification_time) { | 95 const base::Time& expected_modification_time) { |
93 type_ = TYPE_FILE; | 96 type_ = TYPE_FILE; |
94 file_path_ = path; | 97 file_path_ = path; |
95 file_range_offset_ = offset; | 98 file_range_offset_ = offset; |
96 file_range_length_ = length; | 99 file_range_length_ = length; |
97 expected_file_modification_time_ = expected_modification_time; | 100 expected_file_modification_time_ = expected_modification_time; |
98 } | 101 } |
99 | 102 |
103 void SetToFileSystemURLRange( | |
104 const GURL& url, | |
105 uint64 offset, uint64 length, | |
106 const base::Time& expected_modification_time) { | |
107 type_ = TYPE_FILE_FILESYSTEM; | |
108 url_ = url; | |
109 file_range_offset_ = offset; | |
110 file_range_length_ = length; | |
111 expected_file_modification_time_ = expected_modification_time; | |
112 } | |
113 | |
100 // TODO(jianli): UploadData should not contain any blob reference. We need | 114 // TODO(jianli): UploadData should not contain any blob reference. We need |
101 // to define another structure to represent WebKit::WebHTTPBody. | 115 // to define another structure to represent WebKit::WebHTTPBody. |
102 void SetToBlobUrl(const GURL& blob_url) { | 116 void SetToBlobUrl(const GURL& blob_url) { |
103 type_ = TYPE_BLOB; | 117 type_ = TYPE_BLOB; |
104 blob_url_ = blob_url; | 118 url_ = blob_url; |
105 } | 119 } |
106 | 120 |
107 // Though similar to bytes, a chunk indicates that the element is sent via | 121 // Though similar to bytes, a chunk indicates that the element is sent via |
108 // chunked transfer encoding and not buffered until the full upload data | 122 // chunked transfer encoding and not buffered until the full upload data |
109 // is available. | 123 // is available. |
110 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 124 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
111 | 125 |
112 bool is_last_chunk() const { return is_last_chunk_; } | 126 bool is_last_chunk() const { return is_last_chunk_; } |
113 // Sets whether this is the last chunk. Used during IPC marshalling. | 127 // Sets whether this is the last chunk. Used during IPC marshalling. |
114 void set_is_last_chunk(bool is_last_chunk) { | 128 void set_is_last_chunk(bool is_last_chunk) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
150 override_content_length_ = true; | 164 override_content_length_ = true; |
151 content_length_ = content_length; | 165 content_length_ = content_length; |
152 } | 166 } |
153 | 167 |
154 Type type_; | 168 Type type_; |
155 std::vector<char> bytes_; | 169 std::vector<char> bytes_; |
156 FilePath file_path_; | 170 FilePath file_path_; |
157 uint64 file_range_offset_; | 171 uint64 file_range_offset_; |
158 uint64 file_range_length_; | 172 uint64 file_range_length_; |
159 base::Time expected_file_modification_time_; | 173 base::Time expected_file_modification_time_; |
160 GURL blob_url_; | 174 GURL url_; |
161 bool is_last_chunk_; | 175 bool is_last_chunk_; |
162 bool override_content_length_; | 176 bool override_content_length_; |
163 bool content_length_computed_; | 177 bool content_length_computed_; |
164 uint64 content_length_; | 178 uint64 content_length_; |
165 | 179 |
166 // The byte offset from the beginning of the element data. Used to track | 180 // The byte offset from the beginning of the element data. Used to track |
167 // the current position when reading data. | 181 // the current position when reading data. |
168 size_t offset_; | 182 size_t offset_; |
169 | 183 |
170 // The stream of the element data, if this element is of TYPE_FILE. | 184 // The stream of the element data, if this element is of TYPE_FILE. |
171 FileStream* file_stream_; | 185 FileStream* file_stream_; |
172 | 186 |
173 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 187 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
174 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 188 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
175 UploadFileSmallerThanLength); | 189 UploadFileSmallerThanLength); |
176 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, | 190 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, |
177 UploadFileSmallerThanLength); | 191 UploadFileSmallerThanLength); |
178 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, | 192 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, |
179 UploadFileSmallerThanLength); | 193 UploadFileSmallerThanLength); |
180 }; | 194 }; |
181 | 195 |
182 UploadData(); | 196 UploadData(); |
183 | 197 |
184 void AppendBytes(const char* bytes, int bytes_len); | 198 void AppendBytes(const char* bytes, int bytes_len); |
185 | 199 |
186 void AppendFileRange(const FilePath& file_path, | 200 void AppendFileRange(const FilePath& file_path, |
187 uint64 offset, uint64 length, | 201 uint64 offset, uint64 length, |
188 const base::Time& expected_modification_time); | 202 const base::Time& expected_modification_time); |
189 | 203 |
204 void AppendFileSystemFileRange(const GURL& url, | |
205 uint64 offset, uint64 length, | |
206 const base::Time& expected_modification_time); | |
207 | |
190 void AppendBlob(const GURL& blob_url); | 208 void AppendBlob(const GURL& blob_url); |
191 | 209 |
192 // Adds the given chunk of bytes to be sent immediately with chunked transfer | 210 // Adds the given chunk of bytes to be sent immediately with chunked transfer |
193 // encoding. | 211 // encoding. |
194 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 212 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
195 | 213 |
196 // Sets the callback to be invoked when a new chunk is available to upload. | 214 // Sets the callback to be invoked when a new chunk is available to upload. |
197 void set_chunk_callback(ChunkCallback* callback); | 215 void set_chunk_callback(ChunkCallback* callback); |
198 | 216 |
199 // Initializes the object to send chunks of upload data over time rather | 217 // Initializes the object to send chunks of upload data over time rather |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 return false; | 284 return false; |
267 if (a.type() == UploadData::TYPE_BYTES) | 285 if (a.type() == UploadData::TYPE_BYTES) |
268 return a.bytes() == b.bytes(); | 286 return a.bytes() == b.bytes(); |
269 if (a.type() == UploadData::TYPE_FILE) { | 287 if (a.type() == UploadData::TYPE_FILE) { |
270 return a.file_path() == b.file_path() && | 288 return a.file_path() == b.file_path() && |
271 a.file_range_offset() == b.file_range_offset() && | 289 a.file_range_offset() == b.file_range_offset() && |
272 a.file_range_length() == b.file_range_length() && | 290 a.file_range_length() == b.file_range_length() && |
273 a.expected_file_modification_time() == | 291 a.expected_file_modification_time() == |
274 b.expected_file_modification_time(); | 292 b.expected_file_modification_time(); |
275 } | 293 } |
276 if (a.type() == UploadData::TYPE_BLOB) | 294 if (a.type() == UploadData::TYPE_BLOB || |
277 return a.blob_url() == b.blob_url(); | 295 a.type() == UploadData::TYPE_FILE_FILESYSTEM) |
296 return a.url() == b.url(); | |
278 return false; | 297 return false; |
279 } | 298 } |
280 | 299 |
281 inline bool operator!=(const UploadData::Element& a, | 300 inline bool operator!=(const UploadData::Element& a, |
282 const UploadData::Element& b) { | 301 const UploadData::Element& b) { |
283 return !(a == b); | 302 return !(a == b); |
284 } | 303 } |
285 #endif // defined(UNIT_TEST) | 304 #endif // defined(UNIT_TEST) |
286 | 305 |
287 } // namespace net | 306 } // namespace net |
288 | 307 |
289 #endif // NET_BASE_UPLOAD_DATA_H_ | 308 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |