| 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/upload_data_stream.h" | |
| 6 | |
| 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" | |
| 11 | |
| 12 namespace net { | |
| 13 | |
| 14 UploadDataStream::UploadDataStream(bool is_chunked, int64 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) { | |
| 21 } | |
| 22 | |
| 23 UploadDataStream::~UploadDataStream() { | |
| 24 } | |
| 25 | |
| 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); | |
| 37 } | |
| 38 return result; | |
| 39 } | |
| 40 | |
| 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); | |
| 55 } | |
| 56 return result; | |
| 57 } | |
| 58 | |
| 59 bool UploadDataStream::IsEOF() const { | |
| 60 DCHECK(initialized_successfully_); | |
| 61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | |
| 62 return is_eof_; | |
| 63 } | |
| 64 | |
| 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(); | |
| 72 } | |
| 73 | |
| 74 void UploadDataStream::SetSize(uint64 size) { | |
| 75 DCHECK(!initialized_successfully_); | |
| 76 DCHECK(!is_chunked_); | |
| 77 total_size_ = size; | |
| 78 } | |
| 79 | |
| 80 void UploadDataStream::SetIsFinalChunk() { | |
| 81 DCHECK(initialized_successfully_); | |
| 82 DCHECK(is_chunked_); | |
| 83 DCHECK(!is_eof_); | |
| 84 is_eof_ = true; | |
| 85 } | |
| 86 | |
| 87 bool UploadDataStream::IsInMemory() const { | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 const ScopedVector<UploadElementReader>* | |
| 92 UploadDataStream::GetElementReaders() const { | |
| 93 return NULL; | |
| 94 } | |
| 95 | |
| 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_); | |
| 101 | |
| 102 if (result == OK) { | |
| 103 initialized_successfully_ = true; | |
| 104 if (!is_chunked_ && total_size_ == 0) | |
| 105 is_eof_ = true; | |
| 106 } | |
| 107 if (!callback_.is_null()) | |
| 108 base::ResetAndReturn(&callback_).Run(result); | |
| 109 } | |
| 110 | |
| 111 void UploadDataStream::OnReadCompleted(int result) { | |
| 112 DCHECK_GE(result, 0); | |
| 113 DCHECK(initialized_successfully_); | |
| 114 | |
| 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; | |
| 120 } | |
| 121 | |
| 122 DCHECK(result > 0 || is_eof_); | |
| 123 | |
| 124 if (!callback_.is_null()) | |
| 125 base::ResetAndReturn(&callback_).Run(result); | |
| 126 } | |
| 127 | |
| 128 } // namespace net | |
| OLD | NEW |