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 "storage/browser/blob/blob_async_builder_host.h" | 5 #include "storage/browser/blob/blob_async_builder_host.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
16 #include "storage/browser/blob/blob_data_handle.h" | 16 #include "storage/browser/blob/blob_data_handle.h" |
| 17 #include "storage/browser/blob/blob_memory_controller.h" |
17 #include "storage/browser/blob/blob_storage_context.h" | 18 #include "storage/browser/blob/blob_storage_context.h" |
18 | 19 |
19 namespace storage { | 20 namespace storage { |
20 namespace { | 21 using MemoryStrategyResult = BlobMemoryController::MemoryStrategyResult; |
21 | |
22 bool CalculateBlobMemorySize(const std::vector<DataElement>& elements, | |
23 size_t* shortcut_bytes, | |
24 uint64_t* total_bytes) { | |
25 DCHECK(shortcut_bytes); | |
26 DCHECK(total_bytes); | |
27 base::CheckedNumeric<uint64_t> total_size_checked = 0; | |
28 base::CheckedNumeric<size_t> shortcut_size_checked = 0; | |
29 for (const auto& e : elements) { | |
30 if (e.type() == DataElement::TYPE_BYTES) { | |
31 total_size_checked += e.length(); | |
32 shortcut_size_checked += e.length(); | |
33 } else if (e.type() == DataElement::TYPE_BYTES_DESCRIPTION) { | |
34 total_size_checked += e.length(); | |
35 } else { | |
36 continue; | |
37 } | |
38 if (!total_size_checked.IsValid() || !shortcut_size_checked.IsValid()) { | |
39 return false; | |
40 } | |
41 } | |
42 *shortcut_bytes = shortcut_size_checked.ValueOrDie(); | |
43 *total_bytes = total_size_checked.ValueOrDie(); | |
44 return true; | |
45 } | |
46 | |
47 IPCBlobCreationCancelCode ConvertReferencedBlobErrorToConstructingError( | |
48 IPCBlobCreationCancelCode referenced_blob_error) { | |
49 switch (referenced_blob_error) { | |
50 // For most cases we propagate the error. | |
51 case IPCBlobCreationCancelCode::FILE_WRITE_FAILED: | |
52 case IPCBlobCreationCancelCode::SOURCE_DIED_IN_TRANSIT: | |
53 case IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN: | |
54 case IPCBlobCreationCancelCode::OUT_OF_MEMORY: | |
55 return referenced_blob_error; | |
56 // Others we report that the referenced blob is broken, as we don't know | |
57 // why (the BLOB_DEREFERENCED_WHILE_BUILDING should never happen, as we hold | |
58 // onto the reference of the blobs we're using). | |
59 case IPCBlobCreationCancelCode::BLOB_DEREFERENCED_WHILE_BUILDING: | |
60 DCHECK(false) << "Referenced blob should never be dereferenced while we " | |
61 << "are depending on it, as our system holds a handle."; | |
62 case IPCBlobCreationCancelCode::UNKNOWN: | |
63 return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; | |
64 } | |
65 NOTREACHED(); | |
66 return IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 using MemoryItemRequest = | 22 using MemoryItemRequest = |
72 BlobAsyncTransportRequestBuilder::RendererMemoryItemRequest; | 23 BlobAsyncTransportRequestBuilder::RendererMemoryItemRequest; |
73 | 24 |
74 BlobAsyncBuilderHost::BlobBuildingState::BlobBuildingState( | 25 BlobAsyncBuilderHost::BlobBuildingState::BlobBuildingState( |
75 const std::string& uuid, | 26 const std::string& uuid) |
76 std::set<std::string> referenced_blob_uuids, | 27 : data_builder(uuid) {} |
77 std::vector<std::unique_ptr<BlobDataHandle>>* referenced_blob_handles) | |
78 : data_builder(uuid), | |
79 referenced_blob_uuids(referenced_blob_uuids), | |
80 referenced_blob_handles(std::move(*referenced_blob_handles)) {} | |
81 | 28 |
82 BlobAsyncBuilderHost::BlobBuildingState::~BlobBuildingState() {} | 29 BlobAsyncBuilderHost::BlobBuildingState::~BlobBuildingState() {} |
83 | 30 |
84 BlobAsyncBuilderHost::BlobAsyncBuilderHost() : ptr_factory_(this) {} | 31 BlobAsyncBuilderHost::BlobAsyncBuilderHost() : ptr_factory_(this) {} |
85 | 32 |
86 BlobAsyncBuilderHost::~BlobAsyncBuilderHost() {} | 33 BlobAsyncBuilderHost::~BlobAsyncBuilderHost() {} |
87 | 34 |
88 BlobTransportResult BlobAsyncBuilderHost::RegisterBlobUUID( | 35 BlobStatus BlobAsyncBuilderHost::RegisterBlob( |
89 const std::string& uuid, | 36 const std::string& uuid, |
90 const std::string& content_type, | 37 const std::string& content_type, |
91 const std::string& content_disposition, | 38 const std::string& content_disposition, |
92 const std::set<std::string>& referenced_blob_uuids, | |
93 BlobStorageContext* context) { | |
94 if (async_blob_map_.find(uuid) != async_blob_map_.end()) | |
95 return BlobTransportResult::BAD_IPC; | |
96 if (referenced_blob_uuids.find(uuid) != referenced_blob_uuids.end()) | |
97 return BlobTransportResult::BAD_IPC; | |
98 context->CreatePendingBlob(uuid, content_type, content_disposition); | |
99 std::vector<std::unique_ptr<BlobDataHandle>> handles; | |
100 for (const std::string& referenced_uuid : referenced_blob_uuids) { | |
101 std::unique_ptr<BlobDataHandle> handle = | |
102 context->GetBlobDataFromUUID(referenced_uuid); | |
103 if (!handle || handle->IsBroken()) { | |
104 // We cancel the blob right away, and don't bother storing our state. | |
105 context->CancelPendingBlob( | |
106 uuid, IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN); | |
107 return BlobTransportResult::CANCEL_REFERENCED_BLOB_BROKEN; | |
108 } | |
109 handles.emplace_back(std::move(handle)); | |
110 } | |
111 async_blob_map_[uuid] = base::WrapUnique( | |
112 new BlobBuildingState(uuid, referenced_blob_uuids, &handles)); | |
113 return BlobTransportResult::DONE; | |
114 } | |
115 | |
116 BlobTransportResult BlobAsyncBuilderHost::StartBuildingBlob( | |
117 const std::string& uuid, | |
118 const std::vector<DataElement>& elements, | 39 const std::vector<DataElement>& elements, |
119 size_t memory_available, | |
120 BlobStorageContext* context, | 40 BlobStorageContext* context, |
121 const RequestMemoryCallback& request_memory) { | 41 const RequestMemoryCallback& request_memory, |
| 42 const BlobStatusCallback& status_callback) { |
122 DCHECK(context); | 43 DCHECK(context); |
123 DCHECK(async_blob_map_.find(uuid) != async_blob_map_.end()); | 44 if (async_blob_map_.find(uuid) != async_blob_map_.end()) { |
124 | 45 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
125 // Step 1: Get the sizes. | 46 } |
126 size_t shortcut_memory_size_bytes = 0; | 47 |
127 uint64_t total_memory_size_bytes = 0; | 48 const BlobMemoryController& memory_controller = context->memory_controller(); |
128 if (!CalculateBlobMemorySize(elements, &shortcut_memory_size_bytes, | 49 uint64_t transport_memory_size = 0; |
129 &total_memory_size_bytes)) { | 50 MemoryStrategyResult memory_strategy; |
130 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 51 if (!memory_controller.DecideBlobTransportationMemoryStrategy( |
131 return BlobTransportResult::BAD_IPC; | 52 elements, &transport_memory_size, &memory_strategy)) |
132 } | 53 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
133 | 54 |
134 // Step 2: Check if we have enough memory to store the blob. | 55 // Validate that our referenced blobs aren't us. |
135 if (total_memory_size_bytes > memory_available) { | 56 for (const DataElement& e : elements) { |
136 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::OUT_OF_MEMORY, context); | 57 if (e.type() == DataElement::TYPE_BLOB && e.blob_uuid() == uuid) |
137 return BlobTransportResult::CANCEL_MEMORY_FULL; | 58 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
138 } | 59 } |
139 | 60 |
140 // From here on, we know we can fit the blob in memory. | 61 std::unique_ptr<BlobBuildingState> state(new BlobBuildingState(uuid)); |
141 BlobBuildingState* state_ptr = async_blob_map_[uuid].get(); | 62 state->data_builder.set_content_type(content_type); |
142 if (!state_ptr->request_builder.requests().empty()) { | 63 state->data_builder.set_content_disposition(content_disposition); |
143 // Check that we're not a duplicate call. | 64 state->request_memory_callback = request_memory; |
144 return BlobTransportResult::BAD_IPC; | 65 |
145 } | 66 std::unique_ptr<BlobDataHandle> temp_handle; |
| 67 |
| 68 switch (memory_strategy) { |
| 69 case MemoryStrategyResult::TOO_LARGE: |
| 70 temp_handle = |
| 71 context->AddBrokenBlob(uuid, content_type, content_disposition, |
| 72 BlobStatus::ERR_OUT_OF_MEMORY); |
| 73 context->IncrementBlobRefCount(uuid); |
| 74 return BlobStatus::ERR_OUT_OF_MEMORY; |
| 75 case MemoryStrategyResult::NONE_NEEDED: { |
| 76 for (const DataElement& e : elements) { |
| 77 DCHECK_NE(e.type(), DataElement::TYPE_BYTES_DESCRIPTION); |
| 78 state->data_builder.AppendIPCDataElement(e); |
| 79 } |
| 80 temp_handle = |
| 81 context->BuildBlob(state->data_builder, BlobStatusCallback()); |
| 82 context->IncrementBlobRefCount(uuid); |
| 83 return BlobStatus::DONE; |
| 84 } |
| 85 case MemoryStrategyResult::IPC: |
| 86 state->strategy = IPCBlobItemRequestStrategy::IPC; |
| 87 state->request_builder.InitializeForIPCRequests( |
| 88 memory_controller.max_ipc_memory_size(), transport_memory_size, |
| 89 elements, &(state->data_builder)); |
| 90 break; |
| 91 case MemoryStrategyResult::SHARED_MEMORY: |
| 92 state->strategy = IPCBlobItemRequestStrategy::SHARED_MEMORY; |
| 93 state->request_builder.InitializeForSharedMemoryRequests( |
| 94 memory_controller.max_shared_memory_size(), transport_memory_size, |
| 95 elements, &(state->data_builder)); |
| 96 break; |
| 97 case MemoryStrategyResult::FILE: |
| 98 state->strategy = IPCBlobItemRequestStrategy::FILE; |
| 99 state->request_builder.InitializeForFileRequests( |
| 100 memory_controller.max_file_size(), transport_memory_size, elements, |
| 101 &(state->data_builder)); |
| 102 break; |
| 103 } |
| 104 // We initialize our requests received state now that they are populated. |
| 105 state->request_received.resize(state->request_builder.requests().size(), |
| 106 false); |
| 107 |
| 108 BlobBuildingState* state_ptr = state.get(); |
| 109 async_blob_map_[uuid] = std::move(state); |
| 110 |
146 state_ptr->request_memory_callback = request_memory; | 111 state_ptr->request_memory_callback = request_memory; |
147 | 112 state_ptr->status_callback = status_callback; |
148 // Step 3: Check to make sure the referenced blob information we received | 113 |
149 // earlier is correct: | 114 // We special case the file strategy, where we know they don't have to fit in |
150 std::set<std::string> extracted_blob_uuids; | 115 // memory, and we create the temporary files right away. |
151 for (const DataElement& e : elements) { | 116 if (memory_strategy == MemoryStrategyResult::FILE) { |
152 if (e.type() == DataElement::TYPE_BLOB) { | 117 LOG(ERROR) << "file!"; |
153 extracted_blob_uuids.insert(e.blob_uuid()); | 118 const auto& file_sizes = state_ptr->request_builder.file_sizes(); |
154 // We can't depend on ourselves. | 119 state_ptr->files.resize(file_sizes.size()); |
155 if (e.blob_uuid() == uuid) { | 120 for (size_t i = 0; i < file_sizes.size(); i++) { |
156 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 121 context->mutable_memory_controller()->CreateTemporaryFile( |
157 return BlobTransportResult::BAD_IPC; | 122 file_sizes[i], base::Bind(&BlobAsyncBuilderHost::OnFileCreated, |
158 } | 123 ptr_factory_.GetWeakPtr(), uuid, i)); |
159 } | 124 } |
160 } | 125 temp_handle = |
161 if (extracted_blob_uuids != state_ptr->referenced_blob_uuids) { | 126 context->BuildBlob(state_ptr->data_builder, BlobStatusCallback()); |
162 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 127 context->IncrementBlobRefCount(uuid); |
163 return BlobTransportResult::BAD_IPC; | 128 return BlobStatus::PENDING_DATA_POPULATION; |
164 } | 129 } |
165 | 130 |
166 // Step 4: Decide if we're using the shortcut method. This will also catch | 131 temp_handle = context->BuildBlob( |
167 // the case where we don't have any memory items. | 132 state_ptr->data_builder, |
168 if (shortcut_memory_size_bytes == total_memory_size_bytes && | 133 base::Bind(&BlobAsyncBuilderHost::OnCanStartBuildingBlob, |
169 shortcut_memory_size_bytes <= memory_available) { | 134 ptr_factory_.GetWeakPtr(), uuid, context->AsWeakPtr())); |
170 for (const DataElement& e : elements) { | 135 context->IncrementBlobRefCount(uuid); |
171 state_ptr->data_builder.AppendIPCDataElement(e); | 136 |
172 } | 137 if (temp_handle->IsBroken()) { |
173 FinishBuildingBlob(state_ptr, context); | 138 async_blob_map_.erase(uuid); |
174 return BlobTransportResult::DONE; | 139 } |
175 } | 140 return temp_handle->GetBlobStatus(); |
176 | 141 } |
177 // From here on, we know the blob's size is less than |memory_available|, | 142 |
178 // so we know we're < max(size_t). | 143 BlobStatus BlobAsyncBuilderHost::OnMemoryResponses( |
179 // Step 5: Decide if we're using shared memory. | |
180 if (total_memory_size_bytes > max_ipc_memory_size_) { | |
181 state_ptr->request_builder.InitializeForSharedMemoryRequests( | |
182 max_shared_memory_size_, total_memory_size_bytes, elements, | |
183 &(state_ptr->data_builder)); | |
184 } else { | |
185 // Step 6: We can fit in IPC. | |
186 state_ptr->request_builder.InitializeForIPCRequests( | |
187 max_ipc_memory_size_, total_memory_size_bytes, elements, | |
188 &(state_ptr->data_builder)); | |
189 } | |
190 // We initialize our requests received state now that they are populated. | |
191 state_ptr->request_received.resize( | |
192 state_ptr->request_builder.requests().size(), false); | |
193 return ContinueBlobMemoryRequests(uuid, context); | |
194 } | |
195 | |
196 BlobTransportResult BlobAsyncBuilderHost::OnMemoryResponses( | |
197 const std::string& uuid, | 144 const std::string& uuid, |
198 const std::vector<BlobItemBytesResponse>& responses, | 145 const std::vector<BlobItemBytesResponse>& responses, |
199 BlobStorageContext* context) { | 146 BlobStorageContext* context) { |
200 AsyncBlobMap::const_iterator state_it = async_blob_map_.find(uuid); | 147 AsyncBlobMap::const_iterator state_it = async_blob_map_.find(uuid); |
201 if (state_it == async_blob_map_.end()) { | 148 if (state_it == async_blob_map_.end()) { |
202 DVLOG(1) << "Could not find blob " << uuid; | 149 DVLOG(1) << "Could not find blob " << uuid; |
203 return BlobTransportResult::BAD_IPC; | 150 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
204 } | 151 } |
205 if (responses.empty()) { | 152 if (responses.empty()) { |
206 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 153 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
207 return BlobTransportResult::BAD_IPC; | 154 context); |
208 } | 155 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 156 } |
| 157 |
| 158 // Validate response sanity: it should refer to a legal request number, and |
| 159 // we shouldn't have received an answer for that request yet. |
209 BlobAsyncBuilderHost::BlobBuildingState* state = state_it->second.get(); | 160 BlobAsyncBuilderHost::BlobBuildingState* state = state_it->second.get(); |
210 BlobAsyncTransportRequestBuilder& request_builder = state->request_builder; | 161 const auto& requests = state->request_builder.requests(); |
211 const auto& requests = request_builder.requests(); | |
212 for (const BlobItemBytesResponse& response : responses) { | 162 for (const BlobItemBytesResponse& response : responses) { |
213 if (response.request_number >= requests.size()) { | 163 if (response.request_number >= requests.size()) { |
214 // Bad IPC, so we delete our record and ignore. | 164 // Bad IPC, so we delete our record and ignore. |
215 DVLOG(1) << "Invalid request number " << response.request_number; | 165 DVLOG(1) << "Invalid request number " << response.request_number; |
216 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 166 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
217 return BlobTransportResult::BAD_IPC; | 167 context); |
| 168 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
218 } | 169 } |
219 DCHECK_LT(response.request_number, state->request_received.size()); | 170 DCHECK_LT(response.request_number, state->request_received.size()); |
220 const MemoryItemRequest& request = requests[response.request_number]; | |
221 if (state->request_received[response.request_number]) { | 171 if (state->request_received[response.request_number]) { |
222 // Bad IPC, so we delete our record. | 172 // Bad IPC, so we delete our record. |
223 DVLOG(1) << "Already received response for that request."; | 173 DVLOG(1) << "Already received response for that request."; |
224 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | 174 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
225 return BlobTransportResult::BAD_IPC; | 175 context); |
| 176 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
226 } | 177 } |
227 state->request_received[response.request_number] = true; | 178 state->request_received[response.request_number] = true; |
228 bool invalid_ipc = false; | 179 } |
229 bool memory_error = false; | 180 switch (state->strategy) { |
230 switch (request.message.transport_strategy) { | 181 case IPCBlobItemRequestStrategy::IPC: |
231 case IPCBlobItemRequestStrategy::IPC: | 182 return OnIPCResponses(uuid, state, responses, context); |
232 if (response.inline_data.size() < request.message.size) { | 183 case IPCBlobItemRequestStrategy::SHARED_MEMORY: |
233 DVLOG(1) << "Invalid data size " << response.inline_data.size() | 184 return OnSharedMemoryResponses(uuid, state, responses, context); |
234 << " vs requested size of " << request.message.size; | 185 case IPCBlobItemRequestStrategy::FILE: |
235 invalid_ipc = true; | 186 return OnFileResponses(uuid, state, responses, context); |
236 break; | 187 case IPCBlobItemRequestStrategy::UNKNOWN: |
237 } | 188 break; |
238 invalid_ipc = !state->data_builder.PopulateFutureData( | 189 } |
239 request.browser_item_index, &response.inline_data[0], | 190 NOTREACHED(); |
240 request.browser_item_offset, request.message.size); | 191 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
241 break; | |
242 case IPCBlobItemRequestStrategy::SHARED_MEMORY: | |
243 if (state->num_shared_memory_requests == 0) { | |
244 DVLOG(1) << "Received too many responses for shared memory."; | |
245 invalid_ipc = true; | |
246 break; | |
247 } | |
248 state->num_shared_memory_requests--; | |
249 if (!state->shared_memory_block->memory()) { | |
250 // We just map the whole block, as we'll probably be accessing the | |
251 // whole thing in this group of responses. Another option is to use | |
252 // MapAt, remove the mapped boolean, and then exclude the | |
253 // handle_offset below. | |
254 size_t handle_size = request_builder.shared_memory_sizes() | |
255 [state->current_shared_memory_handle_index]; | |
256 if (!state->shared_memory_block->Map(handle_size)) { | |
257 DVLOG(1) << "Unable to map memory to size " << handle_size; | |
258 memory_error = true; | |
259 break; | |
260 } | |
261 } | |
262 | |
263 invalid_ipc = !state->data_builder.PopulateFutureData( | |
264 request.browser_item_index, | |
265 static_cast<const char*>(state->shared_memory_block->memory()) + | |
266 request.message.handle_offset, | |
267 request.browser_item_offset, request.message.size); | |
268 break; | |
269 case IPCBlobItemRequestStrategy::FILE: | |
270 case IPCBlobItemRequestStrategy::UNKNOWN: | |
271 DVLOG(1) << "Not implemented."; | |
272 invalid_ipc = true; | |
273 break; | |
274 } | |
275 if (invalid_ipc) { | |
276 // Bad IPC, so we delete our record and return false. | |
277 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::UNKNOWN, context); | |
278 return BlobTransportResult::BAD_IPC; | |
279 } | |
280 if (memory_error) { | |
281 DVLOG(1) << "Shared memory error."; | |
282 CancelBuildingBlob(uuid, IPCBlobCreationCancelCode::OUT_OF_MEMORY, | |
283 context); | |
284 return BlobTransportResult::CANCEL_MEMORY_FULL; | |
285 } | |
286 state->num_fulfilled_requests++; | |
287 } | |
288 return ContinueBlobMemoryRequests(uuid, context); | |
289 } | 192 } |
290 | 193 |
291 void BlobAsyncBuilderHost::CancelBuildingBlob(const std::string& uuid, | 194 void BlobAsyncBuilderHost::CancelBuildingBlob(const std::string& uuid, |
292 IPCBlobCreationCancelCode code, | 195 BlobStatus code, |
293 BlobStorageContext* context) { | 196 BlobStorageContext* context) { |
294 DCHECK(context); | 197 DCHECK(context); |
| 198 DCHECK(BlobStatusIsError(code)); |
295 auto state_it = async_blob_map_.find(uuid); | 199 auto state_it = async_blob_map_.find(uuid); |
296 if (state_it == async_blob_map_.end()) { | 200 if (state_it == async_blob_map_.end()) |
297 return; | 201 return; |
298 } | |
299 // We can have the blob dereferenced by the renderer, but have it still being | 202 // We can have the blob dereferenced by the renderer, but have it still being |
300 // 'built'. In this case, it's destructed in the context, but we still have | 203 // 'built'. In this case, it's destructed in the context, but we still have |
301 // it in our map. Hence we make sure the context has the entry before | 204 // it in our map. Hence we make sure the context has the entry before |
302 // calling cancel. | 205 // calling cancel. |
| 206 async_blob_map_.erase(state_it); |
303 if (context->registry().HasEntry(uuid)) | 207 if (context->registry().HasEntry(uuid)) |
304 context->CancelPendingBlob(uuid, code); | 208 context->BreakAndFinishPendingBlob(uuid, code); |
305 async_blob_map_.erase(state_it); | |
306 } | 209 } |
307 | 210 |
308 void BlobAsyncBuilderHost::CancelAll(BlobStorageContext* context) { | 211 void BlobAsyncBuilderHost::CancelAll(BlobStorageContext* context) { |
309 DCHECK(context); | 212 DCHECK(context); |
310 // If the blob still exists in the context (and is being built), then we know | 213 // If the blob still exists in the context, then we know that someone else is |
311 // that someone else is expecting our blob, and we need to cancel it to let | 214 // expecting our blob, and we need to cancel it to let the dependency know |
312 // the dependency know it's gone. | 215 // it's gone. |
313 std::vector<std::unique_ptr<BlobDataHandle>> referenced_pending_blobs; | 216 std::vector<std::unique_ptr<BlobDataHandle>> referenced_pending_blobs; |
314 for (const auto& uuid_state_pair : async_blob_map_) { | 217 for (const auto& uuid_state_pair : async_blob_map_) { |
315 if (context->IsBeingBuilt(uuid_state_pair.first)) { | 218 std::unique_ptr<BlobDataHandle> handle = |
316 referenced_pending_blobs.emplace_back( | 219 context->GetBlobDataFromUUID(uuid_state_pair.first); |
| 220 if (handle) { |
| 221 referenced_pending_blobs.push_back( |
317 context->GetBlobDataFromUUID(uuid_state_pair.first)); | 222 context->GetBlobDataFromUUID(uuid_state_pair.first)); |
318 } | 223 } |
319 } | 224 } |
320 // We clear the map before canceling them to prevent any strange reentry into | 225 // We clear the map before canceling them to prevent any strange reentry into |
321 // our class (see ReferencedBlobFinished) if any blobs were waiting for others | 226 // our class (see OnCanStartBuildingBlob) if any blobs were waiting for others |
322 // to construct. | 227 // to construct. |
323 async_blob_map_.clear(); | 228 async_blob_map_.clear(); |
324 for (const std::unique_ptr<BlobDataHandle>& handle : | 229 for (const std::unique_ptr<BlobDataHandle>& handle : |
325 referenced_pending_blobs) { | 230 referenced_pending_blobs) { |
326 context->CancelPendingBlob( | 231 context->BreakAndFinishPendingBlob(handle->uuid(), |
327 handle->uuid(), IPCBlobCreationCancelCode::SOURCE_DIED_IN_TRANSIT); | 232 BlobStatus::ERR_SOURCE_DIED_IN_TRANSIT); |
328 } | 233 } |
329 } | 234 } |
330 | 235 |
331 BlobTransportResult BlobAsyncBuilderHost::ContinueBlobMemoryRequests( | 236 BlobStatus BlobAsyncBuilderHost::StartRequests(const std::string& uuid, |
332 const std::string& uuid, | 237 BlobBuildingState* state, |
| 238 BlobStorageContext* context) { |
| 239 switch (state->strategy) { |
| 240 case IPCBlobItemRequestStrategy::IPC: |
| 241 SendIPCRequests(state, context); |
| 242 return BlobStatus::PENDING_DATA_POPULATION; |
| 243 case IPCBlobItemRequestStrategy::SHARED_MEMORY: |
| 244 return ContinueSharedMemoryRequests(uuid, state, context); |
| 245 case IPCBlobItemRequestStrategy::FILE: |
| 246 case IPCBlobItemRequestStrategy::UNKNOWN: |
| 247 break; |
| 248 } |
| 249 NOTREACHED(); |
| 250 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 251 } |
| 252 |
| 253 // Note: This can be called when we cancel a blob in the context. |
| 254 void BlobAsyncBuilderHost::OnCanStartBuildingBlob( |
| 255 const std::string& uuid, |
| 256 base::WeakPtr<BlobStorageContext> context, |
| 257 BlobStatus status) { |
| 258 LOG(ERROR) << "We can fit now!"; |
| 259 if (!context) { |
| 260 async_blob_map_.erase(uuid); |
| 261 return; |
| 262 } |
| 263 AsyncBlobMap::const_iterator state_it = async_blob_map_.find(uuid); |
| 264 if (state_it == async_blob_map_.end()) |
| 265 return; |
| 266 |
| 267 BlobBuildingState* state = state_it->second.get(); |
| 268 if (status == BlobStatus::PENDING_DATA_POPULATION) { |
| 269 status = StartRequests(uuid, state, context.get()); |
| 270 if (status == BlobStatus::PENDING_DATA_POPULATION) |
| 271 return; |
| 272 } else { |
| 273 LOG(ERROR) << "Got error state!"; |
| 274 } |
| 275 BlobStatusCallback status_callback = state->status_callback; |
| 276 async_blob_map_.erase(state_it); |
| 277 status_callback.Run(status); |
| 278 } |
| 279 |
| 280 void BlobAsyncBuilderHost::SendIPCRequests(BlobBuildingState* state, |
| 281 BlobStorageContext* context) { |
| 282 const std::vector<MemoryItemRequest>& requests = |
| 283 state->request_builder.requests(); |
| 284 std::unique_ptr<std::vector<BlobItemBytesRequest>> byte_requests( |
| 285 new std::vector<BlobItemBytesRequest>()); |
| 286 |
| 287 DCHECK(!requests.empty()); |
| 288 for (const MemoryItemRequest& request : requests) { |
| 289 byte_requests->push_back(request.message); |
| 290 } |
| 291 |
| 292 state->request_memory_callback.Run( |
| 293 std::move(byte_requests), |
| 294 base::WrapUnique(new std::vector<base::SharedMemoryHandle>()), |
| 295 base::WrapUnique(new std::vector<base::File>())); |
| 296 } |
| 297 |
| 298 BlobStatus BlobAsyncBuilderHost::OnIPCResponses( |
| 299 const std::string& uuid, |
| 300 BlobBuildingState* state, |
| 301 const std::vector<BlobItemBytesResponse>& responses, |
333 BlobStorageContext* context) { | 302 BlobStorageContext* context) { |
334 AsyncBlobMap::const_iterator state_it = async_blob_map_.find(uuid); | 303 const auto& requests = state->request_builder.requests(); |
335 DCHECK(state_it != async_blob_map_.end()); | 304 size_t num_requests = requests.size(); |
336 BlobAsyncBuilderHost::BlobBuildingState* state = state_it->second.get(); | 305 for (const BlobItemBytesResponse& response : responses) { |
337 | 306 const MemoryItemRequest& request = requests[response.request_number]; |
| 307 if (response.inline_data.size() < request.message.size) { |
| 308 DVLOG(1) << "Invalid data size " << response.inline_data.size() |
| 309 << " vs requested size of " << request.message.size; |
| 310 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 311 context); |
| 312 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 313 } |
| 314 bool success = state->data_builder.PopulateFutureData( |
| 315 request.browser_item_index, response.inline_data.data(), |
| 316 request.browser_item_offset, request.message.size); |
| 317 if (!success) { |
| 318 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 319 context); |
| 320 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 321 } |
| 322 state->num_fulfilled_requests++; |
| 323 } |
| 324 if (state->num_fulfilled_requests == num_requests) { |
| 325 FinishBuildingBlob(state, context); |
| 326 return BlobStatus::DONE; |
| 327 } |
| 328 return BlobStatus::PENDING_DATA_POPULATION; |
| 329 } |
| 330 |
| 331 BlobStatus BlobAsyncBuilderHost::ContinueSharedMemoryRequests( |
| 332 const std::string& uuid, |
| 333 BlobBuildingState* state, |
| 334 BlobStorageContext* context) { |
338 BlobAsyncTransportRequestBuilder& request_builder = state->request_builder; | 335 BlobAsyncTransportRequestBuilder& request_builder = state->request_builder; |
339 const std::vector<MemoryItemRequest>& requests = request_builder.requests(); | 336 const std::vector<MemoryItemRequest>& requests = request_builder.requests(); |
340 size_t num_requests = requests.size(); | 337 size_t num_requests = requests.size(); |
341 if (state->num_fulfilled_requests == num_requests) { | |
342 FinishBuildingBlob(state, context); | |
343 return BlobTransportResult::DONE; | |
344 } | |
345 DCHECK_LT(state->num_fulfilled_requests, num_requests); | 338 DCHECK_LT(state->num_fulfilled_requests, num_requests); |
346 if (state->next_request == num_requests) { | 339 if (state->next_request == num_requests) { |
347 // We are still waiting on other requests to come back. | 340 // We are still waiting on other requests to come back. |
348 return BlobTransportResult::PENDING_RESPONSES; | 341 return BlobStatus::PENDING_DATA_POPULATION; |
349 } | 342 } |
350 | 343 |
351 std::unique_ptr<std::vector<BlobItemBytesRequest>> byte_requests( | 344 std::unique_ptr<std::vector<BlobItemBytesRequest>> byte_requests( |
352 new std::vector<BlobItemBytesRequest>()); | 345 new std::vector<BlobItemBytesRequest>()); |
353 std::unique_ptr<std::vector<base::SharedMemoryHandle>> shared_memory( | 346 std::unique_ptr<std::vector<base::SharedMemoryHandle>> shared_memory( |
354 new std::vector<base::SharedMemoryHandle>()); | 347 new std::vector<base::SharedMemoryHandle>()); |
355 | 348 |
356 for (; state->next_request < num_requests; ++state->next_request) { | 349 for (; state->next_request < num_requests; ++state->next_request) { |
357 const MemoryItemRequest& request = requests[state->next_request]; | 350 const MemoryItemRequest& request = requests[state->next_request]; |
358 | |
359 bool stop_accumulating = false; | |
360 bool using_shared_memory_handle = state->num_shared_memory_requests > 0; | 351 bool using_shared_memory_handle = state->num_shared_memory_requests > 0; |
361 switch (request.message.transport_strategy) { | 352 if (using_shared_memory_handle && |
362 case IPCBlobItemRequestStrategy::IPC: | 353 state->current_shared_memory_handle_index != |
363 byte_requests->push_back(request.message); | 354 request.message.handle_index) { |
364 break; | 355 // We only want one shared memory per requesting blob. |
365 case IPCBlobItemRequestStrategy::SHARED_MEMORY: | |
366 if (using_shared_memory_handle && | |
367 state->current_shared_memory_handle_index != | |
368 request.message.handle_index) { | |
369 // We only want one shared memory per requesting blob. | |
370 stop_accumulating = true; | |
371 break; | |
372 } | |
373 using_shared_memory_handle = true; | |
374 state->current_shared_memory_handle_index = | |
375 request.message.handle_index; | |
376 state->num_shared_memory_requests++; | |
377 | |
378 if (!state->shared_memory_block) { | |
379 state->shared_memory_block.reset(new base::SharedMemory()); | |
380 size_t size = | |
381 request_builder | |
382 .shared_memory_sizes()[request.message.handle_index]; | |
383 if (!state->shared_memory_block->CreateAnonymous(size)) { | |
384 DVLOG(1) << "Unable to allocate shared memory for blob transfer."; | |
385 return BlobTransportResult::CANCEL_MEMORY_FULL; | |
386 } | |
387 } | |
388 shared_memory->push_back(state->shared_memory_block->handle()); | |
389 byte_requests->push_back(request.message); | |
390 // Since we are only using one handle at a time, transform our handle | |
391 // index correctly back to 0. | |
392 byte_requests->back().handle_index = 0; | |
393 break; | |
394 case IPCBlobItemRequestStrategy::FILE: | |
395 case IPCBlobItemRequestStrategy::UNKNOWN: | |
396 NOTREACHED() << "Not implemented yet."; | |
397 break; | |
398 } | |
399 if (stop_accumulating) { | |
400 break; | 356 break; |
401 } | 357 } |
| 358 state->current_shared_memory_handle_index = request.message.handle_index; |
| 359 state->num_shared_memory_requests++; |
| 360 |
| 361 if (!state->shared_memory_block) { |
| 362 state->shared_memory_block.reset(new base::SharedMemory()); |
| 363 size_t size = |
| 364 request_builder.shared_memory_sizes()[request.message.handle_index]; |
| 365 if (!state->shared_memory_block->CreateAnonymous(size)) { |
| 366 DVLOG(1) << "Unable to allocate shared memory for blob transfer."; |
| 367 return BlobStatus::ERR_OUT_OF_MEMORY; |
| 368 } |
| 369 } |
| 370 shared_memory->push_back(state->shared_memory_block->handle()); |
| 371 byte_requests->push_back(request.message); |
| 372 // Since we are only using one handle at a time, transform our handle |
| 373 // index correctly back to 0. |
| 374 byte_requests->back().handle_index = 0; |
402 } | 375 } |
403 DCHECK(!requests.empty()); | 376 DCHECK(!requests.empty()); |
404 | 377 |
405 state->request_memory_callback.Run( | 378 state->request_memory_callback.Run( |
406 std::move(byte_requests), std::move(shared_memory), | 379 std::move(byte_requests), std::move(shared_memory), |
407 base::WrapUnique(new std::vector<base::File>())); | 380 base::WrapUnique(new std::vector<base::File>())); |
408 return BlobTransportResult::PENDING_RESPONSES; | 381 return BlobStatus::PENDING_DATA_POPULATION; |
409 } | 382 } |
410 | 383 |
411 void BlobAsyncBuilderHost::ReferencedBlobFinished( | 384 BlobStatus BlobAsyncBuilderHost::OnSharedMemoryResponses( |
412 const std::string& owning_blob_uuid, | 385 const std::string& uuid, |
413 base::WeakPtr<BlobStorageContext> context, | 386 BlobBuildingState* state, |
414 bool construction_success, | 387 const std::vector<BlobItemBytesResponse>& responses, |
415 IPCBlobCreationCancelCode reason) { | 388 BlobStorageContext* context) { |
416 if (!context) { | 389 BlobAsyncTransportRequestBuilder& request_builder = state->request_builder; |
| 390 const auto& requests = request_builder.requests(); |
| 391 for (const BlobItemBytesResponse& response : responses) { |
| 392 const MemoryItemRequest& request = requests[response.request_number]; |
| 393 if (state->num_shared_memory_requests == 0) { |
| 394 DVLOG(1) << "Received too many responses for shared memory."; |
| 395 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 396 context); |
| 397 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 398 } |
| 399 state->num_shared_memory_requests--; |
| 400 if (!state->shared_memory_block->memory()) { |
| 401 // We just map the whole block, as we'll probably be accessing the |
| 402 // whole thing in this group of responses. |
| 403 size_t handle_size = |
| 404 request_builder |
| 405 .shared_memory_sizes()[state->current_shared_memory_handle_index]; |
| 406 if (!state->shared_memory_block->Map(handle_size)) { |
| 407 DVLOG(1) << "Unable to map memory to size " << handle_size; |
| 408 CancelBuildingBlob(uuid, BlobStatus::ERR_OUT_OF_MEMORY, context); |
| 409 return BlobStatus::ERR_OUT_OF_MEMORY; |
| 410 } |
| 411 } |
| 412 |
| 413 bool success = state->data_builder.PopulateFutureData( |
| 414 request.browser_item_index, |
| 415 static_cast<const char*>(state->shared_memory_block->memory()) + |
| 416 request.message.handle_offset, |
| 417 request.browser_item_offset, request.message.size); |
| 418 |
| 419 if (!success) { |
| 420 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 421 context); |
| 422 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 423 } |
| 424 state->num_fulfilled_requests++; |
| 425 } |
| 426 if (state->num_fulfilled_requests == requests.size()) { |
| 427 FinishBuildingBlob(state, context); |
| 428 return BlobStatus::DONE; |
| 429 } |
| 430 return ContinueSharedMemoryRequests(uuid, state, context); |
| 431 } |
| 432 |
| 433 void BlobAsyncBuilderHost::OnFileCreated( |
| 434 const std::string& uuid, |
| 435 size_t handle_index, |
| 436 BlobMemoryController::FileCreationInfo file_info) { |
| 437 LOG(ERROR) << "File created for renderer."; |
| 438 auto state_it = async_blob_map_.find(uuid); |
| 439 if (state_it == async_blob_map_.end()) |
417 return; | 440 return; |
| 441 BlobBuildingState* state = state_it->second.get(); |
| 442 DCHECK_LT(handle_index, state->files.size()); |
| 443 state->files[handle_index] = std::move(file_info.file_reference); |
| 444 |
| 445 const BlobAsyncTransportRequestBuilder& request_builder = |
| 446 state->request_builder; |
| 447 const std::vector<MemoryItemRequest>& requests = request_builder.requests(); |
| 448 |
| 449 std::unique_ptr<std::vector<BlobItemBytesRequest>> byte_requests( |
| 450 new std::vector<BlobItemBytesRequest>()); |
| 451 |
| 452 std::unique_ptr<std::vector<base::File>> files(new std::vector<base::File>()); |
| 453 files->push_back(std::move(file_info.file)); |
| 454 |
| 455 for (const MemoryItemRequest& request : requests) { |
| 456 if (request.message.handle_index != handle_index) |
| 457 continue; |
| 458 byte_requests->push_back(request.message); |
| 459 byte_requests->back().handle_index = 0; |
418 } | 460 } |
419 auto state_it = async_blob_map_.find(owning_blob_uuid); | 461 |
420 if (state_it == async_blob_map_.end()) { | 462 state->request_memory_callback.Run( |
421 return; | 463 std::move(byte_requests), |
| 464 base::WrapUnique(new std::vector<base::SharedMemoryHandle>()), |
| 465 std::move(files)); |
| 466 } |
| 467 |
| 468 BlobStatus BlobAsyncBuilderHost::OnFileResponses( |
| 469 const std::string& uuid, |
| 470 BlobBuildingState* state, |
| 471 const std::vector<BlobItemBytesResponse>& responses, |
| 472 BlobStorageContext* context) { |
| 473 BlobAsyncTransportRequestBuilder& request_builder = state->request_builder; |
| 474 const auto& requests = request_builder.requests(); |
| 475 for (const BlobItemBytesResponse& response : responses) { |
| 476 const MemoryItemRequest& request = requests[response.request_number]; |
| 477 const scoped_refptr<ShareableFileReference>& file_ref = |
| 478 state->files[request.message.handle_index]; |
| 479 bool success = state->data_builder.PopulateFutureFile( |
| 480 request.browser_item_index, file_ref, response.time_file_modified); |
| 481 if (!success) { |
| 482 CancelBuildingBlob(uuid, BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS, |
| 483 context); |
| 484 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 485 } |
| 486 state->num_fulfilled_requests++; |
422 } | 487 } |
423 if (!construction_success) { | 488 if (state->num_fulfilled_requests == requests.size()) { |
424 CancelBuildingBlob(owning_blob_uuid, | 489 FinishBuildingBlob(state, context); |
425 ConvertReferencedBlobErrorToConstructingError(reason), | 490 return BlobStatus::DONE; |
426 context.get()); | |
427 return; | |
428 } | 491 } |
429 BlobBuildingState* state = state_it->second.get(); | 492 return BlobStatus::PENDING_DATA_POPULATION; |
430 DCHECK_GT(state->num_referenced_blobs_building, 0u); | |
431 if (--state->num_referenced_blobs_building == 0) { | |
432 context->CompletePendingBlob(state->data_builder); | |
433 async_blob_map_.erase(state->data_builder.uuid()); | |
434 } | |
435 } | 493 } |
436 | 494 |
437 void BlobAsyncBuilderHost::FinishBuildingBlob(BlobBuildingState* state, | 495 void BlobAsyncBuilderHost::FinishBuildingBlob(BlobBuildingState* state, |
438 BlobStorageContext* context) { | 496 BlobStorageContext* context) { |
439 if (!state->referenced_blob_uuids.empty()) { | 497 std::string uuid = state->data_builder.uuid(); |
440 DCHECK_EQ(0u, state->num_referenced_blobs_building); | |
441 state->num_referenced_blobs_building = 0; | |
442 // We assume re-entry is not possible, as RunOnConstructionComplete | |
443 // will schedule a task when the blob is being built. Thus we can't have the | |
444 // case where |num_referenced_blobs_building| reaches 0 in the | |
445 // ReferencedBlobFinished method before we're finished looping. | |
446 for (const std::string& referenced_uuid : state->referenced_blob_uuids) { | |
447 if (context->IsBeingBuilt(referenced_uuid)) { | |
448 state->num_referenced_blobs_building++; | |
449 context->RunOnConstructionComplete( | |
450 referenced_uuid, | |
451 base::Bind(&BlobAsyncBuilderHost::ReferencedBlobFinished, | |
452 ptr_factory_.GetWeakPtr(), state->data_builder.uuid(), | |
453 context->AsWeakPtr())); | |
454 } | |
455 } | |
456 if (state->num_referenced_blobs_building > 0) { | |
457 // We wait until referenced blobs are done. | |
458 return; | |
459 } | |
460 } | |
461 context->CompletePendingBlob(state->data_builder); | |
462 async_blob_map_.erase(state->data_builder.uuid()); | 498 async_blob_map_.erase(state->data_builder.uuid()); |
| 499 context->FinishedPopulatingPendingBlob(uuid); |
463 } | 500 } |
464 | 501 |
465 } // namespace storage | 502 } // namespace storage |
OLD | NEW |