OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/blob_flattener.h" |
| 6 |
| 7 #include <limits> |
| 8 #include <set> |
| 9 #include <utility> |
| 10 |
| 11 #include "storage/browser/blob/blob_async_transport_request_builder.h" |
| 12 #include "storage/browser/blob/blob_data_builder.h" |
| 13 #include "storage/browser/blob/blob_slice.h" |
| 14 #include "storage/browser/blob/blob_storage_context.h" |
| 15 #include "storage/browser/blob/blob_storage_registry.h" |
| 16 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 17 |
| 18 namespace storage { |
| 19 namespace { |
| 20 using ItemCopyEntry = BlobStorageRegistry::ItemCopyEntry; |
| 21 using BlobEntry = BlobStorageRegistry::Entry; |
| 22 |
| 23 bool IsBytes(DataElement::Type type) { |
| 24 return type == DataElement::TYPE_BYTES || |
| 25 type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 // static |
| 31 BlobFlattener::BlobFlattener(const BlobDataBuilder& transportation_result, |
| 32 InternalBlobData* output_blob, |
| 33 BlobStorageRegistry* registry) { |
| 34 const std::vector<scoped_refptr<BlobDataItem>>& transport_items = |
| 35 transportation_result.items_; |
| 36 const std::string& uuid = transportation_result.uuid_; |
| 37 |
| 38 std::set<std::string> dependent_blob_uuids; |
| 39 for (scoped_refptr<BlobDataItem> transport_item : transport_items) { |
| 40 DataElement::Type type = transport_item->type(); |
| 41 contains_pending_content |= type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 42 |
| 43 if (IsBytes(type)) { |
| 44 memory_needed += transport_item->length(); |
| 45 total_size += transport_item->length(); |
| 46 output_blob->AppendSharedBlobItem( |
| 47 uuid, new ShareableBlobDataItem(std::move(transport_item))); |
| 48 } else if (type == DataElement::TYPE_BLOB) { |
| 49 BlobEntry* ref_entry = registry->GetEntry(transport_item->blob_uuid()); |
| 50 |
| 51 if (!ref_entry || BlobStatusIsError(ref_entry->status) || |
| 52 transport_item->blob_uuid() == uuid) { |
| 53 contains_broken_references = true; |
| 54 return; |
| 55 } |
| 56 |
| 57 if (dependent_blob_uuids.find(transport_item->blob_uuid()) == |
| 58 dependent_blob_uuids.end()) { |
| 59 dependent_blobs.push_back( |
| 60 std::make_pair(transport_item->blob_uuid(), ref_entry)); |
| 61 dependent_blob_uuids.insert(transport_item->blob_uuid()); |
| 62 } |
| 63 |
| 64 uint64_t length = transport_item->length() == DataElement::kUnknownSize |
| 65 ? ref_entry->data.total_size() |
| 66 : transport_item->length(); |
| 67 |
| 68 total_size += length; |
| 69 |
| 70 // If we're referencing the whole blob, then we don't need to slice. |
| 71 if (transport_item->offset() == 0 && |
| 72 length == ref_entry->data.total_size()) { |
| 73 for (const auto& shareable_item : ref_entry->data.items()) { |
| 74 output_blob->AppendSharedBlobItem(uuid, shareable_item); |
| 75 } |
| 76 continue; |
| 77 } |
| 78 |
| 79 // Validate our reference has good offset & length. |
| 80 if (transport_item->offset() + transport_item->length() > |
| 81 ref_entry->data.total_size()) { |
| 82 contains_invalid_references = true; |
| 83 return; |
| 84 } |
| 85 |
| 86 BlobSlice slice(ref_entry->data, transport_item->offset(), |
| 87 transport_item->length()); |
| 88 |
| 89 if (slice.has_sliced_first_memory_item) { |
| 90 copies.push_back(ItemCopyEntry(slice.first_source_item, |
| 91 slice.first_item_slice_offset, |
| 92 slice.dest_items.front())); |
| 93 } |
| 94 if (slice.has_sliced_last_memory_item) { |
| 95 copies.push_back( |
| 96 ItemCopyEntry(slice.last_source_item, 0, slice.dest_items.back())); |
| 97 } |
| 98 |
| 99 memory_needed += slice.copying_memory_size; |
| 100 for (auto& shareable_item : slice.dest_items) { |
| 101 output_blob->AppendSharedBlobItem(uuid, std::move(shareable_item)); |
| 102 } |
| 103 } else { |
| 104 if (type == DataElement::TYPE_FILE) { |
| 105 contains_pending_content |= |
| 106 transport_item->path().value() == |
| 107 BlobDataBuilder::kAppendFutureFileTemporaryFileName; |
| 108 } |
| 109 total_size += transport_item->length(); |
| 110 output_blob->AppendSharedBlobItem( |
| 111 uuid, new ShareableBlobDataItem(std::move(transport_item))); |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 BlobFlattener::~BlobFlattener() {} |
| 117 |
| 118 } // namespace storage |
OLD | NEW |