[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / base / upload_data_stream.cc
blobc2d88f9742ac5056650b1b72b80d5c9a6d820476
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"
12 namespace net {
14 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier)
15 : total_size_(0),
16 current_position_(0),
17 identifier_(identifier),
18 is_chunked_(is_chunked),
19 initialized_successfully_(false),
20 is_eof_(false) {
23 UploadDataStream::~UploadDataStream() {
26 int UploadDataStream::Init(const CompletionCallback& callback) {
27 Reset();
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());
34 callback_ = callback;
35 } else {
36 OnInitCompleted(result);
38 return result;
41 int UploadDataStream::Read(IOBuffer* buf,
42 int buf_len,
43 const CompletionCallback& callback) {
44 DCHECK(!callback.is_null() || IsInMemory());
45 DCHECK(initialized_successfully_);
46 DCHECK_GT(buf_len, 0);
47 if (is_eof_)
48 return 0;
49 int result = ReadInternal(buf, buf_len);
50 if (result == ERR_IO_PENDING) {
51 DCHECK(!IsInMemory());
52 callback_ = callback;
53 } else {
54 OnReadCompleted(result);
56 return result;
59 bool UploadDataStream::IsEOF() const {
60 DCHECK(initialized_successfully_);
61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_));
62 return is_eof_;
65 void UploadDataStream::Reset() {
66 current_position_ = 0;
67 initialized_successfully_ = false;
68 is_eof_ = false;
69 total_size_ = 0;
70 callback_.Reset();
71 ResetInternal();
74 void UploadDataStream::SetSize(uint64_t size) {
75 DCHECK(!initialized_successfully_);
76 DCHECK(!is_chunked_);
77 total_size_ = size;
80 void UploadDataStream::SetIsFinalChunk() {
81 DCHECK(initialized_successfully_);
82 DCHECK(is_chunked_);
83 DCHECK(!is_eof_);
84 is_eof_ = true;
87 bool UploadDataStream::IsInMemory() const {
88 return false;
91 const ScopedVector<UploadElementReader>*
92 UploadDataStream::GetElementReaders() const {
93 return NULL;
96 void UploadDataStream::OnInitCompleted(int result) {
97 DCHECK_NE(ERR_IO_PENDING, result);
98 DCHECK(!initialized_successfully_);
99 DCHECK_EQ(0u, current_position_);
100 DCHECK(!is_eof_);
102 if (result == OK) {
103 initialized_successfully_ = true;
104 if (!is_chunked_ && total_size_ == 0)
105 is_eof_ = true;
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;
116 if (!is_chunked_) {
117 DCHECK_LE(current_position_, total_size_);
118 if (current_position_ == total_size_)
119 is_eof_ = true;
122 DCHECK(result > 0 || is_eof_);
124 if (!callback_.is_null())
125 base::ResetAndReturn(&callback_).Run(result);
128 } // namespace net