OLD | NEW |
(Empty) | |
| 1 // TODO: Insert description here. (generated by dmurph) |
| 2 |
| 3 #include "storage/browser/blob/blob_flattener.h" |
| 4 |
| 5 #include <limits> |
| 6 #include <set> |
| 7 #include <utility> |
| 8 |
| 9 #include "storage/browser/blob/blob_async_transport_request_builder.h" |
| 10 #include "storage/browser/blob/blob_data_builder.h" |
| 11 #include "storage/browser/blob/blob_storage_context.h" |
| 12 #include "storage/browser/blob/blob_storage_registry.h" |
| 13 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 14 |
| 15 namespace storage { |
| 16 namespace { |
| 17 using ItemCopyEntry = BlobStorageRegistry::ItemCopyEntry; |
| 18 using BlobEntry = BlobStorageRegistry::Entry; |
| 19 using BlobState = BlobStorageRegistry::BlobState; |
| 20 |
| 21 bool IsBytes(DataElement::Type type) { |
| 22 return type == DataElement::TYPE_BYTES || |
| 23 type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 // static |
| 29 BlobFlattener::BlobFlattener(const BlobDataBuilder& transportation_result, |
| 30 InternalBlobData* output_blob, |
| 31 BlobStorageRegistry* registry) { |
| 32 const std::vector<scoped_refptr<BlobDataItem>>& transport_items = |
| 33 transportation_result.items_; |
| 34 const std::string& uuid = transportation_result.uuid_; |
| 35 |
| 36 size_t current_browser_index = 0; |
| 37 std::set<std::string> dependent_blob_uuids; |
| 38 for (size_t transport_item_index = 0; |
| 39 transport_item_index < transport_items.size(); transport_item_index++) { |
| 40 scoped_refptr<BlobDataItem> transport_item = |
| 41 transport_items[transport_item_index]; |
| 42 |
| 43 DataElement::Type type = transport_item->type(); |
| 44 contains_pending_content |= type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 45 |
| 46 if (IsBytes(type)) { |
| 47 memory_needed += transport_item->length(); |
| 48 total_size += transport_item->length(); |
| 49 output_blob->AppendSharedBlobItem( |
| 50 uuid, |
| 51 new ShareableBlobDataItem(BlobStorageContext::GetAndIncrementItemId(), |
| 52 std::move(transport_item))); |
| 53 current_browser_index++; |
| 54 } else if (type == DataElement::TYPE_BLOB) { |
| 55 BlobEntry* ref_entry = registry->GetEntry(transport_item->blob_uuid()); |
| 56 |
| 57 if (!ref_entry || ref_entry->state == BlobState::BROKEN || |
| 58 transport_item->blob_uuid() == uuid) { |
| 59 contains_broken_references = true; |
| 60 return; |
| 61 } |
| 62 |
| 63 if (ref_entry->state == BlobState::PENDING && |
| 64 dependent_blob_uuids.find(transport_item->blob_uuid()) == |
| 65 dependent_blob_uuids.end()) { |
| 66 pending_dependent_blobs.push_back( |
| 67 std::make_pair(transport_item->blob_uuid(), ref_entry)); |
| 68 dependent_blob_uuids.insert(transport_item->blob_uuid()); |
| 69 } |
| 70 |
| 71 uint64_t length = |
| 72 transport_item->length() == std::numeric_limits<uint64_t>::max() |
| 73 ? ref_entry->data.total_size() |
| 74 : transport_item->length(); |
| 75 |
| 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 && |
| 80 length == ref_entry->data.total_size()) { |
| 81 for (const auto& shareable_item : ref_entry->data.items()) { |
| 82 output_blob->AppendSharedBlobItem(uuid, shareable_item); |
| 83 ++current_browser_index; |
| 84 } |
| 85 continue; |
| 86 } |
| 87 |
| 88 BlobSlice slice(ref_entry->data, transport_item->offset(), |
| 89 transport_item->length()); |
| 90 |
| 91 if (slice.has_sliced_first_memory_item) { |
| 92 copies.push_back(ItemCopyEntry(slice.first_source_item, |
| 93 slice.first_item_slice_offset, |
| 94 slice.dest_items.front())); |
| 95 } |
| 96 |
| 97 memory_needed += slice.copying_memory_size; |
| 98 for (const auto& shareable_item : slice.dest_items) { |
| 99 output_blob->AppendSharedBlobItem(uuid, std::move(shareable_item)); |
| 100 ++current_browser_index; |
| 101 } |
| 102 |
| 103 if (slice.has_sliced_last_memory_item) { |
| 104 copies.push_back( |
| 105 ItemCopyEntry(slice.last_source_item, 0, slice.dest_items.back())); |
| 106 } |
| 107 } else { |
| 108 if (type == DataElement::TYPE_FILE) { |
| 109 contains_pending_content = |
| 110 transport_item->path().value() == |
| 111 BlobDataBuilder::kAppendFutureFileTemporaryFileName; |
| 112 } |
| 113 total_size += transport_item->length(); |
| 114 output_blob->AppendSharedBlobItem( |
| 115 uuid, |
| 116 new ShareableBlobDataItem(BlobStorageContext::GetAndIncrementItemId(), |
| 117 std::move(transport_item))); |
| 118 current_browser_index++; |
| 119 } |
| 120 } |
| 121 } |
| 122 |
| 123 BlobFlattener::~BlobFlattener() {} |
| 124 |
| 125 } // namespace storage |
OLD | NEW |