| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/blob_reader.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <limits> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "base/task_runner.h" |
| 13 #include "base/time/time.h" |
| 14 #include "base/trace_event/trace_event.h" |
| 15 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/disk_cache/disk_cache.h" |
| 18 #include "storage/browser/blob/blob_data_handle.h" |
| 19 #include "storage/browser/blob/blob_data_snapshot.h" |
| 20 #include "storage/browser/fileapi/file_stream_reader.h" |
| 21 #include "storage/browser/fileapi/file_system_context.h" |
| 22 #include "storage/browser/fileapi/file_system_url.h" |
| 23 #include "storage/common/data_element.h" |
| 24 |
| 25 namespace storage { |
| 26 namespace { |
| 27 bool IsFileType(DataElement::Type type) { |
| 28 switch (type) { |
| 29 case DataElement::TYPE_FILE: |
| 30 case DataElement::TYPE_FILE_FILESYSTEM: |
| 31 return true; |
| 32 default: |
| 33 return false; |
| 34 } |
| 35 } |
| 36 } // namespace |
| 37 |
| 38 BlobReader::FileStreamReaderProvider::~FileStreamReaderProvider() {} |
| 39 |
| 40 BlobReader::BlobReader( |
| 41 const BlobDataHandle* blob_handle, |
| 42 scoped_ptr<FileStreamReaderProvider> file_stream_provider, |
| 43 base::SingleThreadTaskRunner* file_task_runner) |
| 44 : blob_handle_(blob_handle), |
| 45 file_stream_provider_(file_stream_provider.Pass()), |
| 46 file_task_runner_(file_task_runner), |
| 47 net_error_(net::OK), |
| 48 item_list_populated_(false), |
| 49 total_size_calculated_(false), |
| 50 total_size_(0), |
| 51 remaining_bytes_(0), |
| 52 pending_get_file_info_count_(0), |
| 53 current_item_index_(0), |
| 54 current_item_offset_(0), |
| 55 io_pending_(false), |
| 56 read_is_async_(false), |
| 57 weak_factory_(this) { |
| 58 if (blob_handle_) { |
| 59 blob_data_ = blob_handle_->CreateSnapshot().Pass(); |
| 60 } |
| 61 } |
| 62 |
| 63 BlobReader::~BlobReader() { |
| 64 STLDeleteValues(&index_to_reader_); |
| 65 } |
| 66 |
| 67 BlobReader::Status BlobReader::CalculateSize(net::CompletionCallback done) { |
| 68 DCHECK(!total_size_calculated_); |
| 69 if (!blob_handle_) { |
| 70 return ReportError(net::ERR_FILE_NOT_FOUND); |
| 71 } |
| 72 |
| 73 total_size_ = 0; |
| 74 const auto& items = blob_data_->items(); |
| 75 item_length_list_.resize(items.size()); |
| 76 pending_get_file_info_count_ = 0; |
| 77 for (size_t i = 0; i < items.size(); ++i) { |
| 78 const BlobDataItem& item = *items.at(i); |
| 79 if (IsFileType(item.type())) { |
| 80 ++pending_get_file_info_count_; |
| 81 storage::FileStreamReader* const reader = FileReaderAtIndex(i); |
| 82 if (!reader) { |
| 83 return ReportError(net::ERR_FAILED); |
| 84 } |
| 85 int64_t length_output = reader->GetLength(base::Bind( |
| 86 &BlobReader::DidGetFileItemLength, weak_factory_.GetWeakPtr(), i)); |
| 87 if (length_output == net::ERR_IO_PENDING) { |
| 88 continue; |
| 89 } |
| 90 if (length_output < 0) { |
| 91 return ReportError(length_output); |
| 92 } |
| 93 // We got the length right away |
| 94 --pending_get_file_info_count_; |
| 95 uint64_t resolved_length; |
| 96 if (!ResolveFileItemLength(item, length_output, &resolved_length)) { |
| 97 return ReportError(net::ERR_FILE_NOT_FOUND); |
| 98 } |
| 99 if (!AddItemLength(i, resolved_length)) { |
| 100 return ReportError(net::ERR_FAILED); |
| 101 } |
| 102 continue; |
| 103 } |
| 104 |
| 105 if (!AddItemLength(i, item.length())) |
| 106 return ReportError(net::ERR_FAILED); |
| 107 } |
| 108 |
| 109 if (pending_get_file_info_count_ == 0) { |
| 110 DidCountSize(); |
| 111 return Status::DONE; |
| 112 } |
| 113 |
| 114 size_done_ = done; |
| 115 return Status::PENDING_IO; |
| 116 } |
| 117 |
| 118 BlobReader::Status BlobReader::SetReadRange(uint64_t offset, uint64_t length) { |
| 119 if (!blob_handle_) { |
| 120 return ReportError(net::ERR_FILE_NOT_FOUND); |
| 121 } |
| 122 // Skip the initial items that are not in the range. |
| 123 if (offset + length > total_size_) { |
| 124 return ReportError(net::ERR_FILE_NOT_FOUND); |
| 125 } |
| 126 remaining_bytes_ = length; |
| 127 const auto& items = blob_data_->items(); |
| 128 for (current_item_index_ = 0; |
| 129 current_item_index_ < items.size() && |
| 130 offset >= item_length_list_[current_item_index_]; |
| 131 ++current_item_index_) { |
| 132 offset -= item_length_list_[current_item_index_]; |
| 133 } |
| 134 |
| 135 // Set the offset that need to jump to for the first item in the range. |
| 136 current_item_offset_ = offset; |
| 137 if (current_item_offset_ == 0) |
| 138 return Status::DONE; |
| 139 |
| 140 // Adjust the offset of the first stream if it is of file type. |
| 141 const BlobDataItem& item = *items.at(current_item_index_); |
| 142 if (IsFileType(item.type())) { |
| 143 SetFileReaderAtIndex(current_item_index_, |
| 144 CreateFileStreamReader(item, offset)); |
| 145 } |
| 146 return Status::DONE; |
| 147 } |
| 148 |
| 149 BlobReader::Status BlobReader::Read(net::IOBuffer* buffer, |
| 150 size_t dest_size, |
| 151 int* bytes_read, |
| 152 net::CompletionCallback done) { |
| 153 if (!blob_handle_) { |
| 154 return ReportError(net::ERR_FILE_NOT_FOUND); |
| 155 } |
| 156 DCHECK(bytes_read); |
| 157 DCHECK_GE(remaining_bytes_, 0ul); |
| 158 DCHECK(total_size_calculated_); |
| 159 DCHECK(read_done_.is_null()); |
| 160 read_done_ = done; |
| 161 read_is_async_ = false; |
| 162 |
| 163 // Bail out immediately if we encounter an error. |
| 164 if (net_error_ != net::OK) { |
| 165 *bytes_read = 0; |
| 166 return Status::NET_ERROR; |
| 167 } |
| 168 |
| 169 DCHECK_GE(dest_size, 0ul); |
| 170 if (remaining_bytes_ < static_cast<uint64_t>(dest_size)) |
| 171 dest_size = static_cast<int>(remaining_bytes_); |
| 172 |
| 173 // If we should copy zero bytes because |remaining_bytes_| is zero, short |
| 174 // circuit here. |
| 175 if (!dest_size) { |
| 176 *bytes_read = 0; |
| 177 return Status::DONE; |
| 178 } |
| 179 |
| 180 // Keep track of the buffer. |
| 181 DCHECK(!read_buf_.get()); |
| 182 read_buf_ = new net::DrainableIOBuffer(buffer, dest_size); |
| 183 |
| 184 Status status = ReadLoop(bytes_read); |
| 185 if (status != Status::PENDING_IO) { |
| 186 read_done_.Reset(); |
| 187 } |
| 188 return status; |
| 189 } |
| 190 |
| 191 void BlobReader::Kill() { |
| 192 DeleteCurrentFileReader(); |
| 193 weak_factory_.InvalidateWeakPtrs(); |
| 194 } |
| 195 |
| 196 bool BlobReader::IsInMemory() const { |
| 197 for (const auto& item : blob_data_->items()) { |
| 198 if (item->type() != DataElement::TYPE_BYTES) { |
| 199 return false; |
| 200 } |
| 201 } |
| 202 return true; |
| 203 } |
| 204 |
| 205 void BlobReader::InvalidateCallbacksAndDone(int net_error, |
| 206 net::CompletionCallback done) { |
| 207 net_error_ = net_error; |
| 208 weak_factory_.InvalidateWeakPtrs(); |
| 209 size_done_.Reset(); |
| 210 read_done_.Reset(); |
| 211 read_buf_ = nullptr; |
| 212 done.Run(net_error); |
| 213 } |
| 214 |
| 215 BlobReader::Status BlobReader::ReportError(int net_error) { |
| 216 net_error_ = net_error; |
| 217 return Status::NET_ERROR; |
| 218 } |
| 219 |
| 220 bool BlobReader::AddItemLength(size_t index, uint64_t item_length) { |
| 221 if (item_length > std::numeric_limits<uint64_t>::max() - total_size_) { |
| 222 return false; |
| 223 } |
| 224 |
| 225 // Cache the size and add it to the total size. |
| 226 DCHECK_LT(index, item_length_list_.size()); |
| 227 item_length_list_[index] = item_length; |
| 228 total_size_ += item_length; |
| 229 return true; |
| 230 } |
| 231 |
| 232 bool BlobReader::ResolveFileItemLength(const BlobDataItem& item, |
| 233 int64_t total_length, |
| 234 uint64_t* output_length) { |
| 235 DCHECK(IsFileType(item.type())); |
| 236 DCHECK(output_length); |
| 237 uint64_t file_length = total_length; |
| 238 uint64_t item_offset = item.offset(); |
| 239 uint64_t item_length = item.length(); |
| 240 if (item_offset > file_length) { |
| 241 return false; |
| 242 } |
| 243 |
| 244 uint64 max_length = file_length - item_offset; |
| 245 |
| 246 // If item length is undefined, then we need to use the file size being |
| 247 // resolved in the real time. |
| 248 if (item_length == std::numeric_limits<uint64>::max()) { |
| 249 item_length = max_length; |
| 250 } else if (item_length > max_length) { |
| 251 return false; |
| 252 } |
| 253 |
| 254 *output_length = item_length; |
| 255 return true; |
| 256 } |
| 257 |
| 258 void BlobReader::DidGetFileItemLength(size_t index, int64_t result) { |
| 259 // Do nothing if we have encountered an error. |
| 260 if (net_error_) |
| 261 return; |
| 262 |
| 263 if (result == net::ERR_UPLOAD_FILE_CHANGED) { |
| 264 InvalidateCallbacksAndDone(net::ERR_FILE_NOT_FOUND, size_done_); |
| 265 return; |
| 266 } else if (result < 0) { |
| 267 InvalidateCallbacksAndDone(result, size_done_); |
| 268 return; |
| 269 } |
| 270 |
| 271 const auto& items = blob_data_->items(); |
| 272 DCHECK_LT(index, items.size()); |
| 273 const BlobDataItem& item = *items.at(index); |
| 274 uint64_t length; |
| 275 if (!ResolveFileItemLength(item, result, &length)) { |
| 276 InvalidateCallbacksAndDone(net::ERR_FILE_NOT_FOUND, size_done_); |
| 277 return; |
| 278 } |
| 279 if (!AddItemLength(index, length)) { |
| 280 InvalidateCallbacksAndDone(net::ERR_FAILED, size_done_); |
| 281 return; |
| 282 } |
| 283 |
| 284 if (--pending_get_file_info_count_ == 0) { |
| 285 DidCountSize(); |
| 286 net::CompletionCallback done = size_done_; |
| 287 size_done_.Reset(); |
| 288 done.Run(total_size_); |
| 289 } |
| 290 } |
| 291 |
| 292 void BlobReader::DidCountSize() { |
| 293 DCHECK(!net_error_); |
| 294 total_size_calculated_ = true; |
| 295 remaining_bytes_ = total_size_; |
| 296 } |
| 297 |
| 298 BlobReader::Status BlobReader::ReadLoop(int* bytes_read) { |
| 299 // Read until we encounter an error or could not get the data immediately. |
| 300 while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) { |
| 301 Status read_status = ReadItem(); |
| 302 if (read_status == Status::DONE) { |
| 303 continue; |
| 304 } |
| 305 return read_status; |
| 306 } |
| 307 |
| 308 *bytes_read = BytesReadCompleted(); |
| 309 return Status::DONE; |
| 310 } |
| 311 |
| 312 BlobReader::Status BlobReader::ReadItem() { |
| 313 // Are we done with reading all the blob data? |
| 314 if (remaining_bytes_ == 0) |
| 315 return Status::DONE; |
| 316 |
| 317 const auto& items = blob_data_->items(); |
| 318 // If we get to the last item but still expect something to read, bail out |
| 319 // since something is wrong. |
| 320 if (current_item_index_ >= items.size()) { |
| 321 return ReportError(net::ERR_FAILED); |
| 322 } |
| 323 |
| 324 // Compute the bytes to read for current item. |
| 325 int bytes_to_read = ComputeBytesToRead(); |
| 326 |
| 327 // If nothing to read for current item, advance to next item. |
| 328 if (bytes_to_read == 0) { |
| 329 AdvanceItem(); |
| 330 return Status::DONE; |
| 331 } |
| 332 |
| 333 // Do the reading. |
| 334 const BlobDataItem& item = *items.at(current_item_index_); |
| 335 if (item.type() == DataElement::TYPE_BYTES) { |
| 336 ReadBytesItem(item, bytes_to_read); |
| 337 return Status::DONE; |
| 338 } |
| 339 if (item.type() == DataElement::TYPE_DISK_CACHE_ENTRY) |
| 340 return ReadDiskCacheEntryItem(item, bytes_to_read); |
| 341 if (!IsFileType(item.type())) { |
| 342 NOTREACHED(); |
| 343 return ReportError(net::ERR_FAILED); |
| 344 } |
| 345 storage::FileStreamReader* const reader = |
| 346 FileReaderAtIndex(current_item_index_); |
| 347 if (!reader) { |
| 348 return ReportError(net::ERR_FAILED); |
| 349 } |
| 350 |
| 351 return ReadFileItem(reader, bytes_to_read); |
| 352 } |
| 353 |
| 354 void BlobReader::AdvanceItem() { |
| 355 // Close the file if the current item is a file. |
| 356 DeleteCurrentFileReader(); |
| 357 |
| 358 // Advance to the next item. |
| 359 current_item_index_++; |
| 360 current_item_offset_ = 0; |
| 361 } |
| 362 |
| 363 void BlobReader::AdvanceBytesRead(int result) { |
| 364 DCHECK_GT(result, 0); |
| 365 |
| 366 // Do we finish reading the current item? |
| 367 current_item_offset_ += result; |
| 368 if (current_item_offset_ == item_length_list_[current_item_index_]) |
| 369 AdvanceItem(); |
| 370 |
| 371 // Subtract the remaining bytes. |
| 372 remaining_bytes_ -= result; |
| 373 DCHECK_GE(remaining_bytes_, 0ul); |
| 374 |
| 375 // Adjust the read buffer. |
| 376 read_buf_->DidConsume(result); |
| 377 DCHECK_GE(read_buf_->BytesRemaining(), 0); |
| 378 } |
| 379 |
| 380 void BlobReader::ReadBytesItem(const BlobDataItem& item, int bytes_to_read) { |
| 381 TRACE_EVENT1("Blob", "BlobReader::ReadBytesItem", "uuid", blob_data_->uuid()); |
| 382 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 383 |
| 384 memcpy(read_buf_->data(), item.bytes() + item.offset() + current_item_offset_, |
| 385 bytes_to_read); |
| 386 |
| 387 AdvanceBytesRead(bytes_to_read); |
| 388 } |
| 389 |
| 390 BlobReader::Status BlobReader::ReadFileItem(FileStreamReader* reader, |
| 391 int bytes_to_read) { |
| 392 DCHECK(!io_pending_) |
| 393 << "Can't begin IO while another IO operation is pending."; |
| 394 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 395 DCHECK(reader); |
| 396 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadFileItem", this, "uuid", |
| 397 blob_data_->uuid()); |
| 398 const int result = reader->Read( |
| 399 read_buf_.get(), bytes_to_read, |
| 400 base::Bind(&BlobReader::DidReadFile, weak_factory_.GetWeakPtr())); |
| 401 if (result >= 0) { |
| 402 AdvanceBytesRead(result); |
| 403 return Status::DONE; |
| 404 } |
| 405 if (result == net::ERR_IO_PENDING) { |
| 406 io_pending_ = true; |
| 407 read_is_async_ = true; |
| 408 return Status::PENDING_IO; |
| 409 } |
| 410 return ReportError(result); |
| 411 } |
| 412 |
| 413 void BlobReader::DidReadFile(int result) { |
| 414 DCHECK(io_pending_) << "Asynchronous IO completed while IO wasn't pending?"; |
| 415 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadFileItem", this, "uuid", |
| 416 blob_data_->uuid()); |
| 417 io_pending_ = false; |
| 418 if (result <= 0) { |
| 419 InvalidateCallbacksAndDone(result, read_done_); |
| 420 return; |
| 421 } |
| 422 |
| 423 AdvanceBytesRead(result); |
| 424 |
| 425 // Otherwise, continue the reading. |
| 426 ContinueAsyncReadLoop(); |
| 427 } |
| 428 |
| 429 void BlobReader::ContinueAsyncReadLoop() { |
| 430 int bytes_read = 0; |
| 431 Status read_status = ReadLoop(&bytes_read); |
| 432 switch (read_status) { |
| 433 case Status::DONE: { |
| 434 net::CompletionCallback done = read_done_; |
| 435 read_done_.Reset(); |
| 436 done.Run(bytes_read); |
| 437 return; |
| 438 } |
| 439 case Status::NET_ERROR: |
| 440 InvalidateCallbacksAndDone(net_error_, read_done_); |
| 441 return; |
| 442 case Status::PENDING_IO: |
| 443 return; |
| 444 } |
| 445 } |
| 446 |
| 447 void BlobReader::DeleteCurrentFileReader() { |
| 448 auto found = index_to_reader_.find(current_item_index_); |
| 449 if (found != index_to_reader_.end() && found->second) { |
| 450 delete found->second; |
| 451 index_to_reader_.erase(found); |
| 452 } |
| 453 } |
| 454 |
| 455 BlobReader::Status BlobReader::ReadDiskCacheEntryItem(const BlobDataItem& item, |
| 456 int bytes_to_read) { |
| 457 DCHECK(!io_pending_) |
| 458 << "Can't begin IO while another IO operation is pending."; |
| 459 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadDiskCacheItem", this, |
| 460 "uuid", blob_data_->uuid()); |
| 461 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 462 |
| 463 const int result = item.disk_cache_entry()->ReadData( |
| 464 item.disk_cache_stream_index(), current_item_offset_, read_buf_.get(), |
| 465 bytes_to_read, base::Bind(&BlobReader::DidReadDiskCacheEntry, |
| 466 weak_factory_.GetWeakPtr())); |
| 467 if (result >= 0) { |
| 468 AdvanceBytesRead(result); |
| 469 return Status::DONE; |
| 470 } |
| 471 if (result == net::ERR_IO_PENDING) { |
| 472 io_pending_ = true; |
| 473 read_is_async_ = true; |
| 474 return Status::PENDING_IO; |
| 475 } |
| 476 return ReportError(result); |
| 477 } |
| 478 |
| 479 void BlobReader::DidReadDiskCacheEntry(int result) { |
| 480 DCHECK(io_pending_) << "Asynchronous IO completed while IO wasn't pending?"; |
| 481 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadDiskCacheItem", this, "uuid", |
| 482 blob_data_->uuid()); |
| 483 io_pending_ = false; |
| 484 if (result <= 0) { |
| 485 InvalidateCallbacksAndDone(result, read_done_); |
| 486 return; |
| 487 } |
| 488 |
| 489 AdvanceBytesRead(result); |
| 490 |
| 491 ContinueAsyncReadLoop(); |
| 492 } |
| 493 |
| 494 int BlobReader::BytesReadCompleted() { |
| 495 int bytes_read = read_buf_->BytesConsumed(); |
| 496 read_buf_ = nullptr; |
| 497 return bytes_read; |
| 498 } |
| 499 |
| 500 int BlobReader::ComputeBytesToRead() const { |
| 501 uint64_t current_item_length = item_length_list_[current_item_index_]; |
| 502 |
| 503 uint64_t item_remaining = current_item_length - current_item_offset_; |
| 504 uint64_t buf_remaining = read_buf_->BytesRemaining(); |
| 505 uint64_t max_remaining = std::numeric_limits<int>::max(); |
| 506 |
| 507 uint64_t min = std::min( |
| 508 std::min(std::min(item_remaining, buf_remaining), remaining_bytes_), |
| 509 max_remaining); |
| 510 |
| 511 return static_cast<int>(min); |
| 512 } |
| 513 |
| 514 FileStreamReader* BlobReader::FileReaderAtIndex(size_t index) { |
| 515 const auto& items = blob_data_->items(); |
| 516 DCHECK_LT(index, items.size()); |
| 517 const BlobDataItem& item = *items.at(index); |
| 518 if (!IsFileType(item.type())) |
| 519 return nullptr; |
| 520 FileStreamReader* reader; |
| 521 auto it = index_to_reader_.find(index); |
| 522 if (it != index_to_reader_.end()) { |
| 523 DCHECK(it->second); |
| 524 return it->second; |
| 525 } |
| 526 reader = CreateFileStreamReader(item, 0); |
| 527 if (!reader) |
| 528 return nullptr; |
| 529 index_to_reader_[index] = reader; |
| 530 return reader; |
| 531 } |
| 532 |
| 533 FileStreamReader* BlobReader::CreateFileStreamReader( |
| 534 const BlobDataItem& item, |
| 535 uint64_t additional_offset) { |
| 536 DCHECK(IsFileType(item.type())); |
| 537 |
| 538 FileStreamReader* reader = nullptr; |
| 539 switch (item.type()) { |
| 540 case DataElement::TYPE_FILE: |
| 541 reader = file_stream_provider_->CreateForLocalFile( |
| 542 file_task_runner_.get(), item.path(), |
| 543 item.offset() + additional_offset, item.expected_modification_time()); |
| 544 DCHECK(reader); |
| 545 return reader; |
| 546 case DataElement::TYPE_FILE_FILESYSTEM: |
| 547 reader = file_stream_provider_->CreateFileStreamReader( |
| 548 item.filesystem_url(), item.offset() + additional_offset, |
| 549 item.length() == std::numeric_limits<uint64_t>::max() |
| 550 ? storage::kMaximumLength |
| 551 : item.length() - additional_offset, |
| 552 item.expected_modification_time()); |
| 553 return reader; |
| 554 default: |
| 555 break; |
| 556 } |
| 557 |
| 558 NOTREACHED(); |
| 559 return nullptr; |
| 560 } |
| 561 |
| 562 void BlobReader::SetFileReaderAtIndex(size_t index, FileStreamReader* reader) { |
| 563 auto found = index_to_reader_.find(current_item_index_); |
| 564 if (found != index_to_reader_.end()) { |
| 565 if (found->second) { |
| 566 delete found->second; |
| 567 } |
| 568 found->second = reader; |
| 569 } else if (reader) { |
| 570 index_to_reader_[current_item_index_] = reader; |
| 571 } |
| 572 } |
| 573 |
| 574 } // namespace storage |
| OLD | NEW |