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_controller.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/memory/shared_memory.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "content/child/blob_storage/blob_consolidation.h" |
| 14 #include "content/child/thread_safe_sender.h" |
| 15 #include "ipc/ipc_sender.h" |
| 16 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
| 17 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
| 18 #include "storage/common/data_element.h" |
| 19 |
| 20 using base::SharedMemory; |
| 21 using base::SharedMemoryHandle; |
| 22 using storage::BlobItemBytesRequest; |
| 23 using storage::BlobItemBytesResponse; |
| 24 using storage::IPCBlobItemRequestStrategy; |
| 25 using storage::DataElement; |
| 26 |
| 27 namespace content { |
| 28 |
| 29 using storage::IPCBlobCreationCancelCode; |
| 30 |
| 31 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
| 32 using ReadStatus = BlobConsolidation::ReadStatus; |
| 33 |
| 34 namespace { |
| 35 const size_t kLargeThresholdBytes = 250 * 1024; |
| 36 static base::LazyInstance<BlobTransportController> g_controller = |
| 37 LAZY_INSTANCE_INITIALIZER; |
| 38 } // namespace |
| 39 |
| 40 BlobTransportController* BlobTransportController::GetInstance() { |
| 41 return g_controller.Pointer(); |
| 42 } |
| 43 |
| 44 BlobTransportController::~BlobTransportController() {} |
| 45 |
| 46 void BlobTransportController::InitiateBlobTransfer( |
| 47 const std::string& uuid, |
| 48 const std::string& type, |
| 49 scoped_ptr<BlobConsolidation> consolidation, |
| 50 IPC::Sender* sender) { |
| 51 BlobConsolidation* consolidation_ptr = consolidation.get(); |
| 52 blob_storage_.insert(uuid, consolidation.Pass()); |
| 53 std::vector<storage::DataElement> descriptions; |
| 54 GetDescriptions(consolidation_ptr, kLargeThresholdBytes, &descriptions); |
| 55 // TODO(dmurph): Uncomment when IPC messages are added. |
| 56 // sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, type, |
| 57 // descriptions)); |
| 58 } |
| 59 |
| 60 void BlobTransportController::OnMemoryRequest( |
| 61 const std::string& uuid, |
| 62 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 63 std::vector<base::SharedMemoryHandle>* memory_handles, |
| 64 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 65 IPC::Sender* sender) { |
| 66 std::vector<storage::BlobItemBytesResponse> responses; |
| 67 ResponsesStatus status = |
| 68 GetResponses(uuid, requests, memory_handles, file_handles, &responses); |
| 69 |
| 70 switch (status) { |
| 71 case ResponsesStatus::BLOB_NOT_FOUND: |
| 72 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, |
| 73 // IPCBlobCreationCancelCode::UNKNOWN)); |
| 74 return; |
| 75 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: |
| 76 // This would happen if the renderer process doesn't have enough memory |
| 77 // to map the shared memory, which is possible if we don't have much |
| 78 // memory. If this scenario happens often, we could delay the response |
| 79 // until we have enough memory. For now we just fail. |
| 80 CHECK(false) << "Unable to map shared memory to send blob " << uuid |
| 81 << "."; |
| 82 break; |
| 83 case ResponsesStatus::SUCCESS: |
| 84 break; |
| 85 } |
| 86 |
| 87 // TODO(dmurph): Uncomment when IPC messages are added. |
| 88 // sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); |
| 89 } |
| 90 |
| 91 void BlobTransportController::OnCancel( |
| 92 const std::string& uuid, |
| 93 storage::IPCBlobCreationCancelCode code) { |
| 94 DVLOG(1) << "Received blob cancel for blob " << uuid << " with reason:"; |
| 95 switch (code) { |
| 96 case IPCBlobCreationCancelCode::UNKNOWN: |
| 97 DVLOG(1) << "Unknown."; |
| 98 break; |
| 99 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: |
| 100 DVLOG(1) << "Out of Memory."; |
| 101 break; |
| 102 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: |
| 103 DVLOG(1) << "File Write Failed (Invalid cancel reason!)."; |
| 104 break; |
| 105 } |
| 106 ReleaseBlobConsolidation(uuid); |
| 107 } |
| 108 |
| 109 void BlobTransportController::OnDone(const std::string& uuid) { |
| 110 ReleaseBlobConsolidation(uuid); |
| 111 } |
| 112 |
| 113 void BlobTransportController::Clear() { |
| 114 blob_storage_.clear(); |
| 115 } |
| 116 |
| 117 BlobTransportController::BlobTransportController() {} |
| 118 |
| 119 void BlobTransportController::CancelBlobTransfer(const std::string& uuid, |
| 120 IPCBlobCreationCancelCode code, |
| 121 IPC::Sender* sender) { |
| 122 // TODO(dmurph): Uncomment when IPC messages are added. |
| 123 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, code)); |
| 124 ReleaseBlobConsolidation(uuid); |
| 125 } |
| 126 |
| 127 void BlobTransportController::GetDescriptions( |
| 128 BlobConsolidation* consolidation, |
| 129 size_t max_data_population, |
| 130 std::vector<storage::DataElement>* out) { |
| 131 DCHECK(out->empty()); |
| 132 DCHECK(consolidation); |
| 133 const auto& consolidated_items = consolidation->consolidated_items(); |
| 134 |
| 135 size_t current_memory_population = 0; |
| 136 size_t current_item = 0; |
| 137 out->reserve(consolidated_items.size()); |
| 138 for (const ConsolidatedItem& item : consolidated_items) { |
| 139 out->push_back(DataElement()); |
| 140 auto& element = out->back(); |
| 141 |
| 142 switch (item.type) { |
| 143 case DataElement::TYPE_BYTES: { |
| 144 size_t bytes_length = static_cast<size_t>(item.length); |
| 145 if (current_memory_population + bytes_length <= max_data_population) { |
| 146 element.SetToAllocatedBytes(bytes_length); |
| 147 consolidation->ReadMemory(current_item, 0, bytes_length, |
| 148 element.mutable_bytes()); |
| 149 current_memory_population += bytes_length; |
| 150 } else { |
| 151 element.SetToBytesDescription(bytes_length); |
| 152 } |
| 153 break; |
| 154 } |
| 155 case DataElement::TYPE_FILE: { |
| 156 element.SetToFilePathRange( |
| 157 item.path, item.offset, item.length, |
| 158 base::Time::FromDoubleT(item.expected_modification_time)); |
| 159 break; |
| 160 } |
| 161 case DataElement::TYPE_BLOB: { |
| 162 element.SetToBlobRange(item.blob_uuid, item.offset, item.length); |
| 163 break; |
| 164 } |
| 165 case DataElement::TYPE_FILE_FILESYSTEM: { |
| 166 element.SetToFileSystemUrlRange( |
| 167 item.filesystem_url, item.offset, item.length, |
| 168 base::Time::FromDoubleT(item.expected_modification_time)); |
| 169 break; |
| 170 } |
| 171 case DataElement::TYPE_DISK_CACHE_ENTRY: |
| 172 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 173 case DataElement::TYPE_UNKNOWN: |
| 174 NOTREACHED(); |
| 175 } |
| 176 ++current_item; |
| 177 } |
| 178 } |
| 179 |
| 180 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( |
| 181 const std::string& uuid, |
| 182 const std::vector<BlobItemBytesRequest>& requests, |
| 183 std::vector<SharedMemoryHandle>* memory_handles, |
| 184 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 185 std::vector<BlobItemBytesResponse>* out) { |
| 186 DCHECK(out->empty()); |
| 187 auto it = blob_storage_.find(uuid); |
| 188 if (it == blob_storage_.end()) |
| 189 return ResponsesStatus::BLOB_NOT_FOUND; |
| 190 |
| 191 BlobConsolidation* consolidation = it->second; |
| 192 const auto& consolidated_items = consolidation->consolidated_items(); |
| 193 |
| 194 // Since we can be writing to the same shared memory handle from multiple |
| 195 // requests, we keep them in a vector and lazily create them. |
| 196 ScopedVector<SharedMemory> opened_memory; |
| 197 opened_memory.resize(memory_handles->size()); |
| 198 for (const BlobItemBytesRequest& request : requests) { |
| 199 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) |
| 200 << "Invalid item index"; |
| 201 |
| 202 const ConsolidatedItem& item = |
| 203 consolidated_items[request.renderer_item_index]; |
| 204 DCHECK_LE(request.renderer_item_offset + request.size, item.length) |
| 205 << "Invalid data range"; |
| 206 DCHECK_EQ(item.type, DataElement::TYPE_BYTES) << "Invalid element type"; |
| 207 |
| 208 out->push_back(BlobItemBytesResponse(request.request_number)); |
| 209 switch (request.transport_strategy) { |
| 210 case IPCBlobItemRequestStrategy::IPC: { |
| 211 BlobItemBytesResponse& response = out->back(); |
| 212 ReadStatus status = consolidation->ReadMemory( |
| 213 request.renderer_item_index, request.renderer_item_offset, |
| 214 request.size, response.allocate_mutable_data(request.size)); |
| 215 DCHECK(status == ReadStatus::OK) |
| 216 << "Error reading from consolidated blob: " |
| 217 << static_cast<int>(status); |
| 218 break; |
| 219 } |
| 220 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { |
| 221 DCHECK_LT(request.handle_index, memory_handles->size()) |
| 222 << "Invalid handle index."; |
| 223 SharedMemory* memory = opened_memory[request.handle_index]; |
| 224 if (!memory) { |
| 225 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; |
| 226 DCHECK(SharedMemory::IsHandleValid(handle)); |
| 227 scoped_ptr<SharedMemory> shared_memory( |
| 228 new SharedMemory(handle, false)); |
| 229 if (!shared_memory->Map(request.size)) |
| 230 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; |
| 231 memory = shared_memory.get(); |
| 232 opened_memory[request.handle_index] = shared_memory.release(); |
| 233 } |
| 234 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; |
| 235 ReadStatus status = consolidation->ReadMemory( |
| 236 request.renderer_item_index, request.renderer_item_offset, |
| 237 request.size, |
| 238 static_cast<char*>(memory->memory()) + request.handle_offset); |
| 239 DCHECK(status == ReadStatus::OK) |
| 240 << "Error reading from consolidated blob: " |
| 241 << static_cast<int>(status); |
| 242 break; |
| 243 } |
| 244 case IPCBlobItemRequestStrategy::FILE: |
| 245 NOTREACHED() << "TODO(dmurph): Not implemented."; |
| 246 break; |
| 247 case IPCBlobItemRequestStrategy::UNKNOWN: |
| 248 NOTREACHED(); |
| 249 break; |
| 250 } |
| 251 } |
| 252 return ResponsesStatus::SUCCESS; |
| 253 } |
| 254 |
| 255 void BlobTransportController::ReleaseBlobConsolidation( |
| 256 const std::string& uuid) { |
| 257 blob_storage_.erase(uuid); |
| 258 } |
| 259 |
| 260 } // namespace content |
OLD | NEW |