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 "webkit/blob/blob_url_request_job.h" | 5 #include "webkit/blob/blob_url_request_job.h" |
6 | 6 |
7 #include <limits> | |
8 | |
9 #include "base/bind.h" | 7 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
11 #include "base/files/file_util_proxy.h" | 9 #include "base/files/file_util_proxy.h" |
12 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
13 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
14 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
15 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
16 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
18 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
(...skipping 22 matching lines...) Expand all Loading... |
41 | 39 |
42 const char kHTTPOKText[] = "OK"; | 40 const char kHTTPOKText[] = "OK"; |
43 const char kHTTPPartialContentText[] = "Partial Content"; | 41 const char kHTTPPartialContentText[] = "Partial Content"; |
44 const char kHTTPNotAllowedText[] = "Not Allowed"; | 42 const char kHTTPNotAllowedText[] = "Not Allowed"; |
45 const char kHTTPNotFoundText[] = "Not Found"; | 43 const char kHTTPNotFoundText[] = "Not Found"; |
46 const char kHTTPMethodNotAllowText[] = "Method Not Allowed"; | 44 const char kHTTPMethodNotAllowText[] = "Method Not Allowed"; |
47 const char kHTTPRequestedRangeNotSatisfiableText[] = | 45 const char kHTTPRequestedRangeNotSatisfiableText[] = |
48 "Requested Range Not Satisfiable"; | 46 "Requested Range Not Satisfiable"; |
49 const char kHTTPInternalErrorText[] = "Internal Server Error"; | 47 const char kHTTPInternalErrorText[] = "Internal Server Error"; |
50 | 48 |
51 const int64 kMaxTotalSize = std::numeric_limits<int64>::max(); | |
52 | |
53 bool IsFileType(BlobData::Item::Type type) { | 49 bool IsFileType(BlobData::Item::Type type) { |
54 switch (type) { | 50 switch (type) { |
55 case BlobData::Item::TYPE_FILE: | 51 case BlobData::Item::TYPE_FILE: |
56 case BlobData::Item::TYPE_FILE_FILESYSTEM: | 52 case BlobData::Item::TYPE_FILE_FILESYSTEM: |
57 return true; | 53 return true; |
58 default: | 54 default: |
59 return false; | 55 return false; |
60 } | 56 } |
61 } | 57 } |
62 | 58 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 176 |
181 // If the blob data is not present, bail out. | 177 // If the blob data is not present, bail out. |
182 if (!blob_data_) { | 178 if (!blob_data_) { |
183 NotifyFailure(net::ERR_FILE_NOT_FOUND); | 179 NotifyFailure(net::ERR_FILE_NOT_FOUND); |
184 return; | 180 return; |
185 } | 181 } |
186 | 182 |
187 CountSize(); | 183 CountSize(); |
188 } | 184 } |
189 | 185 |
190 bool BlobURLRequestJob::AddItemLength(size_t index, int64 item_length) { | |
191 if (item_length > kMaxTotalSize - total_size_) { | |
192 NotifyFailure(net::ERR_FAILED); | |
193 return false; | |
194 } | |
195 | |
196 // Cache the size and add it to the total size. | |
197 DCHECK_LT(index, item_length_list_.size()); | |
198 item_length_list_[index] = item_length; | |
199 total_size_ += item_length; | |
200 return true; | |
201 } | |
202 | |
203 void BlobURLRequestJob::CountSize() { | 186 void BlobURLRequestJob::CountSize() { |
204 error_ = false; | 187 error_ = false; |
205 pending_get_file_info_count_ = 0; | 188 pending_get_file_info_count_ = 0; |
206 total_size_ = 0; | 189 total_size_ = 0; |
207 item_length_list_.resize(blob_data_->items().size()); | 190 item_length_list_.resize(blob_data_->items().size()); |
208 | 191 |
209 for (size_t i = 0; i < blob_data_->items().size(); ++i) { | 192 for (size_t i = 0; i < blob_data_->items().size(); ++i) { |
210 const BlobData::Item& item = blob_data_->items().at(i); | 193 const BlobData::Item& item = blob_data_->items().at(i); |
211 if (IsFileType(item.type())) { | 194 if (IsFileType(item.type())) { |
212 ++pending_get_file_info_count_; | 195 ++pending_get_file_info_count_; |
213 GetFileStreamReader(i)->GetLength( | 196 GetFileStreamReader(i)->GetLength( |
214 base::Bind(&BlobURLRequestJob::DidGetFileItemLength, | 197 base::Bind(&BlobURLRequestJob::DidGetFileItemLength, |
215 weak_factory_.GetWeakPtr(), i)); | 198 weak_factory_.GetWeakPtr(), i)); |
216 continue; | 199 continue; |
217 } | 200 } |
218 | 201 // Cache the size and add it to the total size. |
219 if (!AddItemLength(i, item.length())) | 202 int64 item_length = static_cast<int64>(item.length()); |
220 return; | 203 item_length_list_[i] = item_length; |
| 204 total_size_ += item_length; |
221 } | 205 } |
222 | 206 |
223 if (pending_get_file_info_count_ == 0) | 207 if (pending_get_file_info_count_ == 0) |
224 DidCountSize(net::OK); | 208 DidCountSize(net::OK); |
225 } | 209 } |
226 | 210 |
227 void BlobURLRequestJob::DidCountSize(int error) { | 211 void BlobURLRequestJob::DidCountSize(int error) { |
228 DCHECK(!error_); | 212 DCHECK(!error_); |
229 | 213 |
230 // If an error occured, bail out. | 214 // If an error occured, bail out. |
(...skipping 29 matching lines...) Expand all Loading... |
260 return; | 244 return; |
261 } else if (result < 0) { | 245 } else if (result < 0) { |
262 NotifyFailure(result); | 246 NotifyFailure(result); |
263 return; | 247 return; |
264 } | 248 } |
265 | 249 |
266 DCHECK_LT(index, blob_data_->items().size()); | 250 DCHECK_LT(index, blob_data_->items().size()); |
267 const BlobData::Item& item = blob_data_->items().at(index); | 251 const BlobData::Item& item = blob_data_->items().at(index); |
268 DCHECK(IsFileType(item.type())); | 252 DCHECK(IsFileType(item.type())); |
269 | 253 |
270 uint64 file_length = result; | |
271 uint64 item_offset = item.offset(); | |
272 uint64 item_length = item.length(); | |
273 | |
274 if (item_offset > file_length) { | |
275 NotifyFailure(net::ERR_FILE_NOT_FOUND); | |
276 return; | |
277 } | |
278 | |
279 uint64 max_length = file_length - item_offset; | |
280 | |
281 // If item length is -1, we need to use the file size being resolved | 254 // If item length is -1, we need to use the file size being resolved |
282 // in the real time. | 255 // in the real time. |
283 if (item_length == static_cast<uint64>(-1)) { | 256 int64 item_length = static_cast<int64>(item.length()); |
284 item_length = max_length; | 257 if (item_length == -1) |
285 } else if (item_length > max_length) { | 258 item_length = result - item.offset(); |
286 NotifyFailure(net::ERR_FILE_NOT_FOUND); | |
287 return; | |
288 } | |
289 | 259 |
290 if (!AddItemLength(index, item_length)) | 260 // Cache the size and add it to the total size. |
291 return; | 261 DCHECK_LT(index, item_length_list_.size()); |
| 262 item_length_list_[index] = item_length; |
| 263 total_size_ += item_length; |
292 | 264 |
293 if (--pending_get_file_info_count_ == 0) | 265 if (--pending_get_file_info_count_ == 0) |
294 DidCountSize(net::OK); | 266 DidCountSize(net::OK); |
295 } | 267 } |
296 | 268 |
297 void BlobURLRequestJob::Seek(int64 offset) { | 269 void BlobURLRequestJob::Seek(int64 offset) { |
298 // Skip the initial items that are not in the range. | 270 // Skip the initial items that are not in the range. |
299 for (current_item_index_ = 0; | 271 for (current_item_index_ = 0; |
300 current_item_index_ < blob_data_->items().size() && | 272 current_item_index_ < blob_data_->items().size() && |
301 offset >= item_length_list_[current_item_index_]; | 273 offset >= item_length_list_[current_item_index_]; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 } | 413 } |
442 } | 414 } |
443 | 415 |
444 int BlobURLRequestJob::BytesReadCompleted() { | 416 int BlobURLRequestJob::BytesReadCompleted() { |
445 int bytes_read = read_buf_->BytesConsumed(); | 417 int bytes_read = read_buf_->BytesConsumed(); |
446 read_buf_ = NULL; | 418 read_buf_ = NULL; |
447 return bytes_read; | 419 return bytes_read; |
448 } | 420 } |
449 | 421 |
450 int BlobURLRequestJob::ComputeBytesToRead() const { | 422 int BlobURLRequestJob::ComputeBytesToRead() const { |
451 int64 current_item_length = item_length_list_[current_item_index_]; | 423 int64 current_item_remaining_bytes = |
| 424 item_length_list_[current_item_index_] - current_item_offset_; |
| 425 int64 remaining_bytes = std::min(current_item_remaining_bytes, |
| 426 remaining_bytes_); |
452 | 427 |
453 int64 item_remaining = current_item_length - current_item_offset_; | 428 return static_cast<int>(std::min( |
454 int64 buf_remaining = read_buf_->BytesRemaining(); | 429 static_cast<int64>(read_buf_->BytesRemaining()), |
455 int64 max_remaining = std::numeric_limits<int>::max(); | 430 remaining_bytes)); |
456 | |
457 int64 min = std::min(std::min(std::min(item_remaining, | |
458 buf_remaining), | |
459 remaining_bytes_), | |
460 max_remaining); | |
461 | |
462 return static_cast<int>(min); | |
463 } | 431 } |
464 | 432 |
465 bool BlobURLRequestJob::ReadLoop(int* bytes_read) { | 433 bool BlobURLRequestJob::ReadLoop(int* bytes_read) { |
466 // Read until we encounter an error or could not get the data immediately. | 434 // Read until we encounter an error or could not get the data immediately. |
467 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) { | 435 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) { |
468 if (!ReadItem()) | 436 if (!ReadItem()) |
469 return false; | 437 return false; |
470 } | 438 } |
471 | 439 |
472 *bytes_read = BytesReadCompleted(); | 440 *bytes_read = BytesReadCompleted(); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 item.expected_modification_time()); | 567 item.expected_modification_time()); |
600 break; | 568 break; |
601 default: | 569 default: |
602 NOTREACHED(); | 570 NOTREACHED(); |
603 } | 571 } |
604 DCHECK(reader); | 572 DCHECK(reader); |
605 index_to_reader_[index] = reader; | 573 index_to_reader_[index] = reader; |
606 } | 574 } |
607 | 575 |
608 } // namespace webkit_blob | 576 } // namespace webkit_blob |
OLD | NEW |