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, ERR_ABORTED); |
112 } else { | 113 } else { |
113 // Otherwise, a read is being aborted. | 114 // Otherwise, a read is being aborted. |
114 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | 115 net_log_.EndEventWithNetErrorCode( |
115 ERR_ABORTED); | 116 NetLogEventType::UPLOAD_DATA_STREAM_READ, ERR_ABORTED); |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 current_position_ = 0; | 120 current_position_ = 0; |
120 initialized_successfully_ = false; | 121 initialized_successfully_ = false; |
121 is_eof_ = false; | 122 is_eof_ = false; |
122 total_size_ = 0; | 123 total_size_ = 0; |
123 callback_.Reset(); | 124 callback_.Reset(); |
124 ResetInternal(); | 125 ResetInternal(); |
125 } | 126 } |
(...skipping 26 matching lines...) Expand all Loading... |
152 DCHECK_EQ(0u, current_position_); | 153 DCHECK_EQ(0u, current_position_); |
153 DCHECK(!is_eof_); | 154 DCHECK(!is_eof_); |
154 | 155 |
155 if (result == OK) { | 156 if (result == OK) { |
156 initialized_successfully_ = true; | 157 initialized_successfully_ = true; |
157 if (!is_chunked_ && total_size_ == 0) | 158 if (!is_chunked_ && total_size_ == 0) |
158 is_eof_ = true; | 159 is_eof_ = true; |
159 } | 160 } |
160 | 161 |
161 net_log_.EndEvent( | 162 net_log_.EndEvent( |
162 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, | 163 NetLogEventType::UPLOAD_DATA_STREAM_INIT, |
163 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); | 164 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); |
164 | 165 |
165 if (!callback_.is_null()) | 166 if (!callback_.is_null()) |
166 base::ResetAndReturn(&callback_).Run(result); | 167 base::ResetAndReturn(&callback_).Run(result); |
167 } | 168 } |
168 | 169 |
169 void UploadDataStream::OnReadCompleted(int result) { | 170 void UploadDataStream::OnReadCompleted(int result) { |
170 DCHECK(initialized_successfully_); | 171 DCHECK(initialized_successfully_); |
171 DCHECK(result != 0 || is_eof_); | 172 DCHECK(result != 0 || is_eof_); |
172 DCHECK_NE(ERR_IO_PENDING, result); | 173 DCHECK_NE(ERR_IO_PENDING, result); |
173 | 174 |
174 if (result > 0) { | 175 if (result > 0) { |
175 current_position_ += result; | 176 current_position_ += result; |
176 if (!is_chunked_) { | 177 if (!is_chunked_) { |
177 DCHECK_LE(current_position_, total_size_); | 178 DCHECK_LE(current_position_, total_size_); |
178 if (current_position_ == total_size_) | 179 if (current_position_ == total_size_) |
179 is_eof_ = true; | 180 is_eof_ = true; |
180 } | 181 } |
181 } | 182 } |
182 | 183 |
183 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, | 184 net_log_.EndEventWithNetErrorCode(NetLogEventType::UPLOAD_DATA_STREAM_READ, |
184 result); | 185 result); |
185 | 186 |
186 if (!callback_.is_null()) | 187 if (!callback_.is_null()) |
187 base::ResetAndReturn(&callback_).Run(result); | 188 base::ResetAndReturn(&callback_).Run(result); |
188 } | 189 } |
189 | 190 |
190 } // namespace net | 191 } // namespace net |
OLD | NEW |