Index: webkit/browser/blob/blob_reader.cc |
diff --git a/webkit/browser/blob/blob_reader.cc b/webkit/browser/blob/blob_reader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..875f03ce9bfd7237aafef0381c7a4f08c2b272a9 |
--- /dev/null |
+++ b/webkit/browser/blob/blob_reader.cc |
@@ -0,0 +1,452 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "webkit/browser/blob/blob_reader.h" |
+ |
+#include "net/base/io_buffer.h" |
+#include "net/base/net_errors.h" |
+#include "webkit/browser/blob/local_file_stream_reader.h" |
+#include "webkit/browser/fileapi/file_system_context.h" |
+ |
+namespace webkit_blob { |
+ |
+namespace { |
+bool IsFileType(BlobData::Item::Type type) { |
+ return type == BlobData::Item::TYPE_FILE || |
+ type == BlobData::Item::TYPE_FILE_FILESYSTEM; |
+} |
+} // namespace |
+ |
+BlobReader::BlobReader( |
+ BlobData* blob_data, |
+ fileapi::FileSystemContext* file_system_context) |
+ : weak_factory_(this), |
+ blob_data_(blob_data), |
+ file_system_context_(file_system_context), |
+ total_size_(0), |
+ remaining_bytes_(0), |
+ pending_get_file_info_count_(0), |
+ current_item_index_(0), |
+ current_item_offset_(0), |
+ error_(false) { |
+ is_counting_size_ = false; |
+ has_started_reading_ = false; |
+ has_total_size_ = false; |
+ initial_offset_ = 0; |
+ file_task_runner_ = file_system_context_->default_file_task_runner(); |
+} |
+ |
+BlobReader::~BlobReader() { |
+ STLDeleteValues(&index_to_reader_); |
+} |
+ |
+void BlobReader::SetInitialOffset(int64 offset) { |
+ DCHECK(!has_started_reading_); |
+ initial_offset_ = offset; |
+} |
+ |
+int BlobReader::Read(net::IOBuffer* buf, int buf_len, |
+ const net::CompletionCallback& callback) { |
+ if (callback.is_null() || !pending_read_callback_.is_null()) |
+ return net::ERR_UNEXPECTED; |
+ if (error_) |
+ return error_; |
+ |
+ // Keep track of the buffer. |
+ DCHECK(!pending_read_buf_.get()); |
+ pending_read_buf_ = buf; |
+ pending_read_length_ = buf_len; |
+ |
+ // Before reading, we need to compute the size of blob data elements. |
+ if (!has_total_size_) { |
+ CountSize(); |
+ if (!has_total_size_) { |
+ pending_read_callback_ = callback; |
+ return net::ERR_IO_PENDING; |
+ } |
+ } |
+ |
+ int rv = DoRead(); |
+ if (rv == net::ERR_IO_PENDING) |
+ pending_read_callback_ = callback; |
+ return rv; |
+} |
+ |
+int64 BlobReader::GetLength(const net::Int64CompletionCallback& callback) { |
+ if (callback.is_null() || pending_size_callback_.is_null()) |
+ return net::ERR_UNEXPECTED; |
+ if (error_) |
+ return error_; |
+ if (has_total_size_) |
+ return total_size_; |
+ |
+ CountSize(); |
+ if (!has_total_size_) { |
+ // We'll complete the callback when we know the total size. |
+ pending_size_callback_ = callback; |
+ return net::ERR_IO_PENDING; |
+ } |
+ return total_size_; |
+} |
+ |
+void BlobReader::CountSize() { |
+ DCHECK(!has_total_size_); |
+ if (is_counting_size_) |
+ return; |
+ pending_get_file_info_count_ = 0; |
+ total_size_ = 0; |
+ item_length_list_.resize(blob_data_->items().size()); |
+ |
+ for (size_t i = 0; i < blob_data_->items().size(); ++i) { |
+ const BlobData::Item& item = blob_data_->items().at(i); |
+ if (IsFileType(item.type())) { |
+ ++pending_get_file_info_count_; |
+ GetFileStreamReader(i)->GetLength( |
+ base::Bind(&BlobReader::DidGetFileItemLength, |
+ weak_factory_.GetWeakPtr(), i)); |
+ continue; |
+ } |
+ |
+ if (!AddItemLength(i, item.length())) |
+ return; |
+ } |
+ |
+ if (pending_get_file_info_count_ == 0) |
+ OnSizeCounted(); |
+} |
+ |
+bool BlobReader::AddItemLength(size_t index, int64 item_length) { |
+ if (item_length > kint64max - total_size_) { |
+ OnError(net::ERR_FAILED); |
+ return false; |
+ } |
+ |
+ // Cache the size and add it to the total size. |
+ DCHECK_LT(index, item_length_list_.size()); |
+ item_length_list_[index] = item_length; |
+ total_size_ += item_length; |
+ return true; |
+} |
+ |
+void BlobReader::DidGetFileItemLength(size_t index, int64 result) { |
+ // Do nothing if we have already encountered an error. |
+ if (error_) |
+ return; |
+ |
+ if (result == net::ERR_UPLOAD_FILE_CHANGED) { |
+ OnError(net::ERR_FILE_NOT_FOUND); |
+ return; |
+ } |
+ if (result < 0) { |
+ OnError(result); |
+ return; |
+ } |
+ |
+ DCHECK_LT(index, blob_data_->items().size()); |
+ const BlobData::Item& item = blob_data_->items().at(index); |
+ DCHECK(IsFileType(item.type())); |
+ |
+ uint64 file_length = result; |
+ uint64 item_offset = item.offset(); |
+ uint64 item_length = item.length(); |
+ |
+ if (item_offset > file_length) { |
+ OnError(net::ERR_FILE_NOT_FOUND); |
+ return; |
+ } |
+ |
+ uint64 max_length = file_length - item_offset; |
+ |
+ // If item length is -1, we need to use the file size being resolved |
+ // in the real time. |
+ if (item_length == static_cast<uint64>(-1)) { |
+ item_length = max_length; |
+ } else if (item_length > max_length) { |
+ OnError(net::ERR_FILE_NOT_FOUND); |
+ return; |
+ } |
+ |
+ if (!AddItemLength(index, item_length)) |
+ return; |
+ |
+ if (--pending_get_file_info_count_ == 0) |
+ OnSizeCounted(); |
+} |
+ |
+int BlobReader::DoRead() { |
+ DCHECK(!error_); |
+ DCHECK(pending_read_buf_.get()); |
+ |
+ if (!has_started_reading_) { |
+ has_started_reading_ = true; |
+ if (initial_offset_) |
+ PerformInitialSeek(); |
+ } |
+ |
+ int desired_read_size = pending_read_length_; |
+ |
+ if (remaining_bytes_ < desired_read_size) |
+ desired_read_size = static_cast<int>(remaining_bytes_); |
+ |
+ // If we should copy zero bytes because |remaining_bytes_| is zero, short |
+ // circuit here. |
+ if (!desired_read_size) { |
+ pending_read_buf_ = NULL; |
+ return 0; |
+ } |
+ |
+ // Keep track of the buffer. |
+ DCHECK(!read_buf_.get()); |
+ read_buf_ = new net::DrainableIOBuffer(pending_read_buf_, desired_read_size); |
+ pending_read_buf_ = NULL; |
+ |
+ return ReadLoop(); |
+} |
+ |
+void BlobReader::PerformInitialSeek() { |
+ remaining_bytes_ -= initial_offset_; |
+ if (remaining_bytes_ < 0) |
+ remaining_bytes_ = 0; |
+ |
+ int64 offset = initial_offset_; |
+ |
+ // Private method to skip the initial items when reading from |
+ // an offset. |
+ for (current_item_index_ = 0; |
+ current_item_index_ < blob_data_->items().size() && |
+ offset >= item_length_list_[current_item_index_]; |
+ ++current_item_index_) { |
+ offset -= item_length_list_[current_item_index_]; |
+ } |
+ |
+ // Set the offset that need to jump to for the first item in the range. |
+ current_item_offset_ = offset; |
+ |
+ if (offset == 0) |
+ return; |
+ |
+ // Adjust the offset of the first stream if it is of file type. |
+ const BlobData::Item& item = blob_data_->items().at(current_item_index_); |
+ if (IsFileType(item.type())) { |
+ DeleteCurrentFileReader(); |
+ CreateFileStreamReader(current_item_index_, offset); |
+ } |
+} |
+ |
+ |
+int BlobReader::ReadLoop() { |
+ // Read until we encounter an error or could not get the data immediately. |
+ while (remaining_bytes_ > 0 && read_buf_->BytesRemaining() > 0) { |
+ if (!ReadItem()) |
+ return error_ ? error_ : net::ERR_IO_PENDING; |
+ } |
+ return OnReadComplete(); |
+} |
+ |
+bool BlobReader::ReadItem() { |
+ // Are we done with reading all the blob data? |
+ if (remaining_bytes_ == 0) |
+ return true; |
+ |
+ // If we get to the last item but still expect something to read, bail out |
+ // since something is wrong. |
+ if (current_item_index_ >= blob_data_->items().size()) { |
+ OnError(net::ERR_FAILED); |
+ return false; |
+ } |
+ |
+ // Compute the bytes to read for current item. |
+ int bytes_to_read = ComputeBytesToRead(); |
+ |
+ // If nothing to read for current item, advance to next item. |
+ if (bytes_to_read == 0) { |
+ AdvanceItem(); |
+ return ReadItem(); |
+ } |
+ |
+ // Do the reading. |
+ const BlobData::Item& item = blob_data_->items().at(current_item_index_); |
+ if (item.type() == BlobData::Item::TYPE_BYTES) |
+ return ReadBytesItem(item, bytes_to_read); |
+ if (IsFileType(item.type())) { |
+ return ReadFileItem(GetFileStreamReader(current_item_index_), |
+ bytes_to_read); |
+ } |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+void BlobReader::AdvanceItem() { |
+ // Close the file if the current item is a file. |
+ DeleteCurrentFileReader(); |
+ |
+ // Advance to the next item. |
+ current_item_index_++; |
+ current_item_offset_ = 0; |
+} |
+ |
+void BlobReader::AdvanceBytesRead(int result) { |
+ DCHECK_GT(result, 0); |
+ |
+ // Do we finish reading the current item? |
+ current_item_offset_ += result; |
+ if (current_item_offset_ == item_length_list_[current_item_index_]) |
+ AdvanceItem(); |
+ |
+ // Subtract the remaining bytes. |
+ remaining_bytes_ -= result; |
+ DCHECK_GE(remaining_bytes_, 0); |
+ |
+ // Adjust the read buffer. |
+ read_buf_->DidConsume(result); |
+ DCHECK_GE(read_buf_->BytesRemaining(), 0); |
+} |
+ |
+bool BlobReader::ReadBytesItem(const BlobData::Item& item, |
+ int bytes_to_read) { |
+ DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
+ |
+ memcpy(read_buf_->data(), |
+ item.bytes() + item.offset() + current_item_offset_, |
+ bytes_to_read); |
+ |
+ AdvanceBytesRead(bytes_to_read); |
+ return true; |
+} |
+ |
+bool BlobReader::ReadFileItem(FileStreamReader* reader, int bytes_to_read) { |
+ DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
+ DCHECK_GT(bytes_to_read, 0); |
+ DCHECK(reader); |
+ const int result = reader->Read( |
+ read_buf_.get(), |
+ bytes_to_read, |
+ base::Bind(&BlobReader::DidReadFile, base::Unretained(this))); |
+ if (result >= 0) { |
+ DidReadFile(result); |
+ return true; |
+ } |
+ if (result == net::ERR_IO_PENDING) |
+ return false; |
+ OnError(result); |
+ return false; |
+} |
+ |
+void BlobReader::DidReadFile(int result) { |
+ if (result <= 0) { |
+ OnError(net::ERR_FAILED); |
+ return; |
+ } |
+ |
+ AdvanceBytesRead(result); |
+ |
+ // If the read buffer is completely filled, we're done. |
+ if (!read_buf_->BytesRemaining()) { |
+ OnReadComplete(); |
+ return; |
+ } |
+ |
+ // Otherwise, continue the reading. |
+ ReadLoop(); |
+} |
+ |
+int BlobReader::ComputeBytesToRead() const { |
+ int64 current_item_length = item_length_list_[current_item_index_]; |
+ int64 item_remaining = current_item_length - current_item_offset_; |
+ int64 buf_remaining = read_buf_->BytesRemaining(); |
+ int64 max_remaining = std::numeric_limits<int>::max(); |
+ int64 min = std::min(std::min(std::min(item_remaining, |
+ buf_remaining), |
+ remaining_bytes_), |
+ max_remaining); |
+ return static_cast<int>(min); |
+} |
+ |
+void BlobReader::OnSizeCounted() { |
+ DCHECK(!error_); |
+ has_total_size_ = true; |
+ remaining_bytes_ = total_size_; |
+ if (!pending_read_callback_.is_null()) { |
+ DoRead(); |
+ } |
+ if (!pending_size_callback_.is_null()) { |
+ net::Int64CompletionCallback callback = pending_size_callback_; |
+ pending_size_callback_.Reset(); |
+ callback.Run(total_size_); |
+ } |
+} |
+ |
+int BlobReader::OnReadComplete() { |
+ int bytes_read = read_buf_->BytesConsumed(); |
+ read_buf_ = NULL; |
+ if (!pending_read_callback_.is_null()) { |
+ net::CompletionCallback callback = pending_read_callback_; |
+ pending_read_callback_.Reset(); |
+ callback.Run(bytes_read); |
+ } |
+ return bytes_read; |
+} |
+ |
+void BlobReader::OnError(int error_code) { |
+ DCHECK(error_code); |
+ if (error_) |
+ return; |
+ error_ = error_code; |
+ if (!pending_read_callback_.is_null()) { |
+ pending_read_callback_.Run(error_code); |
+ pending_read_callback_.Reset(); |
+ } else if (!pending_size_callback_.is_null()) { |
+ pending_size_callback_.Run(error_code); |
+ pending_size_callback_.Reset(); |
+ } |
+} |
+ |
+FileStreamReader* BlobReader::GetFileStreamReader(size_t index) { |
+ DCHECK_LT(index, blob_data_->items().size()); |
+ const BlobData::Item& item = blob_data_->items().at(index); |
+ if (!IsFileType(item.type())) |
+ return NULL; |
+ if (index_to_reader_.find(index) == index_to_reader_.end()) |
+ CreateFileStreamReader(index, 0); |
+ DCHECK(index_to_reader_[index]); |
+ return index_to_reader_[index]; |
+} |
+ |
+void BlobReader::CreateFileStreamReader(size_t index, |
+ int64 additional_offset) { |
+ DCHECK_LT(index, blob_data_->items().size()); |
+ const BlobData::Item& item = blob_data_->items().at(index); |
+ DCHECK(IsFileType(item.type())); |
+ DCHECK_EQ(0U, index_to_reader_.count(index)); |
+ |
+ FileStreamReader* reader = NULL; |
+ switch (item.type()) { |
+ case BlobData::Item::TYPE_FILE: |
+ reader = new LocalFileStreamReader(file_task_runner_.get(), |
+ item.path(), |
+ item.offset() + additional_offset, |
+ item.expected_modification_time()); |
+ break; |
+ case BlobData::Item::TYPE_FILE_FILESYSTEM: |
+ reader = file_system_context_->CreateFileStreamReader( |
+ fileapi::FileSystemURL(file_system_context_->CrackURL(item.url())), |
+ item.offset() + additional_offset, |
+ item.expected_modification_time()).release(); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+ DCHECK(reader); |
+ index_to_reader_[index] = reader; |
+} |
+ |
+void BlobReader::DeleteCurrentFileReader() { |
+ IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_); |
+ if (found != index_to_reader_.end() && found->second) { |
+ delete found->second; |
+ index_to_reader_.erase(found); |
+ } |
+} |
+ |
+} // namespace webkit_blob |