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