OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/blob_storage/blob_transport_controller.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "content/child/blob_storage/blob_consolidation.h" |
| 11 #include "content/child/thread_safe_sender.h" |
| 12 #include "ipc/ipc_sender.h" |
| 13 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
| 14 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 using storage::IPCBlobCreationCancelCode; |
| 19 |
| 20 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; |
| 21 using ReadStatus = BlobConsolidation::ReadStatus; |
| 22 using ResponsesStatus = BlobTransportTemporaryHolder::ResponsesStatus; |
| 23 |
| 24 namespace { |
| 25 const size_t kLargeThresholdBytes = 250 * 1024; |
| 26 static base::LazyInstance<BlobTransportController> controller_ = |
| 27 LAZY_INSTANCE_INITIALIZER; |
| 28 } |
| 29 |
| 30 BlobTransportController* BlobTransportController::GetInstance() { |
| 31 return controller_.Pointer(); |
| 32 } |
| 33 |
| 34 BlobTransportController::~BlobTransportController() {} |
| 35 |
| 36 void BlobTransportController::InitiateBlobTransfer( |
| 37 const std::string& uuid, |
| 38 const std::string& type, |
| 39 scoped_ptr<BlobConsolidation> consolidation, |
| 40 IPC::Sender* sender) { |
| 41 holder_.HoldBlobConsolidation(uuid, consolidation.Pass()); |
| 42 |
| 43 std::vector<storage::DataElement> descriptions; |
| 44 holder_.GetDescriptions(uuid, kLargeThresholdBytes, &descriptions); |
| 45 // TODO(dmurph): Uncomment when IPC messages are added. |
| 46 // sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, type, |
| 47 // descriptions)); |
| 48 } |
| 49 |
| 50 void BlobTransportController::OnMemoryRequest( |
| 51 const std::string& uuid, |
| 52 const std::vector<storage::BlobItemBytesRequest>& requests, |
| 53 std::vector<base::SharedMemoryHandle>* memory_handles, |
| 54 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 55 IPC::Sender* sender) { |
| 56 std::vector<storage::BlobItemBytesResponse> responses; |
| 57 ResponsesStatus status = holder_.GetResponses(uuid, requests, memory_handles, |
| 58 file_handles, &responses); |
| 59 |
| 60 bool success = false; |
| 61 switch (status) { |
| 62 case ResponsesStatus::BLOB_NOT_FOUND: |
| 63 DVLOG(1) << "Unable to find blob " << uuid << "."; |
| 64 return; |
| 65 case ResponsesStatus::INVALID_ITEM_INDEX: |
| 66 DVLOG(1) << "Index out of bounds for blob item in blob " << uuid << "."; |
| 67 break; |
| 68 case ResponsesStatus::INVALID_DATA_RANGE: |
| 69 DVLOG(1) << "Data range out of bounds for blob item in blob " << uuid |
| 70 << "."; |
| 71 break; |
| 72 case ResponsesStatus::INVALID_ITEM: |
| 73 DVLOG(1) << "Requesting data from non-memory item in blob " << uuid |
| 74 << "."; |
| 75 break; |
| 76 case ResponsesStatus::INVALID_HANDLE_INDEX: |
| 77 DVLOG(1) << "Invalid handle index for transferring in blob " << uuid |
| 78 << "."; |
| 79 break; |
| 80 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: |
| 81 // This would happen if the renderer process doesn't have enough memory |
| 82 // to map the shared memory, which is possible if we don't have much |
| 83 // memory. If this scenario happens often, we could delay the response |
| 84 // until we have enough memory. For now we just fail. |
| 85 DVLOG(1) << "Unable to map shared memory to send blob " << uuid << "."; |
| 86 break; |
| 87 case ResponsesStatus::UNKNOWN: |
| 88 DVLOG(1) << "Unknown error while populating data for blob " << uuid |
| 89 << "."; |
| 90 break; |
| 91 case ResponsesStatus::SUCCESS: |
| 92 success = true; |
| 93 break; |
| 94 } |
| 95 |
| 96 if (success) { |
| 97 // TODO(dmurph): Uncomment when IPC messages are added. |
| 98 // sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); |
| 99 } else { |
| 100 DVLOG(1) << "Canceling blob transfer for blob " << uuid; |
| 101 CancelBlobTransfer(uuid, IPCBlobCreationCancelCode::UNKNOWN, sender); |
| 102 } |
| 103 } |
| 104 |
| 105 void BlobTransportController::OnCancel( |
| 106 const std::string& uuid, |
| 107 storage::IPCBlobCreationCancelCode code) { |
| 108 DVLOG(1) << "Received blob cancel for blob " << uuid << " with reason:"; |
| 109 switch (code) { |
| 110 case IPCBlobCreationCancelCode::UNKNOWN: |
| 111 DVLOG(1) << "Unknown."; |
| 112 break; |
| 113 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: |
| 114 DVLOG(1) << "Out of Memory."; |
| 115 break; |
| 116 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: |
| 117 DVLOG(1) << "File Write Failed (Invalid cancel reason!)."; |
| 118 break; |
| 119 } |
| 120 holder_.ReleaseBlob(uuid); |
| 121 } |
| 122 |
| 123 void BlobTransportController::OnDone(const std::string& uuid) { |
| 124 holder_.ReleaseBlob(uuid); |
| 125 } |
| 126 |
| 127 BlobTransportController::BlobTransportController() {} |
| 128 |
| 129 void BlobTransportController::CancelBlobTransfer( |
| 130 const std::string& uuid, |
| 131 storage::IPCBlobCreationCancelCode code, |
| 132 IPC::Sender* sender) { |
| 133 // TODO(dmurph): Uncomment when IPC messages are added. |
| 134 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, code)); |
| 135 holder_.ReleaseBlob(uuid); |
| 136 } |
| 137 |
| 138 } // namespace content |
OLD | NEW |