| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/base/elements_upload_data_stream.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "net/base/completion_callback.h" | |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/upload_bytes_element_reader.h" | |
| 13 #include "net/base/upload_element_reader.h" | |
| 14 | |
| 15 namespace net { | |
| 16 | |
| 17 ElementsUploadDataStream::ElementsUploadDataStream( | |
| 18 ScopedVector<UploadElementReader> element_readers, | |
| 19 int64 identifier) | |
| 20 : UploadDataStream(false, identifier), | |
| 21 element_readers_(element_readers.Pass()), | |
| 22 element_index_(0), | |
| 23 read_failed_(false), | |
| 24 weak_ptr_factory_(this) { | |
| 25 } | |
| 26 | |
| 27 ElementsUploadDataStream::~ElementsUploadDataStream() { | |
| 28 } | |
| 29 | |
| 30 scoped_ptr<UploadDataStream> ElementsUploadDataStream::CreateWithReader( | |
| 31 scoped_ptr<UploadElementReader> reader, | |
| 32 int64 identifier) { | |
| 33 ScopedVector<UploadElementReader> readers; | |
| 34 readers.push_back(reader.release()); | |
| 35 return scoped_ptr<UploadDataStream>( | |
| 36 new ElementsUploadDataStream(readers.Pass(), identifier)); | |
| 37 } | |
| 38 | |
| 39 int ElementsUploadDataStream::InitInternal() { | |
| 40 return InitElements(0); | |
| 41 } | |
| 42 | |
| 43 int ElementsUploadDataStream::ReadInternal( | |
| 44 IOBuffer* buf, | |
| 45 int buf_len) { | |
| 46 DCHECK_GT(buf_len, 0); | |
| 47 return ReadElements(new DrainableIOBuffer(buf, buf_len)); | |
| 48 } | |
| 49 | |
| 50 bool ElementsUploadDataStream::IsInMemory() const { | |
| 51 for (size_t i = 0; i < element_readers_.size(); ++i) { | |
| 52 if (!element_readers_[i]->IsInMemory()) | |
| 53 return false; | |
| 54 } | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 const ScopedVector<UploadElementReader>* | |
| 59 ElementsUploadDataStream::GetElementReaders() const { | |
| 60 return &element_readers_; | |
| 61 } | |
| 62 | |
| 63 void ElementsUploadDataStream::ResetInternal() { | |
| 64 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 65 read_failed_ = false; | |
| 66 element_index_ = 0; | |
| 67 } | |
| 68 | |
| 69 int ElementsUploadDataStream::InitElements(size_t start_index) { | |
| 70 // Call Init() for all elements. | |
| 71 for (size_t i = start_index; i < element_readers_.size(); ++i) { | |
| 72 UploadElementReader* reader = element_readers_[i]; | |
| 73 // When new_result is ERR_IO_PENDING, InitInternal() will be called | |
| 74 // with start_index == i + 1 when reader->Init() finishes. | |
| 75 int result = reader->Init( | |
| 76 base::Bind(&ElementsUploadDataStream::OnInitElementCompleted, | |
| 77 weak_ptr_factory_.GetWeakPtr(), | |
| 78 i)); | |
| 79 DCHECK(result != ERR_IO_PENDING || !reader->IsInMemory()); | |
| 80 DCHECK_LE(result, OK); | |
| 81 if (result != OK) | |
| 82 return result; | |
| 83 } | |
| 84 | |
| 85 uint64 total_size = 0; | |
| 86 for (size_t i = 0; i < element_readers_.size(); ++i) { | |
| 87 total_size += element_readers_[i]->GetContentLength(); | |
| 88 } | |
| 89 SetSize(total_size); | |
| 90 return OK; | |
| 91 } | |
| 92 | |
| 93 void ElementsUploadDataStream::OnInitElementCompleted(size_t index, | |
| 94 int result) { | |
| 95 DCHECK_NE(ERR_IO_PENDING, result); | |
| 96 | |
| 97 // Check the last result. | |
| 98 if (result == OK) | |
| 99 result = InitElements(index + 1); | |
| 100 | |
| 101 if (result != ERR_IO_PENDING) | |
| 102 OnInitCompleted(result); | |
| 103 } | |
| 104 | |
| 105 int ElementsUploadDataStream::ReadElements( | |
| 106 const scoped_refptr<DrainableIOBuffer>& buf) { | |
| 107 while (!read_failed_ && element_index_ < element_readers_.size()) { | |
| 108 UploadElementReader* reader = element_readers_[element_index_]; | |
| 109 | |
| 110 if (reader->BytesRemaining() == 0) { | |
| 111 ++element_index_; | |
| 112 continue; | |
| 113 } | |
| 114 | |
| 115 if (buf->BytesRemaining() == 0) | |
| 116 break; | |
| 117 | |
| 118 int result = reader->Read( | |
| 119 buf.get(), | |
| 120 buf->BytesRemaining(), | |
| 121 base::Bind(&ElementsUploadDataStream::OnReadElementCompleted, | |
| 122 weak_ptr_factory_.GetWeakPtr(), | |
| 123 buf)); | |
| 124 if (result == ERR_IO_PENDING) | |
| 125 return ERR_IO_PENDING; | |
| 126 ProcessReadResult(buf, result); | |
| 127 } | |
| 128 | |
| 129 if (read_failed_) { | |
| 130 // If an error occured during read operation, then pad with zero. | |
| 131 // Otherwise the server will hang waiting for the rest of the data. | |
| 132 int num_bytes_to_fill = static_cast<int>(std::min( | |
| 133 static_cast<uint64>(buf->BytesRemaining()), | |
| 134 size() - position() - buf->BytesConsumed())); | |
| 135 DCHECK_GE(num_bytes_to_fill, 0); | |
| 136 memset(buf->data(), 0, num_bytes_to_fill); | |
| 137 buf->DidConsume(num_bytes_to_fill); | |
| 138 } | |
| 139 | |
| 140 return buf->BytesConsumed(); | |
| 141 } | |
| 142 | |
| 143 void ElementsUploadDataStream::OnReadElementCompleted( | |
| 144 const scoped_refptr<DrainableIOBuffer>& buf, | |
| 145 int result) { | |
| 146 ProcessReadResult(buf, result); | |
| 147 | |
| 148 result = ReadElements(buf); | |
| 149 if (result != ERR_IO_PENDING) | |
| 150 OnReadCompleted(result); | |
| 151 } | |
| 152 | |
| 153 void ElementsUploadDataStream::ProcessReadResult( | |
| 154 const scoped_refptr<DrainableIOBuffer>& buf, | |
| 155 int result) { | |
| 156 DCHECK_NE(ERR_IO_PENDING, result); | |
| 157 DCHECK(!read_failed_); | |
| 158 | |
| 159 if (result >= 0) { | |
| 160 buf->DidConsume(result); | |
| 161 } else { | |
| 162 read_failed_ = true; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 } // namespace net | |
| OLD | NEW |