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" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 class FileStream; | 19 class FileStream; |
20 | 20 |
21 // A class representing an element contained by UploadData. | 21 // A class representing an element contained by UploadData. |
22 class NET_EXPORT UploadElement { | 22 class NET_EXPORT UploadElement { |
23 public: | 23 public: |
24 enum Type { | 24 enum Type { |
25 TYPE_BYTES, | 25 TYPE_BYTES, |
26 TYPE_FILE, | 26 TYPE_FILE, |
27 TYPE_BLOB, | |
28 | 27 |
29 // A block of bytes to be sent in chunked encoding immediately, without | 28 // A block of bytes to be sent in chunked encoding immediately, without |
30 // waiting for rest of the data. | 29 // waiting for rest of the data. |
31 TYPE_CHUNK, | 30 TYPE_CHUNK, |
32 }; | 31 }; |
33 | 32 |
34 UploadElement(); | 33 UploadElement(); |
35 ~UploadElement(); | 34 ~UploadElement(); |
36 | 35 |
37 Type type() const { return type_; } | 36 Type type() const { return type_; } |
38 // Explicitly sets the type of this UploadElement. Used during IPC | 37 // Explicitly sets the type of this UploadElement. Used during IPC |
39 // marshalling. | 38 // marshalling. |
40 void set_type(Type type) { | 39 void set_type(Type type) { |
41 type_ = type; | 40 type_ = type; |
42 } | 41 } |
43 | 42 |
44 const std::vector<char>& bytes() const { return bytes_; } | 43 const char* bytes() const { return bytes_start_ ? bytes_start_ : &buf_[0]; } |
| 44 uint64 bytes_length() const { return buf_.size() + bytes_length_; } |
45 const FilePath& file_path() const { return file_path_; } | 45 const FilePath& file_path() const { return file_path_; } |
46 uint64 file_range_offset() const { return file_range_offset_; } | 46 uint64 file_range_offset() const { return file_range_offset_; } |
47 uint64 file_range_length() const { return file_range_length_; } | 47 uint64 file_range_length() const { return file_range_length_; } |
48 // If NULL time is returned, we do not do the check. | 48 // If NULL time is returned, we do not do the check. |
49 const base::Time& expected_file_modification_time() const { | 49 const base::Time& expected_file_modification_time() const { |
50 return expected_file_modification_time_; | 50 return expected_file_modification_time_; |
51 } | 51 } |
52 const GURL& blob_url() const { return blob_url_; } | |
53 | 52 |
54 void SetToBytes(const char* bytes, int bytes_len) { | 53 void SetToBytes(const char* bytes, int bytes_len) { |
55 type_ = TYPE_BYTES; | 54 type_ = TYPE_BYTES; |
56 bytes_.assign(bytes, bytes + bytes_len); | 55 buf_.assign(bytes, bytes + bytes_len); |
| 56 } |
| 57 |
| 58 // This does not copy the given data and the caller should make sure |
| 59 // the data is secured somewhere else (e.g. by attaching the data |
| 60 // using SetUserData). |
| 61 void SetToSharedBytes(const char* bytes, int bytes_len) { |
| 62 type_ = TYPE_BYTES; |
| 63 bytes_start_ = bytes; |
| 64 bytes_length_ = bytes_len; |
57 } | 65 } |
58 | 66 |
59 void SetToFilePath(const FilePath& path) { | 67 void SetToFilePath(const FilePath& path) { |
60 SetToFilePathRange(path, 0, kuint64max, base::Time()); | 68 SetToFilePathRange(path, 0, kuint64max, base::Time()); |
61 } | 69 } |
62 | 70 |
63 // If expected_modification_time is NULL, we do not check for the file | 71 // If expected_modification_time is NULL, we do not check for the file |
64 // change. Also note that the granularity for comparison is time_t, not | 72 // change. Also note that the granularity for comparison is time_t, not |
65 // the full precision. | 73 // the full precision. |
66 void SetToFilePathRange(const FilePath& path, | 74 void SetToFilePathRange(const FilePath& path, |
67 uint64 offset, uint64 length, | 75 uint64 offset, uint64 length, |
68 const base::Time& expected_modification_time) { | 76 const base::Time& expected_modification_time) { |
69 type_ = TYPE_FILE; | 77 type_ = TYPE_FILE; |
70 file_path_ = path; | 78 file_path_ = path; |
71 file_range_offset_ = offset; | 79 file_range_offset_ = offset; |
72 file_range_length_ = length; | 80 file_range_length_ = length; |
73 expected_file_modification_time_ = expected_modification_time; | 81 expected_file_modification_time_ = expected_modification_time; |
74 } | 82 } |
75 | 83 |
76 // TODO(jianli): UploadData should not contain any blob reference. We need | |
77 // to define another structure to represent WebKit::WebHTTPBody. | |
78 void SetToBlobUrl(const GURL& blob_url) { | |
79 type_ = TYPE_BLOB; | |
80 blob_url_ = blob_url; | |
81 } | |
82 | |
83 // Though similar to bytes, a chunk indicates that the element is sent via | 84 // Though similar to bytes, a chunk indicates that the element is sent via |
84 // chunked transfer encoding and not buffered until the full upload data | 85 // chunked transfer encoding and not buffered until the full upload data |
85 // is available. | 86 // is available. |
86 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); | 87 void SetToChunk(const char* bytes, int bytes_len, bool is_last_chunk); |
87 | 88 |
88 bool is_last_chunk() const { return is_last_chunk_; } | 89 bool is_last_chunk() const { return is_last_chunk_; } |
89 // Sets whether this is the last chunk. Used during IPC marshalling. | 90 // Sets whether this is the last chunk. Used during IPC marshalling. |
90 void set_is_last_chunk(bool is_last_chunk) { | 91 void set_is_last_chunk(bool is_last_chunk) { |
91 is_last_chunk_ = is_last_chunk; | 92 is_last_chunk_ = is_last_chunk; |
92 } | 93 } |
(...skipping 28 matching lines...) Expand all Loading... |
121 // TYPE_FILE). | 122 // TYPE_FILE). |
122 int ReadFromFileSync(char* buf, int buf_len); | 123 int ReadFromFileSync(char* buf, int buf_len); |
123 | 124 |
124 // Allows tests to override the result of GetContentLength. | 125 // Allows tests to override the result of GetContentLength. |
125 void SetContentLength(uint64 content_length) { | 126 void SetContentLength(uint64 content_length) { |
126 override_content_length_ = true; | 127 override_content_length_ = true; |
127 content_length_ = content_length; | 128 content_length_ = content_length; |
128 } | 129 } |
129 | 130 |
130 Type type_; | 131 Type type_; |
131 std::vector<char> bytes_; | 132 std::vector<char> buf_; |
| 133 const char* bytes_start_; |
| 134 uint64 bytes_length_; |
132 FilePath file_path_; | 135 FilePath file_path_; |
133 uint64 file_range_offset_; | 136 uint64 file_range_offset_; |
134 uint64 file_range_length_; | 137 uint64 file_range_length_; |
135 base::Time expected_file_modification_time_; | 138 base::Time expected_file_modification_time_; |
136 GURL blob_url_; | |
137 bool is_last_chunk_; | 139 bool is_last_chunk_; |
138 bool override_content_length_; | 140 bool override_content_length_; |
139 bool content_length_computed_; | 141 bool content_length_computed_; |
140 uint64 content_length_; | 142 uint64 content_length_; |
141 | 143 |
142 // The byte offset from the beginning of the element data. Used to track | 144 // The byte offset from the beginning of the element data. Used to track |
143 // the current position when reading data. | 145 // the current position when reading data. |
144 size_t offset_; | 146 size_t offset_; |
145 | 147 |
146 // The stream of the element data, if this element is of TYPE_FILE. | 148 // The stream of the element data, if this element is of TYPE_FILE. |
147 FileStream* file_stream_; | 149 FileStream* file_stream_; |
148 | 150 |
149 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); | 151 FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength); |
150 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, | 152 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, |
151 UploadFileSmallerThanLength); | 153 UploadFileSmallerThanLength); |
152 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, | 154 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy2Test, |
153 UploadFileSmallerThanLength); | 155 UploadFileSmallerThanLength); |
154 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, | 156 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionSpdy3Test, |
155 UploadFileSmallerThanLength); | 157 UploadFileSmallerThanLength); |
156 }; | 158 }; |
157 | 159 |
158 #if defined(UNIT_TEST) | 160 #if defined(UNIT_TEST) |
159 inline bool operator==(const UploadElement& a, | 161 inline bool operator==(const UploadElement& a, |
160 const UploadElement& b) { | 162 const UploadElement& b) { |
161 if (a.type() != b.type()) | 163 if (a.type() != b.type()) |
162 return false; | 164 return false; |
163 if (a.type() == UploadElement::TYPE_BYTES) | 165 if (a.type() == UploadElement::TYPE_BYTES) |
164 return a.bytes() == b.bytes(); | 166 return a.bytes_length() == b.bytes_length() && |
| 167 memcmp(a.bytes(), b.bytes(), b.bytes_length()) == 0; |
165 if (a.type() == UploadElement::TYPE_FILE) { | 168 if (a.type() == UploadElement::TYPE_FILE) { |
166 return a.file_path() == b.file_path() && | 169 return a.file_path() == b.file_path() && |
167 a.file_range_offset() == b.file_range_offset() && | 170 a.file_range_offset() == b.file_range_offset() && |
168 a.file_range_length() == b.file_range_length() && | 171 a.file_range_length() == b.file_range_length() && |
169 a.expected_file_modification_time() == | 172 a.expected_file_modification_time() == |
170 b.expected_file_modification_time(); | 173 b.expected_file_modification_time(); |
171 } | 174 } |
172 if (a.type() == UploadElement::TYPE_BLOB) | |
173 return a.blob_url() == b.blob_url(); | |
174 return false; | 175 return false; |
175 } | 176 } |
176 | 177 |
177 inline bool operator!=(const UploadElement& a, | 178 inline bool operator!=(const UploadElement& a, |
178 const UploadElement& b) { | 179 const UploadElement& b) { |
179 return !(a == b); | 180 return !(a == b); |
180 } | 181 } |
181 #endif // defined(UNIT_TEST) | 182 #endif // defined(UNIT_TEST) |
182 | 183 |
183 } // namespace net | 184 } // namespace net |
184 | 185 |
185 #endif // NET_BASE_UPLOAD_ELEMENT_H_ | 186 #endif // NET_BASE_UPLOAD_ELEMENT_H_ |
OLD | NEW |