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" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
| 13 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "content/child/blob_storage/blob_consolidation.h" | 15 #include "content/child/blob_storage/blob_consolidation.h" |
| 16 #include "content/child/child_process.h" |
15 #include "content/child/thread_safe_sender.h" | 17 #include "content/child/thread_safe_sender.h" |
| 18 #include "content/common/fileapi/webblob_messages.h" |
16 #include "ipc/ipc_sender.h" | 19 #include "ipc/ipc_sender.h" |
17 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 20 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
18 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 21 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
19 #include "storage/common/data_element.h" | 22 #include "storage/common/data_element.h" |
20 | 23 |
21 using base::SharedMemory; | 24 using base::SharedMemory; |
22 using base::SharedMemoryHandle; | 25 using base::SharedMemoryHandle; |
23 using storage::BlobItemBytesRequest; | 26 using storage::BlobItemBytesRequest; |
24 using storage::BlobItemBytesResponse; | 27 using storage::BlobItemBytesResponse; |
25 using storage::IPCBlobItemRequestStrategy; | 28 using storage::IPCBlobItemRequestStrategy; |
26 using storage::DataElement; | 29 using storage::DataElement; |
27 | 30 |
28 namespace content { | 31 namespace content { |
29 | 32 |
30 using storage::IPCBlobCreationCancelCode; | 33 using storage::IPCBlobCreationCancelCode; |
31 | 34 |
32 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | 35 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
33 using ReadStatus = BlobConsolidation::ReadStatus; | 36 using ReadStatus = BlobConsolidation::ReadStatus; |
34 | 37 |
35 namespace { | 38 namespace { |
36 const size_t kLargeThresholdBytes = 250 * 1024; | 39 const size_t kLargeThresholdBytes = 250 * 1024; |
37 static base::LazyInstance<BlobTransportController> g_controller = | 40 static base::LazyInstance<BlobTransportController> g_controller = |
38 LAZY_INSTANCE_INITIALIZER; | 41 LAZY_INSTANCE_INITIALIZER; |
| 42 |
| 43 // This keeps the process alive while blobs are being transferred. |
| 44 void IncChildProcessRefCount() { |
| 45 ChildProcess::current()->AddRefProcess(); |
| 46 } |
| 47 |
| 48 void DecChildProcessRefCount() { |
| 49 ChildProcess::current()->ReleaseProcess(); |
| 50 } |
39 } // namespace | 51 } // namespace |
40 | 52 |
41 BlobTransportController* BlobTransportController::GetInstance() { | 53 BlobTransportController* BlobTransportController::GetInstance() { |
42 return g_controller.Pointer(); | 54 return g_controller.Pointer(); |
43 } | 55 } |
44 | 56 |
45 BlobTransportController::~BlobTransportController() {} | 57 BlobTransportController::~BlobTransportController() {} |
46 | 58 |
47 void BlobTransportController::InitiateBlobTransfer( | 59 void BlobTransportController::InitiateBlobTransfer( |
48 const std::string& uuid, | 60 const std::string& uuid, |
49 const std::string& type, | |
50 scoped_ptr<BlobConsolidation> consolidation, | 61 scoped_ptr<BlobConsolidation> consolidation, |
51 IPC::Sender* sender) { | 62 IPC::Sender* sender, |
| 63 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
52 BlobConsolidation* consolidation_ptr = consolidation.get(); | 64 BlobConsolidation* consolidation_ptr = consolidation.get(); |
53 blob_storage_.insert(std::make_pair(uuid, std::move(consolidation))); | 65 if (blob_storage_.empty()) { |
| 66 main_thread_runner_ = std::move(main_runner); |
| 67 main_thread_runner_->PostTask(FROM_HERE, |
| 68 base::Bind(&IncChildProcessRefCount)); |
| 69 } |
| 70 blob_storage_[uuid] = std::move(consolidation); |
54 std::vector<storage::DataElement> descriptions; | 71 std::vector<storage::DataElement> descriptions; |
55 GetDescriptions(consolidation_ptr, kLargeThresholdBytes, &descriptions); | 72 GetDescriptions(consolidation_ptr, kLargeThresholdBytes, &descriptions); |
56 // TODO(dmurph): Uncomment when IPC messages are added. | 73 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); |
57 // sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, type, | |
58 // descriptions)); | |
59 } | 74 } |
60 | 75 |
61 void BlobTransportController::OnMemoryRequest( | 76 void BlobTransportController::OnMemoryRequest( |
62 const std::string& uuid, | 77 const std::string& uuid, |
63 const std::vector<storage::BlobItemBytesRequest>& requests, | 78 const std::vector<storage::BlobItemBytesRequest>& requests, |
64 std::vector<base::SharedMemoryHandle>* memory_handles, | 79 std::vector<base::SharedMemoryHandle>* memory_handles, |
65 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 80 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
66 IPC::Sender* sender) { | 81 IPC::Sender* sender) { |
67 std::vector<storage::BlobItemBytesResponse> responses; | 82 std::vector<storage::BlobItemBytesResponse> responses; |
68 ResponsesStatus status = | 83 ResponsesStatus status = |
69 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | 84 GetResponses(uuid, requests, memory_handles, file_handles, &responses); |
70 | 85 |
71 switch (status) { | 86 switch (status) { |
72 case ResponsesStatus::BLOB_NOT_FOUND: | 87 case ResponsesStatus::BLOB_NOT_FOUND: |
73 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, | 88 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, |
74 // IPCBlobCreationCancelCode::UNKNOWN)); | 89 // IPCBlobCreationCancelCode::UNKNOWN)); |
75 return; | 90 return; |
76 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | 91 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: |
77 // This would happen if the renderer process doesn't have enough memory | 92 // This would happen if the renderer process doesn't have enough memory |
78 // to map the shared memory, which is possible if we don't have much | 93 // to map the shared memory, which is possible if we don't have much |
79 // memory. If this scenario happens often, we could delay the response | 94 // memory. If this scenario happens often, we could delay the response |
80 // until we have enough memory. For now we just fail. | 95 // until we have enough memory. For now we just fail. |
81 CHECK(false) << "Unable to map shared memory to send blob " << uuid | 96 CHECK(false) << "Unable to map shared memory to send blob " << uuid |
82 << "."; | 97 << "."; |
83 break; | 98 break; |
84 case ResponsesStatus::SUCCESS: | 99 case ResponsesStatus::SUCCESS: |
85 break; | 100 break; |
86 } | 101 } |
87 | 102 |
88 // TODO(dmurph): Uncomment when IPC messages are added. | 103 sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); |
89 // sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | |
90 } | 104 } |
91 | 105 |
92 void BlobTransportController::OnCancel( | 106 void BlobTransportController::OnCancel( |
93 const std::string& uuid, | 107 const std::string& uuid, |
94 storage::IPCBlobCreationCancelCode code) { | 108 storage::IPCBlobCreationCancelCode code) { |
95 DVLOG(1) << "Received blob cancel for blob " << uuid << " with reason:"; | 109 DVLOG(1) << "Received blob cancel for blob " << uuid |
96 switch (code) { | 110 << " with code: " << static_cast<int>(code); |
97 case IPCBlobCreationCancelCode::UNKNOWN: | |
98 DVLOG(1) << "Unknown."; | |
99 break; | |
100 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: | |
101 DVLOG(1) << "Out of Memory."; | |
102 break; | |
103 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: | |
104 DVLOG(1) << "File Write Failed (Invalid cancel reason!)."; | |
105 break; | |
106 } | |
107 ReleaseBlobConsolidation(uuid); | 111 ReleaseBlobConsolidation(uuid); |
108 } | 112 } |
109 | 113 |
110 void BlobTransportController::OnDone(const std::string& uuid) { | 114 void BlobTransportController::OnDone(const std::string& uuid) { |
111 ReleaseBlobConsolidation(uuid); | 115 ReleaseBlobConsolidation(uuid); |
112 } | 116 } |
113 | 117 |
114 void BlobTransportController::Clear() { | 118 void BlobTransportController::ClearForTesting() { |
| 119 if (!blob_storage_.empty() && main_thread_runner_) { |
| 120 main_thread_runner_->PostTask(FROM_HERE, |
| 121 base::Bind(&DecChildProcessRefCount)); |
| 122 } |
115 blob_storage_.clear(); | 123 blob_storage_.clear(); |
116 } | 124 } |
117 | 125 |
118 BlobTransportController::BlobTransportController() {} | 126 BlobTransportController::BlobTransportController() {} |
119 | 127 |
120 void BlobTransportController::CancelBlobTransfer(const std::string& uuid, | |
121 IPCBlobCreationCancelCode code, | |
122 IPC::Sender* sender) { | |
123 // TODO(dmurph): Uncomment when IPC messages are added. | |
124 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, code)); | |
125 ReleaseBlobConsolidation(uuid); | |
126 } | |
127 | |
128 void BlobTransportController::GetDescriptions( | 128 void BlobTransportController::GetDescriptions( |
129 BlobConsolidation* consolidation, | 129 BlobConsolidation* consolidation, |
130 size_t max_data_population, | 130 size_t max_data_population, |
131 std::vector<storage::DataElement>* out) { | 131 std::vector<storage::DataElement>* out) { |
132 DCHECK(out->empty()); | 132 DCHECK(out->empty()); |
133 DCHECK(consolidation); | 133 DCHECK(consolidation); |
134 const auto& consolidated_items = consolidation->consolidated_items(); | 134 const auto& consolidated_items = consolidation->consolidated_items(); |
135 | 135 |
136 size_t current_memory_population = 0; | 136 size_t current_memory_population = 0; |
137 size_t current_item = 0; | 137 size_t current_item = 0; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 case IPCBlobItemRequestStrategy::UNKNOWN: | 248 case IPCBlobItemRequestStrategy::UNKNOWN: |
249 NOTREACHED(); | 249 NOTREACHED(); |
250 break; | 250 break; |
251 } | 251 } |
252 } | 252 } |
253 return ResponsesStatus::SUCCESS; | 253 return ResponsesStatus::SUCCESS; |
254 } | 254 } |
255 | 255 |
256 void BlobTransportController::ReleaseBlobConsolidation( | 256 void BlobTransportController::ReleaseBlobConsolidation( |
257 const std::string& uuid) { | 257 const std::string& uuid) { |
258 blob_storage_.erase(uuid); | 258 // If we erased something and we're now empty, release the child process |
| 259 // ref count and deref the main thread runner. |
| 260 if (blob_storage_.erase(uuid) && blob_storage_.empty()) { |
| 261 main_thread_runner_->PostTask(FROM_HERE, |
| 262 base::Bind(&DecChildProcessRefCount)); |
| 263 main_thread_runner_ = nullptr; |
| 264 } |
259 } | 265 } |
260 | 266 |
261 } // namespace content | 267 } // namespace content |
OLD | NEW |