Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/blob_storage/blob_transport_controller.h" | 5 #include "content/child/blob_storage/blob_transport_controller.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 8 #include <memory> | |
| 7 #include <utility> | 9 #include <utility> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 12 #include "base/bind.h" | |
| 13 #include "base/bind_helpers.h" | |
| 14 #include "base/callback.h" | |
| 15 #include "base/files/file.h" | |
| 10 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/location.h" | |
| 18 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
| 12 #include "base/memory/shared_memory.h" | 20 #include "base/memory/shared_memory.h" |
| 21 #include "base/metrics/histogram_macros.h" | |
| 22 #include "base/optional.h" | |
| 13 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stl_util.h" | 24 #include "base/stl_util.h" |
| 25 #include "base/task_runner.h" | |
| 26 #include "base/task_runner_util.h" | |
| 27 #include "base/thread_task_runner_handle.h" | |
| 28 #include "base/time/time.h" | |
| 15 #include "content/child/blob_storage/blob_consolidation.h" | 29 #include "content/child/blob_storage/blob_consolidation.h" |
| 16 #include "content/child/child_process.h" | 30 #include "content/child/child_process.h" |
| 17 #include "content/child/thread_safe_sender.h" | 31 #include "content/child/thread_safe_sender.h" |
| 18 #include "content/common/fileapi/webblob_messages.h" | 32 #include "content/common/fileapi/webblob_messages.h" |
| 33 #include "ipc/ipc_message.h" | |
| 19 #include "ipc/ipc_sender.h" | 34 #include "ipc/ipc_sender.h" |
| 20 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 35 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
| 21 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 36 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
| 22 #include "storage/common/data_element.h" | 37 #include "storage/common/data_element.h" |
| 23 #include "third_party/WebKit/public/platform/Platform.h" | 38 #include "third_party/WebKit/public/platform/Platform.h" |
| 24 | 39 |
| 40 using base::File; | |
| 25 using base::SharedMemory; | 41 using base::SharedMemory; |
| 26 using base::SharedMemoryHandle; | 42 using base::SharedMemoryHandle; |
| 27 using storage::BlobItemBytesRequest; | 43 using storage::BlobItemBytesRequest; |
| 28 using storage::BlobItemBytesResponse; | 44 using storage::BlobItemBytesResponse; |
| 29 using storage::IPCBlobItemRequestStrategy; | 45 using storage::IPCBlobItemRequestStrategy; |
| 30 using storage::DataElement; | 46 using storage::DataElement; |
| 31 using storage::kBlobStorageIPCThresholdBytes; | 47 using storage::kBlobStorageIPCThresholdBytes; |
| 32 | 48 |
| 33 namespace content { | 49 using storage::BlobItemBytesResponse; |
| 34 | 50 using storage::BlobItemBytesRequest; |
| 35 using storage::IPCBlobCreationCancelCode; | 51 using storage::IPCBlobCreationCancelCode; |
| 36 | 52 |
| 53 namespace content { | |
| 37 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | 54 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
| 38 using ReadStatus = BlobConsolidation::ReadStatus; | 55 using ReadStatus = BlobConsolidation::ReadStatus; |
| 39 | 56 |
| 40 namespace { | 57 namespace { |
| 41 static base::LazyInstance<BlobTransportController>::Leaky g_controller = | 58 static base::LazyInstance<BlobTransportController>::Leaky g_controller = |
| 42 LAZY_INSTANCE_INITIALIZER; | 59 LAZY_INSTANCE_INITIALIZER; |
| 43 | 60 |
| 44 // This keeps the process alive while blobs are being transferred. | 61 // This keeps the process alive while blobs are being transferred. |
| 45 // These need to be called on the main thread. | 62 // These need to be called on the main thread. |
| 46 void IncChildProcessRefCount() { | 63 void IncChildProcessRefCount() { |
| 47 blink::Platform::current()->suddenTerminationChanged(false); | 64 blink::Platform::current()->suddenTerminationChanged(false); |
| 48 ChildProcess::current()->AddRefProcess(); | 65 ChildProcess::current()->AddRefProcess(); |
| 49 } | 66 } |
| 50 | 67 |
| 51 void DecChildProcessRefCount() { | 68 void DecChildProcessRefCount() { |
| 52 blink::Platform::current()->suddenTerminationChanged(true); | 69 blink::Platform::current()->suddenTerminationChanged(true); |
| 53 ChildProcess::current()->ReleaseProcess(); | 70 ChildProcess::current()->ReleaseProcess(); |
| 54 } | 71 } |
| 72 | |
| 73 void DecChildProcessRefCountTimes(size_t times) { | |
| 74 for (size_t i = 0; i < times; i++) { | |
| 75 blink::Platform::current()->suddenTerminationChanged(true); | |
| 76 ChildProcess::current()->ReleaseProcess(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 bool WriteSingleChunk(base::File* file, const char* memory, size_t size) { | |
| 81 size_t written = 0; | |
| 82 size_t max_int = static_cast<size_t>(std::numeric_limits<int>::max()); | |
| 83 while (written < size) { | |
| 84 size_t writing_size = std::min(max_int, size - written); | |
| 85 int actual_written = | |
| 86 file->WriteAtCurrentPos(memory, static_cast<int>(writing_size)); | |
| 87 if (actual_written < 0) { | |
| 88 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.RendererFileWriteFailed", true); | |
| 89 return false; | |
| 90 } | |
| 91 written += writing_size; | |
|
michaeln
2016/04/28 00:16:56
should this be written += actual_written?
and wha
dmurph
2016/05/09 19:55:47
how would that happen? We wouldn't be calling file
michaeln
2016/05/09 22:24:52
right, let me be more clear
What if (actual_writt
michaeln
2016/05/09 23:01:08
nm, i see you changed it to += actual_written :)
| |
| 92 } | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 base::Optional<base::Time> WriteSingleRequestToDisk( | |
| 97 const BlobConsolidation* consolidation, | |
| 98 const BlobItemBytesRequest& request, | |
| 99 const IPC::PlatformFileForTransit& transit_file) { | |
| 100 File file = IPC::PlatformFileForTransitToFile(transit_file); | |
| 101 if (!file.IsValid()) | |
| 102 return base::nullopt; | |
| 103 CHECK_LE(request.handle_offset, | |
| 104 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())); | |
| 105 int64_t seek_distance = | |
| 106 file.Seek(File::FROM_BEGIN, static_cast<int64_t>(request.handle_offset)); | |
| 107 if (seek_distance < 0) { | |
| 108 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.RendererFileSeekFailed", true); | |
| 109 return base::nullopt; | |
| 110 } | |
| 111 BlobConsolidation::ReadStatus status = consolidation->VisitMemory( | |
| 112 request.renderer_item_index, request.renderer_item_offset, request.size, | |
| 113 base::Bind(&WriteSingleChunk, &file)); | |
| 114 if (status != BlobConsolidation::ReadStatus::OK) | |
| 115 return base::nullopt; | |
| 116 File::Info info; | |
| 117 file.GetInfo(&info); | |
| 118 // We need to release the file so we don't automatically close the file. | |
|
michaeln
2016/04/28 00:16:57
i'm curious, when do the file handles get closed?
dmurph
2016/05/09 19:55:47
Good point. I changed this to accept a file, and I
| |
| 119 file.TakePlatformFile(); | |
| 120 return base::make_optional(info.last_modified); | |
| 121 } | |
| 122 | |
| 123 // This returns either the responses, or if they're empty, an error code. | |
| 124 std::pair<std::vector<storage::BlobItemBytesResponse>, | |
| 125 IPCBlobCreationCancelCode> | |
| 126 WriteDiskRequests( | |
| 127 scoped_refptr<BlobConsolidation> consolidation, | |
| 128 std::unique_ptr<std::vector<BlobItemBytesRequest>> requests, | |
| 129 const std::vector<IPC::PlatformFileForTransit>& file_handles) { | |
| 130 std::vector<BlobItemBytesResponse> responses; | |
| 131 std::vector<base::Time> last_modified_times; | |
| 132 last_modified_times.resize(file_handles.size()); | |
| 133 for (const auto& request : *requests) { | |
| 134 base::Optional<base::Time> last_modified = WriteSingleRequestToDisk( | |
| 135 consolidation.get(), request, file_handles[request.handle_index]); | |
| 136 if (!last_modified) { | |
| 137 return std::make_pair(std::vector<storage::BlobItemBytesResponse>(), | |
| 138 IPCBlobCreationCancelCode::FILE_WRITE_FAILED); | |
| 139 } | |
| 140 last_modified_times[request.handle_index] = last_modified.value(); | |
| 141 } | |
| 142 for (const auto& request : *requests) { | |
| 143 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 144 responses.back().time_file_modified = | |
| 145 last_modified_times[request.handle_index]; | |
| 146 } | |
| 147 return std::make_pair(responses, IPCBlobCreationCancelCode::UNKNOWN); | |
| 148 } | |
| 149 | |
| 55 } // namespace | 150 } // namespace |
| 56 | 151 |
| 57 BlobTransportController* BlobTransportController::GetInstance() { | 152 BlobTransportController* BlobTransportController::GetInstance() { |
| 58 return g_controller.Pointer(); | 153 return g_controller.Pointer(); |
| 59 } | 154 } |
| 60 | 155 |
| 61 // static | 156 // static |
| 62 void BlobTransportController::InitiateBlobTransfer( | 157 void BlobTransportController::InitiateBlobTransfer( |
| 63 const std::string& uuid, | 158 const std::string& uuid, |
| 64 const std::string& content_type, | 159 const std::string& content_type, |
| 65 std::unique_ptr<BlobConsolidation> consolidation, | 160 scoped_refptr<BlobConsolidation> consolidation, |
| 66 scoped_refptr<ThreadSafeSender> sender, | 161 scoped_refptr<ThreadSafeSender> sender, |
| 67 base::SingleThreadTaskRunner* io_runner, | 162 base::SingleThreadTaskRunner* io_runner, |
| 68 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 163 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
| 69 if (main_runner->BelongsToCurrentThread()) { | 164 if (main_runner->BelongsToCurrentThread()) { |
| 70 IncChildProcessRefCount(); | 165 IncChildProcessRefCount(); |
| 71 } else { | 166 } else { |
| 72 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); | 167 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); |
| 73 } | 168 } |
| 74 | 169 |
| 75 std::vector<storage::DataElement> descriptions; | 170 std::vector<storage::DataElement> descriptions; |
| 76 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); | 171 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); |
| 77 BlobTransportController::GetDescriptions( | 172 BlobTransportController::GetDescriptions( |
| 78 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); | 173 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); |
| 79 // I post the task first to make sure that we store our consolidation before | 174 // I post the task first to make sure that we store our consolidation before |
| 80 // we get a request back from the browser. | 175 // we get a request back from the browser. |
| 81 io_runner->PostTask( | 176 io_runner->PostTask( |
| 82 FROM_HERE, | 177 FROM_HERE, |
| 83 base::Bind(&BlobTransportController::StoreBlobDataForRequests, | 178 base::Bind(&BlobTransportController::StoreBlobDataForRequests, |
| 84 base::Unretained(BlobTransportController::GetInstance()), uuid, | 179 base::Unretained(BlobTransportController::GetInstance()), uuid, |
| 85 base::Passed(std::move(consolidation)), | 180 base::Passed(&consolidation), base::Passed(&main_runner))); |
| 86 base::Passed(std::move(main_runner)))); | |
| 87 // TODO(dmurph): Merge register and start messages. | 181 // TODO(dmurph): Merge register and start messages. |
| 88 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", | 182 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", |
| 89 referenced_blobs)); | 183 referenced_blobs)); |
| 90 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); | 184 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); |
| 91 } | 185 } |
| 92 | 186 |
| 93 void BlobTransportController::OnMemoryRequest( | 187 void BlobTransportController::OnMemoryRequest( |
| 94 const std::string& uuid, | 188 const std::string& uuid, |
| 95 const std::vector<storage::BlobItemBytesRequest>& requests, | 189 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 96 std::vector<base::SharedMemoryHandle>* memory_handles, | 190 std::vector<base::SharedMemoryHandle>* memory_handles, |
| 97 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 191 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 192 base::TaskRunner* file_runner, | |
| 98 IPC::Sender* sender) { | 193 IPC::Sender* sender) { |
| 99 std::vector<storage::BlobItemBytesResponse> responses; | 194 std::vector<BlobItemBytesResponse> responses; |
| 100 ResponsesStatus status = | 195 auto it = blob_storage_.find(uuid); |
| 101 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | 196 // Ignore invalid messages. |
| 197 if (it == blob_storage_.end()) | |
| 198 return; | |
| 102 | 199 |
| 103 switch (status) { | 200 BlobConsolidation* consolidation = it->second.get(); |
| 104 case ResponsesStatus::BLOB_NOT_FOUND: | 201 const auto& consolidated_items = consolidation->consolidated_items(); |
| 105 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, | 202 |
| 106 // IPCBlobCreationCancelCode::UNKNOWN)); | 203 std::unique_ptr<std::vector<BlobItemBytesRequest>> file_requests( |
| 107 return; | 204 new std::vector<BlobItemBytesRequest>()); |
| 108 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | 205 |
| 109 // This would happen if the renderer process doesn't have enough memory | 206 // Since we can be writing to the same shared memory handle from multiple |
| 110 // to map the shared memory, which is possible if we don't have much | 207 // requests, we keep them in a vector and lazily create them. |
| 111 // memory. If this scenario happens often, we could delay the response | 208 ScopedVector<SharedMemory> opened_memory; |
| 112 // until we have enough memory. For now we just fail. | 209 opened_memory.resize(memory_handles->size()); |
| 113 CHECK(false) << "Unable to map shared memory to send blob " << uuid | 210 for (const BlobItemBytesRequest& request : requests) { |
| 114 << "."; | 211 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) |
| 115 break; | 212 << "Invalid item index"; |
| 116 case ResponsesStatus::SUCCESS: | 213 |
| 117 break; | 214 const ConsolidatedItem& item = |
| 215 consolidated_items[request.renderer_item_index]; | |
| 216 DCHECK_LE(request.renderer_item_offset + request.size, item.length) | |
| 217 << "Invalid data range"; | |
| 218 DCHECK_EQ(item.type, DataElement::TYPE_BYTES) << "Invalid element type"; | |
| 219 | |
| 220 switch (request.transport_strategy) { | |
| 221 case IPCBlobItemRequestStrategy::IPC: { | |
| 222 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 223 BlobItemBytesResponse& response = responses.back(); | |
| 224 ReadStatus status = consolidation->ReadMemory( | |
| 225 request.renderer_item_index, request.renderer_item_offset, | |
| 226 request.size, response.allocate_mutable_data(request.size)); | |
| 227 DCHECK(status == ReadStatus::OK) | |
| 228 << "Error reading from consolidated blob: " | |
| 229 << static_cast<int>(status); | |
| 230 break; | |
| 231 } | |
| 232 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
| 233 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 234 DCHECK_LT(request.handle_index, memory_handles->size()) | |
| 235 << "Invalid handle index."; | |
| 236 SharedMemory* memory = opened_memory[request.handle_index]; | |
| 237 if (!memory) { | |
| 238 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
| 239 DCHECK(SharedMemory::IsHandleValid(handle)); | |
| 240 std::unique_ptr<SharedMemory> shared_memory( | |
| 241 new SharedMemory(handle, false)); | |
| 242 | |
| 243 if (!shared_memory->Map(request.size)) { | |
| 244 // This would happen if the renderer process doesn't have enough | |
| 245 // memory | |
|
michaeln
2016/04/28 00:16:56
nit: line wraps are arbitrary
dmurph
2016/05/09 19:55:47
Done.
| |
| 246 // to map the shared memory, which is possible if we don't have much | |
| 247 // memory. If this scenario happens often, we could delay the | |
| 248 // response | |
| 249 // until we have enough memory. For now we just fail. | |
| 250 CHECK(false) << "Unable to map shared memory to send blob " << uuid | |
| 251 << "."; | |
| 252 return; | |
| 253 } | |
| 254 memory = shared_memory.get(); | |
| 255 opened_memory[request.handle_index] = shared_memory.release(); | |
| 256 } | |
| 257 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
| 258 ReadStatus status = consolidation->ReadMemory( | |
| 259 request.renderer_item_index, request.renderer_item_offset, | |
| 260 request.size, | |
| 261 static_cast<char*>(memory->memory()) + request.handle_offset); | |
| 262 DCHECK(status == ReadStatus::OK) | |
| 263 << "Error reading from consolidated blob: " | |
| 264 << static_cast<int>(status); | |
| 265 break; | |
| 266 } | |
| 267 case IPCBlobItemRequestStrategy::FILE: | |
| 268 DCHECK_LT(request.handle_index, file_handles.size()) | |
| 269 << "Invalid handle index."; | |
| 270 file_requests->push_back(request); | |
| 271 break; | |
| 272 case IPCBlobItemRequestStrategy::UNKNOWN: | |
| 273 NOTREACHED(); | |
| 274 break; | |
| 275 } | |
| 276 } | |
| 277 if (!file_requests->empty()) { | |
| 278 base::PostTaskAndReplyWithResult( | |
| 279 file_runner, FROM_HERE, | |
| 280 base::Bind(&WriteDiskRequests, make_scoped_refptr(consolidation), | |
| 281 base::Passed(&file_requests), file_handles), | |
| 282 base::Bind(&BlobTransportController::OnFileWriteComplete, | |
| 283 weak_factory_.GetWeakPtr(), sender, uuid)); | |
| 118 } | 284 } |
| 119 | 285 |
| 120 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | 286 if (!responses.empty()) |
| 287 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | |
| 121 } | 288 } |
| 122 | 289 |
| 123 void BlobTransportController::OnCancel( | 290 void BlobTransportController::OnCancel( |
| 124 const std::string& uuid, | 291 const std::string& uuid, |
| 125 storage::IPCBlobCreationCancelCode code) { | 292 storage::IPCBlobCreationCancelCode code) { |
| 126 DVLOG(1) << "Received blob cancel for blob " << uuid | 293 DVLOG(1) << "Received blob cancel for blob " << uuid |
| 127 << " with code: " << static_cast<int>(code); | 294 << " with code: " << static_cast<int>(code); |
| 128 ReleaseBlobConsolidation(uuid); | 295 ReleaseBlobConsolidation(uuid); |
| 129 } | 296 } |
| 130 | 297 |
| 131 void BlobTransportController::OnDone(const std::string& uuid) { | 298 void BlobTransportController::OnDone(const std::string& uuid) { |
| 132 ReleaseBlobConsolidation(uuid); | 299 ReleaseBlobConsolidation(uuid); |
| 133 } | 300 } |
| 134 | 301 |
| 302 void BlobTransportController::CancelAllBlobTransfers() { | |
| 303 weak_factory_.InvalidateWeakPtrs(); | |
| 304 if (!blob_storage_.empty() && main_thread_runner_) { | |
| 305 main_thread_runner_->PostTask( | |
| 306 FROM_HERE, | |
| 307 base::Bind(&DecChildProcessRefCountTimes, blob_storage_.size())); | |
| 308 } | |
| 309 main_thread_runner_ = nullptr; | |
| 310 blob_storage_.clear(); | |
| 311 } | |
| 312 | |
| 135 // static | 313 // static |
| 136 void BlobTransportController::GetDescriptions( | 314 void BlobTransportController::GetDescriptions( |
| 137 BlobConsolidation* consolidation, | 315 BlobConsolidation* consolidation, |
| 138 size_t max_data_population, | 316 size_t max_data_population, |
| 139 std::vector<storage::DataElement>* out) { | 317 std::vector<storage::DataElement>* out) { |
| 140 DCHECK(out->empty()); | 318 DCHECK(out->empty()); |
| 141 DCHECK(consolidation); | 319 DCHECK(consolidation); |
| 142 const auto& consolidated_items = consolidation->consolidated_items(); | 320 const auto& consolidated_items = consolidation->consolidated_items(); |
| 143 | 321 |
| 144 size_t current_memory_population = 0; | 322 size_t current_memory_population = 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 } | 357 } |
| 180 case DataElement::TYPE_DISK_CACHE_ENTRY: | 358 case DataElement::TYPE_DISK_CACHE_ENTRY: |
| 181 case DataElement::TYPE_BYTES_DESCRIPTION: | 359 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 182 case DataElement::TYPE_UNKNOWN: | 360 case DataElement::TYPE_UNKNOWN: |
| 183 NOTREACHED(); | 361 NOTREACHED(); |
| 184 } | 362 } |
| 185 ++current_item; | 363 ++current_item; |
| 186 } | 364 } |
| 187 } | 365 } |
| 188 | 366 |
| 189 BlobTransportController::BlobTransportController() {} | 367 BlobTransportController::BlobTransportController() : weak_factory_(this) {} |
| 190 | 368 |
| 191 BlobTransportController::~BlobTransportController() {} | 369 BlobTransportController::~BlobTransportController() {} |
| 192 | 370 |
| 193 void BlobTransportController::ClearForTesting() { | 371 void BlobTransportController::OnFileWriteComplete( |
| 194 if (!blob_storage_.empty() && main_thread_runner_) { | 372 IPC::Sender* sender, |
| 195 main_thread_runner_->PostTask(FROM_HERE, | 373 const std::string& uuid, |
| 196 base::Bind(&DecChildProcessRefCount)); | 374 const std::pair<std::vector<BlobItemBytesResponse>, |
| 375 IPCBlobCreationCancelCode>& result) { | |
| 376 if (blob_storage_.find(uuid) == blob_storage_.end()) | |
| 377 return; | |
| 378 if (!result.first.empty()) { | |
| 379 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, result.first)); | |
| 380 return; | |
| 197 } | 381 } |
| 198 blob_storage_.clear(); | 382 sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, result.second)); |
| 383 ReleaseBlobConsolidation(uuid); | |
| 199 } | 384 } |
| 200 | 385 |
| 201 void BlobTransportController::StoreBlobDataForRequests( | 386 void BlobTransportController::StoreBlobDataForRequests( |
| 202 const std::string& uuid, | 387 const std::string& uuid, |
| 203 std::unique_ptr<BlobConsolidation> consolidation, | 388 scoped_refptr<BlobConsolidation> consolidation, |
| 204 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 389 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
| 205 if (!main_thread_runner_.get()) { | 390 if (!main_thread_runner_.get()) { |
| 206 main_thread_runner_ = std::move(main_runner); | 391 main_thread_runner_ = std::move(main_runner); |
| 207 } | 392 } |
| 208 blob_storage_[uuid] = std::move(consolidation); | 393 blob_storage_[uuid] = std::move(consolidation); |
| 209 } | 394 } |
| 210 | 395 |
| 211 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( | |
| 212 const std::string& uuid, | |
| 213 const std::vector<BlobItemBytesRequest>& requests, | |
| 214 std::vector<SharedMemoryHandle>* memory_handles, | |
| 215 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
| 216 std::vector<BlobItemBytesResponse>* out) { | |
| 217 DCHECK(out->empty()); | |
| 218 auto it = blob_storage_.find(uuid); | |
| 219 if (it == blob_storage_.end()) | |
| 220 return ResponsesStatus::BLOB_NOT_FOUND; | |
| 221 | |
| 222 BlobConsolidation* consolidation = it->second.get(); | |
| 223 const auto& consolidated_items = consolidation->consolidated_items(); | |
| 224 | |
| 225 // Since we can be writing to the same shared memory handle from multiple | |
| 226 // requests, we keep them in a vector and lazily create them. | |
| 227 ScopedVector<SharedMemory> opened_memory; | |
| 228 opened_memory.resize(memory_handles->size()); | |
| 229 for (const BlobItemBytesRequest& request : requests) { | |
| 230 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) | |
| 231 << "Invalid item index"; | |
| 232 | |
| 233 const ConsolidatedItem& item = | |
| 234 consolidated_items[request.renderer_item_index]; | |
| 235 DCHECK_LE(request.renderer_item_offset + request.size, item.length) | |
| 236 << "Invalid data range"; | |
| 237 DCHECK_EQ(item.type, DataElement::TYPE_BYTES) << "Invalid element type"; | |
| 238 | |
| 239 out->push_back(BlobItemBytesResponse(request.request_number)); | |
| 240 switch (request.transport_strategy) { | |
| 241 case IPCBlobItemRequestStrategy::IPC: { | |
| 242 BlobItemBytesResponse& response = out->back(); | |
| 243 ReadStatus status = consolidation->ReadMemory( | |
| 244 request.renderer_item_index, request.renderer_item_offset, | |
| 245 request.size, response.allocate_mutable_data(request.size)); | |
| 246 DCHECK(status == ReadStatus::OK) | |
| 247 << "Error reading from consolidated blob: " | |
| 248 << static_cast<int>(status); | |
| 249 break; | |
| 250 } | |
| 251 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
| 252 DCHECK_LT(request.handle_index, memory_handles->size()) | |
| 253 << "Invalid handle index."; | |
| 254 SharedMemory* memory = opened_memory[request.handle_index]; | |
| 255 if (!memory) { | |
| 256 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
| 257 DCHECK(SharedMemory::IsHandleValid(handle)); | |
| 258 std::unique_ptr<SharedMemory> shared_memory( | |
| 259 new SharedMemory(handle, false)); | |
| 260 if (!shared_memory->Map(request.size)) | |
| 261 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; | |
| 262 memory = shared_memory.get(); | |
| 263 opened_memory[request.handle_index] = shared_memory.release(); | |
| 264 } | |
| 265 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
| 266 ReadStatus status = consolidation->ReadMemory( | |
| 267 request.renderer_item_index, request.renderer_item_offset, | |
| 268 request.size, | |
| 269 static_cast<char*>(memory->memory()) + request.handle_offset); | |
| 270 DCHECK(status == ReadStatus::OK) | |
| 271 << "Error reading from consolidated blob: " | |
| 272 << static_cast<int>(status); | |
| 273 break; | |
| 274 } | |
| 275 case IPCBlobItemRequestStrategy::FILE: | |
| 276 NOTREACHED() << "TODO(dmurph): Not implemented."; | |
| 277 break; | |
| 278 case IPCBlobItemRequestStrategy::UNKNOWN: | |
| 279 NOTREACHED(); | |
| 280 break; | |
| 281 } | |
| 282 } | |
| 283 return ResponsesStatus::SUCCESS; | |
| 284 } | |
| 285 | |
| 286 void BlobTransportController::ReleaseBlobConsolidation( | 396 void BlobTransportController::ReleaseBlobConsolidation( |
| 287 const std::string& uuid) { | 397 const std::string& uuid) { |
| 288 if (blob_storage_.erase(uuid)) { | 398 if (blob_storage_.erase(uuid)) { |
| 289 main_thread_runner_->PostTask(FROM_HERE, | 399 main_thread_runner_->PostTask(FROM_HERE, |
| 290 base::Bind(&DecChildProcessRefCount)); | 400 base::Bind(&DecChildProcessRefCount)); |
| 291 } | 401 } |
| 292 } | 402 } |
| 293 | 403 |
| 294 } // namespace content | 404 } // namespace content |
| OLD | NEW |