Chromium Code Reviews| 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 "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 10 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 15 namespace { | |
| 16 | |
| 17 std::unique_ptr<base::Value> NetLogInitEndInfoCallback( | |
| 18 int result, | |
| 19 int total_size, | |
| 20 bool is_chunked, | |
| 21 NetLogCaptureMode /* capture_mode */) { | |
| 22 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 23 | |
| 24 dict->SetInteger("result", result); | |
|
mmenke
2016/08/16 19:40:09
Per below comment, call this net_error to get it f
maksims (do not use this acc)
2016/08/17 05:11:57
Done.
| |
| 25 dict->SetInteger("total_size", total_size); | |
| 26 dict->SetBoolean("is_chunked", is_chunked); | |
| 27 return std::move(dict); | |
| 28 } | |
| 29 | |
| 30 std::unique_ptr<base::Value> NetLogReadInfoCallback( | |
| 31 int current_position, | |
| 32 NetLogCaptureMode /* capture_mode */) { | |
| 33 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 34 | |
| 35 dict->SetInteger("current_position", current_position); | |
| 36 return std::move(dict); | |
| 37 } | |
| 38 | |
| 39 std::unique_ptr<base::Value> NetLogReadEndInfoCallback( | |
|
mmenke
2016/08/16 18:54:13
Can actually get rid of this, and use "EndEventWit
maksims (do not use this acc)
2016/08/16 19:21:37
but wouldn't it mean that there will be no "result
mmenke
2016/08/16 19:40:09
EndEventWithNetErrorCode takes a network error cod
maksims (do not use this acc)
2016/08/17 05:11:57
Done.
| |
| 40 int result, | |
| 41 NetLogCaptureMode /* capture_mode */) { | |
| 42 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 43 | |
| 44 dict->SetInteger("result", result); | |
| 45 return std::move(dict); | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 14 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) | 50 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) |
| 15 : total_size_(0), | 51 : total_size_(0), |
| 16 current_position_(0), | 52 current_position_(0), |
| 17 identifier_(identifier), | 53 identifier_(identifier), |
| 18 is_chunked_(is_chunked), | 54 is_chunked_(is_chunked), |
| 19 initialized_successfully_(false), | 55 initialized_successfully_(false), |
| 20 is_eof_(false) { | 56 is_eof_(false) { |
| 21 } | 57 } |
| 22 | 58 |
| 23 UploadDataStream::~UploadDataStream() { | 59 UploadDataStream::~UploadDataStream() { |
| 24 } | 60 } |
| 25 | 61 |
| 26 int UploadDataStream::Init(const CompletionCallback& callback) { | 62 int UploadDataStream::Init(const CompletionCallback& callback, |
| 63 const BoundNetLog& net_log) { | |
| 27 Reset(); | 64 Reset(); |
| 28 DCHECK(!initialized_successfully_); | 65 DCHECK(!initialized_successfully_); |
| 29 DCHECK(callback_.is_null()); | 66 DCHECK(callback_.is_null()); |
| 30 DCHECK(!callback.is_null() || IsInMemory()); | 67 DCHECK(!callback.is_null() || IsInMemory()); |
| 31 int result = InitInternal(); | 68 net_log_ = net_log; |
| 69 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_INIT); | |
| 70 | |
| 71 int result = InitInternal(net_log_); | |
| 32 if (result == ERR_IO_PENDING) { | 72 if (result == ERR_IO_PENDING) { |
| 33 DCHECK(!IsInMemory()); | 73 DCHECK(!IsInMemory()); |
| 34 callback_ = callback; | 74 callback_ = callback; |
| 35 } else { | 75 } else { |
| 36 OnInitCompleted(result); | 76 OnInitCompleted(result); |
| 37 } | 77 } |
| 78 | |
| 38 return result; | 79 return result; |
| 39 } | 80 } |
| 40 | 81 |
| 41 int UploadDataStream::Read(IOBuffer* buf, | 82 int UploadDataStream::Read(IOBuffer* buf, |
| 42 int buf_len, | 83 int buf_len, |
| 43 const CompletionCallback& callback) { | 84 const CompletionCallback& callback) { |
| 44 DCHECK(!callback.is_null() || IsInMemory()); | 85 DCHECK(!callback.is_null() || IsInMemory()); |
| 45 DCHECK(initialized_successfully_); | 86 DCHECK(initialized_successfully_); |
| 46 DCHECK_GT(buf_len, 0); | 87 DCHECK_GT(buf_len, 0); |
| 47 if (is_eof_) | 88 |
| 48 return 0; | 89 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, |
| 49 int result = ReadInternal(buf, buf_len); | 90 base::Bind(&NetLogReadInfoCallback, current_position_)); |
| 91 | |
| 92 int result = 0; | |
| 93 if (!is_eof_) | |
| 94 result = ReadInternal(buf, buf_len); | |
| 95 | |
| 50 if (result == ERR_IO_PENDING) { | 96 if (result == ERR_IO_PENDING) { |
| 51 DCHECK(!IsInMemory()); | 97 DCHECK(!IsInMemory()); |
| 52 callback_ = callback; | 98 callback_ = callback; |
| 53 } else { | 99 } else { |
| 54 OnReadCompleted(result); | 100 OnReadCompleted(result); |
| 55 } | 101 } |
| 102 | |
| 56 return result; | 103 return result; |
| 57 } | 104 } |
| 58 | 105 |
| 59 bool UploadDataStream::IsEOF() const { | 106 bool UploadDataStream::IsEOF() const { |
| 60 DCHECK(initialized_successfully_); | 107 DCHECK(initialized_successfully_); |
| 61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | 108 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); |
| 62 return is_eof_; | 109 return is_eof_; |
| 63 } | 110 } |
| 64 | 111 |
| 65 void UploadDataStream::Reset() { | 112 void UploadDataStream::Reset() { |
| 113 if (initialized_successfully_) { | |
| 114 net_log.EndEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | |
| 115 base::Bind(&NetLogReadEndInfoCallback, ERR_ABORTED)); | |
|
mmenke
2016/08/16 18:54:13
Sorry, should have been clearer. My suggestion wa
maksims (do not use this acc)
2016/08/17 05:11:57
Oh, silly me. I've left your comments to make this
| |
| 116 } | |
| 117 | |
| 66 current_position_ = 0; | 118 current_position_ = 0; |
| 67 initialized_successfully_ = false; | 119 initialized_successfully_ = false; |
| 68 is_eof_ = false; | 120 is_eof_ = false; |
| 69 total_size_ = 0; | 121 total_size_ = 0; |
| 70 callback_.Reset(); | 122 callback_.Reset(); |
| 71 ResetInternal(); | 123 ResetInternal(); |
| 72 } | 124 } |
| 73 | 125 |
| 74 void UploadDataStream::SetSize(uint64_t size) { | 126 void UploadDataStream::SetSize(uint64_t size) { |
| 75 DCHECK(!initialized_successfully_); | 127 DCHECK(!initialized_successfully_); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 97 DCHECK_NE(ERR_IO_PENDING, result); | 149 DCHECK_NE(ERR_IO_PENDING, result); |
| 98 DCHECK(!initialized_successfully_); | 150 DCHECK(!initialized_successfully_); |
| 99 DCHECK_EQ(0u, current_position_); | 151 DCHECK_EQ(0u, current_position_); |
| 100 DCHECK(!is_eof_); | 152 DCHECK(!is_eof_); |
| 101 | 153 |
| 102 if (result == OK) { | 154 if (result == OK) { |
| 103 initialized_successfully_ = true; | 155 initialized_successfully_ = true; |
| 104 if (!is_chunked_ && total_size_ == 0) | 156 if (!is_chunked_ && total_size_ == 0) |
| 105 is_eof_ = true; | 157 is_eof_ = true; |
| 106 } | 158 } |
| 159 | |
| 160 net_log_.EndEvent( | |
| 161 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | |
| 162 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); | |
| 163 | |
| 107 if (!callback_.is_null()) | 164 if (!callback_.is_null()) |
| 108 base::ResetAndReturn(&callback_).Run(result); | 165 base::ResetAndReturn(&callback_).Run(result); |
| 109 } | 166 } |
| 110 | 167 |
| 111 void UploadDataStream::OnReadCompleted(int result) { | 168 void UploadDataStream::OnReadCompleted(int result) { |
| 112 DCHECK(initialized_successfully_); | 169 DCHECK(initialized_successfully_); |
| 113 DCHECK(result != 0 || is_eof_); | 170 DCHECK(result != 0 || is_eof_); |
| 114 DCHECK_NE(ERR_IO_PENDING, result); | 171 DCHECK_NE(ERR_IO_PENDING, result); |
| 115 | 172 |
| 116 if (result > 0) { | 173 if (result > 0) { |
| 117 current_position_ += result; | 174 current_position_ += result; |
| 118 if (!is_chunked_) { | 175 if (!is_chunked_) { |
| 119 DCHECK_LE(current_position_, total_size_); | 176 DCHECK_LE(current_position_, total_size_); |
| 120 if (current_position_ == total_size_) | 177 if (current_position_ == total_size_) |
| 121 is_eof_ = true; | 178 is_eof_ = true; |
| 122 } | 179 } |
| 123 } | 180 } |
| 124 | 181 |
| 182 net_log_.EndEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | |
| 183 base::Bind(&NetLogReadEndInfoCallback, result)); | |
| 184 | |
| 125 if (!callback_.is_null()) | 185 if (!callback_.is_null()) |
| 126 base::ResetAndReturn(&callback_).Run(result); | 186 base::ResetAndReturn(&callback_).Run(result); |
| 127 } | 187 } |
| 128 | 188 |
| 129 } // namespace net | 189 } // namespace net |
| OLD | NEW |