| 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 <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 215 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 216 std::vector<BlobItemBytesResponse>* out) { | 216 std::vector<BlobItemBytesResponse>* out) { |
| 217 DCHECK(out->empty()); | 217 DCHECK(out->empty()); |
| 218 auto it = blob_storage_.find(uuid); | 218 auto it = blob_storage_.find(uuid); |
| 219 if (it == blob_storage_.end()) | 219 if (it == blob_storage_.end()) |
| 220 return ResponsesStatus::BLOB_NOT_FOUND; | 220 return ResponsesStatus::BLOB_NOT_FOUND; |
| 221 | 221 |
| 222 BlobConsolidation* consolidation = it->second.get(); | 222 BlobConsolidation* consolidation = it->second.get(); |
| 223 const auto& consolidated_items = consolidation->consolidated_items(); | 223 const auto& consolidated_items = consolidation->consolidated_items(); |
| 224 | 224 |
| 225 // We need to calculate how much memory we expect to be writing to the memory |
| 226 // segments so we can correctly map it the first time. |
| 227 std::vector<size_t> shared_memory_sizes(memory_handles->size()); |
| 228 for (const BlobItemBytesRequest& request : requests) { |
| 229 if (request.transport_strategy != |
| 230 IPCBlobItemRequestStrategy::SHARED_MEMORY) { |
| 231 continue; |
| 232 } |
| 233 DCHECK_LT(request.handle_index, memory_handles->size()) |
| 234 << "Invalid handle index."; |
| 235 shared_memory_sizes[request.handle_index] = |
| 236 std::max<size_t>(shared_memory_sizes[request.handle_index], |
| 237 request.size + request.handle_offset); |
| 238 } |
| 239 |
| 225 // Since we can be writing to the same shared memory handle from multiple | 240 // 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. | 241 // requests, we keep them in a vector and lazily create them. |
| 227 ScopedVector<SharedMemory> opened_memory; | 242 ScopedVector<SharedMemory> opened_memory; |
| 228 opened_memory.resize(memory_handles->size()); | 243 opened_memory.resize(memory_handles->size()); |
| 229 for (const BlobItemBytesRequest& request : requests) { | 244 for (const BlobItemBytesRequest& request : requests) { |
| 230 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) | 245 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) |
| 231 << "Invalid item index"; | 246 << "Invalid item index"; |
| 232 | 247 |
| 233 const ConsolidatedItem& item = | 248 const ConsolidatedItem& item = |
| 234 consolidated_items[request.renderer_item_index]; | 249 consolidated_items[request.renderer_item_index]; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 247 << "Error reading from consolidated blob: " | 262 << "Error reading from consolidated blob: " |
| 248 << static_cast<int>(status); | 263 << static_cast<int>(status); |
| 249 break; | 264 break; |
| 250 } | 265 } |
| 251 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | 266 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { |
| 252 DCHECK_LT(request.handle_index, memory_handles->size()) | 267 DCHECK_LT(request.handle_index, memory_handles->size()) |
| 253 << "Invalid handle index."; | 268 << "Invalid handle index."; |
| 254 SharedMemory* memory = opened_memory[request.handle_index]; | 269 SharedMemory* memory = opened_memory[request.handle_index]; |
| 255 if (!memory) { | 270 if (!memory) { |
| 256 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | 271 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; |
| 272 size_t size = shared_memory_sizes[request.handle_index]; |
| 257 DCHECK(SharedMemory::IsHandleValid(handle)); | 273 DCHECK(SharedMemory::IsHandleValid(handle)); |
| 258 std::unique_ptr<SharedMemory> shared_memory( | 274 std::unique_ptr<SharedMemory> shared_memory( |
| 259 new SharedMemory(handle, false)); | 275 new SharedMemory(handle, false)); |
| 260 if (!shared_memory->Map(request.size)) | 276 if (!shared_memory->Map(size)) |
| 261 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; | 277 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; |
| 262 memory = shared_memory.get(); | 278 memory = shared_memory.get(); |
| 263 opened_memory[request.handle_index] = shared_memory.release(); | 279 opened_memory[request.handle_index] = shared_memory.release(); |
| 264 } | 280 } |
| 265 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | 281 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; |
| 266 ReadStatus status = consolidation->ReadMemory( | 282 ReadStatus status = consolidation->ReadMemory( |
| 267 request.renderer_item_index, request.renderer_item_offset, | 283 request.renderer_item_index, request.renderer_item_offset, |
| 268 request.size, | 284 request.size, |
| 269 static_cast<char*>(memory->memory()) + request.handle_offset); | 285 static_cast<char*>(memory->memory()) + request.handle_offset); |
| 270 DCHECK(status == ReadStatus::OK) | 286 DCHECK(status == ReadStatus::OK) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 285 | 301 |
| 286 void BlobTransportController::ReleaseBlobConsolidation( | 302 void BlobTransportController::ReleaseBlobConsolidation( |
| 287 const std::string& uuid) { | 303 const std::string& uuid) { |
| 288 if (blob_storage_.erase(uuid)) { | 304 if (blob_storage_.erase(uuid)) { |
| 289 main_thread_runner_->PostTask(FROM_HERE, | 305 main_thread_runner_->PostTask(FROM_HERE, |
| 290 base::Bind(&DecChildProcessRefCount)); | 306 base::Bind(&DecChildProcessRefCount)); |
| 291 } | 307 } |
| 292 } | 308 } |
| 293 | 309 |
| 294 } // namespace content | 310 } // namespace content |
| OLD | NEW |