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_element.h" | 5 #include "net/base/upload_element.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "net/base/file_stream.h" | 11 #include "net/base/file_stream.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 UploadElement::UploadElement() | 16 UploadElement::UploadElement() |
17 : type_(TYPE_BYTES), | 17 : type_(TYPE_BYTES), |
| 18 bytes_start_(NULL), |
| 19 bytes_length_(0), |
18 file_range_offset_(0), | 20 file_range_offset_(0), |
19 file_range_length_(kuint64max), | 21 file_range_length_(kuint64max), |
20 is_last_chunk_(false), | 22 is_last_chunk_(false), |
21 override_content_length_(false), | 23 override_content_length_(false), |
22 content_length_computed_(false), | 24 content_length_computed_(false), |
23 content_length_(-1), | 25 content_length_(-1), |
24 offset_(0), | 26 offset_(0), |
25 file_stream_(NULL) { | 27 file_stream_(NULL) { |
26 } | 28 } |
27 | 29 |
28 UploadElement::~UploadElement() { | 30 UploadElement::~UploadElement() { |
29 // In the common case |file__stream_| will be null. | 31 // In the common case |file__stream_| will be null. |
30 if (file_stream_) { | 32 if (file_stream_) { |
31 // Temporarily allow until fix: http://crbug.com/72001. | 33 // Temporarily allow until fix: http://crbug.com/72001. |
32 base::ThreadRestrictions::ScopedAllowIO allow_io; | 34 base::ThreadRestrictions::ScopedAllowIO allow_io; |
33 file_stream_->CloseSync(); | 35 file_stream_->CloseSync(); |
34 delete file_stream_; | 36 delete file_stream_; |
35 } | 37 } |
36 } | 38 } |
37 | 39 |
38 void UploadElement::SetToChunk(const char* bytes, | 40 void UploadElement::SetToChunk(const char* bytes, |
39 int bytes_len, | 41 int bytes_len, |
40 bool is_last_chunk) { | 42 bool is_last_chunk) { |
41 bytes_.clear(); | |
42 bytes_.insert(bytes_.end(), bytes, bytes + bytes_len); | |
43 type_ = TYPE_CHUNK; | 43 type_ = TYPE_CHUNK; |
| 44 buf_.assign(bytes, bytes + bytes_len); |
44 is_last_chunk_ = is_last_chunk; | 45 is_last_chunk_ = is_last_chunk; |
45 } | 46 } |
46 | 47 |
47 uint64 UploadElement::GetContentLength() { | 48 uint64 UploadElement::GetContentLength() { |
48 if (override_content_length_ || content_length_computed_) | 49 if (override_content_length_ || content_length_computed_) |
49 return content_length_; | 50 return content_length_; |
50 | 51 |
51 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) | 52 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) |
52 return static_cast<uint64>(bytes_.size()); | 53 return bytes_length(); |
53 else if (type_ == TYPE_BLOB) | |
54 // The blob reference will be resolved later. | |
55 return 0; | |
56 | 54 |
57 DCHECK_EQ(TYPE_FILE, type_); | 55 DCHECK_EQ(TYPE_FILE, type_); |
58 DCHECK(!file_stream_); | 56 DCHECK(!file_stream_); |
59 | 57 |
60 // TODO(darin): This size calculation could be out of sync with the state of | 58 // TODO(darin): This size calculation could be out of sync with the state of |
61 // the file when we get around to reading it. We should probably find a way | 59 // the file when we get around to reading it. We should probably find a way |
62 // to lock the file or somehow protect against this error condition. | 60 // to lock the file or somehow protect against this error condition. |
63 | 61 |
64 content_length_computed_ = true; | 62 content_length_computed_ = true; |
65 content_length_ = 0; | 63 content_length_ = 0; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 int UploadElement::ReadFromMemorySync(char* buf, int buf_len) { | 137 int UploadElement::ReadFromMemorySync(char* buf, int buf_len) { |
140 DCHECK_LT(0, buf_len); | 138 DCHECK_LT(0, buf_len); |
141 DCHECK(type_ == TYPE_BYTES || type_ == TYPE_CHUNK); | 139 DCHECK(type_ == TYPE_BYTES || type_ == TYPE_CHUNK); |
142 | 140 |
143 const size_t num_bytes_to_read = std::min(BytesRemaining(), | 141 const size_t num_bytes_to_read = std::min(BytesRemaining(), |
144 static_cast<uint64>(buf_len)); | 142 static_cast<uint64>(buf_len)); |
145 | 143 |
146 // Check if we have anything to copy first, because we are getting | 144 // Check if we have anything to copy first, because we are getting |
147 // the address of an element in |bytes_| and that will throw an | 145 // the address of an element in |bytes_| and that will throw an |
148 // exception if |bytes_| is an empty vector. | 146 // exception if |bytes_| is an empty vector. |
149 if (num_bytes_to_read > 0) { | 147 if (num_bytes_to_read > 0) |
150 memcpy(buf, &bytes_[offset_], num_bytes_to_read); | 148 memcpy(buf, bytes() + offset_, num_bytes_to_read); |
151 } | |
152 | 149 |
153 offset_ += num_bytes_to_read; | 150 offset_ += num_bytes_to_read; |
154 return num_bytes_to_read; | 151 return num_bytes_to_read; |
155 } | 152 } |
156 | 153 |
157 int UploadElement::ReadFromFileSync(char* buf, int buf_len) { | 154 int UploadElement::ReadFromFileSync(char* buf, int buf_len) { |
158 DCHECK_LT(0, buf_len); | 155 DCHECK_LT(0, buf_len); |
159 DCHECK_EQ(TYPE_FILE, type_); | 156 DCHECK_EQ(TYPE_FILE, type_); |
160 | 157 |
161 // Open the file of the current element if not yet opened. | 158 // Open the file of the current element if not yet opened. |
(...skipping 23 matching lines...) Expand all Loading... |
185 // rest of the data. | 182 // rest of the data. |
186 memset(buf, 0, num_bytes_to_read); | 183 memset(buf, 0, num_bytes_to_read); |
187 } | 184 } |
188 } | 185 } |
189 | 186 |
190 offset_ += num_bytes_to_read; | 187 offset_ += num_bytes_to_read; |
191 return num_bytes_to_read; | 188 return num_bytes_to_read; |
192 } | 189 } |
193 | 190 |
194 } // namespace net | 191 } // namespace net |
OLD | NEW |