OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "content/child/blob_storage/blob_consolidation.h" | 5 #include "content/child/blob_storage/blob_consolidation.h" |
6 | 6 |
7 #include <stdint.h> | |
8 | |
9 #include <algorithm> | 7 #include <algorithm> |
10 #include <limits> | 8 #include <limits> |
11 #include <string> | 9 #include <string> |
12 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/callback.h" |
| 13 |
13 using storage::DataElement; | 14 using storage::DataElement; |
14 using blink::WebThreadSafeData; | 15 using blink::WebThreadSafeData; |
15 | 16 |
16 namespace content { | 17 namespace content { |
| 18 namespace { |
| 19 bool WriteMemory(void* memory_out, |
| 20 size_t* total_read, |
| 21 const char* memory, |
| 22 size_t size) { |
| 23 memcpy(static_cast<char*>(memory_out) + *total_read, memory, size); |
| 24 *total_read += size; |
| 25 return true; |
| 26 } |
| 27 } // namespace |
17 | 28 |
18 using ReadStatus = BlobConsolidation::ReadStatus; | 29 using ReadStatus = BlobConsolidation::ReadStatus; |
19 | 30 |
20 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() | 31 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() |
21 : type(DataElement::TYPE_UNKNOWN), | 32 : type(DataElement::TYPE_UNKNOWN), |
22 offset(0), | 33 offset(0), |
23 length(std::numeric_limits<uint64_t>::max()), | 34 length(std::numeric_limits<uint64_t>::max()), |
24 expected_modification_time(0) {} | 35 expected_modification_time(0) {} |
25 | 36 |
26 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() { | 37 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 double expected_modification_time) { | 103 double expected_modification_time) { |
93 if (length == 0) | 104 if (length == 0) |
94 return; | 105 return; |
95 consolidated_items_.push_back( | 106 consolidated_items_.push_back( |
96 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); | 107 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); |
97 ConsolidatedItem& item = consolidated_items_.back(); | 108 ConsolidatedItem& item = consolidated_items_.back(); |
98 item.filesystem_url = url; | 109 item.filesystem_url = url; |
99 item.expected_modification_time = expected_modification_time; | 110 item.expected_modification_time = expected_modification_time; |
100 } | 111 } |
101 | 112 |
102 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, | 113 ReadStatus BlobConsolidation::VisitMemory(size_t consolidated_item_index, |
103 size_t consolidated_offset, | 114 size_t consolidated_offset, |
104 size_t consolidated_size, | 115 size_t consolidated_size, |
105 void* memory_out) { | 116 const MemoryVisitor& visitor) const { |
106 CHECK(memory_out); | |
107 if (consolidated_item_index >= consolidated_items_.size()) | 117 if (consolidated_item_index >= consolidated_items_.size()) |
108 return ReadStatus::ERROR_OUT_OF_BOUNDS; | 118 return ReadStatus::ERROR_OUT_OF_BOUNDS; |
109 | 119 |
110 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index]; | 120 const ConsolidatedItem& item = consolidated_items_[consolidated_item_index]; |
111 if (item.type != DataElement::TYPE_BYTES) | 121 if (item.type != DataElement::TYPE_BYTES) |
112 return ReadStatus::ERROR_WRONG_TYPE; | 122 return ReadStatus::ERROR_WRONG_TYPE; |
113 | 123 |
114 if (consolidated_size + consolidated_offset > item.length) { | 124 if (consolidated_size + consolidated_offset > item.length) { |
115 LOG(ERROR) << "Invalid consolidated size " << consolidated_size | |
116 << " and offset " << consolidated_offset << " vs item length of " | |
117 << item.length; | |
118 return ReadStatus::ERROR_OUT_OF_BOUNDS; | 125 return ReadStatus::ERROR_OUT_OF_BOUNDS; |
119 } | 126 } |
120 | 127 |
121 // We do a binary search to find the correct data to start with in the data | 128 // We do a binary search to find the correct data to start with in the data |
122 // elements. This is slightly customized due to our unique storage and | 129 // elements. This is slightly customized due to our unique storage and |
123 // constraints. | 130 // constraints. |
124 size_t mid = 0; | 131 size_t mid = 0; |
125 size_t offset_from_mid = consolidated_offset; | 132 size_t offset_from_mid = consolidated_offset; |
126 size_t num_items = item.data.size(); | 133 size_t num_items = item.data.size(); |
127 if (!item.offsets.empty()) { | 134 if (!item.offsets.empty()) { |
(...skipping 20 matching lines...) Expand all Loading... |
148 } | 155 } |
149 } | 156 } |
150 } | 157 } |
151 | 158 |
152 DCHECK_LT(offset_from_mid, item.data[mid].size()); | 159 DCHECK_LT(offset_from_mid, item.data[mid].size()); |
153 // Read starting from 'mid' and 'offset_from_mid'. | 160 // Read starting from 'mid' and 'offset_from_mid'. |
154 for (size_t memory_read = 0; | 161 for (size_t memory_read = 0; |
155 mid < num_items && memory_read < consolidated_size; mid++) { | 162 mid < num_items && memory_read < consolidated_size; mid++) { |
156 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, | 163 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, |
157 consolidated_size - memory_read); | 164 consolidated_size - memory_read); |
158 memcpy(static_cast<char*>(memory_out) + memory_read, | 165 bool continu = |
159 item.data[mid].data() + offset_from_mid, read_size); | 166 visitor.Run(item.data[mid].data() + offset_from_mid, read_size); |
| 167 if (!continu) |
| 168 return ReadStatus::CANCELLED_BY_VISITOR; |
160 offset_from_mid = 0; | 169 offset_from_mid = 0; |
161 memory_read += read_size; | 170 memory_read += read_size; |
162 } | 171 } |
163 return ReadStatus::OK; | 172 return ReadStatus::OK; |
164 } | 173 } |
165 | 174 |
| 175 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, |
| 176 size_t consolidated_offset, |
| 177 size_t consolidated_size, |
| 178 void* memory_out) const { |
| 179 size_t total_read = 0; |
| 180 return VisitMemory(consolidated_item_index, consolidated_offset, |
| 181 consolidated_size, |
| 182 base::Bind(&WriteMemory, memory_out, &total_read)); |
| 183 } |
| 184 |
166 } // namespace content | 185 } // namespace content |
OLD | NEW |