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" |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/supports_user_data.h" | 15 #include "base/supports_user_data.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "googleurl/src/gurl.h" | |
18 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
19 | 18 |
20 namespace net { | 19 namespace net { |
21 | 20 |
22 class FileStream; | 21 class FileStream; |
23 | 22 |
24 // Interface implemented by callers who require callbacks when new chunks | 23 // Interface implemented by callers who require callbacks when new chunks |
25 // of data are added. | 24 // of data are added. |
26 class NET_EXPORT_PRIVATE ChunkCallback { | 25 class NET_EXPORT_PRIVATE ChunkCallback { |
27 public: | 26 public: |
(...skipping 11 matching lines...) Expand all Loading... |
39 // Until there is a more abstract class for this, this one derives from | 38 // Until there is a more abstract class for this, this one derives from |
40 // SupportsUserData to allow users to stash random data by | 39 // SupportsUserData to allow users to stash random data by |
41 // key and ensure its destruction when UploadData is finally deleted. | 40 // key and ensure its destruction when UploadData is finally deleted. |
42 class NET_EXPORT UploadData | 41 class NET_EXPORT UploadData |
43 : public base::RefCounted<UploadData>, | 42 : public base::RefCounted<UploadData>, |
44 public base::SupportsUserData { | 43 public base::SupportsUserData { |
45 public: | 44 public: |
46 enum Type { | 45 enum Type { |
47 TYPE_BYTES, | 46 TYPE_BYTES, |
48 TYPE_FILE, | 47 TYPE_FILE, |
49 TYPE_BLOB, | |
50 | 48 |
51 // A block of bytes to be sent in chunked encoding immediately, without | 49 // A block of bytes to be sent in chunked encoding immediately, without |
52 // waiting for rest of the data. | 50 // waiting for rest of the data. |
53 TYPE_CHUNK, | 51 TYPE_CHUNK, |
54 }; | 52 }; |
55 | 53 |
56 class NET_EXPORT Element { | 54 class NET_EXPORT Element { |
57 public: | 55 public: |
58 Element(); | 56 Element(); |
59 ~Element(); | 57 ~Element(); |
60 | 58 |
61 Type type() const { return type_; } | 59 Type type() const { return type_; } |
62 // Explicitly sets the type of this Element. Used during IPC | 60 // Explicitly sets the type of this Element. Used during IPC |
63 // marshalling. | 61 // marshalling. |
64 void set_type(Type type) { | 62 void set_type(Type type) { |
65 type_ = type; | 63 type_ = type; |
66 } | 64 } |
67 | 65 |
68 const std::vector<char>& bytes() const { return bytes_; } | 66 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } |
| 67 uint64 bytes_length() const { return buf_.size() + bytes_length_; } |
69 const FilePath& file_path() const { return file_path_; } | 68 const FilePath& file_path() const { return file_path_; } |
70 uint64 file_range_offset() const { return file_range_offset_; } | 69 uint64 file_range_offset() const { return file_range_offset_; } |
71 uint64 file_range_length() const { return file_range_length_; } | 70 uint64 file_range_length() const { return file_range_length_; } |
72 // If NULL time is returned, we do not do the check. | 71 // If NULL time is returned, we do not do the check. |
73 const base::Time& expected_file_modification_time() const { | 72 const base::Time& expected_file_modification_time() const { |
74 return expected_file_modification_time_; | 73 return expected_file_modification_time_; |
75 } | 74 } |
76 const GURL& blob_url() const { return blob_url_; } | |
77 | 75 |
78 void SetToBytes(const char* bytes, int bytes_len) { | 76 void SetToBytes(const char* bytes, int bytes_len) { |
79 type_ = TYPE_BYTES; | 77 type_ = TYPE_BYTES; |
80 bytes_.assign(bytes, bytes + bytes_len); | 78 buf_.assign(bytes, bytes + bytes_len); |
| 79 } |
| 80 |
| 81 // This does not copy the given data and the caller should make sure |
| 82 // the data is secured somewhere else (e.g. by attaching the data |
| 83 // using SetUserData). |
| 84 void SetToSharedBytes(const char* bytes, int bytes_len) { |
| 85 type_ = TYPE_BYTES; |
| 86 bytes_start_ = bytes; |
| 87 bytes_length_ = bytes_len; |
81 } | 88 } |
82 | 89 |
83 void SetToFilePath(const FilePath& path) { | 90 void SetToFilePath(const FilePath& path) { |
84 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 91 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
85 } | 92 } |
86 | 93 |
87 // If expected_modification_time is NULL, we do not check for the file | 94 // 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 | 95 // change. Also note that the granularity for comparison is time_t, not |
89 // the full precision. | 96 // the full precision. |
90 void SetToFilePathRange(const FilePath& path, | 97 void SetToFilePathRange(const FilePath& path, |
91 uint64 offset, uint64 length, | 98 uint64 offset, uint64 length, |
92 const base::Time& expected_modification_time) { | 99 const base::Time& expected_modification_time) { |
93 type_ = TYPE_FILE; | 100 type_ = TYPE_FILE; |
94 file_path_ = path; | 101 file_path_ = path; |
95 file_range_offset_ = offset; | 102 file_range_offset_ = offset; |
96 file_range_length_ = length; | 103 file_range_length_ = length; |
97 expected_file_modification_time_ = expected_modification_time; | 104 expected_file_modification_time_ = expected_modification_time; |
98 } | 105 } |
99 | 106 |
100 // TODO(jianli): UploadData should not contain any blob reference. We need | |
101 // to define another structure to represent WebKit::WebHTTPBody. | |
102 void SetToBlobUrl(const GURL& blob_url) { | |
103 type_ = TYPE_BLOB; | |
104 blob_url_ = blob_url; | |
105 } | |
106 | |
107 // Though similar to bytes, a chunk indicates that the element is sent via | 107 // 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 | 108 // chunked transfer encoding and not buffered until the full upload data |
109 // is available. | 109 // is available. |
110 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 110 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
111 | 111 |
112 bool is_last_chunk() const { return is_last_chunk_; } | 112 bool is_last_chunk() const { return is_last_chunk_; } |
113 // Sets whether this is the last chunk. Used during IPC marshalling. | 113 // Sets whether this is the last chunk. Used during IPC marshalling. |
114 void set_is_last_chunk(bool is_last_chunk) { | 114 void set_is_last_chunk(bool is_last_chunk) { |
115 is_last_chunk_ = is_last_chunk; | 115 is_last_chunk_ = is_last_chunk; |
116 } | 116 } |
(...skipping 28 matching lines...) Expand all Loading... |
145 // TYPE_FILE). | 145 // TYPE_FILE). |
146 int ReadFromFileSync(char* buf, int buf_len); | 146 int ReadFromFileSync(char* buf, int buf_len); |
147 | 147 |
148 // Allows tests to override the result of GetContentLength. | 148 // Allows tests to override the result of GetContentLength. |
149 void SetContentLength(uint64 content_length) { | 149 void SetContentLength(uint64 content_length) { |
150 override_content_length_ = true; | 150 override_content_length_ = true; |
151 content_length_ = content_length; | 151 content_length_ = content_length; |
152 } | 152 } |
153 | 153 |
154 Type type_; | 154 Type type_; |
155 std::vector<char> bytes_; | 155 std::vector<char> buf_; |
| 156 const char* bytes_start_; |
| 157 uint64 bytes_length_; |
156 FilePath file_path_; | 158 FilePath file_path_; |
157 uint64 file_range_offset_; | 159 uint64 file_range_offset_; |
158 uint64 file_range_length_; | 160 uint64 file_range_length_; |
159 base::Time expected_file_modification_time_; | 161 base::Time expected_file_modification_time_; |
160 GURL blob_url_; | |
161 bool is_last_chunk_; | 162 bool is_last_chunk_; |
162 bool override_content_length_; | 163 bool override_content_length_; |
163 bool content_length_computed_; | 164 bool content_length_computed_; |
164 uint64 content_length_; | 165 uint64 content_length_; |
165 | 166 |
166 // The byte offset from the beginning of the element data. Used to track | 167 // The byte offset from the beginning of the element data. Used to track |
167 // the current position when reading data. | 168 // the current position when reading data. |
168 size_t offset_; | 169 size_t offset_; |
169 | 170 |
170 // The stream of the element data, if this element is of TYPE_FILE. | 171 // The stream of the element data, if this element is of TYPE_FILE. |
171 FileStream* file_stream_; | 172 FileStream* file_stream_; |
172 | 173 |
173 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 174 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
174 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 175 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
175 UploadFileSmallerThanLength); | 176 UploadFileSmallerThanLength); |
176 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, | 177 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, |
177 UploadFileSmallerThanLength); | 178 UploadFileSmallerThanLength); |
178 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, | 179 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, |
179 UploadFileSmallerThanLength); | 180 UploadFileSmallerThanLength); |
180 }; | 181 }; |
181 | 182 |
182 UploadData(); | 183 UploadData(); |
183 | 184 |
184 void AppendBytes(const char* bytes, int bytes_len); | 185 void AppendBytes(const char* bytes, int bytes_len); |
185 | 186 |
186 void AppendFileRange(const FilePath& file_path, | 187 void AppendFileRange(const FilePath& file_path, |
187 uint64 offset, uint64 length, | 188 uint64 offset, uint64 length, |
188 const base::Time& expected_modification_time); | 189 const base::Time& expected_modification_time); |
189 | 190 |
190 void AppendBlob(const GURL& blob_url); | |
191 | |
192 // Adds the given chunk of bytes to be sent immediately with chunked transfer | 191 // Adds the given chunk of bytes to be sent immediately with chunked transfer |
193 // encoding. | 192 // encoding. |
194 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 193 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
195 | 194 |
196 // Sets the callback to be invoked when a new chunk is available to upload. | 195 // Sets the callback to be invoked when a new chunk is available to upload. |
197 void set_chunk_callback(ChunkCallback* callback); | 196 void set_chunk_callback(ChunkCallback* callback); |
198 | 197 |
199 // Initializes the object to send chunks of upload data over time rather | 198 // Initializes the object to send chunks of upload data over time rather |
200 // than all at once. | 199 // than all at once. |
201 void set_is_chunked(bool set) { is_chunked_ = set; } | 200 void set_is_chunked(bool set) { is_chunked_ = set; } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 virtual ~UploadData(); | 251 virtual ~UploadData(); |
253 | 252 |
254 std::vector<Element> elements_; | 253 std::vector<Element> elements_; |
255 int64 identifier_; | 254 int64 identifier_; |
256 ChunkCallback* chunk_callback_; | 255 ChunkCallback* chunk_callback_; |
257 bool is_chunked_; | 256 bool is_chunked_; |
258 | 257 |
259 DISALLOW_COPY_AND_ASSIGN(UploadData); | 258 DISALLOW_COPY_AND_ASSIGN(UploadData); |
260 }; | 259 }; |
261 | 260 |
262 #if defined(UNIT_TEST) | |
263 inline bool operator==(const UploadData::Element& a, | |
264 const UploadData::Element& b) { | |
265 if (a.type() != b.type()) | |
266 return false; | |
267 if (a.type() == UploadData::TYPE_BYTES) | |
268 return a.bytes() == b.bytes(); | |
269 if (a.type() == UploadData::TYPE_FILE) { | |
270 return a.file_path() == b.file_path() && | |
271 a.file_range_offset() == b.file_range_offset() && | |
272 a.file_range_length() == b.file_range_length() && | |
273 a.expected_file_modification_time() == | |
274 b.expected_file_modification_time(); | |
275 } | |
276 if (a.type() == UploadData::TYPE_BLOB) | |
277 return a.blob_url() == b.blob_url(); | |
278 return false; | |
279 } | |
280 | |
281 inline bool operator!=(const UploadData::Element& a, | |
282 const UploadData::Element& b) { | |
283 return !(a == b); | |
284 } | |
285 #endif // defined(UNIT_TEST) | |
286 | |
287 } // namespace net | 261 } // namespace net |
288 | 262 |
289 #endif // NET_BASE_UPLOAD_DATA_H_ | 263 #endif // NET_BASE_UPLOAD_DATA_H_ |
OLD | NEW |