OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "content/child/blob_storage/blob_consolidation.h" |
| 8 |
| 9 using storage::DataElement; |
| 10 using blink::WebThreadSafeData; |
| 11 |
| 12 namespace content { |
| 13 |
| 14 using ReadStatus = BlobConsolidation::ReadStatus; |
| 15 |
| 16 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() |
| 17 : type(DataElement::TYPE_UNKNOWN), |
| 18 offset(0), |
| 19 length(kuint64max), |
| 20 expected_modification_time(0) {} |
| 21 |
| 22 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() {} |
| 23 |
| 24 BlobConsolidation::ConsolidatedItem::ConsolidatedItem(DataElement::Type type, |
| 25 uint64_t offset, |
| 26 uint64_t length) |
| 27 : type(type), |
| 28 offset(offset), |
| 29 length(length), |
| 30 expected_modification_time(0) {} |
| 31 |
| 32 BlobConsolidation::BlobConsolidation() |
| 33 : total_memory_(0) {} |
| 34 |
| 35 BlobConsolidation::~BlobConsolidation() {} |
| 36 |
| 37 void BlobConsolidation::AddDataItem(const WebThreadSafeData& data) { |
| 38 if (data.size() == 0) |
| 39 return; |
| 40 if (consolidated_items_.empty() || |
| 41 consolidated_items_.back().type != DataElement::TYPE_BYTES) { |
| 42 consolidated_items_.push_back( |
| 43 ConsolidatedItem(DataElement::TYPE_BYTES, 0, 0)); |
| 44 } |
| 45 ConsolidatedItem& item = consolidated_items_.back(); |
| 46 if (!item.data.empty()) { |
| 47 item.offsets.push_back(static_cast<size_t>(item.length)); |
| 48 } |
| 49 item.length += data.size(); |
| 50 total_memory_ += data.size(); |
| 51 item.data.push_back(data); |
| 52 } |
| 53 |
| 54 void BlobConsolidation::AddFileItem(const base::FilePath& path, uint64_t offset, |
| 55 uint64_t length, |
| 56 double expected_modification_time) { |
| 57 if (length == 0) |
| 58 return; |
| 59 consolidated_items_.push_back( |
| 60 ConsolidatedItem(DataElement::TYPE_FILE, offset, length)); |
| 61 ConsolidatedItem& item = consolidated_items_.back(); |
| 62 item.path = path; |
| 63 item.expected_modification_time = expected_modification_time; |
| 64 } |
| 65 |
| 66 void BlobConsolidation::AddBlobItem(const std::string& uuid, uint64_t offset, |
| 67 uint64_t length) { |
| 68 if (length == 0) |
| 69 return; |
| 70 consolidated_items_.push_back( |
| 71 ConsolidatedItem(DataElement::TYPE_BLOB, offset, length)); |
| 72 ConsolidatedItem& item = consolidated_items_.back(); |
| 73 item.blob_uuid = uuid; |
| 74 } |
| 75 |
| 76 void BlobConsolidation::AddFileSystemItem(const GURL& url, |
| 77 uint64_t offset, uint64_t length, |
| 78 double expected_modification_time) { |
| 79 if (length == 0) |
| 80 return; |
| 81 consolidated_items_.push_back( |
| 82 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); |
| 83 ConsolidatedItem& item = consolidated_items_.back(); |
| 84 item.filesystem_url = url; |
| 85 item.expected_modification_time = expected_modification_time; |
| 86 } |
| 87 |
| 88 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, |
| 89 size_t consolidated_offset, |
| 90 size_t consolidated_size, |
| 91 void* memory_out) { |
| 92 CHECK(memory_out); |
| 93 if (consolidated_item_index >= consolidated_items_.size()) |
| 94 return ReadStatus::ERROR_OUT_OF_BOUNDS; |
| 95 |
| 96 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index]; |
| 97 if (item.type != DataElement::TYPE_BYTES) |
| 98 return ReadStatus::ERROR_WRONG_TYPE; |
| 99 |
| 100 if (consolidated_size + consolidated_offset > item.length) { |
| 101 LOG(ERROR) << "Invalid consolidated size " << consolidated_size |
| 102 << " and offset " << consolidated_offset << " vs item length of " |
| 103 << item.length; |
| 104 return ReadStatus::ERROR_OUT_OF_BOUNDS; |
| 105 } |
| 106 |
| 107 // We do a binary search to find the correct data to start with in the data |
| 108 // elements. This is slightly customized due to our unique storage and |
| 109 // constraints. |
| 110 size_t mid = 0; |
| 111 size_t offset_from_mid = consolidated_offset; |
| 112 size_t num_items = item.data.size(); |
| 113 if (!item.offsets.empty()) { |
| 114 size_t low = 0; |
| 115 size_t high = num_items - 1; |
| 116 while (true) { |
| 117 mid = (high + low) / 2; |
| 118 // Note: we don't include the implicit '0' for the first item in offsets. |
| 119 size_t item_offset = (mid == 0 ? 0 : item.offsets[mid - 1]); |
| 120 offset_from_mid = consolidated_offset - item_offset; |
| 121 size_t next_item_offset = (mid + 1 == num_items ? 0 : item.offsets[mid]); |
| 122 if (item_offset == consolidated_offset) { |
| 123 // found exact match. |
| 124 break; |
| 125 } else if (item_offset > consolidated_offset) { |
| 126 high = mid - 1; |
| 127 } else if (mid + 1 == num_items || |
| 128 next_item_offset > consolidated_offset) { |
| 129 // We are at the last item, or the next offset is greater than the one |
| 130 // we want, so the current item wins. |
| 131 break; |
| 132 } else { |
| 133 low = mid + 1; |
| 134 } |
| 135 } |
| 136 } |
| 137 |
| 138 DCHECK_LT(offset_from_mid, item.data[mid].size()); |
| 139 // Read starting from 'mid' and 'offset_from_mid'. |
| 140 for (size_t memory_read = 0; |
| 141 mid < num_items && memory_read < consolidated_size; mid++) { |
| 142 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, |
| 143 consolidated_size - memory_read); |
| 144 memcpy(static_cast<char*>(memory_out) + memory_read, |
| 145 item.data[mid].data() + offset_from_mid, read_size); |
| 146 offset_from_mid = 0; |
| 147 memory_read += read_size; |
| 148 } |
| 149 return ReadStatus::DONE; |
| 150 } |
| 151 |
| 152 } // namespace content |
OLD | NEW |