Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: storage/browser/blob/blob_async_builder_host.cc

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

Powered by Google App Engine
This is Rietveld 408576698