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/containers/scoped_ptr_hash_map.h" | |
10 #include "base/lazy_instance.h" | |
11 #include "base/memory/shared_memory.h" | |
12 #include "base/stl_util.h" | |
13 #include "content/child/blob_storage/blob_consolidation.h" | |
14 #include "content/child/thread_safe_sender.h" | |
15 #include "ipc/ipc_sender.h" | |
16 #include "storage/common/blob_storage/blob_item_bytes_request.h" | |
17 #include "storage/common/blob_storage/blob_item_bytes_response.h" | |
18 #include "storage/common/data_element.h" | |
19 | |
20 using base::SharedMemory; | |
21 using base::SharedMemoryHandle; | |
22 using storage::BlobItemBytesRequest; | |
23 using storage::BlobItemBytesResponse; | |
24 using storage::IPCBlobItemRequestStrategy; | |
25 using storage::DataElement; | |
26 | |
27 namespace content { | |
28 | |
29 using storage::IPCBlobCreationCancelCode; | |
30 | |
31 using ConsolidatedItem = BlobConsolidation::ConsolidatedItem; | |
32 using ReadStatus = BlobConsolidation::ReadStatus; | |
33 | |
34 namespace { | |
35 const size_t kLargeThresholdBytes = 250 * 1024; | |
36 static base::LazyInstance<BlobTransportController> g_controller = | |
37 LAZY_INSTANCE_INITIALIZER; | |
38 } | |
39 | |
40 BlobTransportController* BlobTransportController::GetInstance() { | |
41 return g_controller.Pointer(); | |
42 } | |
43 | |
44 BlobTransportController::~BlobTransportController() {} | |
45 | |
46 void BlobTransportController::InitiateBlobTransfer( | |
47 const std::string& uuid, | |
48 const std::string& type, | |
49 scoped_ptr<BlobConsolidation> consolidation, | |
50 IPC::Sender* sender) { | |
51 BlobConsolidation* consolidation_ptr = consolidation.get(); | |
52 blob_storage_.insert(uuid, consolidation.Pass()); | |
53 std::vector<storage::DataElement> descriptions; | |
54 GetDescriptions(consolidation_ptr, kLargeThresholdBytes, &descriptions); | |
55 // TODO(dmurph): Uncomment when IPC messages are added. | |
56 // sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, type, | |
57 // descriptions)); | |
58 } | |
59 | |
60 void BlobTransportController::OnMemoryRequest( | |
61 const std::string& uuid, | |
62 const std::vector<storage::BlobItemBytesRequest>& requests, | |
63 std::vector<base::SharedMemoryHandle>* memory_handles, | |
64 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
65 IPC::Sender* sender) { | |
66 std::vector<storage::BlobItemBytesResponse> responses; | |
67 ResponsesStatus status = | |
68 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | |
69 | |
70 switch (status) { | |
71 case ResponsesStatus::BLOB_NOT_FOUND: | |
72 LOG(FATAL) << "Unable to find blob " << uuid << "."; | |
73 return; | |
74 case ResponsesStatus::INVALID_ITEM_INDEX: | |
75 LOG(FATAL) << "Index out of bounds for blob item in blob " << uuid << "."; | |
76 break; | |
77 case ResponsesStatus::INVALID_DATA_RANGE: | |
78 LOG(FATAL) << "Data range out of bounds for blob item in blob " << uuid | |
79 << "."; | |
80 break; | |
81 case ResponsesStatus::INVALID_ITEM: | |
82 LOG(FATAL) << "Requesting data from non-memory item in blob " << uuid | |
83 << "."; | |
84 break; | |
85 case ResponsesStatus::INVALID_HANDLE_INDEX: | |
86 LOG(FATAL) << "Invalid handle index for transferring in blob " << uuid | |
87 << "."; | |
88 break; | |
89 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | |
90 // This would happen if the renderer process doesn't have enough memory | |
91 // to map the shared memory, which is possible if we don't have much | |
92 // memory. If this scenario happens often, we could delay the response | |
93 // until we have enough memory. For now we just fail. | |
94 LOG(FATAL) << "Unable to map shared memory to send blob " << uuid << "."; | |
95 break; | |
96 case ResponsesStatus::SUCCESS: | |
97 break; | |
kinuko (google)
2015/10/28 03:53:36
If we're basically doing LOG(FATAL) it feels we co
dmurph
2015/10/29 20:42:27
I changed to DCHECKS in the method and then CHECKs
| |
98 } | |
99 | |
100 // TODO(dmurph): Uncomment when IPC messages are added. | |
101 // sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | |
102 } | |
103 | |
104 void BlobTransportController::OnCancel( | |
105 const std::string& uuid, | |
106 storage::IPCBlobCreationCancelCode code) { | |
107 DVLOG(1) << "Received blob cancel for blob " << uuid << " with reason:"; | |
108 switch (code) { | |
109 case IPCBlobCreationCancelCode::UNKNOWN: | |
110 DVLOG(1) << "Unknown."; | |
111 break; | |
112 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: | |
113 DVLOG(1) << "Out of Memory."; | |
114 break; | |
115 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: | |
116 DVLOG(1) << "File Write Failed (Invalid cancel reason!)."; | |
117 break; | |
118 } | |
119 ReleaseBlobConsolidation(uuid); | |
120 } | |
121 | |
122 void BlobTransportController::OnDone(const std::string& uuid) { | |
123 ReleaseBlobConsolidation(uuid); | |
124 } | |
125 | |
126 void BlobTransportController::Clear() { | |
127 blob_storage_.clear(); | |
128 } | |
129 | |
130 BlobTransportController::BlobTransportController() {} | |
131 | |
132 void BlobTransportController::CancelBlobTransfer( | |
133 const std::string& uuid, | |
134 storage::IPCBlobCreationCancelCode code, | |
135 IPC::Sender* sender) { | |
136 // TODO(dmurph): Uncomment when IPC messages are added. | |
137 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, code)); | |
138 ReleaseBlobConsolidation(uuid); | |
139 } | |
140 | |
141 void BlobTransportController::GetDescriptions( | |
142 BlobConsolidation* consolidation, | |
143 size_t max_data_population, | |
144 std::vector<storage::DataElement>* out) { | |
145 DCHECK(out->empty()); | |
146 DCHECK(consolidation); | |
147 const auto& consolidated_items = consolidation->consolidated_items(); | |
148 | |
149 size_t current_memory_population = 0; | |
150 size_t current_item = 0; | |
151 out->reserve(consolidated_items.size()); | |
152 for (const ConsolidatedItem& item : consolidated_items) { | |
153 out->push_back(DataElement()); | |
154 auto& element = out->back(); | |
155 | |
156 switch (item.type) { | |
157 case DataElement::TYPE_BYTES: { | |
158 size_t bytes_length = static_cast<size_t>(item.length); | |
159 if (current_memory_population + bytes_length <= max_data_population) { | |
160 element.SetToAllocatedBytes(bytes_length); | |
161 consolidation->ReadMemory(current_item, 0, bytes_length, | |
162 element.mutable_bytes()); | |
163 current_memory_population += bytes_length; | |
164 } else { | |
165 element.SetToBytesDescription(bytes_length); | |
166 } | |
167 break; | |
168 } | |
169 case DataElement::TYPE_FILE: { | |
170 element.SetToFilePathRange( | |
171 item.path, item.offset, item.length, | |
172 base::Time::FromDoubleT(item.expected_modification_time)); | |
173 break; | |
174 } | |
175 case DataElement::TYPE_BLOB: { | |
176 element.SetToBlobRange(item.blob_uuid, item.offset, item.length); | |
177 break; | |
178 } | |
179 case DataElement::TYPE_FILE_FILESYSTEM: { | |
180 element.SetToFileSystemUrlRange( | |
181 item.filesystem_url, item.offset, item.length, | |
182 base::Time::FromDoubleT(item.expected_modification_time)); | |
183 break; | |
184 } | |
185 case DataElement::TYPE_DISK_CACHE_ENTRY: | |
186 case DataElement::TYPE_BYTES_DESCRIPTION: | |
187 case DataElement::TYPE_UNKNOWN: | |
188 NOTREACHED(); | |
189 } | |
190 ++current_item; | |
191 } | |
192 } | |
193 | |
194 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( | |
195 const std::string& uuid, | |
196 const std::vector<BlobItemBytesRequest>& requests, | |
197 std::vector<SharedMemoryHandle>* memory_handles, | |
198 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
199 std::vector<BlobItemBytesResponse>* out) { | |
200 DCHECK(out->empty()); | |
201 auto it = blob_storage_.find(uuid); | |
202 if (it == blob_storage_.end()) | |
203 return ResponsesStatus::BLOB_NOT_FOUND; | |
204 | |
205 BlobConsolidation* consolidation = it->second; | |
206 const auto& consolidated_items = consolidation->consolidated_items(); | |
207 | |
208 base::ScopedPtrHashMap<size_t, scoped_ptr<SharedMemory>> opened_memory; | |
209 for (const BlobItemBytesRequest& request : requests) { | |
210 if (request.renderer_item_index >= consolidated_items.size()) | |
211 return ResponsesStatus::INVALID_ITEM_INDEX; | |
212 | |
213 const ConsolidatedItem& item = | |
214 consolidated_items[request.renderer_item_index]; | |
215 if (request.renderer_item_offset + request.size > item.length) | |
216 return ResponsesStatus::INVALID_DATA_RANGE; | |
217 if (item.type != DataElement::TYPE_BYTES) | |
218 return ResponsesStatus::INVALID_ITEM; | |
219 | |
220 out->push_back(BlobItemBytesResponse(request.request_number)); | |
221 switch (request.transport_strategy) { | |
222 case IPCBlobItemRequestStrategy::IPC: { | |
223 BlobItemBytesResponse& response = out->back(); | |
224 ReadStatus status = consolidation->ReadMemory( | |
225 request.renderer_item_index, request.renderer_item_offset, | |
226 request.size, response.allocate_mutable_data(request.size)); | |
227 CHECK(status == ReadStatus::OK) | |
228 << "Error reading from consolidated blob: " | |
229 << static_cast<int>(status); | |
230 break; | |
231 } | |
232 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
233 if (request.handle_index >= memory_handles->size()) { | |
234 return ResponsesStatus::INVALID_HANDLE_INDEX; | |
235 } | |
236 SharedMemory* memory = nullptr; | |
237 auto memory_it = opened_memory.find(request.handle_index); | |
238 if (memory_it == opened_memory.end()) { | |
239 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
240 DCHECK(SharedMemory::IsHandleValid(handle)); | |
241 scoped_ptr<SharedMemory> shared_memory( | |
242 new SharedMemory(handle, false)); | |
243 if (!shared_memory->Map(request.size)) | |
244 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; | |
245 memory = shared_memory.get(); | |
246 opened_memory.add(request.handle_index, shared_memory.Pass()); | |
247 } else { | |
248 memory = memory_it->second; | |
249 } | |
250 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
251 ReadStatus status = consolidation->ReadMemory( | |
252 request.renderer_item_index, request.renderer_item_offset, | |
253 request.size, | |
254 static_cast<char*>(memory->memory()) + request.handle_offset); | |
255 CHECK(status == ReadStatus::OK) | |
256 << "Error reading from consolidated blob: " | |
257 << static_cast<int>(status); | |
258 break; | |
259 } | |
260 case IPCBlobItemRequestStrategy::FILE: | |
261 NOTREACHED() << "TODO(dmurph): Not implemented."; | |
262 break; | |
263 case IPCBlobItemRequestStrategy::UNKNOWN: | |
264 NOTREACHED(); | |
265 break; | |
266 } | |
267 } | |
268 return ResponsesStatus::SUCCESS; | |
269 } | |
270 | |
271 void BlobTransportController::ReleaseBlobConsolidation( | |
272 const std::string& uuid) { | |
273 auto iter = blob_storage_.find(uuid); | |
274 if (iter != blob_storage_.end()) { | |
275 BlobConsolidation* consolidation = iter->second; | |
276 delete consolidation; | |
277 blob_storage_.erase(iter); | |
278 } | |
279 } | |
280 | |
281 } // namespace content | |
OLD | NEW |