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> NetLogInitInfoCallback( | |
| 18 int result, | |
| 19 bool is_eof, | |
| 20 bool is_chunked, | |
| 21 NetLogCaptureMode /* capture_mode */) { | |
| 22 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 23 | |
| 24 dict->SetInteger("result:", result); | |
| 25 dict->SetBoolean("is_eof:", is_eof); | |
| 26 dict->SetBoolean("is_chunked:", is_chunked); | |
|
mmenke
2016/08/10 20:44:49
Colons aren't needed on any of these. If you want
maksims (do not use this acc)
2016/08/16 12:00:19
I have already checked that before but did not pay
| |
| 27 return std::move(dict); | |
| 28 } | |
| 29 | |
| 30 std::unique_ptr<base::Value> NetLogStartreadingInfoCallback( | |
| 31 int result, | |
| 32 int current_position, | |
| 33 int total_size, | |
| 34 bool is_eof, | |
| 35 bool is_chunked, | |
| 36 NetLogCaptureMode /* capture_mode */) { | |
| 37 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 38 | |
| 39 dict->SetInteger("result:", result); | |
| 40 dict->SetInteger("current_position:", current_position); | |
| 41 dict->SetInteger("total_size:", total_size); | |
| 42 dict->SetBoolean("is_eof:", is_eof); | |
| 43 dict->SetBoolean("is_chunked:", is_chunked); | |
|
mmenke
2016/08/10 20:44:49
Remove colons.
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 44 return std::move(dict); | |
| 45 } | |
| 46 | |
| 47 // Logs when a request has just been started. | |
| 48 void LogInit(const BoundNetLog& net_log, | |
| 49 int error, | |
| 50 bool is_eof, | |
| 51 bool is_chunked) { | |
| 52 net_log.BeginEvent( | |
| 53 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | |
| 54 base::Bind(&NetLogInitInfoCallback, error, is_eof, is_chunked)); | |
| 55 } | |
| 56 | |
| 57 // Logs when a request has just completed (before its callback is run). | |
| 58 void LogRead(const BoundNetLog& net_log, | |
| 59 NetLog::EventType event_type, | |
| 60 int result, | |
| 61 int current_position, | |
| 62 int total_size, | |
| 63 bool is_eof, | |
| 64 bool is_chunked) { | |
| 65 if (event_type == NetLog::TYPE_UPLOAD_DATA_STREAM_START_READING) { | |
| 66 net_log.AddEvent(event_type, base::Bind(&NetLogStartreadingInfoCallback, | |
| 67 result, current_position, | |
| 68 total_size, is_eof, is_chunked)); | |
| 69 } else { | |
| 70 net_log.EndEvent(event_type, base::Bind(&NetLogStartreadingInfoCallback, | |
|
mmenke
2016/08/10 20:44:49
The way End event works is there should be a match
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 71 result, current_position, | |
| 72 total_size, is_eof, is_chunked)); | |
| 73 } | |
| 74 } | |
| 75 } | |
|
mmenke
2016/08/10 20:44:50
nit: } // namespace
(Also should have a blank l
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 76 | |
| 14 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) | 77 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) |
| 15 : total_size_(0), | 78 : total_size_(0), |
| 16 current_position_(0), | 79 current_position_(0), |
| 17 identifier_(identifier), | 80 identifier_(identifier), |
| 18 is_chunked_(is_chunked), | 81 is_chunked_(is_chunked), |
| 19 initialized_successfully_(false), | 82 initialized_successfully_(false), |
| 20 is_eof_(false) { | 83 is_eof_(false) { |
| 21 } | 84 } |
| 22 | 85 |
| 23 UploadDataStream::~UploadDataStream() { | 86 UploadDataStream::~UploadDataStream() { |
| 24 } | 87 } |
| 25 | 88 |
| 26 int UploadDataStream::Init(const CompletionCallback& callback) { | 89 int UploadDataStream::Init(const CompletionCallback& callback, |
| 90 const BoundNetLog& net_log) { | |
| 27 Reset(); | 91 Reset(); |
| 28 DCHECK(!initialized_successfully_); | 92 DCHECK(!initialized_successfully_); |
| 29 DCHECK(callback_.is_null()); | 93 DCHECK(callback_.is_null()); |
| 30 DCHECK(!callback.is_null() || IsInMemory()); | 94 DCHECK(!callback.is_null() || IsInMemory()); |
| 31 int result = InitInternal(); | 95 net_log_ = net_log; |
|
mmenke
2016/08/10 20:44:50
Seems like we should have a begin event here, with
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 96 | |
| 97 int result = InitInternal(net_log_); | |
| 32 if (result == ERR_IO_PENDING) { | 98 if (result == ERR_IO_PENDING) { |
| 33 DCHECK(!IsInMemory()); | 99 DCHECK(!IsInMemory()); |
| 34 callback_ = callback; | 100 callback_ = callback; |
| 35 } else { | 101 } else { |
| 36 OnInitCompleted(result); | 102 OnInitCompleted(result); |
| 37 } | 103 } |
| 104 | |
| 38 return result; | 105 return result; |
| 39 } | 106 } |
| 40 | 107 |
| 41 int UploadDataStream::Read(IOBuffer* buf, | 108 int UploadDataStream::Read(IOBuffer* buf, |
| 42 int buf_len, | 109 int buf_len, |
| 43 const CompletionCallback& callback) { | 110 const CompletionCallback& callback) { |
| 44 DCHECK(!callback.is_null() || IsInMemory()); | 111 DCHECK(!callback.is_null() || IsInMemory()); |
| 45 DCHECK(initialized_successfully_); | 112 DCHECK(initialized_successfully_); |
| 46 DCHECK_GT(buf_len, 0); | 113 DCHECK_GT(buf_len, 0); |
| 114 | |
| 115 LogRead(net_log_, NetLog::TYPE_UPLOAD_DATA_STREAM_START_READING, 0, | |
| 116 current_position_, total_size_, is_eof_, is_chunked_); | |
|
mmenke
2016/08/10 20:44:49
total_size_ is constant, so should just log that w
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 117 | |
| 47 if (is_eof_) | 118 if (is_eof_) |
| 48 return 0; | 119 return 0; |
|
mmenke
2016/08/10 20:44:49
This case is missing an end event. To make sure w
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 49 int result = ReadInternal(buf, buf_len); | 120 int result = ReadInternal(buf, buf_len); |
| 50 if (result == ERR_IO_PENDING) { | 121 if (result == ERR_IO_PENDING) { |
| 51 DCHECK(!IsInMemory()); | 122 DCHECK(!IsInMemory()); |
| 52 callback_ = callback; | 123 callback_ = callback; |
| 53 } else { | 124 } else { |
| 54 OnReadCompleted(result); | 125 OnReadCompleted(result); |
| 55 } | 126 } |
| 127 | |
| 56 return result; | 128 return result; |
| 57 } | 129 } |
| 58 | 130 |
| 59 bool UploadDataStream::IsEOF() const { | 131 bool UploadDataStream::IsEOF() const { |
| 60 DCHECK(initialized_successfully_); | 132 DCHECK(initialized_successfully_); |
| 61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | 133 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); |
| 62 return is_eof_; | 134 return is_eof_; |
| 63 } | 135 } |
| 64 | 136 |
| 65 void UploadDataStream::Reset() { | 137 void UploadDataStream::Reset() { |
|
mmenke
2016/08/10 20:44:49
If callback isn't NULL, we should log a read end e
maksims (do not use this acc)
2016/08/16 12:00:19
Reset() is called on Init(). It will add more conf
| |
| 66 current_position_ = 0; | 138 current_position_ = 0; |
| 67 initialized_successfully_ = false; | 139 initialized_successfully_ = false; |
| 68 is_eof_ = false; | 140 is_eof_ = false; |
| 69 total_size_ = 0; | 141 total_size_ = 0; |
| 70 callback_.Reset(); | 142 callback_.Reset(); |
| 71 ResetInternal(); | 143 ResetInternal(); |
| 72 } | 144 } |
| 73 | 145 |
| 74 void UploadDataStream::SetSize(uint64_t size) { | 146 void UploadDataStream::SetSize(uint64_t size) { |
| 75 DCHECK(!initialized_successfully_); | 147 DCHECK(!initialized_successfully_); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 97 DCHECK_NE(ERR_IO_PENDING, result); | 169 DCHECK_NE(ERR_IO_PENDING, result); |
| 98 DCHECK(!initialized_successfully_); | 170 DCHECK(!initialized_successfully_); |
| 99 DCHECK_EQ(0u, current_position_); | 171 DCHECK_EQ(0u, current_position_); |
| 100 DCHECK(!is_eof_); | 172 DCHECK(!is_eof_); |
| 101 | 173 |
| 102 if (result == OK) { | 174 if (result == OK) { |
| 103 initialized_successfully_ = true; | 175 initialized_successfully_ = true; |
| 104 if (!is_chunked_ && total_size_ == 0) | 176 if (!is_chunked_ && total_size_ == 0) |
| 105 is_eof_ = true; | 177 is_eof_ = true; |
| 106 } | 178 } |
| 179 | |
| 180 LogInit(net_log_, result, is_eof_, is_chunked_); | |
|
mmenke
2016/08/10 20:44:49
I don't think we really need is_eof_ here. total_
maksims (do not use this acc)
2016/08/16 12:00:19
Done.
| |
| 181 | |
| 107 if (!callback_.is_null()) | 182 if (!callback_.is_null()) |
| 108 base::ResetAndReturn(&callback_).Run(result); | 183 base::ResetAndReturn(&callback_).Run(result); |
| 109 } | 184 } |
| 110 | 185 |
| 111 void UploadDataStream::OnReadCompleted(int result) { | 186 void UploadDataStream::OnReadCompleted(int result) { |
| 112 DCHECK(initialized_successfully_); | 187 DCHECK(initialized_successfully_); |
| 113 DCHECK(result != 0 || is_eof_); | 188 DCHECK(result != 0 || is_eof_); |
| 114 DCHECK_NE(ERR_IO_PENDING, result); | 189 DCHECK_NE(ERR_IO_PENDING, result); |
| 115 | 190 |
| 116 if (result > 0) { | 191 if (result > 0) { |
| 117 current_position_ += result; | 192 current_position_ += result; |
| 118 if (!is_chunked_) { | 193 if (!is_chunked_) { |
| 119 DCHECK_LE(current_position_, total_size_); | 194 DCHECK_LE(current_position_, total_size_); |
| 120 if (current_position_ == total_size_) | 195 if (current_position_ == total_size_) |
| 121 is_eof_ = true; | 196 is_eof_ = true; |
| 122 } | 197 } |
| 123 } | 198 } |
| 124 | 199 |
| 200 LogRead(net_log_, NetLog::TYPE_UPLOAD_DATA_STREAM_READING_DONE, result, | |
| 201 current_position_, total_size_, is_eof_, is_chunked_); | |
| 125 if (!callback_.is_null()) | 202 if (!callback_.is_null()) |
| 126 base::ResetAndReturn(&callback_).Run(result); | 203 base::ResetAndReturn(&callback_).Run(result); |
| 127 } | 204 } |
| 128 | 205 |
| 129 } // namespace net | 206 } // namespace net |
| OLD | NEW |