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