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 <algorithm> |
7 #include <limits> | 8 #include <limits> |
8 #include <memory> | 9 #include <memory> |
| 10 #include <set> |
9 #include <utility> | 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/bind.h" | 14 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 15 #include "base/bind_helpers.h" |
14 #include "base/callback.h" | 16 #include "base/callback.h" |
15 #include "base/files/file.h" | 17 #include "base/files/file.h" |
16 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
17 #include "base/location.h" | 19 #include "base/location.h" |
18 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
19 #include "base/memory/scoped_vector.h" | 21 #include "base/memory/scoped_vector.h" |
20 #include "base/memory/shared_memory.h" | 22 #include "base/memory/shared_memory.h" |
21 #include "base/metrics/histogram_macros.h" | 23 #include "base/metrics/histogram_macros.h" |
22 #include "base/numerics/safe_conversions.h" | 24 #include "base/numerics/safe_conversions.h" |
23 #include "base/optional.h" | 25 #include "base/optional.h" |
24 #include "base/single_thread_task_runner.h" | 26 #include "base/single_thread_task_runner.h" |
25 #include "base/stl_util.h" | 27 #include "base/stl_util.h" |
26 #include "base/task_runner.h" | 28 #include "base/task_runner.h" |
27 #include "base/task_runner_util.h" | 29 #include "base/task_runner_util.h" |
28 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
29 #include "base/time/time.h" | 31 #include "base/time/time.h" |
30 #include "content/child/blob_storage/blob_consolidation.h" | 32 #include "content/child/blob_storage/blob_consolidation.h" |
31 #include "content/child/child_process.h" | 33 #include "content/child/child_process.h" |
32 #include "content/child/thread_safe_sender.h" | 34 #include "content/child/thread_safe_sender.h" |
33 #include "content/common/fileapi/webblob_messages.h" | 35 #include "content/common/fileapi/webblob_messages.h" |
34 #include "ipc/ipc_message.h" | 36 #include "ipc/ipc_message.h" |
35 #include "ipc/ipc_sender.h" | 37 #include "ipc/ipc_sender.h" |
| 38 #include "ipc/ipc_sync_message_filter.h" |
36 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 39 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
37 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 40 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
38 #include "storage/common/data_element.h" | 41 #include "storage/common/data_element.h" |
39 #include "third_party/WebKit/public/platform/Platform.h" | 42 #include "third_party/WebKit/public/platform/Platform.h" |
40 | 43 |
41 using base::File; | 44 using base::File; |
42 using base::SharedMemory; | 45 using base::SharedMemory; |
43 using base::SharedMemoryHandle; | 46 using base::SharedMemoryHandle; |
44 using storage::BlobItemBytesRequest; | 47 using storage::BlobItemBytesRequest; |
45 using storage::BlobItemBytesResponse; | 48 using storage::BlobItemBytesResponse; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 scoped_refptr<BlobConsolidation> consolidation, | 166 scoped_refptr<BlobConsolidation> consolidation, |
164 scoped_refptr<ThreadSafeSender> sender, | 167 scoped_refptr<ThreadSafeSender> sender, |
165 base::SingleThreadTaskRunner* io_runner, | 168 base::SingleThreadTaskRunner* io_runner, |
166 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 169 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
167 if (main_runner->BelongsToCurrentThread()) { | 170 if (main_runner->BelongsToCurrentThread()) { |
168 IncChildProcessRefCount(); | 171 IncChildProcessRefCount(); |
169 } else { | 172 } else { |
170 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); | 173 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); |
171 } | 174 } |
172 | 175 |
173 std::vector<storage::DataElement> descriptions; | |
174 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); | |
175 BlobTransportController::GetDescriptions( | |
176 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); | |
177 // I post the task first to make sure that we store our consolidation before | |
178 // we get a request back from the browser. | |
179 io_runner->PostTask( | 176 io_runner->PostTask( |
180 FROM_HERE, | 177 FROM_HERE, |
181 base::Bind(&BlobTransportController::StoreBlobDataForRequests, | 178 base::Bind(&BlobTransportController::StoreBlobDataForRequests, |
182 base::Unretained(BlobTransportController::GetInstance()), uuid, | 179 base::Unretained(BlobTransportController::GetInstance()), uuid, |
183 base::Passed(std::move(consolidation)), | 180 content_type, base::Passed(std::move(consolidation)), |
| 181 sender->sync_message_filter(), |
184 base::Passed(std::move(main_runner)))); | 182 base::Passed(std::move(main_runner)))); |
185 // TODO(dmurph): Merge register and start messages. | |
186 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", | |
187 referenced_blobs)); | |
188 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); | |
189 } | 183 } |
190 | 184 |
191 void BlobTransportController::OnMemoryRequest( | 185 void BlobTransportController::OnMemoryRequest( |
192 const std::string& uuid, | 186 const std::string& uuid, |
193 const std::vector<storage::BlobItemBytesRequest>& requests, | 187 const std::vector<storage::BlobItemBytesRequest>& requests, |
194 std::vector<base::SharedMemoryHandle>* memory_handles, | 188 std::vector<base::SharedMemoryHandle>* memory_handles, |
195 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 189 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
196 base::TaskRunner* file_runner, | 190 base::TaskRunner* file_runner, |
197 IPC::Sender* sender) { | 191 IPC::Sender* sender) { |
198 std::vector<BlobItemBytesResponse> responses; | 192 std::vector<BlobItemBytesResponse> responses; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 base::Bind(&WriteDiskRequests, make_scoped_refptr(consolidation), | 291 base::Bind(&WriteDiskRequests, make_scoped_refptr(consolidation), |
298 base::Passed(&file_requests), file_handles), | 292 base::Passed(&file_requests), file_handles), |
299 base::Bind(&BlobTransportController::OnFileWriteComplete, | 293 base::Bind(&BlobTransportController::OnFileWriteComplete, |
300 weak_factory_.GetWeakPtr(), sender, uuid)); | 294 weak_factory_.GetWeakPtr(), sender, uuid)); |
301 } | 295 } |
302 | 296 |
303 if (!responses.empty()) | 297 if (!responses.empty()) |
304 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | 298 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); |
305 } | 299 } |
306 | 300 |
307 void BlobTransportController::OnCancel( | 301 void BlobTransportController::OnBuildBlobComplete( |
308 const std::string& uuid, | 302 const std::string& uuid, |
309 storage::IPCBlobCreationCancelCode code) { | 303 mojom::BlobAssociatedPtr blob, |
310 DVLOG(1) << "Received blob cancel for blob " << uuid | 304 mojom::BlobCreationCancelCode cancel_code) { |
311 << " with code: " << static_cast<int>(code); | 305 if (blob) { |
312 ReleaseBlobConsolidation(uuid); | 306 ReleaseBlobConsolidation(uuid); |
313 } | 307 blob_proxies_.insert(uuid, std::move(blob)); |
314 | 308 } else { |
315 void BlobTransportController::OnDone(const std::string& uuid) { | 309 DVLOG(1) << "Received blob cancel for blob " << uuid |
316 ReleaseBlobConsolidation(uuid); | 310 << " with code: " << static_cast<int>(code); |
| 311 ReleaseBlobConsolidation(uuid); |
| 312 } |
317 } | 313 } |
318 | 314 |
319 void BlobTransportController::CancelAllBlobTransfers() { | 315 void BlobTransportController::CancelAllBlobTransfers() { |
320 weak_factory_.InvalidateWeakPtrs(); | 316 weak_factory_.InvalidateWeakPtrs(); |
321 if (!blob_storage_.empty() && main_thread_runner_) { | 317 if (!blob_storage_.empty() && main_thread_runner_) { |
322 main_thread_runner_->PostTask( | 318 main_thread_runner_->PostTask( |
323 FROM_HERE, | 319 FROM_HERE, |
324 base::Bind(&DecChildProcessRefCountTimes, blob_storage_.size())); | 320 base::Bind(&DecChildProcessRefCountTimes, blob_storage_.size())); |
325 } | 321 } |
326 main_thread_runner_ = nullptr; | 322 main_thread_runner_ = nullptr; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 if (!result.first.empty()) { | 391 if (!result.first.empty()) { |
396 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, result.first)); | 392 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, result.first)); |
397 return; | 393 return; |
398 } | 394 } |
399 sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, result.second)); | 395 sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, result.second)); |
400 ReleaseBlobConsolidation(uuid); | 396 ReleaseBlobConsolidation(uuid); |
401 } | 397 } |
402 | 398 |
403 void BlobTransportController::StoreBlobDataForRequests( | 399 void BlobTransportController::StoreBlobDataForRequests( |
404 const std::string& uuid, | 400 const std::string& uuid, |
| 401 const std::string& content_type, |
405 scoped_refptr<BlobConsolidation> consolidation, | 402 scoped_refptr<BlobConsolidation> consolidation, |
| 403 scoped_refptr<IPC::SyncMessageFilter> sync_message_filter, |
406 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 404 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
407 if (!main_thread_runner_.get()) { | 405 if (!main_thread_runner_.get()) { |
| 406 DCHECK(!blob_factory_.is_bound()); |
408 main_thread_runner_ = std::move(main_runner); | 407 main_thread_runner_ = std::move(main_runner); |
| 408 sync_message_filter->GetRemoteAssociatedInterface(&blob_factory_); |
409 } | 409 } |
| 410 |
| 411 std::set<std::string> referenced_blob_set = consolidation->referenced_blobs(); |
| 412 std::vector<std::string> referenced_blobs; |
| 413 std::copy(referenced_blob_set.begin(), referenced_blob_set.end(), |
| 414 std::back_inserter(referenced_blobs)); |
| 415 |
| 416 std::vector<storage::DataElement> descriptions; |
| 417 BlobTransportController::GetDescriptions( |
| 418 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); |
| 419 |
| 420 blob_factory_->BuildBlob( |
| 421 uuid, content_type, "", referenced_blobs, descriptions, |
| 422 base::Bind(&OnBuildBlobComplete, base::Unretained(this), uuid)); |
410 blob_storage_[uuid] = std::move(consolidation); | 423 blob_storage_[uuid] = std::move(consolidation); |
411 } | 424 } |
412 | 425 |
413 void BlobTransportController::ReleaseBlobConsolidation( | 426 void BlobTransportController::ReleaseBlobConsolidation( |
414 const std::string& uuid) { | 427 const std::string& uuid) { |
415 if (blob_storage_.erase(uuid)) { | 428 if (blob_storage_.erase(uuid)) { |
416 main_thread_runner_->PostTask(FROM_HERE, | 429 main_thread_runner_->PostTask(FROM_HERE, |
417 base::Bind(&DecChildProcessRefCount)); | 430 base::Bind(&DecChildProcessRefCount)); |
418 } | 431 } |
419 } | 432 } |
420 | 433 |
421 } // namespace content | 434 } // namespace content |
OLD | NEW |