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