OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/base/resource/data_pack.h" | 5 #include "ui/base/resource/data_pack.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 BAD_VERSION, | 53 BAD_VERSION, |
54 INDEX_TRUNCATED, | 54 INDEX_TRUNCATED, |
55 ENTRY_NOT_FOUND, | 55 ENTRY_NOT_FOUND, |
56 HEADER_TRUNCATED, | 56 HEADER_TRUNCATED, |
57 WRONG_ENCODING, | 57 WRONG_ENCODING, |
58 INIT_FAILED_FROM_FILE, | 58 INIT_FAILED_FROM_FILE, |
59 | 59 |
60 LOAD_ERRORS_COUNT, | 60 LOAD_ERRORS_COUNT, |
61 }; | 61 }; |
62 | 62 |
63 class MemoryMappedDataSource : public ui::DataSource { | |
64 public: | |
65 MemoryMappedDataSource(std::unique_ptr<base::MemoryMappedFile> mmap) | |
66 : mmap_(std::move(mmap)) {} | |
67 | |
68 ~MemoryMappedDataSource() override{}; | |
sadrul
2016/05/17 15:07:50
space between override and {}. No ; at the end.
altimin
2016/05/17 15:40:07
Done.
| |
69 | |
70 size_t length() const override { return mmap_->length(); } | |
71 | |
72 const uint8_t* data() const override { return mmap_->data(); } | |
73 | |
74 private: | |
75 std::unique_ptr<base::MemoryMappedFile> mmap_; | |
sadrul
2016/05/17 15:07:50
DISALLOW_COPY_AND_ASSIGN
altimin
2016/05/17 15:40:07
Done.
| |
76 }; | |
77 | |
78 class BufferDataSource : public ui::DataSource { | |
79 public: | |
80 BufferDataSource(base::StringPiece buffer) : buffer_(buffer) {} | |
81 | |
82 ~BufferDataSource() override{}; | |
sadrul
2016/05/17 15:07:50
same as above
altimin
2016/05/17 15:40:07
Done.
| |
83 | |
84 size_t length() const override { return buffer_.length(); } | |
85 | |
86 const uint8_t* data() const override { | |
87 return reinterpret_cast<const uint8_t*>(buffer_.data()); | |
88 } | |
89 | |
90 private: | |
91 base::StringPiece buffer_; | |
92 }; | |
sadrul
2016/05/17 15:07:50
ditto
altimin
2016/05/17 15:40:07
Done.
| |
93 | |
63 } // namespace | 94 } // namespace |
64 | 95 |
65 namespace ui { | 96 namespace ui { |
66 | 97 |
67 DataPack::DataPack(ui::ScaleFactor scale_factor) | 98 DataPack::DataPack(ui::ScaleFactor scale_factor) |
68 : resource_count_(0), | 99 : resource_count_(0), |
69 text_encoding_type_(BINARY), | 100 text_encoding_type_(BINARY), |
70 scale_factor_(scale_factor), | 101 scale_factor_(scale_factor), |
71 has_only_material_design_assets_(false) { | 102 has_only_material_design_assets_(false) { |
72 } | 103 } |
73 | 104 |
74 DataPack::~DataPack() { | 105 DataPack::~DataPack() { |
75 } | 106 } |
76 | 107 |
77 bool DataPack::LoadFromPath(const base::FilePath& path) { | 108 bool DataPack::LoadFromPath(const base::FilePath& path) { |
78 mmap_.reset(new base::MemoryMappedFile); | 109 std::unique_ptr<base::MemoryMappedFile> mmap(new base::MemoryMappedFile); |
79 if (!mmap_->Initialize(path)) { | 110 if (!mmap->Initialize(path)) { |
80 DLOG(ERROR) << "Failed to mmap datapack"; | 111 DLOG(ERROR) << "Failed to mmap datapack"; |
81 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, | 112 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, |
82 LOAD_ERRORS_COUNT); | 113 LOAD_ERRORS_COUNT); |
83 mmap_.reset(); | |
84 return false; | 114 return false; |
85 } | 115 } |
116 data_source_.reset(new MemoryMappedDataSource(std::move(mmap))); | |
86 return LoadImpl(); | 117 return LoadImpl(); |
87 } | 118 } |
88 | 119 |
89 bool DataPack::LoadFromFile(base::File file) { | 120 bool DataPack::LoadFromFile(base::File file) { |
90 return LoadFromFileRegion(std::move(file), | 121 return LoadFromFileRegion(std::move(file), |
91 base::MemoryMappedFile::Region::kWholeFile); | 122 base::MemoryMappedFile::Region::kWholeFile); |
92 } | 123 } |
93 | 124 |
94 bool DataPack::LoadFromFileRegion( | 125 bool DataPack::LoadFromFileRegion( |
95 base::File file, | 126 base::File file, |
96 const base::MemoryMappedFile::Region& region) { | 127 const base::MemoryMappedFile::Region& region) { |
97 mmap_.reset(new base::MemoryMappedFile); | 128 std::unique_ptr<base::MemoryMappedFile> mmap(new base::MemoryMappedFile); |
98 if (!mmap_->Initialize(std::move(file), region)) { | 129 if (!mmap->Initialize(std::move(file), region)) { |
99 DLOG(ERROR) << "Failed to mmap datapack"; | 130 DLOG(ERROR) << "Failed to mmap datapack"; |
100 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED_FROM_FILE, | 131 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED_FROM_FILE, |
101 LOAD_ERRORS_COUNT); | 132 LOAD_ERRORS_COUNT); |
102 mmap_.reset(); | |
103 return false; | 133 return false; |
104 } | 134 } |
135 data_source_.reset(new MemoryMappedDataSource(std::move(mmap))); | |
136 return LoadImpl(); | |
137 } | |
138 | |
139 bool DataPack::LoadFromBuffer(base::StringPiece buffer) { | |
140 data_source_.reset(new BufferDataSource(buffer)); | |
105 return LoadImpl(); | 141 return LoadImpl(); |
106 } | 142 } |
107 | 143 |
108 bool DataPack::LoadImpl() { | 144 bool DataPack::LoadImpl() { |
109 // Sanity check the header of the file. | 145 // Sanity check the header of the file. |
110 if (kHeaderLength > mmap_->length()) { | 146 if (kHeaderLength > data_source_->length()) { |
111 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; | 147 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; |
112 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", HEADER_TRUNCATED, | 148 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", HEADER_TRUNCATED, |
113 LOAD_ERRORS_COUNT); | 149 LOAD_ERRORS_COUNT); |
114 mmap_.reset(); | 150 // Clear memory mapped file even when we're loading data from a buffer, |
151 // in order to simplify code. | |
sadrul
2016/05/17 15:07:50
This comment feels out of place? When loading from
altimin
2016/05/17 15:40:07
Done.
| |
152 data_source_.reset(); | |
115 return false; | 153 return false; |
116 } | 154 } |
117 | 155 |
118 // Parse the header of the file. | 156 // Parse the header of the file. |
119 // First uint32_t: version; second: resource count; | 157 // First uint32_t: version; second: resource count; |
120 const uint32_t* ptr = reinterpret_cast<const uint32_t*>(mmap_->data()); | 158 const uint32_t* ptr = reinterpret_cast<const uint32_t*>(data_source_->data()); |
121 uint32_t version = ptr[0]; | 159 uint32_t version = ptr[0]; |
122 if (version != kFileFormatVersion) { | 160 if (version != kFileFormatVersion) { |
123 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " | 161 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " |
124 << kFileFormatVersion; | 162 << kFileFormatVersion; |
125 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, | 163 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, |
126 LOAD_ERRORS_COUNT); | 164 LOAD_ERRORS_COUNT); |
127 mmap_.reset(); | 165 data_source_.reset(); |
128 return false; | 166 return false; |
129 } | 167 } |
130 resource_count_ = ptr[1]; | 168 resource_count_ = ptr[1]; |
131 | 169 |
132 // third: text encoding. | 170 // third: text encoding. |
133 const uint8_t* ptr_encoding = reinterpret_cast<const uint8_t*>(ptr + 2); | 171 const uint8_t* ptr_encoding = reinterpret_cast<const uint8_t*>(ptr + 2); |
134 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding); | 172 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding); |
135 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && | 173 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && |
136 text_encoding_type_ != BINARY) { | 174 text_encoding_type_ != BINARY) { |
137 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ | 175 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ |
138 << ", expected between " << BINARY << " and " << UTF16; | 176 << ", expected between " << BINARY << " and " << UTF16; |
139 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", WRONG_ENCODING, | 177 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", WRONG_ENCODING, |
140 LOAD_ERRORS_COUNT); | 178 LOAD_ERRORS_COUNT); |
141 mmap_.reset(); | 179 data_source_.reset(); |
142 return false; | 180 return false; |
143 } | 181 } |
144 | 182 |
145 // Sanity check the file. | 183 // Sanity check the file. |
146 // 1) Check we have enough entries. There's an extra entry after the last item | 184 // 1) Check we have enough entries. There's an extra entry after the last item |
147 // which gives the length of the last item. | 185 // which gives the length of the last item. |
148 if (kHeaderLength + (resource_count_ + 1) * sizeof(DataPackEntry) > | 186 if (kHeaderLength + (resource_count_ + 1) * sizeof(DataPackEntry) > |
149 mmap_->length()) { | 187 data_source_->length()) { |
150 LOG(ERROR) << "Data pack file corruption: too short for number of " | 188 LOG(ERROR) << "Data pack file corruption: too short for number of " |
151 "entries specified."; | 189 "entries specified."; |
152 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, | 190 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, |
153 LOAD_ERRORS_COUNT); | 191 LOAD_ERRORS_COUNT); |
154 mmap_.reset(); | 192 data_source_.reset(); |
155 return false; | 193 return false; |
156 } | 194 } |
157 // 2) Verify the entries are within the appropriate bounds. There's an extra | 195 // 2) Verify the entries are within the appropriate bounds. There's an extra |
158 // entry after the last item which gives us the length of the last item. | 196 // entry after the last item which gives us the length of the last item. |
159 for (size_t i = 0; i < resource_count_ + 1; ++i) { | 197 for (size_t i = 0; i < resource_count_ + 1; ++i) { |
160 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( | 198 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
161 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); | 199 data_source_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); |
162 if (entry->file_offset > mmap_->length()) { | 200 if (entry->file_offset > data_source_->length()) { |
163 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " | 201 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " |
164 << "Was the file corrupted?"; | 202 << "Was the file corrupted?"; |
165 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 203 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
166 LOAD_ERRORS_COUNT); | 204 LOAD_ERRORS_COUNT); |
167 mmap_.reset(); | 205 data_source_.reset(); |
168 return false; | 206 return false; |
169 } | 207 } |
170 } | 208 } |
171 | 209 |
172 return true; | 210 return true; |
173 } | 211 } |
174 | 212 |
175 bool DataPack::HasResource(uint16_t resource_id) const { | 213 bool DataPack::HasResource(uint16_t resource_id) const { |
176 return !!bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 214 return !!bsearch(&resource_id, data_source_->data() + kHeaderLength, |
177 sizeof(DataPackEntry), DataPackEntry::CompareById); | 215 resource_count_, sizeof(DataPackEntry), |
216 DataPackEntry::CompareById); | |
178 } | 217 } |
179 | 218 |
180 bool DataPack::GetStringPiece(uint16_t resource_id, | 219 bool DataPack::GetStringPiece(uint16_t resource_id, |
181 base::StringPiece* data) const { | 220 base::StringPiece* data) const { |
182 // It won't be hard to make this endian-agnostic, but it's not worth | 221 // It won't be hard to make this endian-agnostic, but it's not worth |
183 // bothering to do right now. | 222 // bothering to do right now. |
184 #if defined(__BYTE_ORDER) | 223 #if defined(__BYTE_ORDER) |
185 // Linux check | 224 // Linux check |
186 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, | 225 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, |
187 "datapack assumes little endian"); | 226 "datapack assumes little endian"); |
188 #elif defined(__BIG_ENDIAN__) | 227 #elif defined(__BIG_ENDIAN__) |
189 // Mac check | 228 // Mac check |
190 #error DataPack assumes little endian | 229 #error DataPack assumes little endian |
191 #endif | 230 #endif |
192 | 231 |
193 const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>( | 232 const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>(bsearch( |
194 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 233 &resource_id, data_source_->data() + kHeaderLength, resource_count_, |
195 sizeof(DataPackEntry), DataPackEntry::CompareById)); | 234 sizeof(DataPackEntry), DataPackEntry::CompareById)); |
196 if (!target) { | 235 if (!target) { |
197 return false; | 236 return false; |
198 } | 237 } |
199 | 238 |
200 const DataPackEntry* next_entry = target + 1; | 239 const DataPackEntry* next_entry = target + 1; |
201 // If the next entry points beyond the end of the file this data pack's entry | 240 // If the next entry points beyond the end of the file this data pack's entry |
202 // table is corrupt. Log an error and return false. See | 241 // table is corrupt. Log an error and return false. See |
203 // http://crbug.com/371301. | 242 // http://crbug.com/371301. |
204 if (next_entry->file_offset > mmap_->length()) { | 243 if (next_entry->file_offset > data_source_->length()) { |
205 size_t entry_index = target - | 244 size_t entry_index = target - reinterpret_cast<const DataPackEntry*>( |
206 reinterpret_cast<const DataPackEntry*>(mmap_->data() + kHeaderLength); | 245 data_source_->data() + kHeaderLength); |
207 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " | 246 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " |
208 << "of file. This should have been caught when loading. Was the " | 247 << "of file. This should have been caught when loading. Was the " |
209 << "file modified?"; | 248 << "file modified?"; |
210 return false; | 249 return false; |
211 } | 250 } |
212 | 251 |
213 size_t length = next_entry->file_offset - target->file_offset; | 252 size_t length = next_entry->file_offset - target->file_offset; |
214 data->set(reinterpret_cast<const char*>(mmap_->data() + target->file_offset), | 253 data->set( |
215 length); | 254 reinterpret_cast<const char*>(data_source_->data() + target->file_offset), |
255 length); | |
216 return true; | 256 return true; |
217 } | 257 } |
218 | 258 |
219 base::RefCountedStaticMemory* DataPack::GetStaticMemory( | 259 base::RefCountedStaticMemory* DataPack::GetStaticMemory( |
220 uint16_t resource_id) const { | 260 uint16_t resource_id) const { |
221 base::StringPiece piece; | 261 base::StringPiece piece; |
222 if (!GetStringPiece(resource_id, &piece)) | 262 if (!GetStringPiece(resource_id, &piece)) |
223 return NULL; | 263 return NULL; |
224 | 264 |
225 return new base::RefCountedStaticMemory(piece.data(), piece.length()); | 265 return new base::RefCountedStaticMemory(piece.data(), piece.length()); |
226 } | 266 } |
227 | 267 |
228 ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { | 268 ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { |
229 return text_encoding_type_; | 269 return text_encoding_type_; |
230 } | 270 } |
231 | 271 |
232 ui::ScaleFactor DataPack::GetScaleFactor() const { | 272 ui::ScaleFactor DataPack::GetScaleFactor() const { |
233 return scale_factor_; | 273 return scale_factor_; |
234 } | 274 } |
235 | 275 |
236 bool DataPack::HasOnlyMaterialDesignAssets() const { | 276 bool DataPack::HasOnlyMaterialDesignAssets() const { |
237 return has_only_material_design_assets_; | 277 return has_only_material_design_assets_; |
238 } | 278 } |
239 | 279 |
240 #if DCHECK_IS_ON() | 280 #if DCHECK_IS_ON() |
241 void DataPack::CheckForDuplicateResources( | 281 void DataPack::CheckForDuplicateResources( |
242 const ScopedVector<ResourceHandle>& packs) { | 282 const ScopedVector<ResourceHandle>& packs) { |
243 for (size_t i = 0; i < resource_count_ + 1; ++i) { | 283 for (size_t i = 0; i < resource_count_ + 1; ++i) { |
244 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( | 284 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
245 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); | 285 data_source_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); |
246 const uint16_t resource_id = entry->resource_id; | 286 const uint16_t resource_id = entry->resource_id; |
247 const float resource_scale = GetScaleForScaleFactor(scale_factor_); | 287 const float resource_scale = GetScaleForScaleFactor(scale_factor_); |
248 for (const ResourceHandle* handle : packs) { | 288 for (const ResourceHandle* handle : packs) { |
249 if (HasOnlyMaterialDesignAssets() != | 289 if (HasOnlyMaterialDesignAssets() != |
250 handle->HasOnlyMaterialDesignAssets()) { | 290 handle->HasOnlyMaterialDesignAssets()) { |
251 continue; | 291 continue; |
252 } | 292 } |
253 if (GetScaleForScaleFactor(handle->GetScaleFactor()) != resource_scale) | 293 if (GetScaleForScaleFactor(handle->GetScaleFactor()) != resource_scale) |
254 continue; | 294 continue; |
255 DCHECK(!handle->HasResource(resource_id)) << "Duplicate resource " | 295 DCHECK(!handle->HasResource(resource_id)) << "Duplicate resource " |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 return false; | 385 return false; |
346 } | 386 } |
347 } | 387 } |
348 | 388 |
349 base::CloseFile(file); | 389 base::CloseFile(file); |
350 | 390 |
351 return true; | 391 return true; |
352 } | 392 } |
353 | 393 |
354 } // namespace ui | 394 } // namespace ui |
OLD | NEW |