[Mac] Enable MacViews bookmark editor behind --enable-mac-views-dialogs.
[chromium-blink-merge.git] / net / base / elements_upload_data_stream.cc
blobe23f632ab383e139a38e8d3af918be4db8c374ab
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.
5 #include "net/base/elements_upload_data_stream.h"
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"
15 namespace net {
17 ElementsUploadDataStream::ElementsUploadDataStream(
18 ScopedVector<UploadElementReader> element_readers,
19 int64_t identifier)
20 : UploadDataStream(false, identifier),
21 element_readers_(element_readers.Pass()),
22 element_index_(0),
23 read_failed_(false),
24 weak_ptr_factory_(this) {
27 ElementsUploadDataStream::~ElementsUploadDataStream() {
30 scoped_ptr<UploadDataStream> ElementsUploadDataStream::CreateWithReader(
31 scoped_ptr<UploadElementReader> reader,
32 int64_t identifier) {
33 ScopedVector<UploadElementReader> readers;
34 readers.push_back(reader.Pass());
35 return scoped_ptr<UploadDataStream>(
36 new ElementsUploadDataStream(readers.Pass(), identifier));
39 int ElementsUploadDataStream::InitInternal() {
40 return InitElements(0);
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));
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;
55 return true;
58 const ScopedVector<UploadElementReader>*
59 ElementsUploadDataStream::GetElementReaders() const {
60 return &element_readers_;
63 void ElementsUploadDataStream::ResetInternal() {
64 weak_ptr_factory_.InvalidateWeakPtrs();
65 read_failed_ = false;
66 element_index_ = 0;
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;
85 uint64_t total_size = 0;
86 for (size_t i = 0; i < element_readers_.size(); ++i) {
87 total_size += element_readers_[i]->GetContentLength();
89 SetSize(total_size);
90 return OK;
93 void ElementsUploadDataStream::OnInitElementCompleted(size_t index,
94 int result) {
95 DCHECK_NE(ERR_IO_PENDING, result);
97 // Check the last result.
98 if (result == OK)
99 result = InitElements(index + 1);
101 if (result != ERR_IO_PENDING)
102 OnInitCompleted(result);
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_];
110 if (reader->BytesRemaining() == 0) {
111 ++element_index_;
112 continue;
115 if (buf->BytesRemaining() == 0)
116 break;
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);
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 =
133 static_cast<int>(std::min(static_cast<uint64_t>(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);
140 return buf->BytesConsumed();
143 void ElementsUploadDataStream::OnReadElementCompleted(
144 const scoped_refptr<DrainableIOBuffer>& buf,
145 int result) {
146 ProcessReadResult(buf, result);
148 result = ReadElements(buf);
149 if (result != ERR_IO_PENDING)
150 OnReadCompleted(result);
153 void ElementsUploadDataStream::ProcessReadResult(
154 const scoped_refptr<DrainableIOBuffer>& buf,
155 int result) {
156 DCHECK_NE(ERR_IO_PENDING, result);
157 DCHECK(!read_failed_);
159 if (result >= 0) {
160 buf->DidConsume(result);
161 } else {
162 read_failed_ = true;
166 } // namespace net