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 #include "storage/common/data_element.h" |
| 18 |
| 19 namespace storage { |
| 20 namespace { |
| 21 using ItemCopyEntry = InternalBlobData::ItemCopyEntry; |
| 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& input_builder, |
| 32 InternalBlobData* output_blob, |
| 33 BlobStorageRegistry* registry) { |
| 34 const std::vector<scoped_refptr<BlobDataItem>>& transport_items = |
| 35 input_builder.items_; |
| 36 const std::string& uuid = input_builder.uuid_; |
| 37 |
| 38 std::set<std::string> dependent_blob_uuids; |
| 39 bool contains_pending_content = false; |
| 40 for (scoped_refptr<BlobDataItem> transport_item : transport_items) { |
| 41 DataElement::Type type = transport_item->type(); |
| 42 contains_pending_content |= type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 43 |
| 44 if (IsBytes(type)) { |
| 45 DCHECK_NE(0 + DataElement::kUnknownSize, transport_item->length()); |
| 46 memory_needed += transport_item->length(); |
| 47 total_size += transport_item->length(); |
| 48 output_blob->AppendSharedBlobItem( |
| 49 uuid, new ShareableBlobDataItem(std::move(transport_item))); |
| 50 continue; |
| 51 } |
| 52 if (type == DataElement::TYPE_BLOB) { |
| 53 InternalBlobData* ref_entry = |
| 54 registry->GetEntry(transport_item->blob_uuid()); |
| 55 |
| 56 if (!ref_entry || transport_item->blob_uuid() == uuid) { |
| 57 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 58 return; |
| 59 } |
| 60 |
| 61 if (BlobStatusIsError(ref_entry->status())) { |
| 62 status = BlobStatus::ERR_REFERENCED_BLOB_BROKEN; |
| 63 return; |
| 64 } |
| 65 |
| 66 if (dependent_blob_uuids.find(transport_item->blob_uuid()) == |
| 67 dependent_blob_uuids.end()) { |
| 68 dependent_blobs.push_back( |
| 69 std::make_pair(transport_item->blob_uuid(), ref_entry)); |
| 70 dependent_blob_uuids.insert(transport_item->blob_uuid()); |
| 71 } |
| 72 |
| 73 uint64_t length = transport_item->length() == DataElement::kUnknownSize |
| 74 ? ref_entry->total_size() |
| 75 : transport_item->length(); |
| 76 total_size += length; |
| 77 |
| 78 // If we're referencing the whole blob, then we don't need to slice. |
| 79 if (transport_item->offset() == 0 && length == ref_entry->total_size()) { |
| 80 for (const auto& shareable_item : ref_entry->items()) { |
| 81 output_blob->AppendSharedBlobItem(uuid, shareable_item); |
| 82 } |
| 83 continue; |
| 84 } |
| 85 |
| 86 // Validate our reference has good offset & length. |
| 87 if (transport_item->offset() + transport_item->length() > |
| 88 ref_entry->total_size()) { |
| 89 status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
| 90 return; |
| 91 } |
| 92 |
| 93 BlobSlice slice(*ref_entry, transport_item->offset(), |
| 94 transport_item->length()); |
| 95 |
| 96 if (slice.first_source_item) { |
| 97 copies.push_back(ItemCopyEntry(slice.first_source_item, |
| 98 slice.first_item_slice_offset, |
| 99 slice.dest_items.front())); |
| 100 } |
| 101 if (slice.last_source_item) { |
| 102 copies.push_back( |
| 103 ItemCopyEntry(slice.last_source_item, 0, slice.dest_items.back())); |
| 104 } |
| 105 |
| 106 memory_needed += slice.copying_memory_size; |
| 107 for (auto& shareable_item : slice.dest_items) { |
| 108 output_blob->AppendSharedBlobItem(uuid, std::move(shareable_item)); |
| 109 } |
| 110 continue; |
| 111 } |
| 112 if (type == DataElement::TYPE_FILE) { |
| 113 contains_pending_content |= |
| 114 transport_item->path().value() == |
| 115 BlobDataBuilder::kAppendFutureFileTemporaryFileName; |
| 116 } |
| 117 total_size += transport_item->length(); |
| 118 output_blob->AppendSharedBlobItem( |
| 119 uuid, new ShareableBlobDataItem(std::move(transport_item))); |
| 120 } |
| 121 status = contains_pending_content ? BlobStatus::PENDING_DATA_POPULATION |
| 122 : BlobStatus::DONE; |
| 123 } |
| 124 |
| 125 BlobFlattener::~BlobFlattener() {} |
| 126 |
| 127 } // namespace storage |
OLD | NEW |