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/upload_data_stream.h"
7 #include "base/callback_helpers.h"
8 #include "base/logging.h"
9 #include "net/base/io_buffer.h"
10 #include "net/base/net_errors.h"
14 UploadDataStream::UploadDataStream(bool is_chunked
, int64_t identifier
)
17 identifier_(identifier
),
18 is_chunked_(is_chunked
),
19 initialized_successfully_(false),
23 UploadDataStream::~UploadDataStream() {
26 int UploadDataStream::Init(const CompletionCallback
& callback
) {
28 DCHECK(!initialized_successfully_
);
29 DCHECK(callback_
.is_null());
30 DCHECK(!callback
.is_null() || IsInMemory());
31 int result
= InitInternal();
32 if (result
== ERR_IO_PENDING
) {
33 DCHECK(!IsInMemory());
36 OnInitCompleted(result
);
41 int UploadDataStream::Read(IOBuffer
* buf
,
43 const CompletionCallback
& callback
) {
44 DCHECK(!callback
.is_null() || IsInMemory());
45 DCHECK(initialized_successfully_
);
46 DCHECK_GT(buf_len
, 0);
49 int result
= ReadInternal(buf
, buf_len
);
50 if (result
== ERR_IO_PENDING
) {
51 DCHECK(!IsInMemory());
54 OnReadCompleted(result
);
59 bool UploadDataStream::IsEOF() const {
60 DCHECK(initialized_successfully_
);
61 DCHECK(is_chunked_
|| is_eof_
== (current_position_
== total_size_
));
65 void UploadDataStream::Reset() {
66 current_position_
= 0;
67 initialized_successfully_
= false;
74 void UploadDataStream::SetSize(uint64_t size
) {
75 DCHECK(!initialized_successfully_
);
80 void UploadDataStream::SetIsFinalChunk() {
81 DCHECK(initialized_successfully_
);
87 bool UploadDataStream::IsInMemory() const {
91 const ScopedVector
<UploadElementReader
>*
92 UploadDataStream::GetElementReaders() const {
96 void UploadDataStream::OnInitCompleted(int result
) {
97 DCHECK_NE(ERR_IO_PENDING
, result
);
98 DCHECK(!initialized_successfully_
);
99 DCHECK_EQ(0u, current_position_
);
103 initialized_successfully_
= true;
104 if (!is_chunked_
&& total_size_
== 0)
107 if (!callback_
.is_null())
108 base::ResetAndReturn(&callback_
).Run(result
);
111 void UploadDataStream::OnReadCompleted(int result
) {
112 DCHECK_GE(result
, 0);
113 DCHECK(initialized_successfully_
);
115 current_position_
+= result
;
117 DCHECK_LE(current_position_
, total_size_
);
118 if (current_position_
== total_size_
)
122 DCHECK(result
> 0 || is_eof_
);
124 if (!callback_
.is_null())
125 base::ResetAndReturn(&callback_
).Run(result
);