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/single_thread_task_runner.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "content/child/blob_storage/blob_consolidation.h" | 15 #include "content/child/blob_storage/blob_consolidation.h" |
16 #include "content/child/child_process.h" | 16 #include "content/child/child_process.h" |
17 #include "content/child/thread_safe_sender.h" | 17 #include "content/child/thread_safe_sender.h" |
18 #include "content/common/fileapi/webblob_messages.h" | 18 #include "content/common/fileapi/webblob_messages.h" |
19 #include "ipc/ipc_sender.h" | 19 #include "ipc/ipc_sender.h" |
20 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 20 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
21 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 21 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
22 #include "storage/common/data_element.h" | 22 #include "storage/common/data_element.h" |
23 | 23 |
24 using base::SharedMemory; | 24 using base::SharedMemory; |
25 using base::SharedMemoryHandle; | 25 using base::SharedMemoryHandle; |
26 using storage::BlobItemBytesRequest; | 26 using storage::BlobItemBytesRequest; |
27 using storage::BlobItemBytesResponse; | 27 using storage::BlobItemBytesResponse; |
28 using storage::IPCBlobItemRequestStrategy; | 28 using storage::IPCBlobItemRequestStrategy; |
29 using storage::DataElement; | 29 using storage::DataElement; |
30 using storage::kBlobStorageIPCThresholdBytes; | |
30 | 31 |
31 namespace content { | 32 namespace content { |
32 | 33 |
33 using storage::IPCBlobCreationCancelCode; | 34 using storage::IPCBlobCreationCancelCode; |
34 | 35 |
35 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | 36 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
36 using ReadStatus = BlobConsolidation::ReadStatus; | 37 using ReadStatus = BlobConsolidation::ReadStatus; |
37 | 38 |
38 namespace { | 39 namespace { |
39 const size_t kLargeThresholdBytes = 250 * 1024; | 40 static base::LazyInstance<BlobTransportController>::Leaky g_controller = |
40 static base::LazyInstance<BlobTransportController> g_controller = | |
41 LAZY_INSTANCE_INITIALIZER; | 41 LAZY_INSTANCE_INITIALIZER; |
42 | 42 |
43 // This keeps the process alive while blobs are being transferred. | 43 // This keeps the process alive while blobs are being transferred. |
44 void IncChildProcessRefCount() { | 44 void IncChildProcessRefCount() { |
45 ChildProcess::current()->AddRefProcess(); | 45 ChildProcess::current()->AddRefProcess(); |
46 } | 46 } |
47 | 47 |
48 void DecChildProcessRefCount() { | 48 void DecChildProcessRefCount() { |
49 ChildProcess::current()->ReleaseProcess(); | 49 ChildProcess::current()->ReleaseProcess(); |
50 } | 50 } |
51 } // namespace | 51 } // namespace |
52 | 52 |
53 BlobTransportController* BlobTransportController::GetInstance() { | 53 BlobTransportController* BlobTransportController::GetInstance() { |
54 return g_controller.Pointer(); | 54 return g_controller.Pointer(); |
55 } | 55 } |
56 | 56 |
57 BlobTransportController::~BlobTransportController() {} | 57 // static |
58 | |
59 void BlobTransportController::InitiateBlobTransfer( | 58 void BlobTransportController::InitiateBlobTransfer( |
60 const std::string& uuid, | 59 const std::string& uuid, |
60 const std::string& content_type, | |
61 scoped_ptr<BlobConsolidation> consolidation, | 61 scoped_ptr<BlobConsolidation> consolidation, |
62 IPC::Sender* sender, | 62 scoped_refptr<ThreadSafeSender> sender, |
michaeln
2016/04/05 19:25:25
since you no longer xfer or take ownership, this c
| |
63 base::SingleThreadTaskRunner* io_runner, | |
63 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | 64 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { |
64 BlobConsolidation* consolidation_ptr = consolidation.get(); | 65 if (main_runner->BelongsToCurrentThread()) { |
65 if (blob_storage_.empty()) { | 66 IncChildProcessRefCount(); |
66 main_thread_runner_ = std::move(main_runner); | 67 } else { |
67 main_thread_runner_->PostTask(FROM_HERE, | 68 main_runner->PostTask(FROM_HERE, base::Bind(&IncChildProcessRefCount)); |
68 base::Bind(&IncChildProcessRefCount)); | |
69 } | 69 } |
70 blob_storage_[uuid] = std::move(consolidation); | 70 |
71 std::vector<storage::DataElement> descriptions; | 71 std::vector<storage::DataElement> descriptions; |
72 GetDescriptions(consolidation_ptr, kLargeThresholdBytes, &descriptions); | 72 std::set<std::string> referenced_blobs = consolidation->referenced_blobs(); |
73 BlobTransportController::GetDescriptions( | |
74 consolidation.get(), kBlobStorageIPCThresholdBytes, &descriptions); | |
75 // I post the task first to make sure that we store our consolidation before | |
76 // we get a request back from the browser. | |
77 io_runner->PostTask( | |
78 FROM_HERE, | |
79 base::Bind(&BlobTransportController::StoreBlobDataForRequests, | |
80 base::Unretained(BlobTransportController::GetInstance()), uuid, | |
81 base::Passed(std::move(consolidation)), | |
82 base::Passed(std::move(main_runner)))); | |
83 // TODO(dmurph): Merge register and start messages. | |
84 sender->Send(new BlobStorageMsg_RegisterBlobUUID(uuid, content_type, "", | |
85 referenced_blobs)); | |
73 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); | 86 sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, descriptions)); |
74 } | 87 } |
75 | 88 |
76 void BlobTransportController::OnMemoryRequest( | 89 void BlobTransportController::OnMemoryRequest( |
77 const std::string& uuid, | 90 const std::string& uuid, |
78 const std::vector<storage::BlobItemBytesRequest>& requests, | 91 const std::vector<storage::BlobItemBytesRequest>& requests, |
79 std::vector<base::SharedMemoryHandle>* memory_handles, | 92 std::vector<base::SharedMemoryHandle>* memory_handles, |
80 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 93 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
81 IPC::Sender* sender) { | 94 IPC::Sender* sender) { |
82 std::vector<storage::BlobItemBytesResponse> responses; | 95 std::vector<storage::BlobItemBytesResponse> responses; |
(...skipping 25 matching lines...) Expand all Loading... | |
108 storage::IPCBlobCreationCancelCode code) { | 121 storage::IPCBlobCreationCancelCode code) { |
109 DVLOG(1) << "Received blob cancel for blob " << uuid | 122 DVLOG(1) << "Received blob cancel for blob " << uuid |
110 << " with code: " << static_cast<int>(code); | 123 << " with code: " << static_cast<int>(code); |
111 ReleaseBlobConsolidation(uuid); | 124 ReleaseBlobConsolidation(uuid); |
112 } | 125 } |
113 | 126 |
114 void BlobTransportController::OnDone(const std::string& uuid) { | 127 void BlobTransportController::OnDone(const std::string& uuid) { |
115 ReleaseBlobConsolidation(uuid); | 128 ReleaseBlobConsolidation(uuid); |
116 } | 129 } |
117 | 130 |
118 void BlobTransportController::ClearForTesting() { | 131 // static |
119 if (!blob_storage_.empty() && main_thread_runner_) { | |
120 main_thread_runner_->PostTask(FROM_HERE, | |
121 base::Bind(&DecChildProcessRefCount)); | |
122 } | |
123 blob_storage_.clear(); | |
124 } | |
125 | |
126 BlobTransportController::BlobTransportController() {} | |
127 | |
128 void BlobTransportController::GetDescriptions( | 132 void BlobTransportController::GetDescriptions( |
129 BlobConsolidation* consolidation, | 133 BlobConsolidation* consolidation, |
130 size_t max_data_population, | 134 size_t max_data_population, |
131 std::vector<storage::DataElement>* out) { | 135 std::vector<storage::DataElement>* out) { |
132 DCHECK(out->empty()); | 136 DCHECK(out->empty()); |
133 DCHECK(consolidation); | 137 DCHECK(consolidation); |
134 const auto& consolidated_items = consolidation->consolidated_items(); | 138 const auto& consolidated_items = consolidation->consolidated_items(); |
135 | 139 |
136 size_t current_memory_population = 0; | 140 size_t current_memory_population = 0; |
137 size_t current_item = 0; | 141 size_t current_item = 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 } | 175 } |
172 case DataElement::TYPE_DISK_CACHE_ENTRY: | 176 case DataElement::TYPE_DISK_CACHE_ENTRY: |
173 case DataElement::TYPE_BYTES_DESCRIPTION: | 177 case DataElement::TYPE_BYTES_DESCRIPTION: |
174 case DataElement::TYPE_UNKNOWN: | 178 case DataElement::TYPE_UNKNOWN: |
175 NOTREACHED(); | 179 NOTREACHED(); |
176 } | 180 } |
177 ++current_item; | 181 ++current_item; |
178 } | 182 } |
179 } | 183 } |
180 | 184 |
185 BlobTransportController::BlobTransportController() {} | |
186 | |
187 BlobTransportController::~BlobTransportController() {} | |
188 | |
189 void BlobTransportController::ClearForTesting() { | |
190 if (!blob_storage_.empty() && main_thread_runner_) { | |
191 main_thread_runner_->PostTask(FROM_HERE, | |
192 base::Bind(&DecChildProcessRefCount)); | |
193 } | |
194 blob_storage_.clear(); | |
195 } | |
196 | |
197 void BlobTransportController::StoreBlobDataForRequests( | |
198 const std::string& uuid, | |
199 scoped_ptr<BlobConsolidation> consolidation, | |
200 scoped_refptr<base::SingleThreadTaskRunner> main_runner) { | |
201 if (!main_thread_runner_.get()) { | |
202 main_thread_runner_ = std::move(main_runner); | |
203 } | |
204 blob_storage_[uuid] = std::move(consolidation); | |
205 } | |
206 | |
181 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( | 207 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( |
182 const std::string& uuid, | 208 const std::string& uuid, |
183 const std::vector<BlobItemBytesRequest>& requests, | 209 const std::vector<BlobItemBytesRequest>& requests, |
184 std::vector<SharedMemoryHandle>* memory_handles, | 210 std::vector<SharedMemoryHandle>* memory_handles, |
185 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 211 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
186 std::vector<BlobItemBytesResponse>* out) { | 212 std::vector<BlobItemBytesResponse>* out) { |
187 DCHECK(out->empty()); | 213 DCHECK(out->empty()); |
188 auto it = blob_storage_.find(uuid); | 214 auto it = blob_storage_.find(uuid); |
189 if (it == blob_storage_.end()) | 215 if (it == blob_storage_.end()) |
190 return ResponsesStatus::BLOB_NOT_FOUND; | 216 return ResponsesStatus::BLOB_NOT_FOUND; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 case IPCBlobItemRequestStrategy::UNKNOWN: | 274 case IPCBlobItemRequestStrategy::UNKNOWN: |
249 NOTREACHED(); | 275 NOTREACHED(); |
250 break; | 276 break; |
251 } | 277 } |
252 } | 278 } |
253 return ResponsesStatus::SUCCESS; | 279 return ResponsesStatus::SUCCESS; |
254 } | 280 } |
255 | 281 |
256 void BlobTransportController::ReleaseBlobConsolidation( | 282 void BlobTransportController::ReleaseBlobConsolidation( |
257 const std::string& uuid) { | 283 const std::string& uuid) { |
258 // If we erased something and we're now empty, release the child process | 284 if (blob_storage_.erase(uuid)) { |
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, | 285 main_thread_runner_->PostTask(FROM_HERE, |
262 base::Bind(&DecChildProcessRefCount)); | 286 base::Bind(&DecChildProcessRefCount)); |
263 main_thread_runner_ = nullptr; | |
264 } | 287 } |
265 } | 288 } |
266 | 289 |
267 } // namespace content | 290 } // namespace content |
OLD | NEW |