Chromium Code Reviews| 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 "content/child/blob_storage/blob_transport_temporary_holder.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/containers/scoped_ptr_hash_map.h" | |
| 10 #include "base/stl_util.h" | |
| 11 #include "content/child/blob_storage/blob_consolidation.h" | |
| 12 #include "storage/common/blob_storage/blob_item_bytes_request.h" | |
| 13 #include "storage/common/blob_storage/blob_item_bytes_response.h" | |
| 14 #include "storage/common/data_element.h" | |
| 15 | |
| 16 using base::SharedMemory; | |
| 17 using base::SharedMemoryHandle; | |
| 18 using storage::BlobItemBytesRequest; | |
| 19 using storage::BlobItemBytesResponse; | |
| 20 using storage::IPCBlobItemRequestStrategy; | |
| 21 using storage::DataElement; | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | |
| 26 using ReadStatus = BlobConsolidation::ReadStatus; | |
| 27 using ResponsesStatus = BlobTransportTemporaryHolder::ResponsesStatus; | |
| 28 | |
| 29 BlobTransportTemporaryHolder::BlobTransportTemporaryHolder() {} | |
| 30 | |
| 31 BlobTransportTemporaryHolder::~BlobTransportTemporaryHolder() { | |
| 32 STLDeleteValues(&blob_storage_); | |
| 33 } | |
| 34 | |
| 35 bool BlobTransportTemporaryHolder::HoldBlobConsolidation( | |
| 36 const std::string& uuid, | |
| 37 scoped_ptr<BlobConsolidation> consolidation) { | |
| 38 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
| |
| 39 return false; | |
| 40 } | |
| 41 blob_storage_.insert(std::make_pair(uuid, consolidation.release())); | |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 bool BlobTransportTemporaryHolder::GetDescriptions( | |
| 46 const std::string& uuid, | |
| 47 size_t max_data_population, | |
| 48 std::vector<storage::DataElement>* out) { | |
| 49 DCHECK(out->empty()); | |
| 50 BlobConsolidation* consolidation = GetConsolidation(uuid); | |
| 51 if (!consolidation) { | |
| 52 return false; | |
| 53 } | |
| 54 const auto& consolidated_items = consolidation->consolidated_items(); | |
| 55 | |
| 56 size_t current_memory_population = 0; | |
| 57 size_t current_item = 0; | |
| 58 out->reserve(consolidated_items.size()); | |
| 59 for (const ConsolidatedItem& item : consolidated_items) { | |
| 60 out->push_back(DataElement()); | |
| 61 auto& element = out->back(); | |
| 62 | |
| 63 switch (item.type) { | |
| 64 case DataElement::TYPE_BYTES: { | |
| 65 size_t bytes_length = static_cast<size_t>(item.length); | |
| 66 if (current_memory_population + bytes_length <= max_data_population) { | |
| 67 element.SetToAllocatedBytes(bytes_length); | |
| 68 consolidation->ReadMemory(current_item, 0, bytes_length, | |
| 69 element.mutable_bytes()); | |
| 70 current_memory_population += bytes_length; | |
| 71 } else { | |
| 72 element.SetToBytesDescription(bytes_length); | |
| 73 } | |
| 74 break; | |
| 75 } | |
| 76 case DataElement::TYPE_FILE: { | |
| 77 element.SetToFilePathRange( | |
| 78 item.path, item.offset, item.length, | |
| 79 base::Time::FromDoubleT(item.expected_modification_time)); | |
| 80 break; | |
| 81 } | |
| 82 case DataElement::TYPE_BLOB: { | |
| 83 element.SetToBlobRange(item.blob_uuid, item.offset, item.length); | |
| 84 break; | |
| 85 } | |
| 86 case DataElement::TYPE_FILE_FILESYSTEM: { | |
| 87 element.SetToFileSystemUrlRange( | |
| 88 item.filesystem_url, item.offset, item.length, | |
| 89 base::Time::FromDoubleT(item.expected_modification_time)); | |
| 90 break; | |
| 91 } | |
| 92 case DataElement::TYPE_DISK_CACHE_ENTRY: | |
| 93 case DataElement::TYPE_BYTES_DESCRIPTION: | |
| 94 case DataElement::TYPE_UNKNOWN: | |
| 95 NOTREACHED(); | |
| 96 } | |
| 97 ++current_item; | |
| 98 } | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 ResponsesStatus BlobTransportTemporaryHolder::GetResponses( | |
| 103 const std::string& uuid, | |
| 104 const std::vector<BlobItemBytesRequest>& requests, | |
| 105 std::vector<SharedMemoryHandle>* memory_handles, | |
| 106 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
| 107 std::vector<BlobItemBytesResponse>* out) { | |
| 108 DCHECK(out->empty()); | |
| 109 BlobConsolidation* consolidation = GetConsolidation(uuid); | |
| 110 if (!consolidation) | |
| 111 return ResponsesStatus::BLOB_NOT_FOUND; | |
| 112 const auto& consolidated_items = consolidation->consolidated_items(); | |
| 113 | |
| 114 base::ScopedPtrHashMap<size_t, scoped_ptr<SharedMemory>> opened_memory; | |
| 115 for (const BlobItemBytesRequest& request : requests) { | |
| 116 if (request.renderer_item_index >= consolidated_items.size()) | |
| 117 return ResponsesStatus::INVALID_ITEM_INDEX; | |
| 118 | |
| 119 const ConsolidatedItem& item = | |
| 120 consolidated_items[request.renderer_item_index]; | |
| 121 if (request.renderer_item_offset + request.size > item.length) | |
| 122 return ResponsesStatus::INVALID_DATA_RANGE; | |
| 123 if (item.type != DataElement::TYPE_BYTES) | |
| 124 return ResponsesStatus::INVALID_ITEM; | |
| 125 | |
| 126 out->push_back(BlobItemBytesResponse(request.request_number)); | |
| 127 switch (request.transport_strategy) { | |
| 128 case IPCBlobItemRequestStrategy::IPC: { | |
| 129 BlobItemBytesResponse& response = out->back(); | |
| 130 ReadStatus status = consolidation->ReadMemory( | |
| 131 request.renderer_item_index, request.renderer_item_offset, | |
| 132 request.size, response.allocate_mutable_data(request.size)); | |
| 133 CHECK(status == ReadStatus::OK) | |
| 134 << "Error reading from consolidated blob: " | |
| 135 << static_cast<int>(status); | |
| 136 break; | |
| 137 } | |
| 138 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
| 139 if (request.handle_index >= memory_handles->size()) { | |
| 140 DCHECK_LT(request.handle_index, memory_handles->size()); | |
| 141 return ResponsesStatus::INVALID_HANDLE_INDEX; | |
| 142 } | |
| 143 SharedMemory* memory = nullptr; | |
| 144 auto memory_it = opened_memory.find(request.handle_index); | |
| 145 if (memory_it == opened_memory.end()) { | |
| 146 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
| 147 DCHECK(SharedMemory::IsHandleValid(handle)); | |
| 148 scoped_ptr<SharedMemory> shared_memory( | |
| 149 new SharedMemory(handle, false)); | |
| 150 if (!shared_memory->Map(request.size)) { | |
| 151 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; | |
| 152 } | |
| 153 memory = shared_memory.get(); | |
| 154 opened_memory.add(request.handle_index, shared_memory.Pass()); | |
| 155 } else { | |
| 156 memory = memory_it->second; | |
| 157 } | |
| 158 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
| 159 ReadStatus status = consolidation->ReadMemory( | |
| 160 request.renderer_item_index, request.renderer_item_offset, | |
| 161 request.size, | |
| 162 static_cast<char*>(memory->memory()) + request.handle_offset); | |
| 163 CHECK(status == ReadStatus::OK) | |
| 164 << "Error reading from consolidated blob: " | |
| 165 << static_cast<int>(status); | |
| 166 break; | |
| 167 } | |
| 168 case IPCBlobItemRequestStrategy::FILE: | |
| 169 NOTREACHED() << "TODO(dmurph): Not implemented."; | |
| 170 break; | |
| 171 case IPCBlobItemRequestStrategy::UNKNOWN: | |
| 172 NOTREACHED(); | |
| 173 break; | |
| 174 } | |
| 175 } | |
| 176 return ResponsesStatus::SUCCESS; | |
| 177 } | |
| 178 | |
| 179 void BlobTransportTemporaryHolder::ReleaseBlob(const std::string& uuid) { | |
| 180 auto iter = blob_storage_.find(uuid); | |
| 181 if (iter != blob_storage_.end()) { | |
| 182 BlobConsolidation* consolidation = iter->second; | |
| 183 delete consolidation; | |
| 184 blob_storage_.erase(iter); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 BlobConsolidation* BlobTransportTemporaryHolder::GetConsolidation( | |
| 189 const std::string& uuid) { | |
| 190 auto iter = blob_storage_.find(uuid); | |
| 191 if (iter == blob_storage_.end()) { | |
| 192 return nullptr; | |
| 193 } else { | |
| 194 return iter->second; | |
| 195 } | |
|
michaeln
2015/10/21 02:22:24
style nit: maybe could be more early returnish wit
dmurph
2015/10/21 21:47:57
Done.
| |
| 196 } | |
| 197 | |
| 198 } // namespace content | |
| OLD | NEW |