Chromium Code Reviews| 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]; } |
|
darin (slow to review)
2012/08/15 17:49:55
maybe you should just ensure that bytes_start_ is
kinuko
2012/08/16 08:14:59
To do so we need to allocate memory by ourselves a
| |
| 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 void SetToSharedBytes(const char* bytes, int bytes_len) { | |
|
darin (slow to review)
2012/08/15 17:49:55
You should probably add a warning here about memor
kinuko
2012/08/16 08:14:59
Done.
| |
| 82 type_ = TYPE_BYTES; | |
| 83 bytes_start_ = bytes; | |
| 84 bytes_length_ = bytes_len; | |
| 81 } | 85 } |
| 82 | 86 |
| 83 void SetToFilePath(const FilePath& path) { | 87 void SetToFilePath(const FilePath& path) { |
| 84 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 88 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
| 85 } | 89 } |
| 86 | 90 |
| 87 // If expected_modification_time is NULL, we do not check for the file | 91 // 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 | 92 // change. Also note that the granularity for comparison is time_t, not |
| 89 // the full precision. | 93 // the full precision. |
| 90 void SetToFilePathRange(const FilePath& path, | 94 void SetToFilePathRange(const FilePath& path, |
| 91 uint64 offset, uint64 length, | 95 uint64 offset, uint64 length, |
| 92 const base::Time& expected_modification_time) { | 96 const base::Time& expected_modification_time) { |
| 93 type_ = TYPE_FILE; | 97 type_ = TYPE_FILE; |
| 94 file_path_ = path; | 98 file_path_ = path; |
| 95 file_range_offset_ = offset; | 99 file_range_offset_ = offset; |
| 96 file_range_length_ = length; | 100 file_range_length_ = length; |
| 97 expected_file_modification_time_ = expected_modification_time; | 101 expected_file_modification_time_ = expected_modification_time; |
| 98 } | 102 } |
| 99 | 103 |
| 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 | 104 // 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 | 105 // chunked transfer encoding and not buffered until the full upload data |
| 109 // is available. | 106 // is available. |
| 110 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 107 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
| 111 | 108 |
| 112 bool is_last_chunk() const { return is_last_chunk_; } | 109 bool is_last_chunk() const { return is_last_chunk_; } |
| 113 // Sets whether this is the last chunk. Used during IPC marshalling. | 110 // Sets whether this is the last chunk. Used during IPC marshalling. |
| 114 void set_is_last_chunk(bool is_last_chunk) { | 111 void set_is_last_chunk(bool is_last_chunk) { |
| 115 is_last_chunk_ = is_last_chunk; | 112 is_last_chunk_ = is_last_chunk; |
| 116 } | 113 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 145 // TYPE_FILE). | 142 // TYPE_FILE). |
| 146 int ReadFromFileSync(char* buf, int buf_len); | 143 int ReadFromFileSync(char* buf, int buf_len); |
| 147 | 144 |
| 148 // Allows tests to override the result of GetContentLength. | 145 // Allows tests to override the result of GetContentLength. |
| 149 void SetContentLength(uint64 content_length) { | 146 void SetContentLength(uint64 content_length) { |
| 150 override_content_length_ = true; | 147 override_content_length_ = true; |
| 151 content_length_ = content_length; | 148 content_length_ = content_length; |
| 152 } | 149 } |
| 153 | 150 |
| 154 Type type_; | 151 Type type_; |
| 155 std::vector<char> bytes_; | 152 std::vector<char> buf_; |
| 153 const char* bytes_start_; | |
| 154 uint64 bytes_length_; | |
| 156 FilePath file_path_; | 155 FilePath file_path_; |
| 157 uint64 file_range_offset_; | 156 uint64 file_range_offset_; |
| 158 uint64 file_range_length_; | 157 uint64 file_range_length_; |
| 159 base::Time expected_file_modification_time_; | 158 base::Time expected_file_modification_time_; |
| 160 GURL blob_url_; | |
| 161 bool is_last_chunk_; | 159 bool is_last_chunk_; |
| 162 bool override_content_length_; | 160 bool override_content_length_; |
| 163 bool content_length_computed_; | 161 bool content_length_computed_; |
| 164 uint64 content_length_; | 162 uint64 content_length_; |
| 165 | 163 |
| 166 // The byte offset from the beginning of the element data. Used to track | 164 // The byte offset from the beginning of the element data. Used to track |
| 167 // the current position when reading data. | 165 // the current position when reading data. |
| 168 size_t offset_; | 166 size_t offset_; |
| 169 | 167 |
| 170 // The stream of the element data, if this element is of TYPE_FILE. | 168 // The stream of the element data, if this element is of TYPE_FILE. |
| 171 FileStream* file_stream_; | 169 FileStream* file_stream_; |
| 172 | 170 |
| 173 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 171 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
| 174 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 172 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
| 175 UploadFileSmallerThanLength); | 173 UploadFileSmallerThanLength); |
| 176 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, | 174 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, |
| 177 UploadFileSmallerThanLength); | 175 UploadFileSmallerThanLength); |
| 178 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, | 176 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, |
| 179 UploadFileSmallerThanLength); | 177 UploadFileSmallerThanLength); |
| 180 }; | 178 }; |
| 181 | 179 |
| 182 UploadData(); | 180 UploadData(); |
| 183 | 181 |
| 184 void AppendBytes(const char* bytes, int bytes_len); | 182 void AppendBytes(const char* bytes, int bytes_len); |
| 185 | 183 |
| 186 void AppendFileRange(const FilePath& file_path, | 184 void AppendFileRange(const FilePath& file_path, |
| 187 uint64 offset, uint64 length, | 185 uint64 offset, uint64 length, |
| 188 const base::Time& expected_modification_time); | 186 const base::Time& expected_modification_time); |
| 189 | 187 |
| 190 void AppendBlob(const GURL& blob_url); | |
| 191 | |
| 192 // Adds the given chunk of bytes to be sent immediately with chunked transfer | 188 // Adds the given chunk of bytes to be sent immediately with chunked transfer |
| 193 // encoding. | 189 // encoding. |
| 194 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 190 void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
| 195 | 191 |
| 196 // Sets the callback to be invoked when a new chunk is available to upload. | 192 // Sets the callback to be invoked when a new chunk is available to upload. |
| 197 void set_chunk_callback(ChunkCallback* callback); | 193 void set_chunk_callback(ChunkCallback* callback); |
| 198 | 194 |
| 199 // Initializes the object to send chunks of upload data over time rather | 195 // Initializes the object to send chunks of upload data over time rather |
| 200 // than all at once. | 196 // than all at once. |
| 201 void set_is_chunked(bool set) { is_chunked_ = set; } | 197 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(); | 248 virtual ~UploadData(); |
| 253 | 249 |
| 254 std::vector<Element> elements_; | 250 std::vector<Element> elements_; |
| 255 int64 identifier_; | 251 int64 identifier_; |
| 256 ChunkCallback* chunk_callback_; | 252 ChunkCallback* chunk_callback_; |
| 257 bool is_chunked_; | 253 bool is_chunked_; |
| 258 | 254 |
| 259 DISALLOW_COPY_AND_ASSIGN(UploadData); | 255 DISALLOW_COPY_AND_ASSIGN(UploadData); |
| 260 }; | 256 }; |
| 261 | 257 |
| 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 | 258 } // namespace net |
| 288 | 259 |
| 289 #endif // NET_BASE_UPLOAD_DATA_H_ | 260 #endif // NET_BASE_UPLOAD_DATA_H_ |
| OLD | NEW |