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