OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <algorithm> | 7 #include <algorithm> |
11 #include <limits> | 8 #include <limits> |
12 #include <memory> | 9 #include <memory> |
10 #include <set> | |
13 #include <utility> | 11 #include <utility> |
14 | 12 |
15 #include "base/bind.h" | 13 #include "base/bind.h" |
16 #include "base/callback.h" | 14 #include "base/callback.h" |
17 #include "base/location.h" | 15 #include "base/location.h" |
18 #include "base/logging.h" | 16 #include "base/logging.h" |
19 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
20 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
21 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/numerics/safe_conversions.h" | |
21 #include "base/numerics/safe_math.h" | |
22 #include "base/task_runner.h" | |
22 #include "base/threading/thread_task_runner_handle.h" | 23 #include "base/threading/thread_task_runner_handle.h" |
23 #include "base/trace_event/trace_event.h" | 24 #include "base/trace_event/trace_event.h" |
25 #include "storage/browser/blob/blob_async_transport_request_builder.h" | |
24 #include "storage/browser/blob/blob_data_builder.h" | 26 #include "storage/browser/blob/blob_data_builder.h" |
25 #include "storage/browser/blob/blob_data_handle.h" | |
26 #include "storage/browser/blob/blob_data_item.h" | 27 #include "storage/browser/blob/blob_data_item.h" |
27 #include "storage/browser/blob/blob_data_snapshot.h" | 28 #include "storage/browser/blob/blob_data_snapshot.h" |
28 #include "storage/browser/blob/shareable_blob_data_item.h" | 29 #include "storage/browser/blob/shareable_blob_data_item.h" |
30 #include "storage/common/data_element.h" | |
29 #include "url/gurl.h" | 31 #include "url/gurl.h" |
30 | 32 |
31 namespace storage { | 33 namespace storage { |
32 using BlobRegistryEntry = BlobStorageRegistry::Entry; | 34 namespace { |
33 using BlobState = BlobStorageRegistry::BlobState; | 35 using ItemCopyEntry = InternalBlobData::ItemCopyEntry; |
34 | 36 using QuotaAllocationTask = BlobMemoryController::QuotaAllocationTask; |
35 BlobStorageContext::BlobStorageContext() : memory_usage_(0) {} | 37 |
38 bool IsBytes(DataElement::Type type) { | |
39 return type == DataElement::TYPE_BYTES || | |
40 type == DataElement::TYPE_BYTES_DESCRIPTION; | |
41 } | |
42 } // namespace | |
43 | |
44 BlobStorageContext::BlobFlattener::BlobFlattener( | |
45 const BlobDataBuilder& input_builder, | |
46 InternalBlobData* output_blob, | |
47 BlobStorageRegistry* registry) { | |
48 const std::string& uuid = input_builder.uuid_; | |
49 std::set<std::string> dependent_blob_uuids; | |
50 | |
51 size_t num_files_with_unknown_size = 0; | |
52 size_t num_building_dependent_blobs = 0; | |
53 | |
54 base::CheckedNumeric<uint64_t> checked_total_size = 0; | |
55 base::CheckedNumeric<uint64_t> checked_memory_quota_needed = 0; | |
56 | |
57 for (scoped_refptr<BlobDataItem> input_item : input_builder.items_) { | |
58 const DataElement& input_element = input_item->data_element(); | |
59 DataElement::Type type = input_element.type(); | |
60 uint64_t length = input_element.length(); | |
61 | |
62 if (IsBytes(type)) { | |
63 DCHECK_NE(0 + DataElement::kUnknownSize, input_element.length()); | |
64 checked_memory_quota_needed += length; | |
65 checked_total_size += length; | |
66 scoped_refptr<ShareableBlobDataItem> item = new ShareableBlobDataItem( | |
67 std::move(input_item), ShareableBlobDataItem::QUOTA_NEEDED); | |
68 pending_memory_items.push_back(item); | |
69 user_items.push_back(item.get()); | |
70 output_blob->AppendSharedBlobItem(uuid, std::move(item)); | |
71 continue; | |
72 } | |
73 if (type == DataElement::TYPE_BLOB) { | |
74 InternalBlobData* ref_entry = | |
75 registry->GetEntry(input_element.blob_uuid()); | |
76 | |
77 if (!ref_entry || input_element.blob_uuid() == uuid) { | |
78 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
79 return; | |
80 } | |
81 | |
82 if (BlobStatusIsError(ref_entry->status())) { | |
83 status = BlobStatus::ERR_REFERENCED_BLOB_BROKEN; | |
84 return; | |
85 } | |
86 | |
87 if (ref_entry->total_size() == DataElement::kUnknownSize) { | |
88 // We can't reference a blob with unknown size. | |
89 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
90 return; | |
91 } | |
92 | |
93 if (dependent_blob_uuids.find(input_element.blob_uuid()) == | |
94 dependent_blob_uuids.end()) { | |
95 dependent_blobs.push_back( | |
96 std::make_pair(input_element.blob_uuid(), ref_entry)); | |
97 dependent_blob_uuids.insert(input_element.blob_uuid()); | |
98 if (BlobStatusIsPending(ref_entry->status())) { | |
99 num_building_dependent_blobs++; | |
100 } | |
101 } | |
102 | |
103 length = length == DataElement::kUnknownSize ? ref_entry->total_size() | |
104 : input_element.length(); | |
105 checked_total_size += length; | |
106 | |
107 // If we're referencing the whole blob, then we don't need to slice. | |
108 if (input_element.offset() == 0 && length == ref_entry->total_size()) { | |
109 for (const auto& shareable_item : ref_entry->items()) { | |
110 output_blob->AppendSharedBlobItem(uuid, shareable_item); | |
111 } | |
112 continue; | |
113 } | |
114 | |
115 // Validate our reference has good offset & length. | |
116 if (input_element.offset() + length > ref_entry->total_size()) { | |
117 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
118 return; | |
119 } | |
120 | |
121 BlobSlice slice(*ref_entry, input_element.offset(), length); | |
122 | |
123 if (slice.first_source_item) { | |
124 copies.push_back(ItemCopyEntry(slice.first_source_item, | |
125 slice.first_item_slice_offset, | |
126 slice.dest_items.front())); | |
127 pending_memory_items.push_back(slice.dest_items.front()); | |
128 } | |
129 if (slice.last_source_item) { | |
130 copies.push_back( | |
131 ItemCopyEntry(slice.last_source_item, 0, slice.dest_items.back())); | |
132 pending_memory_items.push_back(slice.dest_items.back()); | |
133 } | |
134 checked_memory_quota_needed += slice.copying_memory_size; | |
135 | |
136 for (auto& shareable_item : slice.dest_items) { | |
137 output_blob->AppendSharedBlobItem(uuid, std::move(shareable_item)); | |
138 } | |
139 continue; | |
140 } | |
141 | |
142 DCHECK(!BlobDataBuilder::IsFutureFileItem(input_element)) | |
143 << "File allocation not implemented."; | |
144 if (length == DataElement::kUnknownSize) | |
145 num_files_with_unknown_size++; | |
146 | |
147 scoped_refptr<ShareableBlobDataItem> item = new ShareableBlobDataItem( | |
148 std::move(input_item), ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA); | |
149 | |
150 checked_total_size += length; | |
151 output_blob->AppendSharedBlobItem(uuid, std::move(item)); | |
152 } | |
153 | |
154 if (num_files_with_unknown_size > 1 && input_builder.items_.size() > 1) { | |
155 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
156 return; | |
157 } | |
158 if (!checked_total_size.IsValid() || !checked_memory_quota_needed.IsValid()) { | |
159 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; | |
160 return; | |
161 } | |
162 total_size = checked_total_size.ValueOrDie(); | |
163 memory_quota_needed = checked_memory_quota_needed.ValueOrDie(); | |
164 if (memory_quota_needed) { | |
165 status = BlobStatus::PENDING_QUOTA; | |
166 } else { | |
167 status = BlobStatus::PENDING_INTERNALS; | |
168 } | |
169 } | |
170 | |
171 BlobStorageContext::BlobFlattener::~BlobFlattener() {} | |
172 | |
173 BlobStorageContext::BlobSlice::BlobSlice(const InternalBlobData& source, | |
174 uint64_t slice_offset, | |
175 uint64_t slice_size) { | |
176 const auto& source_items = source.items(); | |
177 const auto& offsets = source.offsets(); | |
178 DCHECK_LE(slice_offset + slice_size, source.total_size()); | |
179 size_t item_index = | |
180 std::upper_bound(offsets.begin(), offsets.end(), slice_offset) - | |
181 offsets.begin(); | |
182 uint64_t item_offset = | |
183 item_index == 0 ? slice_offset : slice_offset - offsets[item_index - 1]; | |
184 size_t num_items = source_items.size(); | |
185 | |
186 size_t first_item_index = item_index; | |
187 copying_memory_size = 0; | |
188 | |
189 // Read starting from 'first_item_index' and 'item_offset'. | |
190 for (uint64_t total_sliced = 0; | |
191 item_index < num_items && total_sliced < slice_size; item_index++) { | |
192 const scoped_refptr<BlobDataItem>& source_item = | |
193 source_items[item_index]->item(); | |
194 uint64_t source_length = source_item->length(); | |
195 DCHECK_NE(source_length, std::numeric_limits<uint64_t>::max()); | |
196 DCHECK_NE(source_length, 0ull); | |
197 | |
198 uint64_t read_size = | |
199 std::min(source_length - item_offset, slice_size - total_sliced); | |
200 total_sliced += read_size; | |
201 | |
202 if (read_size == source_length) { | |
203 // We can share the entire item. | |
204 dest_items.push_back(source_items[item_index]); | |
205 continue; | |
206 } | |
207 | |
208 scoped_refptr<BlobDataItem> data_item; | |
209 ShareableBlobDataItem::State state = | |
210 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA; | |
211 switch (source_item->type()) { | |
212 case DataElement::TYPE_BYTES_DESCRIPTION: | |
213 case DataElement::TYPE_BYTES: { | |
214 if (item_index == first_item_index) { | |
215 first_item_slice_offset = item_offset; | |
216 first_source_item = source_items[item_index]; | |
217 } else { | |
218 last_source_item = source_items[item_index]; | |
219 } | |
220 copying_memory_size += read_size; | |
221 // Since we don't have quota yet for memory, we create temporary items | |
222 // for this data. When our blob is finished constructing, all dependent | |
223 // blobs are done, and we have enough memory quota, we'll copy the data | |
224 // over. | |
225 std::unique_ptr<DataElement> element(new DataElement()); | |
226 element->SetToBytesDescription(base::checked_cast<size_t>(read_size)); | |
227 data_item = new BlobDataItem(std::move(element)); | |
228 state = ShareableBlobDataItem::QUOTA_NEEDED; | |
229 break; | |
230 } | |
231 case DataElement::TYPE_FILE: { | |
232 std::unique_ptr<DataElement> element(new DataElement()); | |
233 element->SetToFilePathRange( | |
234 source_item->path(), source_item->offset() + item_offset, read_size, | |
235 source_item->expected_modification_time()); | |
236 data_item = | |
237 new BlobDataItem(std::move(element), source_item->data_handle_); | |
238 | |
239 DCHECK(!BlobDataBuilder::IsFutureFileItem(source_item->data_element())) | |
240 << "File allocation unimplemented."; | |
241 break; | |
242 } | |
243 case DataElement::TYPE_FILE_FILESYSTEM: { | |
244 std::unique_ptr<DataElement> element(new DataElement()); | |
245 element->SetToFileSystemUrlRange( | |
246 source_item->filesystem_url(), source_item->offset() + item_offset, | |
247 read_size, source_item->expected_modification_time()); | |
248 data_item = new BlobDataItem(std::move(element)); | |
249 break; | |
250 } | |
251 case DataElement::TYPE_DISK_CACHE_ENTRY: { | |
252 std::unique_ptr<DataElement> element(new DataElement()); | |
253 element->SetToDiskCacheEntryRange(source_item->offset() + item_offset, | |
254 read_size); | |
255 data_item = | |
256 new BlobDataItem(std::move(element), source_item->data_handle_, | |
257 source_item->disk_cache_entry(), | |
258 source_item->disk_cache_stream_index(), | |
259 source_item->disk_cache_side_stream_index()); | |
260 break; | |
261 } | |
262 case DataElement::TYPE_BLOB: | |
263 case DataElement::TYPE_UNKNOWN: | |
264 CHECK(false) << "Illegal blob item type: " << source_item->type(); | |
265 } | |
266 dest_items.push_back( | |
267 new ShareableBlobDataItem(std::move(data_item), state)); | |
268 item_offset = 0; | |
269 } | |
270 } | |
271 | |
272 BlobStorageContext::BlobSlice::~BlobSlice() {} | |
273 | |
274 BlobStorageContext::BlobStorageContext() | |
275 : memory_controller_(base::FilePath(), scoped_refptr<base::TaskRunner>()), | |
276 ptr_factory_(this) {} | |
36 | 277 |
37 BlobStorageContext::~BlobStorageContext() { | 278 BlobStorageContext::~BlobStorageContext() { |
38 } | 279 } |
39 | 280 |
40 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( | 281 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
41 const std::string& uuid) { | 282 const std::string& uuid) { |
42 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 283 InternalBlobData* entry = registry_.GetEntry(uuid); |
43 if (!entry) { | 284 if (!entry) |
44 return nullptr; | 285 return nullptr; |
45 } | 286 return CreateHandle(uuid, entry); |
46 return base::WrapUnique( | |
47 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, | |
48 this, base::ThreadTaskRunnerHandle::Get().get())); | |
49 } | 287 } |
50 | 288 |
51 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( | 289 std::unique_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
52 const GURL& url) { | 290 const GURL& url) { |
53 std::string uuid; | 291 std::string uuid; |
54 BlobRegistryEntry* entry = registry_.GetEntryFromURL(url, &uuid); | 292 InternalBlobData* entry = registry_.GetEntryFromURL(url, &uuid); |
55 if (!entry) { | 293 if (!entry) |
56 return nullptr; | 294 return nullptr; |
57 } | 295 return CreateHandle(uuid, entry); |
58 return base::WrapUnique( | |
59 new BlobDataHandle(uuid, entry->content_type, entry->content_disposition, | |
60 this, base::ThreadTaskRunnerHandle::Get().get())); | |
61 } | 296 } |
62 | 297 |
63 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 298 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
64 const BlobDataBuilder& external_builder) { | 299 const BlobDataBuilder& external_builder) { |
65 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); | 300 TRACE_EVENT0("Blob", "Context::AddFinishedBlob"); |
66 CreatePendingBlob(external_builder.uuid(), external_builder.content_type_, | 301 return BuildBlob(external_builder, PopulatationAllowedCallback()); |
67 external_builder.content_disposition_); | |
68 CompletePendingBlob(external_builder); | |
69 std::unique_ptr<BlobDataHandle> handle = | |
70 GetBlobDataFromUUID(external_builder.uuid_); | |
71 DecrementBlobRefCount(external_builder.uuid_); | |
72 return handle; | |
73 } | 302 } |
74 | 303 |
75 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 304 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
76 const BlobDataBuilder* builder) { | 305 const BlobDataBuilder* builder) { |
77 DCHECK(builder); | 306 DCHECK(builder); |
78 return AddFinishedBlob(*builder); | 307 return AddFinishedBlob(*builder); |
79 } | 308 } |
80 | 309 |
310 std::unique_ptr<BlobDataHandle> BlobStorageContext::AddBrokenBlob( | |
311 const std::string& uuid, | |
312 const std::string& content_type, | |
313 const std::string& content_disposition, | |
314 BlobStatus reason) { | |
315 DCHECK(!registry_.HasEntry(uuid)); | |
316 DCHECK(BlobStatusIsError(reason)); | |
317 InternalBlobData* entry = | |
318 registry_.CreateEntry(uuid, content_type, content_disposition); | |
319 entry->status_ = reason; | |
320 FinishBuilding(entry); | |
321 return CreateHandle(uuid, entry); | |
322 } | |
323 | |
81 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, | 324 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, |
82 const std::string& uuid) { | 325 const std::string& uuid) { |
83 if (!registry_.CreateUrlMapping(blob_url, uuid)) { | 326 if (!registry_.CreateUrlMapping(blob_url, uuid)) |
84 return false; | 327 return false; |
85 } | |
86 IncrementBlobRefCount(uuid); | 328 IncrementBlobRefCount(uuid); |
87 return true; | 329 return true; |
88 } | 330 } |
89 | 331 |
90 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { | 332 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
91 std::string uuid; | 333 std::string uuid; |
92 if (!registry_.DeleteURLMapping(blob_url, &uuid)) { | 334 if (!registry_.DeleteURLMapping(blob_url, &uuid)) |
93 return; | 335 return; |
94 } | |
95 DecrementBlobRefCount(uuid); | 336 DecrementBlobRefCount(uuid); |
96 } | 337 } |
97 | 338 |
98 void BlobStorageContext::CreatePendingBlob( | 339 std::unique_ptr<BlobDataHandle> BlobStorageContext::BuildBlob( |
99 const std::string& uuid, | 340 const BlobDataBuilder& content, |
100 const std::string& content_type, | 341 const PopulatationAllowedCallback& can_populate_memory) { |
101 const std::string& content_disposition) { | 342 DCHECK(!registry_.HasEntry(content.uuid_)); |
102 DCHECK(!registry_.GetEntry(uuid) && !uuid.empty()); | 343 |
103 registry_.CreateEntry(uuid, content_type, content_disposition); | 344 InternalBlobData* entry = registry_.CreateEntry( |
345 content.uuid_, content.content_type_, content.content_disposition_); | |
346 | |
347 // This flattens all blob references in the transportion content out and | |
348 // stores the complete item representation in the internal data. | |
349 BlobFlattener flattener(content, entry, ®istry_); | |
350 | |
351 // If we contain invalid references we're invalid input. | |
352 if (flattener.status == BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS) { | |
353 BreakAndFinishPendingBlob(content.uuid_, | |
354 BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS); | |
355 return CreateHandle(content.uuid_, entry); | |
356 } | |
357 | |
358 // If we contain broken references (or we reference ourself) we want to fail. | |
359 if (flattener.status == BlobStatus::ERR_REFERENCED_BLOB_BROKEN) { | |
360 BreakAndFinishPendingBlob(content.uuid_, | |
361 BlobStatus::ERR_REFERENCED_BLOB_BROKEN); | |
362 return CreateHandle(content.uuid_, entry); | |
363 } | |
364 | |
365 if (!memory_controller_.CanReserveQuota(flattener.memory_quota_needed)) { | |
366 BreakAndFinishPendingBlob(content.uuid_, BlobStatus::ERR_OUT_OF_MEMORY); | |
367 return CreateHandle(content.uuid_, entry); | |
368 } | |
369 | |
370 entry->status_ = flattener.status; | |
371 entry->size_ = flattener.total_size; | |
372 std::unique_ptr<BlobDataHandle> handle = CreateHandle(content.uuid_, entry); | |
373 | |
374 size_t num_building_dependent_blobs = 0; | |
375 std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs; | |
376 // We hold a handle to all blobs we're using. This is important, as our memory | |
377 // accounting can be delayed until OnEnoughSizeForBlobData is called, and we | |
378 // only free memory on canceling when we've done this accounting. If a | |
379 // dependent blob is dereferenced, then we're the last blob holding onto that | |
380 // data item, and we need to account for that. So we prevent that case by | |
381 // holding onto all blobs. | |
382 for (const std::pair<std::string, InternalBlobData*>& pending_blob : | |
383 flattener.dependent_blobs) { | |
384 dependent_blobs.push_back( | |
385 CreateHandle(pending_blob.first, pending_blob.second)); | |
386 if (BlobStatusIsPending(pending_blob.second->status())) { | |
387 pending_blob.second->building_state_->build_completion_callbacks | |
388 .push_back(base::Bind(&BlobStorageContext::OnDependentBlobFinished, | |
389 ptr_factory_.GetWeakPtr(), content.uuid_)); | |
390 num_building_dependent_blobs++; | |
391 } | |
392 } | |
393 | |
394 DCHECK(flattener.status != BlobStatus::PENDING_TRANSPORT || | |
395 !can_populate_memory) | |
396 << "There is no pending content for the user to populate, so the " | |
397 "callback should be null."; | |
398 DCHECK(flattener.status != BlobStatus::PENDING_TRANSPORT || | |
399 can_populate_memory) | |
400 << "If we have pending content then there needs to be a callback " | |
401 "present."; | |
402 | |
403 entry->building_state_.reset(new InternalBlobData::BuildingState( | |
404 /* transport_items_present */ !flattener.user_items.empty(), | |
405 /* user_data_population_callback */ can_populate_memory, | |
406 num_building_dependent_blobs, | |
407 /* memory_quota_needed */ flattener.memory_quota_needed > 0)); | |
michaeln
2016/10/28 19:38:29
probably you meant to cleanup these comments?
dmurph
2016/10/28 22:13:56
I thought it made it more readable. Removed.
| |
408 | |
409 InternalBlobData::BuildingState* building_state = | |
410 entry->building_state_.get(); | |
411 std::swap(building_state->copies, flattener.copies); | |
412 std::swap(building_state->dependent_blobs, dependent_blobs); | |
413 std::swap(building_state->user_items, flattener.user_items); | |
414 | |
415 if (flattener.memory_quota_needed > 0) { | |
416 // We can finish our blob before returning from this method. | |
417 base::WeakPtr<QuotaAllocationTask> pending_request = | |
418 memory_controller_.ReserveMemoryQuota( | |
419 std::move(flattener.pending_memory_items), | |
420 base::Bind(&BlobStorageContext::OnEnoughSizeForMemory, | |
421 ptr_factory_.GetWeakPtr(), content.uuid_)); | |
422 if (entry->building_state_) | |
423 entry->building_state_->memory_quota_request = std::move(pending_request); | |
424 } | |
425 | |
426 if (entry->CanFinishBuilding()) | |
427 FinishBuilding(entry); | |
428 | |
429 return handle; | |
104 } | 430 } |
105 | 431 |
106 void BlobStorageContext::CompletePendingBlob( | 432 void BlobStorageContext::BreakAndFinishPendingBlob(const std::string& uuid, |
107 const BlobDataBuilder& external_builder) { | 433 BlobStatus reason) { |
108 BlobRegistryEntry* entry = registry_.GetEntry(external_builder.uuid()); | 434 InternalBlobData* entry = registry_.GetEntry(uuid); |
109 DCHECK(entry); | 435 DCHECK(entry); |
110 DCHECK(!entry->data.get()) << "Blob already constructed: " | 436 DCHECK(BlobStatusIsError(reason)); |
111 << external_builder.uuid(); | 437 PopulatationAllowedCallback user_data_population_callback; |
112 // We want to handle storing our broken blob as well. | 438 if (entry->building_state_ && |
113 switch (entry->state) { | 439 entry->building_state_->user_data_population_callback) { |
114 case BlobState::PENDING: { | 440 user_data_population_callback = |
115 entry->data_builder.reset(new InternalBlobData::Builder()); | 441 entry->building_state_->user_data_population_callback; |
116 InternalBlobData::Builder* internal_data_builder = | 442 entry->building_state_->user_data_population_callback.Reset(); |
117 entry->data_builder.get(); | |
118 | |
119 bool broken = false; | |
120 for (const auto& blob_item : external_builder.items_) { | |
121 IPCBlobCreationCancelCode error_code; | |
122 if (!AppendAllocatedBlobItem(external_builder.uuid_, blob_item, | |
123 internal_data_builder, &error_code)) { | |
124 broken = true; | |
125 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); | |
126 entry->state = BlobState::BROKEN; | |
127 entry->broken_reason = error_code; | |
128 entry->data_builder.reset(new InternalBlobData::Builder()); | |
129 break; | |
130 } | |
131 } | |
132 entry->data = entry->data_builder->Build(); | |
133 entry->data_builder.reset(); | |
134 entry->state = broken ? BlobState::BROKEN : BlobState::COMPLETE; | |
135 break; | |
136 } | |
137 case BlobState::BROKEN: { | |
138 InternalBlobData::Builder builder; | |
139 entry->data = builder.Build(); | |
140 break; | |
141 } | |
142 case BlobState::COMPLETE: | |
143 DCHECK(false) << "Blob already constructed: " << external_builder.uuid(); | |
144 return; | |
145 } | 443 } |
146 | 444 ClearAndFreeMemory(uuid, entry); |
147 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); | 445 entry->status_ = reason; |
148 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.Broken", | 446 if (user_data_population_callback) { |
149 entry->state == BlobState::BROKEN); | 447 user_data_population_callback.Run( |
150 if (entry->state == BlobState::BROKEN) { | 448 reason, std::vector<BlobMemoryController::FileCreationInfo>()); |
151 UMA_HISTOGRAM_ENUMERATION( | |
152 "Storage.Blob.BrokenReason", static_cast<int>(entry->broken_reason), | |
153 (static_cast<int>(IPCBlobCreationCancelCode::LAST) + 1)); | |
154 } | 449 } |
155 size_t total_memory = 0, nonshared_memory = 0; | 450 entry->items_.clear(); |
156 entry->data->GetMemoryUsage(&total_memory, &nonshared_memory); | 451 entry->offsets_.clear(); |
157 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize", total_memory / 1024); | 452 entry->size_ = 0; |
158 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize", | 453 FinishBuilding(entry); |
159 nonshared_memory / 1024); | |
160 TRACE_COUNTER1("Blob", "MemoryStoreUsageBytes", memory_usage_); | |
161 | |
162 auto runner = base::ThreadTaskRunnerHandle::Get(); | |
163 for (const auto& callback : entry->build_completion_callbacks) { | |
164 runner->PostTask(FROM_HERE, | |
165 base::Bind(callback, entry->state == BlobState::COMPLETE, | |
166 entry->broken_reason)); | |
167 } | |
168 entry->build_completion_callbacks.clear(); | |
169 } | 454 } |
170 | 455 |
171 void BlobStorageContext::CancelPendingBlob(const std::string& uuid, | 456 void BlobStorageContext::FinishedPopulatingPendingBlob( |
172 IPCBlobCreationCancelCode reason) { | 457 const std::string& uuid) { |
173 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 458 InternalBlobData* entry = registry_.GetEntry(uuid); |
174 DCHECK(entry && entry->state == BlobState::PENDING); | 459 CHECK(entry) << "There is no blob entry with uuid " << uuid; |
175 entry->state = BlobState::BROKEN; | 460 DCHECK(BlobStatusIsPending(entry->status())); |
176 entry->broken_reason = reason; | 461 FinishedPopulatingPendingBlob(entry); |
177 CompletePendingBlob(BlobDataBuilder(uuid)); | |
178 } | 462 } |
179 | 463 |
180 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { | 464 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
181 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 465 InternalBlobData* entry = registry_.GetEntry(uuid); |
182 DCHECK(entry); | 466 DCHECK(entry); |
183 ++(entry->refcount); | 467 ++(entry->refcount_); |
184 } | 468 } |
185 | 469 |
186 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { | 470 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
187 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 471 InternalBlobData* entry = registry_.GetEntry(uuid); |
188 DCHECK(entry); | 472 DCHECK(entry); |
189 DCHECK_GT(entry->refcount, 0u); | 473 DCHECK_GT(entry->refcount_, 0u); |
190 if (--(entry->refcount) == 0) { | 474 if (--(entry->refcount_) == 0) { |
191 size_t memory_freeing = 0; | 475 ClearAndFreeMemory(uuid, entry); |
192 if (entry->state == BlobState::COMPLETE) { | |
193 memory_freeing = entry->data->GetUnsharedMemoryUsage(); | |
194 entry->data->RemoveBlobFromShareableItems(uuid); | |
195 } | |
196 DCHECK_LE(memory_freeing, memory_usage_); | |
197 memory_usage_ -= memory_freeing; | |
198 registry_.DeleteEntry(uuid); | 476 registry_.DeleteEntry(uuid); |
199 } | 477 } |
200 } | 478 } |
201 | 479 |
202 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( | 480 std::unique_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( |
203 const std::string& uuid) { | 481 const std::string& uuid) { |
204 std::unique_ptr<BlobDataSnapshot> result; | 482 std::unique_ptr<BlobDataSnapshot> result; |
205 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 483 InternalBlobData* entry = registry_.GetEntry(uuid); |
206 if (entry->state != BlobState::COMPLETE) { | 484 if (entry->status() != BlobStatus::DONE) |
207 return result; | 485 return result; |
208 } | |
209 | 486 |
210 const InternalBlobData& data = *entry->data; | |
211 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( | 487 std::unique_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( |
212 uuid, entry->content_type, entry->content_disposition)); | 488 uuid, entry->content_type(), entry->content_disposition())); |
213 snapshot->items_.reserve(data.items().size()); | 489 snapshot->items_.reserve(entry->items().size()); |
214 for (const auto& shareable_item : data.items()) { | 490 for (const auto& shareable_item : entry->items()) { |
215 snapshot->items_.push_back(shareable_item->item()); | 491 snapshot->items_.push_back(shareable_item->item()); |
216 } | 492 } |
493 memory_controller_.NotifyMemoryItemsUsed(entry->items()); | |
217 return snapshot; | 494 return snapshot; |
218 } | 495 } |
219 | 496 |
220 bool BlobStorageContext::IsBroken(const std::string& uuid) const { | 497 BlobStatus BlobStorageContext::GetBlobStatus(const std::string& uuid) const { |
221 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 498 const InternalBlobData* entry = registry_.GetEntry(uuid); |
222 if (!entry) { | 499 if (!entry) |
223 return true; | 500 return BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
224 } | 501 return entry->status(); |
225 return entry->state == BlobState::BROKEN; | |
226 } | |
227 | |
228 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) const { | |
229 const BlobRegistryEntry* entry = registry_.GetEntry(uuid); | |
230 if (!entry) { | |
231 return false; | |
232 } | |
233 return entry->state == BlobState::PENDING; | |
234 } | 502 } |
235 | 503 |
236 void BlobStorageContext::RunOnConstructionComplete( | 504 void BlobStorageContext::RunOnConstructionComplete( |
237 const std::string& uuid, | 505 const std::string& uuid, |
238 const BlobConstructedCallback& done) { | 506 const BlobStatusCallback& done) { |
239 BlobRegistryEntry* entry = registry_.GetEntry(uuid); | 507 InternalBlobData* entry = registry_.GetEntry(uuid); |
240 DCHECK(entry); | 508 DCHECK(entry); |
241 switch (entry->state) { | 509 if (BlobStatusIsPending(entry->status())) { |
242 case BlobState::COMPLETE: | 510 entry->building_state_->build_completion_callbacks.push_back(done); |
243 done.Run(true, IPCBlobCreationCancelCode::UNKNOWN); | 511 return; |
244 return; | |
245 case BlobState::BROKEN: | |
246 done.Run(false, entry->broken_reason); | |
247 return; | |
248 case BlobState::PENDING: | |
249 entry->build_completion_callbacks.push_back(done); | |
250 return; | |
251 } | 512 } |
252 NOTREACHED(); | 513 done.Run(entry->status()); |
253 } | 514 } |
254 | 515 |
255 bool BlobStorageContext::AppendAllocatedBlobItem( | 516 std::unique_ptr<BlobDataHandle> BlobStorageContext::CreateHandle( |
256 const std::string& target_blob_uuid, | 517 const std::string& uuid, |
257 scoped_refptr<BlobDataItem> blob_item, | 518 InternalBlobData* entry) { |
258 InternalBlobData::Builder* target_blob_builder, | 519 return base::WrapUnique(new BlobDataHandle( |
259 IPCBlobCreationCancelCode* error_code) { | 520 uuid, entry->content_type_, entry->content_disposition_, entry->size_, |
260 DCHECK(error_code); | 521 this, base::ThreadTaskRunnerHandle::Get().get())); |
261 *error_code = IPCBlobCreationCancelCode::UNKNOWN; | |
262 bool error = false; | |
263 | |
264 // The blob data is stored in the canonical way which only contains a | |
265 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items | |
266 // are expanded into the primitive constituent types and reused if possible. | |
267 // 1) The Data item is denoted by the raw data and length. | |
268 // 2) The File item is denoted by the file path, the range and the expected | |
269 // modification time. | |
270 // 3) The FileSystem File item is denoted by the FileSystem URL, the range | |
271 // and the expected modification time. | |
272 // 4) The Blob item is denoted by the source blob and an offset and size. | |
273 // Internal items that are fully used by the new blob (not cut by the | |
274 // offset or size) are shared between the blobs. Otherwise, the relevant | |
275 // portion of the item is copied. | |
276 | |
277 DCHECK(blob_item->data_element_ptr()); | |
278 const DataElement& data_element = blob_item->data_element(); | |
279 uint64_t length = data_element.length(); | |
280 uint64_t offset = data_element.offset(); | |
281 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", | |
282 memory_usage_ / 1024); | |
283 switch (data_element.type()) { | |
284 case DataElement::TYPE_BYTES: | |
285 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); | |
286 DCHECK(!offset); | |
287 if (memory_usage_ + length > kBlobStorageMaxMemoryUsage) { | |
288 error = true; | |
289 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY; | |
290 break; | |
291 } | |
292 memory_usage_ += length; | |
293 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
294 target_blob_uuid, blob_item, | |
295 ShareableBlobDataItem::POPULATED_WITH_QUOTA)); | |
296 break; | |
297 case DataElement::TYPE_FILE: { | |
298 bool full_file = (length == std::numeric_limits<uint64_t>::max()); | |
299 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file); | |
300 if (!full_file) { | |
301 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", | |
302 (length - offset) / 1024); | |
303 } | |
304 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
305 target_blob_uuid, blob_item, | |
306 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
307 break; | |
308 } | |
309 case DataElement::TYPE_FILE_FILESYSTEM: { | |
310 bool full_file = (length == std::numeric_limits<uint64_t>::max()); | |
311 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown", | |
312 full_file); | |
313 if (!full_file) { | |
314 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", | |
315 (length - offset) / 1024); | |
316 } | |
317 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
318 target_blob_uuid, blob_item, | |
319 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
320 break; | |
321 } | |
322 case DataElement::TYPE_BLOB: { | |
323 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", | |
324 (length - offset) / 1024); | |
325 // We grab the handle to ensure it stays around while we copy it. | |
326 std::unique_ptr<BlobDataHandle> src = | |
327 GetBlobDataFromUUID(data_element.blob_uuid()); | |
328 if (!src || src->IsBroken() || src->IsBeingBuilt()) { | |
329 error = true; | |
330 *error_code = IPCBlobCreationCancelCode::REFERENCED_BLOB_BROKEN; | |
331 break; | |
332 } | |
333 BlobRegistryEntry* other_entry = | |
334 registry_.GetEntry(data_element.blob_uuid()); | |
335 DCHECK(other_entry->data); | |
336 if (!AppendBlob(target_blob_uuid, *other_entry->data, offset, length, | |
337 target_blob_builder)) { | |
338 error = true; | |
339 *error_code = IPCBlobCreationCancelCode::OUT_OF_MEMORY; | |
340 } | |
341 break; | |
342 } | |
343 case DataElement::TYPE_DISK_CACHE_ENTRY: { | |
344 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.CacheEntry", | |
345 (length - offset) / 1024); | |
346 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
347 target_blob_uuid, blob_item, | |
348 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
349 break; | |
350 } | |
351 case DataElement::TYPE_BYTES_DESCRIPTION: | |
352 case DataElement::TYPE_UNKNOWN: | |
353 NOTREACHED(); | |
354 break; | |
355 } | |
356 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", | |
357 memory_usage_ / 1024); | |
358 return !error; | |
359 } | 522 } |
360 | 523 |
361 bool BlobStorageContext::AppendBlob( | 524 void BlobStorageContext::FinishedPopulatingPendingBlob( |
michaeln
2016/10/28 19:38:29
transporting naming convention? NotifyTransportCom
dmurph
2016/10/28 22:13:56
I used Population as the noun, let me know if you
michaeln
2016/11/07 21:47:04
i do think sticking to 'transport' would help read
dmurph
2016/11/08 21:19:58
Done.
| |
362 const std::string& target_blob_uuid, | 525 InternalBlobData* entry) { |
363 const InternalBlobData& blob, | 526 DCHECK(entry); |
364 uint64_t offset, | 527 SetItemStateToPopulated(&entry->building_state_->user_items); |
365 uint64_t length, | 528 entry->status_ = BlobStatus::PENDING_INTERNALS; |
366 InternalBlobData::Builder* target_blob_builder) { | 529 if (entry->CanFinishBuilding()) |
367 DCHECK_GT(length, 0ull); | 530 FinishBuilding(entry); |
531 } | |
368 | 532 |
369 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items(); | 533 void BlobStorageContext::FinishBuilding(InternalBlobData* entry) { |
370 auto iter = items.begin(); | 534 DCHECK(entry); |
371 if (offset) { | 535 |
372 for (; iter != items.end(); ++iter) { | 536 if (BlobStatusIsPending(entry->status_)) { |
373 const BlobDataItem& item = *(iter->get()->item()); | 537 for (const ItemCopyEntry& copy : entry->building_state_->copies) { |
374 if (offset >= item.length()) | 538 // Our source item can be a file if it was a slice of an unpopulated file, |
375 offset -= item.length(); | 539 // or a slice of data that was then paged to disk. |
376 else | 540 size_t dest_size = static_cast<size_t>(copy.dest_item->item()->length()); |
377 break; | 541 DataElement::Type dest_type = copy.dest_item->item()->type(); |
542 switch (copy.source_item->item()->type()) { | |
543 case DataElement::TYPE_BYTES: { | |
544 DCHECK_EQ(dest_type, DataElement::TYPE_BYTES_DESCRIPTION); | |
545 const char* src_data = | |
546 copy.source_item->item()->bytes() + copy.source_item_offset; | |
547 copy.dest_item->item()->item_->SetToBytes(src_data, dest_size); | |
548 } break; | |
549 case DataElement::TYPE_FILE: | |
550 case DataElement::TYPE_UNKNOWN: | |
551 case DataElement::TYPE_BLOB: | |
552 case DataElement::TYPE_BYTES_DESCRIPTION: | |
553 case DataElement::TYPE_FILE_FILESYSTEM: | |
554 case DataElement::TYPE_DISK_CACHE_ENTRY: | |
555 NOTREACHED(); | |
556 break; | |
557 } | |
558 copy.dest_item->set_state(ShareableBlobDataItem::POPULATED_WITH_QUOTA); | |
559 } | |
560 entry->status_ = BlobStatus::DONE; | |
561 } | |
562 | |
563 std::vector<BlobStatusCallback> callbacks; | |
564 if (entry->building_state_.get()) { | |
565 std::swap(callbacks, entry->building_state_->build_completion_callbacks); | |
566 entry->building_state_.reset(); | |
567 } | |
568 | |
569 memory_controller_.NotifyMemoryItemsUsed(entry->items()); | |
570 | |
571 auto runner = base::ThreadTaskRunnerHandle::Get(); | |
572 for (const auto& callback : callbacks) | |
573 runner->PostTask(FROM_HERE, base::Bind(callback, entry->status())); | |
574 | |
575 for (const auto& shareable_item : entry->items()) { | |
576 DCHECK_NE(DataElement::TYPE_BYTES_DESCRIPTION, | |
577 shareable_item->item()->type()); | |
578 DCHECK(shareable_item->IsPopulated()) << shareable_item->state(); | |
579 } | |
580 } | |
581 | |
582 void BlobStorageContext::RequestUserPopulation( | |
583 InternalBlobData* entry, | |
584 std::vector<BlobMemoryController::FileCreationInfo> files) { | |
585 InternalBlobData::BuildingState* building_state = | |
586 entry->building_state_.get(); | |
587 if (building_state->user_data_population_callback) { | |
588 PopulatationAllowedCallback user_data_population_callback = | |
589 building_state->user_data_population_callback; | |
590 building_state->user_data_population_callback.Reset(); | |
591 user_data_population_callback.Run(BlobStatus::PENDING_TRANSPORT, | |
592 std::move(files)); | |
593 return; | |
594 } | |
595 DCHECK(files.empty()); | |
596 FinishedPopulatingPendingBlob(entry); | |
597 } | |
598 | |
599 void BlobStorageContext::OnEnoughSizeForMemory(const std::string& uuid, | |
600 bool success) { | |
601 if (!success) { | |
602 BreakAndFinishPendingBlob(uuid, BlobStatus::ERR_OUT_OF_MEMORY); | |
603 return; | |
604 } | |
605 InternalBlobData* entry = registry_.GetEntry(uuid); | |
606 if (!entry || !entry->building_state_.get()) | |
607 return; | |
608 InternalBlobData::BuildingState& building_state = *entry->building_state_; | |
609 DCHECK(!building_state.memory_quota_request); | |
610 | |
611 if (building_state.transport_items_present) { | |
612 entry->status_ = BlobStatus::PENDING_TRANSPORT; | |
613 RequestUserPopulation( | |
614 entry, std::vector<BlobMemoryController::FileCreationInfo>()); | |
615 } else { | |
616 entry->status_ = BlobStatus::PENDING_INTERNALS; | |
617 } | |
618 | |
619 if (entry->CanFinishBuilding()) | |
620 FinishBuilding(entry); | |
621 } | |
622 | |
623 void BlobStorageContext::OnDependentBlobFinished( | |
624 const std::string& owning_blob_uuid, | |
625 BlobStatus status) { | |
626 InternalBlobData* entry = registry_.GetEntry(owning_blob_uuid); | |
627 if (!entry || !entry->building_state_) | |
628 return; | |
629 | |
630 if (BlobStatusIsError(status)) { | |
631 DCHECK_NE(BlobStatus::ERR_BLOB_DEREFERENCED_WHILE_BUILDING, status) | |
632 << "Referenced blob should never be dereferenced while we " | |
633 << "are depending on it, as our system holds a handle."; | |
634 BreakAndFinishPendingBlob(owning_blob_uuid, | |
635 BlobStatus::ERR_REFERENCED_BLOB_BROKEN); | |
636 return; | |
637 } | |
638 DCHECK_GT(entry->building_state_->num_building_dependent_blobs, 0u); | |
639 --entry->building_state_->num_building_dependent_blobs; | |
640 | |
641 if (entry->CanFinishBuilding()) | |
642 FinishBuilding(entry); | |
643 } | |
644 | |
645 void BlobStorageContext::ClearAndFreeMemory(const std::string& uuid, | |
646 InternalBlobData* entry) { | |
647 if (entry->building_state_) { | |
648 InternalBlobData::BuildingState* building_state = | |
649 entry->building_state_.get(); | |
650 if (building_state->memory_quota_request) { | |
651 building_state->memory_quota_request->Cancel(); | |
378 } | 652 } |
379 } | 653 } |
654 entry->RemoveBlobFromShareableItems(uuid); | |
655 entry->items_.clear(); | |
656 entry->offsets_.clear(); | |
657 } | |
380 | 658 |
381 for (; iter != items.end() && length > 0; ++iter) { | 659 void BlobStorageContext::SetItemStateToPopulated( |
382 scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get(); | 660 std::vector<ShareableBlobDataItem*>* items) { |
383 const BlobDataItem& item = *(shareable_item->item()); | 661 for (ShareableBlobDataItem* shareable_item : *items) { |
384 uint64_t item_length = item.length(); | 662 DCHECK(shareable_item->state() == ShareableBlobDataItem::QUOTA_GRANTED); |
385 DCHECK_GT(item_length, offset); | 663 shareable_item->set_state(ShareableBlobDataItem::POPULATED_WITH_QUOTA); |
386 uint64_t current_length = item_length - offset; | |
387 uint64_t new_length = current_length > length ? length : current_length; | |
388 | |
389 bool reusing_blob_item = offset == 0 && new_length == item.length(); | |
390 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item); | |
391 if (reusing_blob_item) { | |
392 shareable_item->referencing_blobs_mutable()->insert(target_blob_uuid); | |
393 target_blob_builder->AppendSharedBlobItem(shareable_item); | |
394 length -= new_length; | |
395 continue; | |
396 } | |
397 | |
398 // We need to do copying of the items when we have a different offset or | |
399 // length | |
400 switch (item.type()) { | |
401 case DataElement::TYPE_BYTES: { | |
402 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes", | |
403 new_length / 1024); | |
404 if (memory_usage_ + new_length > kBlobStorageMaxMemoryUsage) { | |
405 return false; | |
406 } | |
407 DCHECK(!item.offset()); | |
408 std::unique_ptr<DataElement> element(new DataElement()); | |
409 element->SetToBytes(item.bytes() + offset, | |
410 static_cast<int64_t>(new_length)); | |
411 memory_usage_ += new_length; | |
412 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
413 target_blob_uuid, new BlobDataItem(std::move(element)), | |
414 ShareableBlobDataItem::POPULATED_WITH_QUOTA)); | |
415 } break; | |
416 case DataElement::TYPE_FILE: { | |
417 DCHECK_NE(item.length(), std::numeric_limits<uint64_t>::max()) | |
418 << "We cannot use a section of a file with an unknown length"; | |
419 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File", | |
420 new_length / 1024); | |
421 std::unique_ptr<DataElement> element(new DataElement()); | |
422 element->SetToFilePathRange(item.path(), item.offset() + offset, | |
423 new_length, | |
424 item.expected_modification_time()); | |
425 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
426 target_blob_uuid, | |
427 new BlobDataItem(std::move(element), item.data_handle_), | |
428 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
429 } break; | |
430 case DataElement::TYPE_FILE_FILESYSTEM: { | |
431 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem", | |
432 new_length / 1024); | |
433 std::unique_ptr<DataElement> element(new DataElement()); | |
434 element->SetToFileSystemUrlRange(item.filesystem_url(), | |
435 item.offset() + offset, new_length, | |
436 item.expected_modification_time()); | |
437 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
438 target_blob_uuid, new BlobDataItem(std::move(element)), | |
439 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
440 } break; | |
441 case DataElement::TYPE_DISK_CACHE_ENTRY: { | |
442 std::unique_ptr<DataElement> element(new DataElement()); | |
443 element->SetToDiskCacheEntryRange(item.offset() + offset, | |
444 new_length); | |
445 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( | |
446 target_blob_uuid, | |
447 new BlobDataItem(std::move(element), item.data_handle_, | |
448 item.disk_cache_entry(), | |
449 item.disk_cache_stream_index(), | |
450 item.disk_cache_side_stream_index()), | |
451 ShareableBlobDataItem::POPULATED_WITHOUT_QUOTA)); | |
452 } break; | |
453 case DataElement::TYPE_BYTES_DESCRIPTION: | |
454 case DataElement::TYPE_BLOB: | |
455 case DataElement::TYPE_UNKNOWN: | |
456 CHECK(false) << "Illegal blob item type: " << item.type(); | |
457 } | |
458 length -= new_length; | |
459 offset = 0; | |
460 } | 664 } |
461 return true; | |
462 } | 665 } |
463 | 666 |
464 } // namespace storage | 667 } // namespace storage |
OLD | NEW |