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), | 18 bytes_start_(NULL), |
19 bytes_length_(0), | 19 bytes_length_(0), |
20 file_range_offset_(0), | 20 file_range_offset_(0), |
21 file_range_length_(kuint64max), | 21 file_range_length_(kuint64max), |
22 is_last_chunk_(false), | |
23 override_content_length_(false), | 22 override_content_length_(false), |
24 content_length_computed_(false), | 23 content_length_computed_(false), |
25 content_length_(-1), | 24 content_length_(-1), |
26 offset_(0), | 25 offset_(0), |
27 file_stream_(NULL) { | 26 file_stream_(NULL) { |
28 } | 27 } |
29 | 28 |
30 UploadElement::~UploadElement() { | 29 UploadElement::~UploadElement() { |
31 // In the common case |file__stream_| will be null. | 30 // In the common case |file__stream_| will be null. |
32 if (file_stream_) { | 31 if (file_stream_) { |
33 // Temporarily allow until fix: http://crbug.com/72001. | 32 // Temporarily allow until fix: http://crbug.com/72001. |
34 base::ThreadRestrictions::ScopedAllowIO allow_io; | 33 base::ThreadRestrictions::ScopedAllowIO allow_io; |
35 file_stream_->CloseSync(); | 34 file_stream_->CloseSync(); |
36 delete file_stream_; | 35 delete file_stream_; |
37 } | 36 } |
38 } | 37 } |
39 | 38 |
40 void UploadElement::SetToChunk(const char* bytes, | |
41 int bytes_len, | |
42 bool is_last_chunk) { | |
43 type_ = TYPE_CHUNK; | |
44 buf_.assign(bytes, bytes + bytes_len); | |
45 is_last_chunk_ = is_last_chunk; | |
46 } | |
47 | |
48 uint64 UploadElement::GetContentLength() { | 39 uint64 UploadElement::GetContentLength() { |
49 if (override_content_length_ || content_length_computed_) | 40 if (override_content_length_ || content_length_computed_) |
50 return content_length_; | 41 return content_length_; |
51 | 42 |
52 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) | 43 if (type_ == TYPE_BYTES) |
53 return bytes_length(); | 44 return bytes_length(); |
54 | 45 |
55 DCHECK_EQ(TYPE_FILE, type_); | 46 DCHECK_EQ(TYPE_FILE, type_); |
56 DCHECK(!file_stream_); | 47 DCHECK(!file_stream_); |
57 | 48 |
58 // TODO(darin): This size calculation could be out of sync with the state of | 49 // TODO(darin): This size calculation could be out of sync with the state of |
59 // the file when we get around to reading it. We should probably find a way | 50 // the file when we get around to reading it. We should probably find a way |
60 // to lock the file or somehow protect against this error condition. | 51 // to lock the file or somehow protect against this error condition. |
61 | 52 |
62 content_length_computed_ = true; | 53 content_length_computed_ = true; |
(...skipping 12 matching lines...) Expand all Loading... |
75 | 66 |
76 if (file_range_offset_ >= static_cast<uint64>(length)) | 67 if (file_range_offset_ >= static_cast<uint64>(length)) |
77 return 0; // range is beyond eof | 68 return 0; // range is beyond eof |
78 | 69 |
79 // compensate for the offset and clip file_range_length_ to eof | 70 // compensate for the offset and clip file_range_length_ to eof |
80 content_length_ = std::min(length - file_range_offset_, file_range_length_); | 71 content_length_ = std::min(length - file_range_offset_, file_range_length_); |
81 return content_length_; | 72 return content_length_; |
82 } | 73 } |
83 | 74 |
84 int UploadElement::ReadSync(char* buf, int buf_len) { | 75 int UploadElement::ReadSync(char* buf, int buf_len) { |
85 if (type_ == TYPE_BYTES || type_ == TYPE_CHUNK) { | 76 if (type_ == TYPE_BYTES) { |
86 return ReadFromMemorySync(buf, buf_len); | 77 return ReadFromMemorySync(buf, buf_len); |
87 } else if (type_ == TYPE_FILE) { | 78 } else if (type_ == TYPE_FILE) { |
88 return ReadFromFileSync(buf, buf_len); | 79 return ReadFromFileSync(buf, buf_len); |
89 } | 80 } |
90 | 81 |
91 NOTREACHED(); | 82 NOTREACHED(); |
92 return 0; | 83 return 0; |
93 } | 84 } |
94 | 85 |
95 uint64 UploadElement::BytesRemaining() { | 86 uint64 UploadElement::BytesRemaining() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 << ")"; | 120 << ")"; |
130 return NULL; | 121 return NULL; |
131 } | 122 } |
132 } | 123 } |
133 | 124 |
134 return file.release(); | 125 return file.release(); |
135 } | 126 } |
136 | 127 |
137 int UploadElement::ReadFromMemorySync(char* buf, int buf_len) { | 128 int UploadElement::ReadFromMemorySync(char* buf, int buf_len) { |
138 DCHECK_LT(0, buf_len); | 129 DCHECK_LT(0, buf_len); |
139 DCHECK(type_ == TYPE_BYTES || type_ == TYPE_CHUNK); | 130 DCHECK(type_ == TYPE_BYTES); |
140 | 131 |
141 const size_t num_bytes_to_read = std::min(BytesRemaining(), | 132 const size_t num_bytes_to_read = std::min(BytesRemaining(), |
142 static_cast<uint64>(buf_len)); | 133 static_cast<uint64>(buf_len)); |
143 | 134 |
144 // Check if we have anything to copy first, because we are getting | 135 // Check if we have anything to copy first, because we are getting |
145 // the address of an element in |bytes_| and that will throw an | 136 // the address of an element in |bytes_| and that will throw an |
146 // exception if |bytes_| is an empty vector. | 137 // exception if |bytes_| is an empty vector. |
147 if (num_bytes_to_read > 0) | 138 if (num_bytes_to_read > 0) |
148 memcpy(buf, bytes() + offset_, num_bytes_to_read); | 139 memcpy(buf, bytes() + offset_, num_bytes_to_read); |
149 | 140 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // rest of the data. | 173 // rest of the data. |
183 memset(buf, 0, num_bytes_to_read); | 174 memset(buf, 0, num_bytes_to_read); |
184 } | 175 } |
185 } | 176 } |
186 | 177 |
187 offset_ += num_bytes_to_read; | 178 offset_ += num_bytes_to_read; |
188 return num_bytes_to_read; | 179 return num_bytes_to_read; |
189 } | 180 } |
190 | 181 |
191 } // namespace net | 182 } // namespace net |
OLD | NEW |