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 #include "net/base/upload_data.h" | 5 #include "net/base/upload_data.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 void OnGetContentLengthComplete( | 24 void OnGetContentLengthComplete( |
25 const UploadData::ContentLengthCallback& callback, | 25 const UploadData::ContentLengthCallback& callback, |
26 uint64* content_length) { | 26 uint64* content_length) { |
27 callback.Run(*content_length); | 27 callback.Run(*content_length); |
28 } | 28 } |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 UploadData::Element::Element() | 32 UploadData::Element::Element() |
33 : type_(TYPE_BYTES), | 33 : type_(TYPE_BYTES), |
| 34 bytes_start_(NULL), |
| 35 bytes_length_(0), |
34 file_range_offset_(0), | 36 file_range_offset_(0), |
35 file_range_length_(kuint64max), | 37 file_range_length_(kuint64max), |
36 is_last_chunk_(false), | 38 is_last_chunk_(false), |
37 override_content_length_(false), | 39 override_content_length_(false), |
38 content_length_computed_(false), | 40 content_length_computed_(false), |
39 content_length_(-1), | 41 content_length_(-1), |
40 offset_(0), | 42 offset_(0), |
41 file_stream_(NULL) { | 43 file_stream_(NULL) { |
42 } | 44 } |
43 | 45 |
44 UploadData::Element::~Element() { | 46 UploadData::Element::~Element() { |
45 // In the common case |file__stream_| will be null. | 47 // In the common case |file__stream_| will be null. |
46 if (file_stream_) { | 48 if (file_stream_) { |
47 // Temporarily allow until fix: http://crbug.com/72001. | 49 // Temporarily allow until fix: http://crbug.com/72001. |
48 base::ThreadRestrictions::ScopedAllowIO allow_io; | 50 base::ThreadRestrictions::ScopedAllowIO allow_io; |
49 file_stream_->CloseSync(); | 51 file_stream_->CloseSync(); |
50 delete file_stream_; | 52 delete file_stream_; |
51 } | 53 } |
52 } | 54 } |
53 | 55 |
54 void UploadData::Element::SetToChunk(const char* bytes, | 56 void UploadData::Element::SetToChunk(const char* bytes, |
55 int bytes_len, | 57 int bytes_len, |
56 bool is_last_chunk) { | 58 bool is_last_chunk) { |
57 bytes_.clear(); | |
58 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); | |
59 type_ = TYPE_CHUNK; | 59 type_ = TYPE_CHUNK; |
| 60 buf_.assign(bytes, bytes + bytes_len); |
60 is_last_chunk_ = is_last_chunk; | 61 is_last_chunk_ = is_last_chunk; |
61 } | 62 } |
62 | 63 |
63 uint64 UploadData::Element::GetContentLength() { | 64 uint64 UploadData::Element::GetContentLength() { |
64 if (override_content_length_ || content_length_computed_) | 65 if (override_content_length_ || content_length_computed_) |
65 return content_length_; | 66 return content_length_; |
66 | 67 |
67 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) | 68 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
68 return static_cast<uint64>(bytes_.size()); | 69 return bytes_length(); |
69 else if (type_ == TYPE_BLOB) | |
70 // The blob reference will be resolved later. | |
71 return 0; | |
72 | 70 |
73 DCHECK_EQ(TYPE_FILE, type_); | 71 DCHECK_EQ(TYPE_FILE, type_); |
74 DCHECK(!file_stream_); | 72 DCHECK(!file_stream_); |
75 | 73 |
76 // TODO(darin): This size calculation could be out of sync with the state of | 74 // TODO(darin): This size calculation could be out of sync with the state of |
77 // the file when we get around to reading it. We should probably find a way | 75 // the file when we get around to reading it. We should probably find a way |
78 // to lock the file or somehow protect against this error condition. | 76 // to lock the file or somehow protect against this error condition. |
79 | 77 |
80 content_length_computed_ = true; | 78 content_length_computed_ = true; |
81 content_length_ = 0; | 79 content_length_ = 0; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 int UploadData::Element::ReadFromMemorySync(char* buf, int buf_len) { | 153 int UploadData::Element::ReadFromMemorySync(char* buf, int buf_len) { |
156 DCHECK_LT(0, buf_len); | 154 DCHECK_LT(0, buf_len); |
157 DCHECK(type_ == UploadData::TYPE_BYTES || type_ == UploadData::TYPE_CHUNK); | 155 DCHECK(type_ == UploadData::TYPE_BYTES || type_ == UploadData::TYPE_CHUNK); |
158 | 156 |
159 const size_t num_bytes_to_read = std::min(BytesRemaining(), | 157 const size_t num_bytes_to_read = std::min(BytesRemaining(), |
160 static_cast<uint64>(buf_len)); | 158 static_cast<uint64>(buf_len)); |
161 | 159 |
162 // Check if we have anything to copy first, because we are getting | 160 // Check if we have anything to copy first, because we are getting |
163 // the address of an element in |bytes_| and that will throw an | 161 // the address of an element in |bytes_| and that will throw an |
164 // exception if |bytes_| is an empty vector. | 162 // exception if |bytes_| is an empty vector. |
165 if (num_bytes_to_read > 0) { | 163 if (num_bytes_to_read > 0) |
166 memcpy(buf, &bytes_[offset_], num_bytes_to_read); | 164 memcpy(buf, bytes() + offset_, num_bytes_to_read); |
167 } | |
168 | 165 |
169 offset_ += num_bytes_to_read; | 166 offset_ += num_bytes_to_read; |
170 return num_bytes_to_read; | 167 return num_bytes_to_read; |
171 } | 168 } |
172 | 169 |
173 int UploadData::Element::ReadFromFileSync(char* buf, int buf_len) { | 170 int UploadData::Element::ReadFromFileSync(char* buf, int buf_len) { |
174 DCHECK_LT(0, buf_len); | 171 DCHECK_LT(0, buf_len); |
175 DCHECK_EQ(UploadData::TYPE_FILE, type_); | 172 DCHECK_EQ(UploadData::TYPE_FILE, type_); |
176 | 173 |
177 // Open the file of the current element if not yet opened. | 174 // Open the file of the current element if not yet opened. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 | 220 |
224 void UploadData::AppendFileRange(const FilePath& file_path, | 221 void UploadData::AppendFileRange(const FilePath& file_path, |
225 uint64 offset, uint64 length, | 222 uint64 offset, uint64 length, |
226 const base::Time& expected_modification_time) { | 223 const base::Time& expected_modification_time) { |
227 DCHECK(!is_chunked_); | 224 DCHECK(!is_chunked_); |
228 elements_.push_back(Element()); | 225 elements_.push_back(Element()); |
229 elements_.back().SetToFilePathRange(file_path, offset, length, | 226 elements_.back().SetToFilePathRange(file_path, offset, length, |
230 expected_modification_time); | 227 expected_modification_time); |
231 } | 228 } |
232 | 229 |
233 void UploadData::AppendBlob(const GURL& blob_url) { | |
234 DCHECK(!is_chunked_); | |
235 elements_.push_back(Element()); | |
236 elements_.back().SetToBlobUrl(blob_url); | |
237 } | |
238 | |
239 void UploadData::AppendChunk(const char* bytes, | 230 void UploadData::AppendChunk(const char* bytes, |
240 int bytes_len, | 231 int bytes_len, |
241 bool is_last_chunk) { | 232 bool is_last_chunk) { |
242 DCHECK(is_chunked_); | 233 DCHECK(is_chunked_); |
243 elements_.push_back(Element()); | 234 elements_.push_back(Element()); |
244 elements_.back().SetToChunk(bytes, bytes_len, is_last_chunk); | 235 elements_.back().SetToChunk(bytes, bytes_len, is_last_chunk); |
245 if (chunk_callback_) | 236 if (chunk_callback_) |
246 chunk_callback_->OnChunkAvailable(); | 237 chunk_callback_->OnChunkAvailable(); |
247 } | 238 } |
248 | 239 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 return; | 290 return; |
300 | 291 |
301 for (size_t i = 0; i < elements_.size(); ++i) | 292 for (size_t i = 0; i < elements_.size(); ++i) |
302 *content_length += elements_[i].GetContentLength(); | 293 *content_length += elements_[i].GetContentLength(); |
303 } | 294 } |
304 | 295 |
305 UploadData::~UploadData() { | 296 UploadData::~UploadData() { |
306 } | 297 } |
307 | 298 |
308 } // namespace net | 299 } // namespace net |
OLD | NEW |