| 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_stream.h" | 5 #include "net/base/upload_data_stream.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/upload_element_reader.h" | 10 #include "net/base/upload_element_reader.h" |
| 11 | 11 |
| 12 namespace net { | 12 namespace net { |
| 13 | 13 |
| 14 bool UploadDataStream::merge_chunks_ = true; | 14 bool UploadDataStream::merge_chunks_ = true; |
| 15 | 15 |
| 16 // static | 16 // static |
| 17 void UploadDataStream::ResetMergeChunks() { | 17 void UploadDataStream::ResetMergeChunks() { |
| 18 // WARNING: merge_chunks_ must match the above initializer. | 18 // WARNING: merge_chunks_ must match the above initializer. |
| 19 merge_chunks_ = true; | 19 merge_chunks_ = true; |
| 20 } | 20 } |
| 21 | 21 |
| 22 UploadDataStream::UploadDataStream(UploadData* upload_data) | 22 UploadDataStream::UploadDataStream(UploadData* upload_data) |
| 23 : upload_data_(upload_data), | 23 : upload_data_(upload_data), |
| 24 element_index_(0), | 24 element_index_(0), |
| 25 total_size_(0), | 25 total_size_(0), |
| 26 current_position_(0), | 26 current_position_(0), |
| 27 initialized_successfully_(false), | 27 initialized_successfully_(false), |
| 28 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 28 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 29 weak_ptr_factory_for_chunks_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 29 weak_ptr_factory_for_chunks_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 30 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 30 const ScopedVector<UploadElement>& elements = upload_data_->elements(); |
| 31 for (size_t i = 0; i < elements.size(); ++i) | 31 for (size_t i = 0; i < elements.size(); ++i) |
| 32 element_readers_.push_back(UploadElementReader::Create(elements[i])); | 32 element_readers_.push_back(UploadElementReader::Create(*elements[i])); |
| 33 | 33 |
| 34 upload_data_->set_chunk_callback( | 34 upload_data_->set_chunk_callback( |
| 35 base::Bind(&UploadDataStream::OnChunkAvailable, | 35 base::Bind(&UploadDataStream::OnChunkAvailable, |
| 36 weak_ptr_factory_for_chunks_.GetWeakPtr())); | 36 weak_ptr_factory_for_chunks_.GetWeakPtr())); |
| 37 } | 37 } |
| 38 | 38 |
| 39 UploadDataStream::~UploadDataStream() { | 39 UploadDataStream::~UploadDataStream() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 int UploadDataStream::Init(const CompletionCallback& callback) { | 42 int UploadDataStream::Init(const CompletionCallback& callback) { |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 DCHECK(!callback.is_null()); | 234 DCHECK(!callback.is_null()); |
| 235 const int result = ReadInternal(buf, callback); | 235 const int result = ReadInternal(buf, callback); |
| 236 if (result != ERR_IO_PENDING) | 236 if (result != ERR_IO_PENDING) |
| 237 callback.Run(result); | 237 callback.Run(result); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void UploadDataStream::OnChunkAvailable() { | 240 void UploadDataStream::OnChunkAvailable() { |
| 241 DCHECK(is_chunked()); | 241 DCHECK(is_chunked()); |
| 242 | 242 |
| 243 // Initialize a reader for the newly appended chunk. | 243 // Initialize a reader for the newly appended chunk. |
| 244 const std::vector<UploadElement>& elements = *upload_data_->elements(); | 244 const ScopedVector<UploadElement>& elements = upload_data_->elements(); |
| 245 DCHECK_EQ(elements.size(), element_readers_.size() + 1); | 245 DCHECK_EQ(elements.size(), element_readers_.size() + 1); |
| 246 | 246 |
| 247 // We can initialize the reader synchronously here because only bytes can be | 247 // We can initialize the reader synchronously here because only bytes can be |
| 248 // appended for chunked data. We leave |total_size_| at zero, since for | 248 // appended for chunked data. We leave |total_size_| at zero, since for |
| 249 // chunked uploads, we may not know the total size. | 249 // chunked uploads, we may not know the total size. |
| 250 const UploadElement& element = elements.back(); | 250 const UploadElement& element = *elements.back(); |
| 251 DCHECK_EQ(UploadElement::TYPE_BYTES, element.type()); | 251 DCHECK_EQ(UploadElement::TYPE_BYTES, element.type()); |
| 252 UploadElementReader* reader = UploadElementReader::Create(element); | 252 UploadElementReader* reader = UploadElementReader::Create(element); |
| 253 const int rv = reader->InitSync(); | 253 const int rv = reader->InitSync(); |
| 254 DCHECK_EQ(OK, rv); | 254 DCHECK_EQ(OK, rv); |
| 255 element_readers_.push_back(reader); | 255 element_readers_.push_back(reader); |
| 256 | 256 |
| 257 // Resume pending read. | 257 // Resume pending read. |
| 258 if (!pending_chunked_read_callback_.is_null()) { | 258 if (!pending_chunked_read_callback_.is_null()) { |
| 259 base::Closure callback = pending_chunked_read_callback_; | 259 base::Closure callback = pending_chunked_read_callback_; |
| 260 pending_chunked_read_callback_.Reset(); | 260 pending_chunked_read_callback_.Reset(); |
| 261 callback.Run(); | 261 callback.Run(); |
| 262 } | 262 } |
| 263 } | 263 } |
| 264 | 264 |
| 265 } // namespace net | 265 } // namespace net |
| OLD | NEW |