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(); | |
|
kinuko
2016/05/10 13:30:50
nit: maybe just call DecChildProcessRefCount() ins
dmurph
2016/05/10 18:22:34
Done.
| |
| 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); | |
|
kinuko
2016/05/10 13:30:50
nit: base::saturated_cast<int>(size - written) ?
dmurph
2016/05/10 18:22:34
awesome, good find. Didn't know about that.
| |
| 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); | |
|
Mark P
2016/05/09 22:00:50
Consider changing this histogram so true and false
dmurph
2016/05/10 18:22:34
Good idea. Thanks.
| |
| 89 return false; | |
| 90 } | |
| 91 written += actual_written; | |
| 92 } | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 base::Optional<base::Time> WriteSingleRequestToDisk( | |
| 97 const BlobConsolidation* consolidation, | |
| 98 const BlobItemBytesRequest& request, | |
| 99 File* file) { | |
| 100 if (!file->IsValid()) | |
| 101 return base::nullopt; | |
| 102 CHECK_LE(request.handle_offset, | |
| 103 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())); | |
|
kinuko
2016/05/10 13:30:50
nit: checked_cast()
dmurph
2016/05/10 18:22:34
thanks
| |
| 104 int64_t seek_distance = | |
| 105 file->Seek(File::FROM_BEGIN, static_cast<int64_t>(request.handle_offset)); | |
| 106 if (seek_distance < 0) { | |
| 107 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.RendererFileSeekFailed", true); | |
| 108 return base::nullopt; | |
| 109 } | |
| 110 BlobConsolidation::ReadStatus status = consolidation->VisitMemory( | |
| 111 request.renderer_item_index, request.renderer_item_offset, request.size, | |
| 112 base::Bind(&WriteSingleChunk, file)); | |
| 113 if (status != BlobConsolidation::ReadStatus::OK) | |
| 114 return base::nullopt; | |
| 115 File::Info info; | |
| 116 file->GetInfo(&info); | |
| 117 return base::make_optional(info.last_modified); | |
| 118 } | |
| 119 | |
| 120 // This returns either the responses, or if they're empty, an error code. | |
| 121 std::pair<std::vector<storage::BlobItemBytesResponse>, | |
| 122 IPCBlobCreationCancelCode> | |
| 123 WriteDiskRequests( | |
| 124 scoped_refptr<BlobConsolidation> consolidation, | |
| 125 std::unique_ptr<std::vector<BlobItemBytesRequest>> requests, | |
| 126 const std::vector<IPC::PlatformFileForTransit>& file_handles) { | |
| 127 std::vector<BlobItemBytesResponse> responses; | |
| 128 std::vector<base::Time> last_modified_times; | |
| 129 last_modified_times.resize(file_handles.size()); | |
| 130 // We grab ownership of the file handles here. When this vector is destroyed | |
| 131 // it will close the files. | |
| 132 std::vector<File> files; | |
| 133 files.reserve(file_handles.size()); | |
| 134 for (const IPC::PlatformFileForTransit& file_handle : file_handles) { | |
|
kinuko
2016/05/10 13:30:50
nit: it feels 'const auto& file_handle' is fine he
dmurph
2016/05/10 18:22:34
Done.
| |
| 135 files.emplace_back(IPC::PlatformFileForTransitToFile(file_handle)); | |
| 136 } | |
| 137 for (const auto& request : *requests) { | |
| 138 base::Optional<base::Time> last_modified = WriteSingleRequestToDisk( | |
| 139 consolidation.get(), request, &files[request.handle_index]); | |
| 140 if (!last_modified) { | |
| 141 return std::make_pair(std::vector<storage::BlobItemBytesResponse>(), | |
| 142 IPCBlobCreationCancelCode::FILE_WRITE_FAILED); | |
| 143 } | |
| 144 last_modified_times[request.handle_index] = last_modified.value(); | |
| 145 } | |
| 146 for (const auto& request : *requests) { | |
| 147 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 148 responses.back().time_file_modified = | |
| 149 last_modified_times[request.handle_index]; | |
| 150 } | |
| 151 | |
| 152 return std::make_pair(responses, IPCBlobCreationCancelCode::UNKNOWN); | |
| 153 } | |
| 154 | |
| 55 } // namespace | 155 } // namespace |
| 56 | 156 |
| 57 BlobTransportController* BlobTransportController::GetInstance() { | 157 BlobTransportController* BlobTransportController::GetInstance() { |
| 58 return g_controller.Pointer(); | 158 return g_controller.Pointer(); |
| 59 } | 159 } |
| 60 | 160 |
| 61 // static | 161 // static |
| 62 void BlobTransportController::InitiateBlobTransfer( | 162 void BlobTransportController::InitiateBlobTransfer( |
| 63 const std::string& uuid, | 163 const std::string& uuid, |
| 64 const std::string& content_type, | 164 const std::string& content_type, |
| 65 std::unique_ptr<BlobConsolidation> consolidation, | 165 scoped_refptr<BlobConsolidation> consolidation, |
| 66 scoped_refptr<ThreadSafeSender> sender, | 166 scoped_refptr<ThreadSafeSender> sender, |
| 67 base::SingleThreadTaskRunner* io_runner, | 167 base::SingleThreadTaskRunner* io_runner, |
| 68 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 168 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
| 69 if (main_runner->BelongsToCurrentThread()) { | 169 if (main_runner->BelongsToCurrentThread()) { |
| 70 IncChildProcessRefCount(); | 170 IncChildProcessRefCount(); |
| 71 } else { | 171 } else { |
| 72 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); | 172 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); |
| 73 } | 173 } |
| 74 | 174 |
| 75 std::vector<storage::DataElement> descriptions; | 175 std::vector<storage::DataElement> descriptions; |
| 76 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); | 176 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); |
| 77 BlobTransportController::GetDescriptions( | 177 BlobTransportController::GetDescriptions( |
| 78 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); | 178 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); |
| 79 // I post the task first to make sure that we store our consolidation before | 179 // I post the task first to make sure that we store our consolidation before |
| 80 // we get a request back from the browser. | 180 // we get a request back from the browser. |
| 81 io_runner->PostTask( | 181 io_runner->PostTask( |
| 82 FROM_HERE, | 182 FROM_HERE, |
| 83 base::Bind(&BlobTransportController::StoreBlobDataForRequests, | 183 base::Bind(&BlobTransportController::StoreBlobDataForRequests, |
| 84 base::Unretained(BlobTransportController::GetInstance()), uuid, | 184 base::Unretained(BlobTransportController::GetInstance()), uuid, |
| 85 base::Passed(std::move(consolidation)), | 185 base::Passed(&consolidation), base::Passed(&main_runner))); |
| 86 base::Passed(std::move(main_runner)))); | |
| 87 // TODO(dmurph): Merge register and start messages. | 186 // TODO(dmurph): Merge register and start messages. |
| 88 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", | 187 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", |
| 89 referenced_blobs)); | 188 referenced_blobs)); |
| 90 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); | 189 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); |
| 91 } | 190 } |
| 92 | 191 |
| 93 void BlobTransportController::OnMemoryRequest( | 192 void BlobTransportController::OnMemoryRequest( |
| 94 const std::string& uuid, | 193 const std::string& uuid, |
| 95 const std::vector<storage::BlobItemBytesRequest>& requests, | 194 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 96 std::vector<base::SharedMemoryHandle>* memory_handles, | 195 std::vector<base::SharedMemoryHandle>* memory_handles, |
| 97 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 196 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 197 base::TaskRunner* file_runner, | |
| 98 IPC::Sender* sender) { | 198 IPC::Sender* sender) { |
| 99 std::vector<storage::BlobItemBytesResponse> responses; | 199 std::vector<BlobItemBytesResponse> responses; |
| 100 ResponsesStatus status = | 200 auto it = blob_storage_.find(uuid); |
| 101 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | 201 // Ignore invalid messages. |
| 202 if (it == blob_storage_.end()) | |
| 203 return; | |
| 102 | 204 |
| 103 switch (status) { | 205 BlobConsolidation* consolidation = it->second.get(); |
| 104 case ResponsesStatus::BLOB_NOT_FOUND: | 206 const auto& consolidated_items = consolidation->consolidated_items(); |
| 105 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, | 207 |
| 106 // IPCBlobCreationCancelCode::UNKNOWN)); | 208 std::unique_ptr<std::vector<BlobItemBytesRequest>> file_requests( |
| 107 return; | 209 new std::vector<BlobItemBytesRequest>()); |
| 108 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | 210 |
| 109 // This would happen if the renderer process doesn't have enough memory | 211 // 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 | 212 // requests, we keep them in a vector and lazily create them. |
| 111 // memory. If this scenario happens often, we could delay the response | 213 ScopedVector<SharedMemory> opened_memory; |
| 112 // until we have enough memory. For now we just fail. | 214 opened_memory.resize(memory_handles->size()); |
| 113 CHECK(false) << "Unable to map shared memory to send blob " << uuid | 215 for (const BlobItemBytesRequest& request : requests) { |
| 114 << "."; | 216 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) |
| 115 break; | 217 << "Invalid item index"; |
| 116 case ResponsesStatus::SUCCESS: | 218 |
| 117 break; | 219 const ConsolidatedItem& item = |
| 220 consolidated_items[request.renderer_item_index]; | |
| 221 DCHECK_LE(request.renderer_item_offset + request.size, item.length) | |
| 222 << "Invalid data range"; | |
| 223 DCHECK_EQ(item.type, DataElement::TYPE_BYTES) << "Invalid element type"; | |
| 224 | |
| 225 switch (request.transport_strategy) { | |
| 226 case IPCBlobItemRequestStrategy::IPC: { | |
| 227 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 228 BlobItemBytesResponse& response = responses.back(); | |
| 229 ReadStatus status = consolidation->ReadMemory( | |
| 230 request.renderer_item_index, request.renderer_item_offset, | |
| 231 request.size, response.allocate_mutable_data(request.size)); | |
| 232 DCHECK(status == ReadStatus::OK) | |
| 233 << "Error reading from consolidated blob: " | |
| 234 << static_cast<int>(status); | |
| 235 break; | |
| 236 } | |
| 237 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
| 238 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
| 239 DCHECK_LT(request.handle_index, memory_handles->size()) | |
| 240 << "Invalid handle index."; | |
| 241 SharedMemory* memory = opened_memory[request.handle_index]; | |
| 242 if (!memory) { | |
| 243 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
| 244 DCHECK(SharedMemory::IsHandleValid(handle)); | |
| 245 std::unique_ptr<SharedMemory> shared_memory( | |
| 246 new SharedMemory(handle, false)); | |
| 247 | |
| 248 if (!shared_memory->Map(request.size)) { | |
| 249 // This would happen if the renderer process doesn't have enough | |
| 250 // memory to map the shared memory, which is possible if we don't | |
| 251 // have much memory. If this scenario happens often, we could delay | |
| 252 // the response until we have enough memory. For now we just fail. | |
| 253 CHECK(false) << "Unable to map shared memory to send blob " << uuid | |
| 254 << "."; | |
| 255 return; | |
| 256 } | |
| 257 memory = shared_memory.get(); | |
| 258 opened_memory[request.handle_index] = shared_memory.release(); | |
| 259 } | |
| 260 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
| 261 ReadStatus status = consolidation->ReadMemory( | |
| 262 request.renderer_item_index, request.renderer_item_offset, | |
| 263 request.size, | |
| 264 static_cast<char*>(memory->memory()) + request.handle_offset); | |
| 265 DCHECK(status == ReadStatus::OK) | |
| 266 << "Error reading from consolidated blob: " | |
| 267 << static_cast<int>(status); | |
| 268 break; | |
| 269 } | |
| 270 case IPCBlobItemRequestStrategy::FILE: | |
| 271 DCHECK_LT(request.handle_index, file_handles.size()) | |
| 272 << "Invalid handle index."; | |
| 273 file_requests->push_back(request); | |
| 274 break; | |
| 275 case IPCBlobItemRequestStrategy::UNKNOWN: | |
| 276 NOTREACHED(); | |
| 277 break; | |
| 278 } | |
| 279 } | |
| 280 if (!file_requests->empty()) { | |
| 281 base::PostTaskAndReplyWithResult( | |
| 282 file_runner, FROM_HERE, | |
| 283 base::Bind(&WriteDiskRequests, make_scoped_refptr(consolidation), | |
| 284 base::Passed(&file_requests), file_handles), | |
| 285 base::Bind(&BlobTransportController::OnFileWriteComplete, | |
| 286 weak_factory_.GetWeakPtr(), sender, uuid)); | |
| 118 } | 287 } |
| 119 | 288 |
| 120 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | 289 if (!responses.empty()) |
| 290 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | |
| 121 } | 291 } |
| 122 | 292 |
| 123 void BlobTransportController::OnCancel( | 293 void BlobTransportController::OnCancel( |
| 124 const std::string& uuid, | 294 const std::string& uuid, |
| 125 storage::IPCBlobCreationCancelCode code) { | 295 storage::IPCBlobCreationCancelCode code) { |
| 126 DVLOG(1) << "Received blob cancel for blob " << uuid | 296 DVLOG(1) << "Received blob cancel for blob " << uuid |
| 127 << " with code: " << static_cast<int>(code); | 297 << " with code: " << static_cast<int>(code); |
| 128 ReleaseBlobConsolidation(uuid); | 298 ReleaseBlobConsolidation(uuid); |
| 129 } | 299 } |
| 130 | 300 |
| 131 void BlobTransportController::OnDone(const std::string& uuid) { | 301 void BlobTransportController::OnDone(const std::string& uuid) { |
| 132 ReleaseBlobConsolidation(uuid); | 302 ReleaseBlobConsolidation(uuid); |
| 133 } | 303 } |
| 134 | 304 |
| 305 void BlobTransportController::CancelAllBlobTransfers() { | |
| 306 weak_factory_.InvalidateWeakPtrs(); | |
| 307 if (!blob_storage_.empty() && main_thread_runner_) { | |
| 308 main_thread_runner_->PostTask( | |
| 309 FROM_HERE, | |
| 310 base::Bind(&DecChildProcessRefCountTimes, blob_storage_.size())); | |
| 311 } | |
| 312 main_thread_runner_ = nullptr; | |
| 313 blob_storage_.clear(); | |
| 314 } | |
| 315 | |
| 135 // static | 316 // static |
| 136 void BlobTransportController::GetDescriptions( | 317 void BlobTransportController::GetDescriptions( |
| 137 BlobConsolidation* consolidation, | 318 BlobConsolidation* consolidation, |
| 138 size_t max_data_population, | 319 size_t max_data_population, |
| 139 std::vector<storage::DataElement>* out) { | 320 std::vector<storage::DataElement>* out) { |
| 140 DCHECK(out->empty()); | 321 DCHECK(out->empty()); |
| 141 DCHECK(consolidation); | 322 DCHECK(consolidation); |
| 142 const auto& consolidated_items = consolidation->consolidated_items(); | 323 const auto& consolidated_items = consolidation->consolidated_items(); |
| 143 | 324 |
| 144 size_t current_memory_population = 0; | 325 size_t current_memory_population = 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 } | 360 } |
| 180 case DataElement::TYPE_DISK_CACHE_ENTRY: | 361 case DataElement::TYPE_DISK_CACHE_ENTRY: |
| 181 case DataElement::TYPE_BYTES_DESCRIPTION: | 362 case DataElement::TYPE_BYTES_DESCRIPTION: |
| 182 case DataElement::TYPE_UNKNOWN: | 363 case DataElement::TYPE_UNKNOWN: |
| 183 NOTREACHED(); | 364 NOTREACHED(); |
| 184 } | 365 } |
| 185 ++current_item; | 366 ++current_item; |
| 186 } | 367 } |
| 187 } | 368 } |
| 188 | 369 |
| 189 BlobTransportController::BlobTransportController() {} | 370 BlobTransportController::BlobTransportController() : weak_factory_(this) {} |
| 190 | 371 |
| 191 BlobTransportController::~BlobTransportController() {} | 372 BlobTransportController::~BlobTransportController() {} |
| 192 | 373 |
| 193 void BlobTransportController::ClearForTesting() { | 374 void BlobTransportController::OnFileWriteComplete( |
| 194 if (!blob_storage_.empty() && main_thread_runner_) { | 375 IPC::Sender* sender, |
| 195 main_thread_runner_->PostTask(FROM_HERE, | 376 const std::string& uuid, |
| 196 base::Bind(&DecChildProcessRefCount)); | 377 const std::pair<std::vector<BlobItemBytesResponse>, |
| 378 IPCBlobCreationCancelCode>& result) { | |
| 379 if (blob_storage_.find(uuid) == blob_storage_.end()) | |
| 380 return; | |
| 381 if (!result.first.empty()) { | |
| 382 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, result.first)); | |
| 383 return; | |
| 197 } | 384 } |
| 198 blob_storage_.clear(); | 385 sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, result.second)); |
| 386 ReleaseBlobConsolidation(uuid); | |
| 199 } | 387 } |
| 200 | 388 |
| 201 void BlobTransportController::StoreBlobDataForRequests( | 389 void BlobTransportController::StoreBlobDataForRequests( |
| 202 const std::string& uuid, | 390 const std::string& uuid, |
| 203 std::unique_ptr<BlobConsolidation> consolidation, | 391 scoped_refptr<BlobConsolidation> consolidation, |
| 204 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 392 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
| 205 if (!main_thread_runner_.get()) { | 393 if (!main_thread_runner_.get()) { |
| 206 main_thread_runner_ = std::move(main_runner); | 394 main_thread_runner_ = std::move(main_runner); |
| 207 } | 395 } |
| 208 blob_storage_[uuid] = std::move(consolidation); | 396 blob_storage_[uuid] = std::move(consolidation); |
| 209 } | 397 } |
| 210 | 398 |
| 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( | 399 void BlobTransportController::ReleaseBlobConsolidation( |
| 287 const std::string& uuid) { | 400 const std::string& uuid) { |
| 288 if (blob_storage_.erase(uuid)) { | 401 if (blob_storage_.erase(uuid)) { |
| 289 main_thread_runner_->PostTask(FROM_HERE, | 402 main_thread_runner_->PostTask(FROM_HERE, |
| 290 base::Bind(&DecChildProcessRefCount)); | 403 base::Bind(&DecChildProcessRefCount)); |
| 291 } | 404 } |
| 292 } | 405 } |
| 293 | 406 |
| 294 } // namespace content | 407 } // namespace content |
| OLD | NEW |