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> | |
7 #include <utility> | 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
11 #include "base/bind.h" | |
12 #include "base/bind_helpers.h" | |
13 #include "base/callback.h" | |
14 #include "base/files/file.h" | |
10 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
16 #include "base/location.h" | |
17 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
12 #include "base/memory/shared_memory.h" | 19 #include "base/memory/shared_memory.h" |
20 #include "base/optional.h" | |
13 #include "base/single_thread_task_runner.h" | 21 #include "base/single_thread_task_runner.h" |
14 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
23 #include "base/task_runner.h" | |
24 #include "base/task_runner_util.h" | |
25 #include "base/thread_task_runner_handle.h" | |
26 #include "base/time/time.h" | |
15 #include "content/child/blob_storage/blob_consolidation.h" | 27 #include "content/child/blob_storage/blob_consolidation.h" |
16 #include "content/child/child_process.h" | 28 #include "content/child/child_process.h" |
17 #include "content/child/thread_safe_sender.h" | 29 #include "content/child/thread_safe_sender.h" |
18 #include "content/common/fileapi/webblob_messages.h" | 30 #include "content/common/fileapi/webblob_messages.h" |
31 #include "ipc/ipc_message.h" | |
19 #include "ipc/ipc_sender.h" | 32 #include "ipc/ipc_sender.h" |
20 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 33 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
21 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 34 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
22 #include "storage/common/data_element.h" | 35 #include "storage/common/data_element.h" |
23 #include "third_party/WebKit/public/platform/Platform.h" | 36 #include "third_party/WebKit/public/platform/Platform.h" |
24 | 37 |
38 using base::File; | |
25 using base::SharedMemory; | 39 using base::SharedMemory; |
26 using base::SharedMemoryHandle; | 40 using base::SharedMemoryHandle; |
27 using storage::BlobItemBytesRequest; | 41 using storage::BlobItemBytesRequest; |
28 using storage::BlobItemBytesResponse; | 42 using storage::BlobItemBytesResponse; |
29 using storage::IPCBlobItemRequestStrategy; | 43 using storage::IPCBlobItemRequestStrategy; |
30 using storage::DataElement; | 44 using storage::DataElement; |
31 using storage::kBlobStorageIPCThresholdBytes; | 45 using storage::kBlobStorageIPCThresholdBytes; |
32 | 46 |
47 using storage::BlobItemBytesResponse; | |
48 using storage::BlobItemBytesRequest; | |
49 using storage::IPCBlobCreationCancelCode; | |
50 | |
33 namespace content { | 51 namespace content { |
34 | 52 using ResponseCallback = base::Callback<void( |
35 using storage::IPCBlobCreationCancelCode; | 53 const std::vector<storage::BlobItemBytesResponse>& /* responses */)>; |
54 using CancelCallback = | |
michaeln
2016/04/26 22:50:08
these callback tyhpes are no longer needed since t
dmurph
2016/04/27 19:14:04
Done.
| |
55 base::Callback<void(storage::IPCBlobCreationCancelCode /* reason */)>; | |
36 | 56 |
37 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | 57 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
38 using ReadStatus = BlobConsolidation::ReadStatus; | 58 using ReadStatus = BlobConsolidation::ReadStatus; |
39 | 59 |
40 namespace { | 60 namespace { |
41 static base::LazyInstance<BlobTransportController>::Leaky g_controller = | 61 static base::LazyInstance<BlobTransportController>::Leaky g_controller = |
42 LAZY_INSTANCE_INITIALIZER; | 62 LAZY_INSTANCE_INITIALIZER; |
43 | 63 |
44 // This keeps the process alive while blobs are being transferred. | 64 // This keeps the process alive while blobs are being transferred. |
45 // These need to be called on the main thread. | 65 // These need to be called on the main thread. |
46 void IncChildProcessRefCount() { | 66 void IncChildProcessRefCount() { |
47 blink::Platform::current()->suddenTerminationChanged(false); | 67 blink::Platform::current()->suddenTerminationChanged(false); |
48 ChildProcess::current()->AddRefProcess(); | 68 ChildProcess::current()->AddRefProcess(); |
49 } | 69 } |
50 | 70 |
51 void DecChildProcessRefCount() { | 71 void DecChildProcessRefCount() { |
52 blink::Platform::current()->suddenTerminationChanged(true); | 72 blink::Platform::current()->suddenTerminationChanged(true); |
53 ChildProcess::current()->ReleaseProcess(); | 73 ChildProcess::current()->ReleaseProcess(); |
54 } | 74 } |
75 | |
76 bool WriteSingleChunk(base::File* file, | |
77 size_t total_read, | |
michaeln
2016/04/26 22:50:08
i noticed that total_read isn't use here since we
dmurph
2016/04/27 19:14:04
you're right, removed.
| |
78 const char* memory, | |
79 size_t size) { | |
80 size_t written = 0; | |
81 size_t max_int = static_cast<size_t>(std::numeric_limits<int>::max()); | |
82 while (written < size) { | |
83 size_t writing_size = std::min(max_int, size - written); | |
84 int actual_written = | |
85 file->WriteAtCurrentPos(memory, static_cast<int>(writing_size)); | |
86 if (actual_written < 0) { | |
87 LOG(ERROR) << "Error writing to file " << actual_written | |
88 << " (dmurph@google.com)"; | |
michaeln
2016/04/26 22:50:08
i'd vote to not bake usernames into logging
do we
dmurph
2016/04/27 19:14:04
Done.
| |
89 return false; | |
90 } | |
91 written += writing_size; | |
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 LOG(ERROR) << "Error seeking " << request.handle_offset << " in file, got " | |
michaeln
2016/04/26 22:50:08
ditto logging question
dmurph
2016/04/27 19:14:04
Done.
| |
109 << seek_distance | |
110 << ". File error: " << File::ErrorToString(file.error_details()); | |
111 return base::nullopt; | |
112 } | |
113 BlobConsolidation::ReadStatus status = consolidation->VisitMemory( | |
114 request.renderer_item_index, request.renderer_item_offset, request.size, | |
115 base::Bind(&WriteSingleChunk, &file)); | |
116 if (status != BlobConsolidation::ReadStatus::OK) | |
117 return base::nullopt; | |
118 File::Info info; | |
119 file.GetInfo(&info); | |
120 // We need to release the file so we don't automatically close the file. | |
121 file.TakePlatformFile(); | |
122 return base::make_optional(info.last_modified); | |
123 } | |
124 | |
125 // This returns either the responses, or if they're empty, an error code. | |
126 std::pair<std::vector<storage::BlobItemBytesResponse>, | |
127 IPCBlobCreationCancelCode> | |
128 WriteDiskRequests( | |
michaeln
2016/04/26 22:50:08
wicked line wrapping
dmurph
2016/04/27 19:14:04
We actually don't have a style guide for this (it'
| |
129 scoped_refptr<BlobConsolidation> consolidation, | |
130 std::unique_ptr<std::vector<BlobItemBytesRequest>> requests, | |
131 const std::vector<IPC::PlatformFileForTransit>& file_handles) { | |
132 std::vector<BlobItemBytesResponse> responses; | |
133 std::vector<base::Time> last_modified_times; | |
134 last_modified_times.resize(file_handles.size()); | |
135 for (const auto& request : *requests) { | |
136 base::Optional<base::Time> last_modified = WriteSingleRequestToDisk( | |
137 consolidation.get(), request, file_handles[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 return std::make_pair(responses, IPCBlobCreationCancelCode::UNKNOWN); | |
150 } | |
151 | |
55 } // namespace | 152 } // namespace |
56 | 153 |
57 BlobTransportController* BlobTransportController::GetInstance() { | 154 BlobTransportController* BlobTransportController::GetInstance() { |
58 return g_controller.Pointer(); | 155 return g_controller.Pointer(); |
59 } | 156 } |
60 | 157 |
61 // static | 158 // static |
62 void BlobTransportController::InitiateBlobTransfer( | 159 void BlobTransportController::InitiateBlobTransfer( |
63 const std::string& uuid, | 160 const std::string& uuid, |
64 const std::string& content_type, | 161 const std::string& content_type, |
65 std::unique_ptr<BlobConsolidation> consolidation, | 162 scoped_refptr<BlobConsolidation> consolidation, |
66 scoped_refptr<ThreadSafeSender> sender, | 163 scoped_refptr<ThreadSafeSender> sender, |
67 base::SingleThreadTaskRunner* io_runner, | 164 base::SingleThreadTaskRunner* io_runner, |
68 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 165 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
69 if (main_runner->BelongsToCurrentThread()) { | 166 if (main_runner->BelongsToCurrentThread()) { |
70 IncChildProcessRefCount(); | 167 IncChildProcessRefCount(); |
71 } else { | 168 } else { |
72 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); | 169 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); |
73 } | 170 } |
74 | 171 |
75 std::vector<storage::DataElement> descriptions; | 172 std::vector<storage::DataElement> descriptions; |
76 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); | 173 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); |
77 BlobTransportController::GetDescriptions( | 174 BlobTransportController::GetDescriptions( |
78 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); | 175 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); |
79 // I post the task first to make sure that we store our consolidation before | 176 // I post the task first to make sure that we store our consolidation before |
80 // we get a request back from the browser. | 177 // we get a request back from the browser. |
81 io_runner->PostTask( | 178 io_runner->PostTask( |
82 FROM_HERE, | 179 FROM_HERE, |
83 base::Bind(&BlobTransportController::StoreBlobDataForRequests, | 180 base::Bind(&BlobTransportController::StoreBlobDataForRequests, |
84 base::Unretained(BlobTransportController::GetInstance()), uuid, | 181 base::Unretained(BlobTransportController::GetInstance()), uuid, |
85 base::Passed(std::move(consolidation)), | 182 base::Passed(&consolidation), base::Passed(&main_runner))); |
86 base::Passed(std::move(main_runner)))); | |
87 // TODO(dmurph): Merge register and start messages. | 183 // TODO(dmurph): Merge register and start messages. |
88 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", | 184 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", |
89 referenced_blobs)); | 185 referenced_blobs)); |
90 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); | 186 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); |
91 } | 187 } |
92 | 188 |
93 void BlobTransportController::OnMemoryRequest( | 189 void BlobTransportController::OnMemoryRequest( |
94 const std::string& uuid, | 190 const std::string& uuid, |
95 const std::vector<storage::BlobItemBytesRequest>& requests, | 191 const std::vector<storage::BlobItemBytesRequest>& requests, |
96 std::vector<base::SharedMemoryHandle>* memory_handles, | 192 std::vector<base::SharedMemoryHandle>* memory_handles, |
97 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 193 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
194 base::TaskRunner* file_runner, | |
98 IPC::Sender* sender) { | 195 IPC::Sender* sender) { |
99 std::vector<storage::BlobItemBytesResponse> responses; | 196 std::vector<BlobItemBytesResponse> responses; |
100 ResponsesStatus status = | 197 auto it = blob_storage_.find(uuid); |
101 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | 198 // Ignore invalid messages. |
199 if (it == blob_storage_.end()) | |
200 return; | |
102 | 201 |
103 switch (status) { | 202 BlobConsolidation* consolidation = it->second.get(); |
104 case ResponsesStatus::BLOB_NOT_FOUND: | 203 const auto& consolidated_items = consolidation->consolidated_items(); |
105 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, | 204 |
106 // IPCBlobCreationCancelCode::UNKNOWN)); | 205 scoped_ptr<std::vector<BlobItemBytesRequest>> file_requests( |
107 return; | 206 new std::vector<BlobItemBytesRequest>()); |
108 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | 207 |
109 // This would happen if the renderer process doesn't have enough memory | 208 // 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 | 209 // requests, we keep them in a vector and lazily create them. |
111 // memory. If this scenario happens often, we could delay the response | 210 ScopedVector<SharedMemory> opened_memory; |
112 // until we have enough memory. For now we just fail. | 211 opened_memory.resize(memory_handles->size()); |
113 CHECK(false) << "Unable to map shared memory to send blob " << uuid | 212 for (const BlobItemBytesRequest& request : requests) { |
114 << "."; | 213 DCHECK_LT(request.renderer_item_index, consolidated_items.size()) |
115 break; | 214 << "Invalid item index"; |
116 case ResponsesStatus::SUCCESS: | 215 |
117 break; | 216 const ConsolidatedItem& item = |
217 consolidated_items[request.renderer_item_index]; | |
218 DCHECK_LE(request.renderer_item_offset + request.size, item.length) | |
219 << "Invalid data range"; | |
220 DCHECK_EQ(item.type, DataElement::TYPE_BYTES) << "Invalid element type"; | |
221 | |
222 switch (request.transport_strategy) { | |
223 case IPCBlobItemRequestStrategy::IPC: { | |
224 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
225 BlobItemBytesResponse& response = responses.back(); | |
226 ReadStatus status = consolidation->ReadMemory( | |
227 request.renderer_item_index, request.renderer_item_offset, | |
228 request.size, response.allocate_mutable_data(request.size)); | |
229 DCHECK(status == ReadStatus::OK) | |
230 << "Error reading from consolidated blob: " | |
231 << static_cast<int>(status); | |
232 break; | |
233 } | |
234 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
235 responses.push_back(BlobItemBytesResponse(request.request_number)); | |
236 DCHECK_LT(request.handle_index, memory_handles->size()) | |
237 << "Invalid handle index."; | |
238 SharedMemory* memory = opened_memory[request.handle_index]; | |
239 if (!memory) { | |
240 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
241 DCHECK(SharedMemory::IsHandleValid(handle)); | |
242 std::unique_ptr<SharedMemory> shared_memory( | |
243 new SharedMemory(handle, false)); | |
244 | |
245 if (!shared_memory->Map(request.size)) { | |
246 // This would happen if the renderer process doesn't have enough | |
247 // memory | |
248 // to map the shared memory, which is possible if we don't have much | |
249 // memory. If this scenario happens often, we could delay the | |
250 // response | |
251 // 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."; | |
michaeln
2016/04/26 22:50:08
style nit: the verbosity of logging at each dcheck
| |
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 ptr_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 ptr_factory_.InvalidateWeakPtrs(); | |
306 if (!blob_storage_.empty() && main_thread_runner_) { | |
307 main_thread_runner_->PostTask(FROM_HERE, | |
308 base::Bind(&DecChildProcessRefCount)); | |
michaeln
2016/04/26 22:50:08
refcounting bug? shouldn't there be one ref for ea
dmurph
2016/04/27 19:14:04
yep, I fixed this. nice catch!
| |
309 } | |
310 main_thread_runner_ = nullptr; | |
311 blob_storage_.clear(); | |
312 } | |
313 | |
135 // static | 314 // static |
136 void BlobTransportController::GetDescriptions( | 315 void BlobTransportController::GetDescriptions( |
137 BlobConsolidation* consolidation, | 316 BlobConsolidation* consolidation, |
138 size_t max_data_population, | 317 size_t max_data_population, |
139 std::vector<storage::DataElement>* out) { | 318 std::vector<storage::DataElement>* out) { |
140 DCHECK(out->empty()); | 319 DCHECK(out->empty()); |
141 DCHECK(consolidation); | 320 DCHECK(consolidation); |
142 const auto& consolidated_items = consolidation->consolidated_items(); | 321 const auto& consolidated_items = consolidation->consolidated_items(); |
143 | 322 |
144 size_t current_memory_population = 0; | 323 size_t current_memory_population = 0; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 } | 358 } |
180 case DataElement::TYPE_DISK_CACHE_ENTRY: | 359 case DataElement::TYPE_DISK_CACHE_ENTRY: |
181 case DataElement::TYPE_BYTES_DESCRIPTION: | 360 case DataElement::TYPE_BYTES_DESCRIPTION: |
182 case DataElement::TYPE_UNKNOWN: | 361 case DataElement::TYPE_UNKNOWN: |
183 NOTREACHED(); | 362 NOTREACHED(); |
184 } | 363 } |
185 ++current_item; | 364 ++current_item; |
186 } | 365 } |
187 } | 366 } |
188 | 367 |
189 BlobTransportController::BlobTransportController() {} | 368 BlobTransportController::BlobTransportController() : ptr_factory_(this) {} |
190 | 369 |
191 BlobTransportController::~BlobTransportController() {} | 370 BlobTransportController::~BlobTransportController() {} |
192 | 371 |
193 void BlobTransportController::ClearForTesting() { | 372 void BlobTransportController::OnFileWriteComplete( |
194 if (!blob_storage_.empty() && main_thread_runner_) { | 373 IPC::Sender* sender, |
195 main_thread_runner_->PostTask(FROM_HERE, | 374 const std::string& uuid, |
196 base::Bind(&DecChildProcessRefCount)); | 375 const std::pair<std::vector<BlobItemBytesResponse>, |
376 IPCBlobCreationCancelCode>& result) { | |
kinuko
2016/04/25 14:10:30
We might get called here after ReleaseBlobConsolid
dmurph
2016/04/26 22:33:28
I don't think this would be called in without it b
michaeln
2016/04/26 22:50:08
+1
michaeln
2016/04/28 00:16:56
+1 adding it
Just looking at this class, it does
| |
377 if (!result.first.empty()) { | |
378 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, result.first)); | |
379 return; | |
197 } | 380 } |
198 blob_storage_.clear(); | 381 sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, result.second)); |
382 ReleaseBlobConsolidation(uuid); | |
199 } | 383 } |
200 | 384 |
201 void BlobTransportController::StoreBlobDataForRequests( | 385 void BlobTransportController::StoreBlobDataForRequests( |
202 const std::string& uuid, | 386 const std::string& uuid, |
203 std::unique_ptr<BlobConsolidation> consolidation, | 387 scoped_refptr<BlobConsolidation> consolidation, |
204 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 388 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
205 if (!main_thread_runner_.get()) { | 389 if (!main_thread_runner_.get()) { |
206 main_thread_runner_ = std::move(main_runner); | 390 main_thread_runner_ = std::move(main_runner); |
207 } | 391 } |
208 blob_storage_[uuid] = std::move(consolidation); | 392 blob_storage_[uuid] = std::move(consolidation); |
209 } | 393 } |
210 | 394 |
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( | 395 void BlobTransportController::ReleaseBlobConsolidation( |
287 const std::string& uuid) { | 396 const std::string& uuid) { |
288 if (blob_storage_.erase(uuid)) { | 397 if (blob_storage_.erase(uuid)) { |
kinuko
2016/04/25 14:10:30
Now that consolidation is ref-counted we might hav
dmurph
2016/04/26 22:33:28
What do you think would happen here? The consolida
| |
289 main_thread_runner_->PostTask(FROM_HERE, | 398 main_thread_runner_->PostTask(FROM_HERE, |
290 base::Bind(&DecChildProcessRefCount)); | 399 base::Bind(&DecChildProcessRefCount)); |
291 } | 400 } |
292 } | 401 } |
293 | 402 |
294 } // namespace content | 403 } // namespace content |
OLD | NEW |