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