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> controller_ = | |
37 LAZY_INSTANCE_INITIALIZER; | |
38 } | |
39 | |
40 BlobTransportController* BlobTransportController::GetInstance() { | |
41 return controller_.Pointer(); | |
42 } | |
43 | |
44 BlobTransportController::~BlobTransportController() { | |
45 STLDeleteValues(&blob_storage_); | |
46 } | |
47 | |
48 void BlobTransportController::InitiateBlobTransfer( | |
49 const std::string& uuid, | |
50 const std::string& type, | |
51 scoped_ptr<BlobConsolidation> consolidation, | |
52 IPC::Sender* sender) { | |
53 bool not_found = HoldBlobConsolidation(uuid, consolidation.Pass()); | |
michaeln
2015/10/22 21:10:22
per the style-guide, to defend against duplicate u
dmurph
2015/10/22 23:43:04
Ok. I'll keep HoldBlob* as we use it in the tests.
| |
54 DCHECK(not_found); | |
55 if (!not_found) { | |
56 DVLOG(1) << "Blob " << uuid << " already being sent to browser."; | |
57 return; | |
58 } | |
59 | |
60 std::vector<storage::DataElement> descriptions; | |
61 GetDescriptions(uuid, kLargeThresholdBytes, &descriptions); | |
62 // TODO(dmurph): Uncomment when IPC messages are added. | |
63 // sender->Send(new BlobStorageMsg_StartBuildingBlob(uuid, type, | |
64 // descriptions)); | |
65 } | |
66 | |
67 void BlobTransportController::OnMemoryRequest( | |
68 const std::string& uuid, | |
69 const std::vector<storage::BlobItemBytesRequest>& requests, | |
70 std::vector<base::SharedMemoryHandle>* memory_handles, | |
71 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
72 IPC::Sender* sender) { | |
73 std::vector<storage::BlobItemBytesResponse> responses; | |
74 ResponsesStatus status = | |
75 GetResponses(uuid, requests, memory_handles, file_handles, &responses); | |
76 | |
77 bool success = false; | |
78 switch (status) { | |
79 case ResponsesStatus::BLOB_NOT_FOUND: | |
80 DVLOG(1) << "Unable to find blob " << uuid << "."; | |
81 return; | |
82 case ResponsesStatus::INVALID_ITEM_INDEX: | |
83 DVLOG(1) << "Index out of bounds for blob item in blob " << uuid << "."; | |
84 break; | |
85 case ResponsesStatus::INVALID_DATA_RANGE: | |
86 DVLOG(1) << "Data range out of bounds for blob item in blob " << uuid | |
87 << "."; | |
88 break; | |
89 case ResponsesStatus::INVALID_ITEM: | |
90 DVLOG(1) << "Requesting data from non-memory item in blob " << uuid | |
91 << "."; | |
92 break; | |
93 case ResponsesStatus::INVALID_HANDLE_INDEX: | |
94 DVLOG(1) << "Invalid handle index for transferring in blob " << uuid | |
95 << "."; | |
96 break; | |
97 case ResponsesStatus::SHARED_MEMORY_MAP_FAILED: | |
98 // This would happen if the renderer process doesn't have enough memory | |
99 // to map the shared memory, which is possible if we don't have much | |
100 // memory. If this scenario happens often, we could delay the response | |
101 // until we have enough memory. For now we just fail. | |
102 DVLOG(1) << "Unable to map shared memory to send blob " << uuid << "."; | |
103 break; | |
104 case ResponsesStatus::SUCCESS: | |
105 success = true; | |
106 break; | |
107 } | |
108 | |
109 if (success) { | |
110 // TODO(dmurph): Uncomment when IPC messages are added. | |
111 // sender->Send(new BlobStorageMsg_MemoryItemResponse(uuid, responses)); | |
112 } else { | |
113 DVLOG(1) << "Canceling blob transfer for blob " << uuid; | |
114 CancelBlobTransfer(uuid, IPCBlobCreationCancelCode::UNKNOWN, sender); | |
115 } | |
116 } | |
117 | |
118 void BlobTransportController::OnCancel( | |
119 const std::string& uuid, | |
120 storage::IPCBlobCreationCancelCode code) { | |
121 DVLOG(1) << "Received blob cancel for blob " << uuid << " with reason:"; | |
122 switch (code) { | |
123 case IPCBlobCreationCancelCode::UNKNOWN: | |
124 DVLOG(1) << "Unknown."; | |
125 break; | |
126 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: | |
127 DVLOG(1) << "Out of Memory."; | |
128 break; | |
129 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: | |
130 DVLOG(1) << "File Write Failed (Invalid cancel reason!)."; | |
131 break; | |
132 } | |
133 ReleaseBlob(uuid); | |
134 } | |
135 | |
136 void BlobTransportController::OnDone(const std::string& uuid) { | |
137 ReleaseBlob(uuid); | |
138 } | |
139 | |
140 void BlobTransportController::Clear() { | |
141 STLDeleteValues(&blob_storage_); | |
142 blob_storage_.clear(); | |
143 } | |
144 | |
145 BlobTransportController::BlobTransportController() {} | |
146 | |
147 void BlobTransportController::CancelBlobTransfer( | |
148 const std::string& uuid, | |
149 storage::IPCBlobCreationCancelCode code, | |
150 IPC::Sender* sender) { | |
151 // TODO(dmurph): Uncomment when IPC messages are added. | |
152 // sender->Send(new BlobStorageMsg_CancelBuildingBlob(uuid, code)); | |
153 ReleaseBlob(uuid); | |
154 } | |
155 | |
156 bool BlobTransportController::HoldBlobConsolidation( | |
157 const std::string& uuid, | |
158 scoped_ptr<BlobConsolidation> consolidation) { | |
159 if (blob_storage_.find(uuid) != blob_storage_.end()) { | |
160 return false; | |
161 } | |
162 blob_storage_.insert(std::make_pair(uuid, consolidation.release())); | |
163 return true; | |
164 } | |
165 | |
166 void BlobTransportController::GetDescriptions( | |
167 const std::string& uuid, | |
168 size_t max_data_population, | |
169 std::vector<storage::DataElement>* out) { | |
170 DCHECK(out->empty()); | |
171 BlobConsolidation* consolidation = GetConsolidation(uuid); | |
172 if (!consolidation) { | |
173 return; | |
174 } | |
175 const auto& consolidated_items = consolidation->consolidated_items(); | |
176 | |
177 size_t current_memory_population = 0; | |
178 size_t current_item = 0; | |
179 out->reserve(consolidated_items.size()); | |
180 for (const ConsolidatedItem& item : consolidated_items) { | |
181 out->push_back(DataElement()); | |
182 auto& element = out->back(); | |
183 | |
184 switch (item.type) { | |
185 case DataElement::TYPE_BYTES: { | |
186 size_t bytes_length = static_cast<size_t>(item.length); | |
187 if (current_memory_population + bytes_length <= max_data_population) { | |
188 element.SetToAllocatedBytes(bytes_length); | |
189 consolidation->ReadMemory(current_item, 0, bytes_length, | |
190 element.mutable_bytes()); | |
191 current_memory_population += bytes_length; | |
192 } else { | |
193 element.SetToBytesDescription(bytes_length); | |
194 } | |
195 break; | |
196 } | |
197 case DataElement::TYPE_FILE: { | |
198 element.SetToFilePathRange( | |
199 item.path, item.offset, item.length, | |
200 base::Time::FromDoubleT(item.expected_modification_time)); | |
201 break; | |
202 } | |
203 case DataElement::TYPE_BLOB: { | |
204 element.SetToBlobRange(item.blob_uuid, item.offset, item.length); | |
205 break; | |
206 } | |
207 case DataElement::TYPE_FILE_FILESYSTEM: { | |
208 element.SetToFileSystemUrlRange( | |
209 item.filesystem_url, item.offset, item.length, | |
210 base::Time::FromDoubleT(item.expected_modification_time)); | |
211 break; | |
212 } | |
213 case DataElement::TYPE_DISK_CACHE_ENTRY: | |
214 case DataElement::TYPE_BYTES_DESCRIPTION: | |
215 case DataElement::TYPE_UNKNOWN: | |
216 NOTREACHED(); | |
217 } | |
218 ++current_item; | |
219 } | |
220 } | |
221 | |
222 BlobTransportController::ResponsesStatus BlobTransportController::GetResponses( | |
223 const std::string& uuid, | |
224 const std::vector<BlobItemBytesRequest>& requests, | |
225 std::vector<SharedMemoryHandle>* memory_handles, | |
226 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
227 std::vector<BlobItemBytesResponse>* out) { | |
228 DCHECK(out->empty()); | |
229 BlobConsolidation* consolidation = GetConsolidation(uuid); | |
230 if (!consolidation) | |
231 return ResponsesStatus::BLOB_NOT_FOUND; | |
232 const auto& consolidated_items = consolidation->consolidated_items(); | |
233 | |
234 base::ScopedPtrHashMap<size_t, scoped_ptr<SharedMemory>> opened_memory; | |
235 for (const BlobItemBytesRequest& request : requests) { | |
236 if (request.renderer_item_index >= consolidated_items.size()) | |
237 return ResponsesStatus::INVALID_ITEM_INDEX; | |
238 | |
239 const ConsolidatedItem& item = | |
240 consolidated_items[request.renderer_item_index]; | |
241 if (request.renderer_item_offset + request.size > item.length) | |
242 return ResponsesStatus::INVALID_DATA_RANGE; | |
243 if (item.type != DataElement::TYPE_BYTES) | |
244 return ResponsesStatus::INVALID_ITEM; | |
245 | |
246 out->push_back(BlobItemBytesResponse(request.request_number)); | |
247 switch (request.transport_strategy) { | |
248 case IPCBlobItemRequestStrategy::IPC: { | |
249 BlobItemBytesResponse& response = out->back(); | |
250 ReadStatus status = consolidation->ReadMemory( | |
251 request.renderer_item_index, request.renderer_item_offset, | |
252 request.size, response.allocate_mutable_data(request.size)); | |
253 CHECK(status == ReadStatus::OK) | |
254 << "Error reading from consolidated blob: " | |
255 << static_cast<int>(status); | |
256 break; | |
257 } | |
258 case IPCBlobItemRequestStrategy::SHARED_MEMORY: { | |
259 if (request.handle_index >= memory_handles->size()) { | |
260 DCHECK_LT(request.handle_index, memory_handles->size()); | |
261 return ResponsesStatus::INVALID_HANDLE_INDEX; | |
262 } | |
263 SharedMemory* memory = nullptr; | |
264 auto memory_it = opened_memory.find(request.handle_index); | |
265 if (memory_it == opened_memory.end()) { | |
266 SharedMemoryHandle& handle = (*memory_handles)[request.handle_index]; | |
267 DCHECK(SharedMemory::IsHandleValid(handle)); | |
268 scoped_ptr<SharedMemory> shared_memory( | |
269 new SharedMemory(handle, false)); | |
270 if (!shared_memory->Map(request.size)) | |
271 return ResponsesStatus::SHARED_MEMORY_MAP_FAILED; | |
272 memory = shared_memory.get(); | |
273 opened_memory.add(request.handle_index, shared_memory.Pass()); | |
274 } else { | |
275 memory = memory_it->second; | |
276 } | |
277 CHECK(memory->memory()) << "Couldn't map memory for blob transfer."; | |
278 ReadStatus status = consolidation->ReadMemory( | |
279 request.renderer_item_index, request.renderer_item_offset, | |
280 request.size, | |
281 static_cast<char*>(memory->memory()) + request.handle_offset); | |
282 CHECK(status == ReadStatus::OK) | |
283 << "Error reading from consolidated blob: " | |
284 << static_cast<int>(status); | |
285 break; | |
286 } | |
287 case IPCBlobItemRequestStrategy::FILE: | |
288 NOTREACHED() << "TODO(dmurph): Not implemented."; | |
289 break; | |
290 case IPCBlobItemRequestStrategy::UNKNOWN: | |
291 NOTREACHED(); | |
292 break; | |
293 } | |
294 } | |
295 return ResponsesStatus::SUCCESS; | |
296 } | |
297 | |
298 void BlobTransportController::ReleaseBlob(const std::string& uuid) { | |
299 auto iter = blob_storage_.find(uuid); | |
300 if (iter != blob_storage_.end()) { | |
301 BlobConsolidation* consolidation = iter->second; | |
302 delete consolidation; | |
303 blob_storage_.erase(iter); | |
304 } | |
305 } | |
306 | |
307 BlobConsolidation* BlobTransportController::GetConsolidation( | |
308 const std::string& uuid) { | |
309 auto iter = blob_storage_.find(uuid); | |
310 if (iter == blob_storage_.end()) | |
311 return nullptr; | |
312 return iter->second; | |
313 } | |
314 | |
315 } // namespace content | |
OLD | NEW |