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 return true; | |
27 } | |
28 } // namespace | |
17 | 29 |
18 using ReadStatus = BlobConsolidation::ReadStatus; | 30 using ReadStatus = BlobConsolidation::ReadStatus; |
19 | 31 |
20 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() | 32 BlobConsolidation::ConsolidatedItem::ConsolidatedItem() |
21 : type(DataElement::TYPE_UNKNOWN), | 33 : type(DataElement::TYPE_UNKNOWN), |
22 offset(0), | 34 offset(0), |
23 length(std::numeric_limits<uint64_t>::max()), | 35 length(std::numeric_limits<uint64_t>::max()), |
24 expected_modification_time(0) {} | 36 expected_modification_time(0) {} |
25 | 37 |
26 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() { | 38 BlobConsolidation::ConsolidatedItem::~ConsolidatedItem() { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 double expected_modification_time) { | 104 double expected_modification_time) { |
93 if (length == 0) | 105 if (length == 0) |
94 return; | 106 return; |
95 consolidated_items_.push_back( | 107 consolidated_items_.push_back( |
96 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); | 108 ConsolidatedItem(DataElement::TYPE_FILE_FILESYSTEM, offset, length)); |
97 ConsolidatedItem& item = consolidated_items_.back(); | 109 ConsolidatedItem& item = consolidated_items_.back(); |
98 item.filesystem_url = url; | 110 item.filesystem_url = url; |
99 item.expected_modification_time = expected_modification_time; | 111 item.expected_modification_time = expected_modification_time; |
100 } | 112 } |
101 | 113 |
102 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, | 114 ReadStatus BlobConsolidation::VisitMemory( |
103 size_t consolidated_offset, | 115 size_t consolidated_item_index, |
104 size_t consolidated_size, | 116 size_t consolidated_offset, |
105 void* memory_out) { | 117 size_t consolidated_size, |
106 CHECK(memory_out); | 118 base::Callback<bool(size_t /* total_memory_read */, |
119 const char* /* memory */, | |
120 size_t /* memory_size */)> 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 | 129 LOG(ERROR) << "Invalid consolidated size " << consolidated_size |
michaeln
2016/04/26 22:50:08
is this worth logging? search content/child for ot
| |
116 << " and offset " << consolidated_offset << " vs item length of " | 130 << " and offset " << consolidated_offset << " vs item length of " |
117 << item.length; | 131 << item.length; |
118 return ReadStatus::ERROR_OUT_OF_BOUNDS; | 132 return ReadStatus::ERROR_OUT_OF_BOUNDS; |
119 } | 133 } |
120 | 134 |
121 // We do a binary search to find the correct data to start with in the data | 135 // 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 | 136 // elements. This is slightly customized due to our unique storage and |
123 // constraints. | 137 // constraints. |
124 size_t mid = 0; | 138 size_t mid = 0; |
125 size_t offset_from_mid = consolidated_offset; | 139 size_t offset_from_mid = consolidated_offset; |
(...skipping 22 matching lines...) Expand all Loading... | |
148 } | 162 } |
149 } | 163 } |
150 } | 164 } |
151 | 165 |
152 DCHECK_LT(offset_from_mid, item.data[mid].size()); | 166 DCHECK_LT(offset_from_mid, item.data[mid].size()); |
153 // Read starting from 'mid' and 'offset_from_mid'. | 167 // Read starting from 'mid' and 'offset_from_mid'. |
154 for (size_t memory_read = 0; | 168 for (size_t memory_read = 0; |
155 mid < num_items && memory_read < consolidated_size; mid++) { | 169 mid < num_items && memory_read < consolidated_size; mid++) { |
156 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, | 170 size_t read_size = std::min(item.data[mid].size() - offset_from_mid, |
157 consolidated_size - memory_read); | 171 consolidated_size - memory_read); |
158 memcpy(static_cast<char*>(memory_out) + memory_read, | 172 bool continu = visitor.Run( |
kinuko
2016/04/25 14:10:30
nit: continu -> continue ?
dmurph
2016/04/26 22:33:28
It's a keyword :(
michaeln
2016/04/26 22:50:08
i said the same thing, but continue already means
| |
159 item.data[mid].data() + offset_from_mid, read_size); | 173 memory_read, item.data[mid].data() + offset_from_mid, read_size); |
174 // If the visitor doesn't want to continue then we return early. | |
175 if (!continu) | |
176 return ReadStatus::CANCELLED_BY_VISITOR; | |
160 offset_from_mid = 0; | 177 offset_from_mid = 0; |
161 memory_read += read_size; | 178 memory_read += read_size; |
162 } | 179 } |
163 return ReadStatus::OK; | 180 return ReadStatus::OK; |
164 } | 181 } |
165 | 182 |
183 ReadStatus BlobConsolidation::ReadMemory(size_t consolidated_item_index, | |
184 size_t consolidated_offset, | |
185 size_t consolidated_size, | |
186 void* memory_out) const { | |
187 CHECK(memory_out); | |
michaeln
2016/04/26 22:50:08
check isn't needed becuase VisitMemory/WriteMemory
dmurph
2016/04/27 19:14:04
ok
| |
188 return VisitMemory(consolidated_item_index, consolidated_offset, | |
189 consolidated_size, base::Bind(&WriteMemory, memory_out)); | |
190 } | |
191 | |
166 } // namespace content | 192 } // namespace content |
OLD | NEW |