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("net_error", 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 } // namespace |
| 40 |
14 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) | 41 UploadDataStream::UploadDataStream(bool is_chunked, int64_t identifier) |
15 : total_size_(0), | 42 : total_size_(0), |
16 current_position_(0), | 43 current_position_(0), |
17 identifier_(identifier), | 44 identifier_(identifier), |
18 is_chunked_(is_chunked), | 45 is_chunked_(is_chunked), |
19 initialized_successfully_(false), | 46 initialized_successfully_(false), |
20 is_eof_(false) { | 47 is_eof_(false) { |
21 } | 48 } |
22 | 49 |
23 UploadDataStream::~UploadDataStream() { | 50 UploadDataStream::~UploadDataStream() { |
24 } | 51 } |
25 | 52 |
26 int UploadDataStream::Init(const CompletionCallback& callback) { | 53 int UploadDataStream::Init(const CompletionCallback& callback, |
| 54 const BoundNetLog& net_log) { |
27 Reset(); | 55 Reset(); |
28 DCHECK(!initialized_successfully_); | 56 DCHECK(!initialized_successfully_); |
29 DCHECK(callback_.is_null()); | 57 DCHECK(callback_.is_null()); |
30 DCHECK(!callback.is_null() || IsInMemory()); | 58 DCHECK(!callback.is_null() || IsInMemory()); |
31 int result = InitInternal(); | 59 net_log_ = net_log; |
| 60 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_INIT); |
| 61 |
| 62 int result = InitInternal(net_log_); |
32 if (result == ERR_IO_PENDING) { | 63 if (result == ERR_IO_PENDING) { |
33 DCHECK(!IsInMemory()); | 64 DCHECK(!IsInMemory()); |
34 callback_ = callback; | 65 callback_ = callback; |
35 } else { | 66 } else { |
36 OnInitCompleted(result); | 67 OnInitCompleted(result); |
37 } | 68 } |
| 69 |
38 return result; | 70 return result; |
39 } | 71 } |
40 | 72 |
41 int UploadDataStream::Read(IOBuffer* buf, | 73 int UploadDataStream::Read(IOBuffer* buf, |
42 int buf_len, | 74 int buf_len, |
43 const CompletionCallback& callback) { | 75 const CompletionCallback& callback) { |
44 DCHECK(!callback.is_null() || IsInMemory()); | 76 DCHECK(!callback.is_null() || IsInMemory()); |
45 DCHECK(initialized_successfully_); | 77 DCHECK(initialized_successfully_); |
46 DCHECK_GT(buf_len, 0); | 78 DCHECK_GT(buf_len, 0); |
47 if (is_eof_) | 79 |
48 return 0; | 80 net_log_.BeginEvent(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, |
49 int result = ReadInternal(buf, buf_len); | 81 base::Bind(&NetLogReadInfoCallback, current_position_)); |
| 82 |
| 83 int result = 0; |
| 84 if (!is_eof_) |
| 85 result = ReadInternal(buf, buf_len); |
| 86 |
50 if (result == ERR_IO_PENDING) { | 87 if (result == ERR_IO_PENDING) { |
51 DCHECK(!IsInMemory()); | 88 DCHECK(!IsInMemory()); |
52 callback_ = callback; | 89 callback_ = callback; |
53 } else { | 90 } else { |
54 OnReadCompleted(result); | 91 OnReadCompleted(result); |
55 } | 92 } |
| 93 |
56 return result; | 94 return result; |
57 } | 95 } |
58 | 96 |
59 bool UploadDataStream::IsEOF() const { | 97 bool UploadDataStream::IsEOF() const { |
60 DCHECK(initialized_successfully_); | 98 DCHECK(initialized_successfully_); |
61 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); | 99 DCHECK(is_chunked_ || is_eof_ == (current_position_ == total_size_)); |
62 return is_eof_; | 100 return is_eof_; |
63 } | 101 } |
64 | 102 |
65 void UploadDataStream::Reset() { | 103 void UploadDataStream::Reset() { |
| 104 // If there's a pending callback, there's a pending init or read call that is |
| 105 // being canceled. |
| 106 if (!callback_.is_null()) { |
| 107 if (!initialized_successfully_) { |
| 108 // If initialization has not yet succeeded, this call is aborting |
| 109 // initialization. |
| 110 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, |
| 111 ERR_ABORTED); |
| 112 } else { |
| 113 // Otherwise, a read is being aborted. |
| 114 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, |
| 115 ERR_ABORTED); |
| 116 } |
| 117 } |
| 118 |
66 current_position_ = 0; | 119 current_position_ = 0; |
67 initialized_successfully_ = false; | 120 initialized_successfully_ = false; |
68 is_eof_ = false; | 121 is_eof_ = false; |
69 total_size_ = 0; | 122 total_size_ = 0; |
70 callback_.Reset(); | 123 callback_.Reset(); |
71 ResetInternal(); | 124 ResetInternal(); |
72 } | 125 } |
73 | 126 |
74 void UploadDataStream::SetSize(uint64_t size) { | 127 void UploadDataStream::SetSize(uint64_t size) { |
75 DCHECK(!initialized_successfully_); | 128 DCHECK(!initialized_successfully_); |
(...skipping 21 matching lines...) Expand all Loading... |
97 DCHECK_NE(ERR_IO_PENDING, result); | 150 DCHECK_NE(ERR_IO_PENDING, result); |
98 DCHECK(!initialized_successfully_); | 151 DCHECK(!initialized_successfully_); |
99 DCHECK_EQ(0u, current_position_); | 152 DCHECK_EQ(0u, current_position_); |
100 DCHECK(!is_eof_); | 153 DCHECK(!is_eof_); |
101 | 154 |
102 if (result == OK) { | 155 if (result == OK) { |
103 initialized_successfully_ = true; | 156 initialized_successfully_ = true; |
104 if (!is_chunked_ && total_size_ == 0) | 157 if (!is_chunked_ && total_size_ == 0) |
105 is_eof_ = true; | 158 is_eof_ = true; |
106 } | 159 } |
| 160 |
| 161 net_log_.EndEvent( |
| 162 NetLog::TYPE_UPLOAD_DATA_STREAM_INIT, |
| 163 base::Bind(&NetLogInitEndInfoCallback, result, total_size_, is_chunked_)); |
| 164 |
107 if (!callback_.is_null()) | 165 if (!callback_.is_null()) |
108 base::ResetAndReturn(&callback_).Run(result); | 166 base::ResetAndReturn(&callback_).Run(result); |
109 } | 167 } |
110 | 168 |
111 void UploadDataStream::OnReadCompleted(int result) { | 169 void UploadDataStream::OnReadCompleted(int result) { |
112 DCHECK(initialized_successfully_); | 170 DCHECK(initialized_successfully_); |
113 DCHECK(result != 0 || is_eof_); | 171 DCHECK(result != 0 || is_eof_); |
114 DCHECK_NE(ERR_IO_PENDING, result); | 172 DCHECK_NE(ERR_IO_PENDING, result); |
115 | 173 |
116 if (result > 0) { | 174 if (result > 0) { |
117 current_position_ += result; | 175 current_position_ += result; |
118 if (!is_chunked_) { | 176 if (!is_chunked_) { |
119 DCHECK_LE(current_position_, total_size_); | 177 DCHECK_LE(current_position_, total_size_); |
120 if (current_position_ == total_size_) | 178 if (current_position_ == total_size_) |
121 is_eof_ = true; | 179 is_eof_ = true; |
122 } | 180 } |
123 } | 181 } |
124 | 182 |
| 183 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UPLOAD_DATA_STREAM_READ, |
| 184 result); |
| 185 |
125 if (!callback_.is_null()) | 186 if (!callback_.is_null()) |
126 base::ResetAndReturn(&callback_).Run(result); | 187 base::ResetAndReturn(&callback_).Run(result); |
127 } | 188 } |
128 | 189 |
129 } // namespace net | 190 } // namespace net |
OLD | NEW |