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); | |
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( | |
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 // Logs when an initialization has been completed. | |
49 void LogInitEnd(const BoundNetLog& net_log, | |
mmenke
2016/08/16 15:40:06
I don't think these methods really get us anything
maksims (do not use this acc)
2016/08/16 18:45:37
Done.
| |
50 int result, | |
51 int total_size_, | |
52 bool is_chunked) { | |
53 net_log.EndEvent( | |
54 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | |
55 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked)); | |
56 } | |
57 | |
58 // Logs when read has been started. | |
59 void LogRead(const BoundNetLog& net_log, int current_position) { | |
60 net_log.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | |
61 base::Bind(&NetLogReadInfoCallback, current_position)); | |
62 } | |
63 | |
64 // Logs when read has been completed (before its callback is run). | |
65 void LogReadEnd(const BoundNetLog& net_log, int result) { | |
66 net_log.EndEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | |
67 base::Bind(&NetLogReadEndInfoCallback, result)); | |
68 } | |
69 | |
70 // Logs when a read request has been cancelled. | |
71 void LogCancelRead(const BoundNetLog& net_log) { | |
72 net_log.AddEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | |
73 NetLog::TYPE_CANCELLED); | |
mmenke
2016/08/16 15:40:06
TYPE_CANCELLED isn't a network error code, it's a
maksims (do not use this acc)
2016/08/16 18:45:37
Done.
| |
74 net_log.EndEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ); | |
75 } | |
76 | |
77 } // namespace | |
78 | |
14 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) | 79 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) |
15 : total_size_(0), | 80 : total_size_(0), |
16 current_position_(0), | 81 current_position_(0), |
17 identifier_(identifier), | 82 identifier_(identifier), |
18 is_chunked_(is_chunked), | 83 is_chunked_(is_chunked), |
19 initialized_successfully_(false), | 84 initialized_successfully_(false), |
20 is_eof_(false) { | 85 is_eof_(false) { |
21 } | 86 } |
22 | 87 |
23 UploadDataStream::~UploadDataStream() { | 88 UploadDataStream::~UploadDataStream() { |
24 } | 89 } |
25 | 90 |
26 int UploadDataStream::Init(const CompletionCallback& callback) { | 91 int UploadDataStream::Init(const CompletionCallback& callback, |
92 const BoundNetLog& net_log) { | |
27 Reset(); | 93 Reset(); |
28 DCHECK(!initialized_successfully_); | 94 DCHECK(!initialized_successfully_); |
29 DCHECK(callback_.is_null()); | 95 DCHECK(callback_.is_null()); |
30 DCHECK(!callback.is_null() || IsInMemory()); | 96 DCHECK(!callback.is_null() || IsInMemory()); |
31 int result = InitInternal(); | 97 net_log_ = net_log; |
98 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ); | |
mmenke
2016/08/16 15:40:06
TYPE_UPLOAD_DATA_STREAM_INIT
maksims (do not use this acc)
2016/08/16 18:45:37
oops.
| |
99 | |
100 int result = InitInternal(net_log_); | |
32 if (result == ERR_IO_PENDING) { | 101 if (result == ERR_IO_PENDING) { |
33 DCHECK(!IsInMemory()); | 102 DCHECK(!IsInMemory()); |
34 callback_ = callback; | 103 callback_ = callback; |
35 } else { | 104 } else { |
36 OnInitCompleted(result); | 105 OnInitCompleted(result); |
37 } | 106 } |
107 | |
38 return result; | 108 return result; |
39 } | 109 } |
40 | 110 |
41 int UploadDataStream::Read(IOBuffer* buf, | 111 int UploadDataStream::Read(IOBuffer* buf, |
42 int buf_len, | 112 int buf_len, |
43 const CompletionCallback& callback) { | 113 const CompletionCallback& callback) { |
44 DCHECK(!callback.is_null() || IsInMemory()); | 114 DCHECK(!callback.is_null() || IsInMemory()); |
45 DCHECK(initialized_successfully_); | 115 DCHECK(initialized_successfully_); |
46 DCHECK_GT(buf_len, 0); | 116 DCHECK_GT(buf_len, 0); |
47 if (is_eof_) | 117 |
48 return 0; | 118 LogRead(net_log_, current_position_); |
49 int result = ReadInternal(buf, buf_len); | 119 |
120 int result = 0; | |
121 if (!is_eof_) | |
122 result = ReadInternal(buf, buf_len); | |
123 | |
50 if (result == ERR_IO_PENDING) { | 124 if (result == ERR_IO_PENDING) { |
51 DCHECK(!IsInMemory()); | 125 DCHECK(!IsInMemory()); |
52 callback_ = callback; | 126 callback_ = callback; |
53 } else { | 127 } else { |
54 OnReadCompleted(result); | 128 OnReadCompleted(result); |
55 } | 129 } |
130 | |
56 return result; | 131 return result; |
57 } | 132 } |
58 | 133 |
59 bool UploadDataStream::IsEOF() const { | 134 bool UploadDataStream::IsEOF() const { |
60 DCHECK(initialized_successfully_); | 135 DCHECK(initialized_successfully_); |
61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | 136 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); |
62 return is_eof_; | 137 return is_eof_; |
63 } | 138 } |
64 | 139 |
65 void UploadDataStream::Reset() { | 140 void UploadDataStream::Reset() { |
141 if (!callback_.is_null()) | |
142 LogCancelRead(net_log_); | |
mmenke
2016/08/16 15:40:06
We may actually be completing initialization. Can
maksims (do not use this acc)
2016/08/16 18:45:37
I though a bit different way. If callback is set,
| |
143 | |
66 current_position_ = 0; | 144 current_position_ = 0; |
67 initialized_successfully_ = false; | 145 initialized_successfully_ = false; |
68 is_eof_ = false; | 146 is_eof_ = false; |
69 total_size_ = 0; | 147 total_size_ = 0; |
70 callback_.Reset(); | 148 callback_.Reset(); |
71 ResetInternal(); | 149 ResetInternal(); |
72 } | 150 } |
73 | 151 |
74 void UploadDataStream::SetSize(uint64_t size) { | 152 void UploadDataStream::SetSize(uint64_t size) { |
75 DCHECK(!initialized_successfully_); | 153 DCHECK(!initialized_successfully_); |
(...skipping 21 matching lines...) Expand all Loading... | |
97 DCHECK_NE(ERR_IO_PENDING, result); | 175 DCHECK_NE(ERR_IO_PENDING, result); |
98 DCHECK(!initialized_successfully_); | 176 DCHECK(!initialized_successfully_); |
99 DCHECK_EQ(0u, current_position_); | 177 DCHECK_EQ(0u, current_position_); |
100 DCHECK(!is_eof_); | 178 DCHECK(!is_eof_); |
101 | 179 |
102 if (result == OK) { | 180 if (result == OK) { |
103 initialized_successfully_ = true; | 181 initialized_successfully_ = true; |
104 if (!is_chunked_ && total_size_ == 0) | 182 if (!is_chunked_ && total_size_ == 0) |
105 is_eof_ = true; | 183 is_eof_ = true; |
106 } | 184 } |
185 | |
186 LogInitEnd(net_log_, result, total_size_, is_chunked_); | |
187 | |
107 if (!callback_.is_null()) | 188 if (!callback_.is_null()) |
108 base::ResetAndReturn(&callback_).Run(result); | 189 base::ResetAndReturn(&callback_).Run(result); |
109 } | 190 } |
110 | 191 |
111 void UploadDataStream::OnReadCompleted(int result) { | 192 void UploadDataStream::OnReadCompleted(int result) { |
112 DCHECK(initialized_successfully_); | 193 DCHECK(initialized_successfully_); |
113 DCHECK(result != 0 || is_eof_); | 194 DCHECK(result != 0 || is_eof_); |
114 DCHECK_NE(ERR_IO_PENDING, result); | 195 DCHECK_NE(ERR_IO_PENDING, result); |
115 | 196 |
116 if (result > 0) { | 197 if (result > 0) { |
117 current_position_ += result; | 198 current_position_ += result; |
118 if (!is_chunked_) { | 199 if (!is_chunked_) { |
119 DCHECK_LE(current_position_, total_size_); | 200 DCHECK_LE(current_position_, total_size_); |
120 if (current_position_ == total_size_) | 201 if (current_position_ == total_size_) |
121 is_eof_ = true; | 202 is_eof_ = true; |
122 } | 203 } |
123 } | 204 } |
124 | 205 |
206 LogReadEnd(net_log_, result); | |
207 | |
125 if (!callback_.is_null()) | 208 if (!callback_.is_null()) |
126 base::ResetAndReturn(&callback_).Run(result); | 209 base::ResetAndReturn(&callback_).Run(result); |
127 } | 210 } |
128 | 211 |
129 } // namespace net | 212 } // namespace net |
OLD | NEW |