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/elements_upload_data_stream.h" | 5 #include "net/base/elements_upload_data_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "net/base/completion_callback.h" | 9 #include "net/base/completion_callback.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/upload_bytes_element_reader.h" | 12 #include "net/base/upload_bytes_element_reader.h" |
13 #include "net/base/upload_element_reader.h" | 13 #include "net/base/upload_element_reader.h" |
14 | 14 |
15 namespace net { | 15 namespace net { |
16 | 16 |
17 ElementsUploadDataStream::ElementsUploadDataStream( | 17 ElementsUploadDataStream::ElementsUploadDataStream( |
18 ScopedVector<UploadElementReader> element_readers, | 18 ScopedVector<UploadElementReader> element_readers, |
19 int64 identifier) | 19 int64_t identifier) |
20 : UploadDataStream(false, identifier), | 20 : UploadDataStream(false, identifier), |
21 element_readers_(element_readers.Pass()), | 21 element_readers_(element_readers.Pass()), |
22 element_index_(0), | 22 element_index_(0), |
23 read_failed_(false), | 23 read_failed_(false), |
24 weak_ptr_factory_(this) { | 24 weak_ptr_factory_(this) { |
25 } | 25 } |
26 | 26 |
27 ElementsUploadDataStream::~ElementsUploadDataStream() { | 27 ElementsUploadDataStream::~ElementsUploadDataStream() { |
28 } | 28 } |
29 | 29 |
30 scoped_ptr<UploadDataStream> ElementsUploadDataStream::CreateWithReader( | 30 scoped_ptr<UploadDataStream> ElementsUploadDataStream::CreateWithReader( |
31 scoped_ptr<UploadElementReader> reader, | 31 scoped_ptr<UploadElementReader> reader, |
32 int64 identifier) { | 32 int64_t identifier) { |
33 ScopedVector<UploadElementReader> readers; | 33 ScopedVector<UploadElementReader> readers; |
34 readers.push_back(reader.Pass()); | 34 readers.push_back(reader.Pass()); |
35 return scoped_ptr<UploadDataStream>( | 35 return scoped_ptr<UploadDataStream>( |
36 new ElementsUploadDataStream(readers.Pass(), identifier)); | 36 new ElementsUploadDataStream(readers.Pass(), identifier)); |
37 } | 37 } |
38 | 38 |
39 int ElementsUploadDataStream::InitInternal() { | 39 int ElementsUploadDataStream::InitInternal() { |
40 return InitElements(0); | 40 return InitElements(0); |
41 } | 41 } |
42 | 42 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 int result = reader->Init( | 75 int result = reader->Init( |
76 base::Bind(&ElementsUploadDataStream::OnInitElementCompleted, | 76 base::Bind(&ElementsUploadDataStream::OnInitElementCompleted, |
77 weak_ptr_factory_.GetWeakPtr(), | 77 weak_ptr_factory_.GetWeakPtr(), |
78 i)); | 78 i)); |
79 DCHECK(result != ERR_IO_PENDING || !reader->IsInMemory()); | 79 DCHECK(result != ERR_IO_PENDING || !reader->IsInMemory()); |
80 DCHECK_LE(result, OK); | 80 DCHECK_LE(result, OK); |
81 if (result != OK) | 81 if (result != OK) |
82 return result; | 82 return result; |
83 } | 83 } |
84 | 84 |
85 uint64 total_size = 0; | 85 uint64_t total_size = 0; |
86 for (size_t i = 0; i < element_readers_.size(); ++i) { | 86 for (size_t i = 0; i < element_readers_.size(); ++i) { |
87 total_size += element_readers_[i]->GetContentLength(); | 87 total_size += element_readers_[i]->GetContentLength(); |
88 } | 88 } |
89 SetSize(total_size); | 89 SetSize(total_size); |
90 return OK; | 90 return OK; |
91 } | 91 } |
92 | 92 |
93 void ElementsUploadDataStream::OnInitElementCompleted(size_t index, | 93 void ElementsUploadDataStream::OnInitElementCompleted(size_t index, |
94 int result) { | 94 int result) { |
95 DCHECK_NE(ERR_IO_PENDING, result); | 95 DCHECK_NE(ERR_IO_PENDING, result); |
(...skipping 26 matching lines...) Expand all Loading... |
122 weak_ptr_factory_.GetWeakPtr(), | 122 weak_ptr_factory_.GetWeakPtr(), |
123 buf)); | 123 buf)); |
124 if (result == ERR_IO_PENDING) | 124 if (result == ERR_IO_PENDING) |
125 return ERR_IO_PENDING; | 125 return ERR_IO_PENDING; |
126 ProcessReadResult(buf, result); | 126 ProcessReadResult(buf, result); |
127 } | 127 } |
128 | 128 |
129 if (read_failed_) { | 129 if (read_failed_) { |
130 // If an error occured during read operation, then pad with zero. | 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. | 131 // Otherwise the server will hang waiting for the rest of the data. |
132 int num_bytes_to_fill = static_cast<int>(std::min( | 132 int num_bytes_to_fill = |
133 static_cast<uint64>(buf->BytesRemaining()), | 133 static_cast<int>(std::min(static_cast<uint64_t>(buf->BytesRemaining()), |
134 size() - position() - buf->BytesConsumed())); | 134 size() - position() - buf->BytesConsumed())); |
135 DCHECK_GE(num_bytes_to_fill, 0); | 135 DCHECK_GE(num_bytes_to_fill, 0); |
136 memset(buf->data(), 0, num_bytes_to_fill); | 136 memset(buf->data(), 0, num_bytes_to_fill); |
137 buf->DidConsume(num_bytes_to_fill); | 137 buf->DidConsume(num_bytes_to_fill); |
138 } | 138 } |
139 | 139 |
140 return buf->BytesConsumed(); | 140 return buf->BytesConsumed(); |
141 } | 141 } |
142 | 142 |
143 void ElementsUploadDataStream::OnReadElementCompleted( | 143 void ElementsUploadDataStream::OnReadElementCompleted( |
144 const scoped_refptr<DrainableIOBuffer>& buf, | 144 const scoped_refptr<DrainableIOBuffer>& buf, |
(...skipping 12 matching lines...) Expand all Loading... |
157 DCHECK(!read_failed_); | 157 DCHECK(!read_failed_); |
158 | 158 |
159 if (result >= 0) { | 159 if (result >= 0) { |
160 buf->DidConsume(result); | 160 buf->DidConsume(result); |
161 } else { | 161 } else { |
162 read_failed_ = true; | 162 read_failed_ = true; |
163 } | 163 } |
164 } | 164 } |
165 | 165 |
166 } // namespace net | 166 } // namespace net |
OLD | NEW |