Chromium Code Reviews| Index: content/child/blob_storage/blob_transport_temporary_holder.cc |
| diff --git a/content/child/blob_storage/blob_transport_temporary_holder.cc b/content/child/blob_storage/blob_transport_temporary_holder.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c49ffc686b1af3552957f44233dfc098b730914 |
| --- /dev/null |
| +++ b/content/child/blob_storage/blob_transport_temporary_holder.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2015 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 "content/child/blob_storage/blob_transport_temporary_holder.h" |
| + |
| +#include <utility> |
| + |
| +#include "base/containers/scoped_ptr_hash_map.h" |
| +#include "base/stl_util.h" |
| +#include "content/child/blob_storage/blob_consolidation.h" |
| +#include "storage/common/blob_storage/blob_item_bytes_request.h" |
| +#include "storage/common/blob_storage/blob_item_bytes_response.h" |
| +#include "storage/common/data_element.h" |
| + |
| +using base::SharedMemory; |
| +using base::SharedMemoryHandle; |
| +using storage::BlobItemBytesRequest; |
| +using storage::BlobItemBytesResponse; |
| +using storage::IPCBlobItemRequestStrategy; |
| +using storage::DataElement; |
| + |
| +namespace content { |
| + |
| +using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
| +using ReadStatus = BlobConsolidation::ReadStatus; |
| +using ResponsesStatus = BlobTransportTemporaryHolder::ResponsesStatus; |
| + |
| +BlobTransportTemporaryHolder::BlobTransportTemporaryHolder() {} |
| + |
| +BlobTransportTemporaryHolder::~BlobTransportTemporaryHolder() { |
| + STLDeleteValues(&blob_storage_); |
| +} |
| + |
| +bool BlobTransportTemporaryHolder::HoldBlobConsolidation( |
| + const std::string& uuid, |
| + scoped_ptr<BlobConsolidation> consolidation) { |
| + if (blob_storage_.find(uuid) != blob_storage_.end()) { |
|
michaeln
2015/10/21 02:22:24
probably a DCHECK(blob_storage_.find(uuid) != blob
dmurph
2015/10/21 21:47:57
I want to make sure we exit early in production in
|
| + return false; |
| + } |
| + blob_storage_.insert(std::make_pair(uuid, consolidation.release())); |
| + return true; |
| +} |
| + |
| +bool BlobTransportTemporaryHolder::GetDescriptions( |
| + const std::string& uuid, |
| + size_t max_data_population, |
| + std::vector<storage::DataElement>* out) { |
| + DCHECK(out->empty()); |
| + BlobConsolidation* consolidation = GetConsolidation(uuid); |
| + if (!consolidation) { |
| + return false; |
| + } |
| + const auto& consolidated_items = consolidation->consolidated_items(); |
| + |
| + size_t current_memory_population = 0; |
| + size_t current_item = 0; |
| + out->reserve(consolidated_items.size()); |
| + for (const ConsolidatedItem& item : consolidated_items) { |
| + out->push_back(DataElement()); |
| + auto& element = out->back(); |
| + |
| + switch (item.type) { |
| + case DataElement::TYPE_BYTES: { |
| + size_t bytes_length = static_cast<size_t>(item.length); |
| + if (current_memory_population + bytes_length <= max_data_population) { |
| + element.SetToAllocatedBytes(bytes_length); |
| + consolidation->ReadMemory(current_item, 0, bytes_length, |
| + element.mutable_bytes()); |
| + current_memory_population += bytes_length; |
| + } else { |
| + element.SetToBytesDescription(bytes_length); |
| + } |
| + break; |
| + } |
| + case DataElement::TYPE_FILE: { |
| + element.SetToFilePathRange( |
| + item.path, item.offset, item.length, |
| + base::Time::FromDoubleT(item.expected_modification_time)); |
| + break; |
| + } |
| + case DataElement::TYPE_BLOB: { |
| + element.SetToBlobRange(item.blob_uuid, item.offset, item.length); |
| + break; |
| + } |
| + case DataElement::TYPE_FILE_FILESYSTEM: { |
| + element.SetToFileSystemUrlRange( |
| + item.filesystem_url, item.offset, item.length, |
| + base::Time::FromDoubleT(item.expected_modification_time)); |
| + break; |
| + } |
| + case DataElement::TYPE_DISK_CACHE_ENTRY: |
| + case DataElement::TYPE_BYTES_DESCRIPTION: |
| + case DataElement::TYPE_UNKNOWN: |
| + NOTREACHED(); |
| + } |
| + ++current_item; |
| + } |
| + return true; |
| +} |
| + |
| +ResponsesStatus BlobTransportTemporaryHolder::GetResponses( |
| + const std::string& uuid, |
| + const std::vector<BlobItemBytesRequest>& requests, |
| + std::vector<SharedMemoryHandle>* memory_handles, |
| + const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| + std::vector<BlobItemBytesResponse>* out) { |
| + DCHECK(out->empty()); |
| + BlobConsolidation* consolidation = GetConsolidation(uuid); |
| + if (!consolidation) |
| + return ResponsesStatus::BLOB_NOT_FOUND; |
| + const auto& consolidated_items = consolidation->consolidated_items(); |
| + |
| + base::ScopedPtrHashMap<size_t, scoped_ptr<SharedMemory>> opened_memory; |
| + for (const BlobItemBytesRequest& request : requests) { |
| + if (request.renderer_item_index >= consolidated_items.size()) |
| + return ResponsesStatus::INVALID_ITEM_INDEX; |
| + |
| + const ConsolidatedItem& item = |
| + consolidated_items[request.renderer_item_index]; |
| + if (request.renderer_item_offset + request.size > item.length) |
| + return ResponsesStatus::INVALID_DATA_RANGE; |
| + if (item.type != DataElement::TYPE_BYTES) |
| + return ResponsesStatus::INVALID_ITEM; |
| + |
| + out->push_back(BlobItemBytesResponse(request.request_number)); |
| + switch (request.transport_strategy) { |
| + case IPCBlobItemRequestStrategy::IPC: { |
| + BlobItemBytesResponse& response = out->back(); |
| + ReadStatus status = consolidation->ReadMemory( |
| + request.renderer_item_index, request.renderer_item_offset, |
| + request.size, response.allocate_mutable_data(request.size)); |
| + CHECK(status == ReadStatus::OK) |
| + << "Error reading from consolidated blob: " |
| + << static_cast<int>(status); |
| + break; |
| + } |
| + case IPCBlobItemRequestStrategy::SHARED_MEMORY: { |
| + if (request.handle_index >= memory_handles->size()) { |
| + DCHECK_LT(request.handle_index, memory_handles->size()); |
| + return ResponsesStatus::INVALID_HANDLE_INDEX; |
| + } |
| + SharedMemory* memory = nullptr; |
| + auto memory_it = opened_memory.find(request.handle_index); |
| + if (memory_it == opened_memory.end()) { |
| + SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; |
| + DCHECK(SharedMemory::IsHandleValid(handle)); |
| + scoped_ptr<SharedMemory> shared_memory( |
| + new SharedMemory(handle, false)); |
| + if (!shared_memory->Map(request.size)) { |
| + return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; |
| + } |
| + memory = shared_memory.get(); |
| + opened_memory.add(request.handle_index, shared_memory.Pass()); |
| + } else { |
| + memory = memory_it->second; |
| + } |
| + CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; |
| + ReadStatus status = consolidation->ReadMemory( |
| + request.renderer_item_index, request.renderer_item_offset, |
| + request.size, |
| + static_cast<char*>(memory->memory()) + request.handle_offset); |
| + CHECK(status == ReadStatus::OK) |
| + << "Error reading from consolidated blob: " |
| + << static_cast<int>(status); |
| + break; |
| + } |
| + case IPCBlobItemRequestStrategy::FILE: |
| + NOTREACHED() << "TODO(dmurph): Not implemented."; |
| + break; |
| + case IPCBlobItemRequestStrategy::UNKNOWN: |
| + NOTREACHED(); |
| + break; |
| + } |
| + } |
| + return ResponsesStatus::SUCCESS; |
| +} |
| + |
| +void BlobTransportTemporaryHolder::ReleaseBlob(const std::string& uuid) { |
| + auto iter = blob_storage_.find(uuid); |
| + if (iter != blob_storage_.end()) { |
| + BlobConsolidation* consolidation = iter->second; |
| + delete consolidation; |
| + blob_storage_.erase(iter); |
| + } |
| +} |
| + |
| +BlobConsolidation* BlobTransportTemporaryHolder::GetConsolidation( |
| + const std::string& uuid) { |
| + auto iter = blob_storage_.find(uuid); |
| + if (iter == blob_storage_.end()) { |
| + return nullptr; |
| + } else { |
| + return iter->second; |
| + } |
|
michaeln
2015/10/21 02:22:24
style nit: maybe could be more early returnish wit
dmurph
2015/10/21 21:47:57
Done.
|
| +} |
| + |
| +} // namespace content |