| 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/upload_data_stream.h" | 5 #include "net/base/upload_data_stream.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/values.h" | 9 #include "base/values.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/log/net_log_event_type.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 std::unique_ptr<base::Value> NetLogInitEndInfoCallback( | 18 std::unique_ptr<base::Value> NetLogInitEndInfoCallback( |
| 18 int result, | 19 int result, |
| 19 int total_size, | 20 int total_size, |
| 20 bool is_chunked, | 21 bool is_chunked, |
| 21 NetLogCaptureMode /* capture_mode */) { | 22 NetLogCaptureMode /* capture_mode */) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 50 UploadDataStream::~UploadDataStream() { | 51 UploadDataStream::~UploadDataStream() { |
| 51 } | 52 } |
| 52 | 53 |
| 53 int UploadDataStream::Init(const CompletionCallback& callback, | 54 int UploadDataStream::Init(const CompletionCallback& callback, |
| 54 const BoundNetLog& net_log) { | 55 const BoundNetLog& net_log) { |
| 55 Reset(); | 56 Reset(); |
| 56 DCHECK(!initialized_successfully_); | 57 DCHECK(!initialized_successfully_); |
| 57 DCHECK(callback_.is_null()); | 58 DCHECK(callback_.is_null()); |
| 58 DCHECK(!callback.is_null() || IsInMemory()); | 59 DCHECK(!callback.is_null() || IsInMemory()); |
| 59 net_log_ = net_log; | 60 net_log_ = net_log; |
| 60 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_INIT); | 61 net_log_.BeginEvent(NetLogEventType::UPLOAD_DATA_STREAM_INIT); |
| 61 | 62 |
| 62 int result = InitInternal(net_log_); | 63 int result = InitInternal(net_log_); |
| 63 if (result == ERR_IO_PENDING) { | 64 if (result == ERR_IO_PENDING) { |
| 64 DCHECK(!IsInMemory()); | 65 DCHECK(!IsInMemory()); |
| 65 callback_ = callback; | 66 callback_ = callback; |
| 66 } else { | 67 } else { |
| 67 OnInitCompleted(result); | 68 OnInitCompleted(result); |
| 68 } | 69 } |
| 69 | 70 |
| 70 return result; | 71 return result; |
| 71 } | 72 } |
| 72 | 73 |
| 73 int UploadDataStream::Read(IOBuffer* buf, | 74 int UploadDataStream::Read(IOBuffer* buf, |
| 74 int buf_len, | 75 int buf_len, |
| 75 const CompletionCallback& callback) { | 76 const CompletionCallback& callback) { |
| 76 DCHECK(!callback.is_null() || IsInMemory()); | 77 DCHECK(!callback.is_null() || IsInMemory()); |
| 77 DCHECK(initialized_successfully_); | 78 DCHECK(initialized_successfully_); |
| 78 DCHECK_GT(buf_len, 0); | 79 DCHECK_GT(buf_len, 0); |
| 79 | 80 |
| 80 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | 81 net_log_.BeginEvent(NetLogEventType::UPLOAD_DATA_STREAM_READ, |
| 81 base::Bind(&NetLogReadInfoCallback, current_position_)); | 82 base::Bind(&NetLogReadInfoCallback, current_position_)); |
| 82 | 83 |
| 83 int result = 0; | 84 int result = 0; |
| 84 if (!is_eof_) | 85 if (!is_eof_) |
| 85 result = ReadInternal(buf, buf_len); | 86 result = ReadInternal(buf, buf_len); |
| 86 | 87 |
| 87 if (result == ERR_IO_PENDING) { | 88 if (result == ERR_IO_PENDING) { |
| 88 DCHECK(!IsInMemory()); | 89 DCHECK(!IsInMemory()); |
| 89 callback_ = callback; | 90 callback_ = callback; |
| 90 } else { | 91 } else { |
| 91 OnReadCompleted(result); | 92 OnReadCompleted(result); |
| 92 } | 93 } |
| 93 | 94 |
| 94 return result; | 95 return result; |
| 95 } | 96 } |
| 96 | 97 |
| 97 bool UploadDataStream::IsEOF() const { | 98 bool UploadDataStream::IsEOF() const { |
| 98 DCHECK(initialized_successfully_); | 99 DCHECK(initialized_successfully_); |
| 99 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | 100 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); |
| 100 return is_eof_; | 101 return is_eof_; |
| 101 } | 102 } |
| 102 | 103 |
| 103 void UploadDataStream::Reset() { | 104 void UploadDataStream::Reset() { |
| 104 // If there's a pending callback, there's a pending init or read call that is | 105 // If there's a pending callback, there's a pending init or read call that is |
| 105 // being canceled. | 106 // being canceled. |
| 106 if (!callback_.is_null()) { | 107 if (!callback_.is_null()) { |
| 107 if (!initialized_successfully_) { | 108 if (!initialized_successfully_) { |
| 108 // If initialization has not yet succeeded, this call is aborting | 109 // If initialization has not yet succeeded, this call is aborting |
| 109 // initialization. | 110 // initialization. |
| 110 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | 111 net_log_.EndEventWithNetErrorCode( |
| 111 ERR_ABORTED); | 112 NetLogEventType::UPLOAD_DATA_STREAM_INIT, |
| 113 ERR_ABORTED); |
| 112 } else { | 114 } else { |
| 113 // Otherwise, a read is being aborted. | 115 // Otherwise, a read is being aborted. |
| 114 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | 116 net_log_.EndEventWithNetErrorCode( |
| 115 ERR_ABORTED); | 117 NetLogEventType::UPLOAD_DATA_STREAM_READ, |
| 118 ERR_ABORTED); |
| 116 } | 119 } |
| 117 } | 120 } |
| 118 | 121 |
| 119 current_position_ = 0; | 122 current_position_ = 0; |
| 120 initialized_successfully_ = false; | 123 initialized_successfully_ = false; |
| 121 is_eof_ = false; | 124 is_eof_ = false; |
| 122 total_size_ = 0; | 125 total_size_ = 0; |
| 123 callback_.Reset(); | 126 callback_.Reset(); |
| 124 ResetInternal(); | 127 ResetInternal(); |
| 125 } | 128 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 152 DCHECK_EQ(0u, current_position_); | 155 DCHECK_EQ(0u, current_position_); |
| 153 DCHECK(!is_eof_); | 156 DCHECK(!is_eof_); |
| 154 | 157 |
| 155 if (result == OK) { | 158 if (result == OK) { |
| 156 initialized_successfully_ = true; | 159 initialized_successfully_ = true; |
| 157 if (!is_chunked_ && total_size_ == 0) | 160 if (!is_chunked_ && total_size_ == 0) |
| 158 is_eof_ = true; | 161 is_eof_ = true; |
| 159 } | 162 } |
| 160 | 163 |
| 161 net_log_.EndEvent( | 164 net_log_.EndEvent( |
| 162 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | 165 NetLogEventType::UPLOAD_DATA_STREAM_INIT, |
| 163 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); | 166 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); |
| 164 | 167 |
| 165 if (!callback_.is_null()) | 168 if (!callback_.is_null()) |
| 166 base::ResetAndReturn(&callback_).Run(result); | 169 base::ResetAndReturn(&callback_).Run(result); |
| 167 } | 170 } |
| 168 | 171 |
| 169 void UploadDataStream::OnReadCompleted(int result) { | 172 void UploadDataStream::OnReadCompleted(int result) { |
| 170 DCHECK(initialized_successfully_); | 173 DCHECK(initialized_successfully_); |
| 171 DCHECK(result != 0 || is_eof_); | 174 DCHECK(result != 0 || is_eof_); |
| 172 DCHECK_NE(ERR_IO_PENDING, result); | 175 DCHECK_NE(ERR_IO_PENDING, result); |
| 173 | 176 |
| 174 if (result > 0) { | 177 if (result > 0) { |
| 175 current_position_ += result; | 178 current_position_ += result; |
| 176 if (!is_chunked_) { | 179 if (!is_chunked_) { |
| 177 DCHECK_LE(current_position_, total_size_); | 180 DCHECK_LE(current_position_, total_size_); |
| 178 if (current_position_ == total_size_) | 181 if (current_position_ == total_size_) |
| 179 is_eof_ = true; | 182 is_eof_ = true; |
| 180 } | 183 } |
| 181 } | 184 } |
| 182 | 185 |
| 183 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | 186 net_log_.EndEventWithNetErrorCode(NetLogEventType::UPLOAD_DATA_STREAM_READ, |
| 184 result); | 187 result); |
| 185 | 188 |
| 186 if (!callback_.is_null()) | 189 if (!callback_.is_null()) |
| 187 base::ResetAndReturn(&callback_).Run(result); | 190 base::ResetAndReturn(&callback_).Run(result); |
| 188 } | 191 } |
| 189 | 192 |
| 190 } // namespace net | 193 } // namespace net |
| OLD | NEW |