| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 | 73 |
| 74 DataPack::~DataPack() { | 74 DataPack::~DataPack() { |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool DataPack::LoadFromPath(const base::FilePath& path) { | 77 bool DataPack::LoadFromPath(const base::FilePath& path) { |
| 78 mmap_.reset(new base::MemoryMappedFile); | 78 mmap_.reset(new base::MemoryMappedFile); |
| 79 if (!mmap_->Initialize(path)) { | 79 if (!mmap_->Initialize(path)) { |
| 80 DLOG(ERROR) << "Failed to mmap datapack"; | 80 DLOG(ERROR) << "Failed to mmap datapack"; |
| 81 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, | 81 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED, |
| 82 LOAD_ERRORS_COUNT); | 82 LOAD_ERRORS_COUNT); |
| 83 mmap_.reset(); | 83 Clear(); |
| 84 return false; | 84 return false; |
| 85 } | 85 } |
| 86 length_ = mmap_->length(); |
| 87 data_ = mmap_->data(); |
| 86 return LoadImpl(); | 88 return LoadImpl(); |
| 87 } | 89 } |
| 88 | 90 |
| 89 bool DataPack::LoadFromFile(base::File file) { | 91 bool DataPack::LoadFromFile(base::File file) { |
| 90 return LoadFromFileRegion(std::move(file), | 92 return LoadFromFileRegion(std::move(file), |
| 91 base::MemoryMappedFile::Region::kWholeFile); | 93 base::MemoryMappedFile::Region::kWholeFile); |
| 92 } | 94 } |
| 93 | 95 |
| 94 bool DataPack::LoadFromFileRegion( | 96 bool DataPack::LoadFromFileRegion( |
| 95 base::File file, | 97 base::File file, |
| 96 const base::MemoryMappedFile::Region& region) { | 98 const base::MemoryMappedFile::Region& region) { |
| 97 mmap_.reset(new base::MemoryMappedFile); | 99 mmap_.reset(new base::MemoryMappedFile); |
| 98 if (!mmap_->Initialize(std::move(file), region)) { | 100 if (!mmap_->Initialize(std::move(file), region)) { |
| 99 DLOG(ERROR) << "Failed to mmap datapack"; | 101 DLOG(ERROR) << "Failed to mmap datapack"; |
| 100 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED_FROM_FILE, | 102 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INIT_FAILED_FROM_FILE, |
| 101 LOAD_ERRORS_COUNT); | 103 LOAD_ERRORS_COUNT); |
| 102 mmap_.reset(); | 104 Clear(); |
| 103 return false; | 105 return false; |
| 104 } | 106 } |
| 107 length_ = mmap_->length(); |
| 108 data_ = mmap_->data(); |
| 109 return LoadImpl(); |
| 110 } |
| 111 |
| 112 bool DataPack::LoadFromBuffer(base::StringPiece buffer) { |
| 113 length_ = buffer.length(); |
| 114 data_ = reinterpret_cast<const uint8_t*>(buffer.data()); |
| 105 return LoadImpl(); | 115 return LoadImpl(); |
| 106 } | 116 } |
| 107 | 117 |
| 108 bool DataPack::LoadImpl() { | 118 bool DataPack::LoadImpl() { |
| 109 // Sanity check the header of the file. | 119 // Sanity check the header of the file. |
| 110 if (kHeaderLength > mmap_->length()) { | 120 if (kHeaderLength > length_) { |
| 111 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; | 121 DLOG(ERROR) << "Data pack file corruption: incomplete file header."; |
| 112 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", HEADER_TRUNCATED, | 122 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", HEADER_TRUNCATED, |
| 113 LOAD_ERRORS_COUNT); | 123 LOAD_ERRORS_COUNT); |
| 114 mmap_.reset(); | 124 // Clear memory mapped file even when we're loading data from a buffer, |
| 125 // in order to simplify code. |
| 126 Clear(); |
| 115 return false; | 127 return false; |
| 116 } | 128 } |
| 117 | 129 |
| 118 // Parse the header of the file. | 130 // Parse the header of the file. |
| 119 // First uint32_t: version; second: resource count; | 131 // First uint32_t: version; second: resource count; |
| 120 const uint32_t* ptr = reinterpret_cast<const uint32_t*>(mmap_->data()); | 132 const uint32_t* ptr = reinterpret_cast<const uint32_t*>(data_); |
| 121 uint32_t version = ptr[0]; | 133 uint32_t version = ptr[0]; |
| 122 if (version != kFileFormatVersion) { | 134 if (version != kFileFormatVersion) { |
| 123 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " | 135 LOG(ERROR) << "Bad data pack version: got " << version << ", expected " |
| 124 << kFileFormatVersion; | 136 << kFileFormatVersion; |
| 125 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, | 137 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", BAD_VERSION, |
| 126 LOAD_ERRORS_COUNT); | 138 LOAD_ERRORS_COUNT); |
| 127 mmap_.reset(); | 139 Clear(); |
| 128 return false; | 140 return false; |
| 129 } | 141 } |
| 130 resource_count_ = ptr[1]; | 142 resource_count_ = ptr[1]; |
| 131 | 143 |
| 132 // third: text encoding. | 144 // third: text encoding. |
| 133 const uint8_t* ptr_encoding = reinterpret_cast<const uint8_t*>(ptr + 2); | 145 const uint8_t* ptr_encoding = reinterpret_cast<const uint8_t*>(ptr + 2); |
| 134 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding); | 146 text_encoding_type_ = static_cast<TextEncodingType>(*ptr_encoding); |
| 135 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && | 147 if (text_encoding_type_ != UTF8 && text_encoding_type_ != UTF16 && |
| 136 text_encoding_type_ != BINARY) { | 148 text_encoding_type_ != BINARY) { |
| 137 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ | 149 LOG(ERROR) << "Bad data pack text encoding: got " << text_encoding_type_ |
| 138 << ", expected between " << BINARY << " and " << UTF16; | 150 << ", expected between " << BINARY << " and " << UTF16; |
| 139 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", WRONG_ENCODING, | 151 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", WRONG_ENCODING, |
| 140 LOAD_ERRORS_COUNT); | 152 LOAD_ERRORS_COUNT); |
| 141 mmap_.reset(); | 153 Clear(); |
| 142 return false; | 154 return false; |
| 143 } | 155 } |
| 144 | 156 |
| 145 // Sanity check the file. | 157 // Sanity check the file. |
| 146 // 1) Check we have enough entries. There's an extra entry after the last item | 158 // 1) Check we have enough entries. There's an extra entry after the last item |
| 147 // which gives the length of the last item. | 159 // which gives the length of the last item. |
| 148 if (kHeaderLength + (resource_count_ + 1) * sizeof(DataPackEntry) > | 160 if (kHeaderLength + (resource_count_ + 1) * sizeof(DataPackEntry) > |
| 149 mmap_->length()) { | 161 length_) { |
| 150 LOG(ERROR) << "Data pack file corruption: too short for number of " | 162 LOG(ERROR) << "Data pack file corruption: too short for number of " |
| 151 "entries specified."; | 163 "entries specified."; |
| 152 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, | 164 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", INDEX_TRUNCATED, |
| 153 LOAD_ERRORS_COUNT); | 165 LOAD_ERRORS_COUNT); |
| 154 mmap_.reset(); | 166 Clear(); |
| 155 return false; | 167 return false; |
| 156 } | 168 } |
| 157 // 2) Verify the entries are within the appropriate bounds. There's an extra | 169 // 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. | 170 // 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) { | 171 for (size_t i = 0; i < resource_count_ + 1; ++i) { |
| 160 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( | 172 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
| 161 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); | 173 data_ + kHeaderLength + (i * sizeof(DataPackEntry))); |
| 162 if (entry->file_offset > mmap_->length()) { | 174 if (entry->file_offset > length_) { |
| 163 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " | 175 LOG(ERROR) << "Entry #" << i << " in data pack points off end of file. " |
| 164 << "Was the file corrupted?"; | 176 << "Was the file corrupted?"; |
| 165 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, | 177 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", ENTRY_NOT_FOUND, |
| 166 LOAD_ERRORS_COUNT); | 178 LOAD_ERRORS_COUNT); |
| 167 mmap_.reset(); | 179 Clear(); |
| 168 return false; | 180 return false; |
| 169 } | 181 } |
| 170 } | 182 } |
| 171 | 183 |
| 172 return true; | 184 return true; |
| 173 } | 185 } |
| 174 | 186 |
| 187 void DataPack::Clear() { |
| 188 mmap_.reset(); |
| 189 length_ = 0; |
| 190 data_ = nullptr; |
| 191 } |
| 192 |
| 175 bool DataPack::HasResource(uint16_t resource_id) const { | 193 bool DataPack::HasResource(uint16_t resource_id) const { |
| 176 return !!bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 194 return !!bsearch(&resource_id, data_ + kHeaderLength, resource_count_, |
| 177 sizeof(DataPackEntry), DataPackEntry::CompareById); | 195 sizeof(DataPackEntry), DataPackEntry::CompareById); |
| 178 } | 196 } |
| 179 | 197 |
| 180 bool DataPack::GetStringPiece(uint16_t resource_id, | 198 bool DataPack::GetStringPiece(uint16_t resource_id, |
| 181 base::StringPiece* data) const { | 199 base::StringPiece* data) const { |
| 182 // It won't be hard to make this endian-agnostic, but it's not worth | 200 // It won't be hard to make this endian-agnostic, but it's not worth |
| 183 // bothering to do right now. | 201 // bothering to do right now. |
| 184 #if defined(__BYTE_ORDER) | 202 #if defined(__BYTE_ORDER) |
| 185 // Linux check | 203 // Linux check |
| 186 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, | 204 static_assert(__BYTE_ORDER == __LITTLE_ENDIAN, |
| 187 "datapack assumes little endian"); | 205 "datapack assumes little endian"); |
| 188 #elif defined(__BIG_ENDIAN__) | 206 #elif defined(__BIG_ENDIAN__) |
| 189 // Mac check | 207 // Mac check |
| 190 #error DataPack assumes little endian | 208 #error DataPack assumes little endian |
| 191 #endif | 209 #endif |
| 192 | 210 |
| 193 const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>( | 211 const DataPackEntry* target = reinterpret_cast<const DataPackEntry*>( |
| 194 bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, | 212 bsearch(&resource_id, data_ + kHeaderLength, resource_count_, |
| 195 sizeof(DataPackEntry), DataPackEntry::CompareById)); | 213 sizeof(DataPackEntry), DataPackEntry::CompareById)); |
| 196 if (!target) { | 214 if (!target) { |
| 197 return false; | 215 return false; |
| 198 } | 216 } |
| 199 | 217 |
| 200 const DataPackEntry* next_entry = target + 1; | 218 const DataPackEntry* next_entry = target + 1; |
| 201 // If the next entry points beyond the end of the file this data pack's entry | 219 // 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 | 220 // table is corrupt. Log an error and return false. See |
| 203 // http://crbug.com/371301. | 221 // http://crbug.com/371301. |
| 204 if (next_entry->file_offset > mmap_->length()) { | 222 if (next_entry->file_offset > length_) { |
| 205 size_t entry_index = target - | 223 size_t entry_index = target - |
| 206 reinterpret_cast<const DataPackEntry*>(mmap_->data() + kHeaderLength); | 224 reinterpret_cast<const DataPackEntry*>(data_ + kHeaderLength); |
| 207 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " | 225 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " |
| 208 << "of file. This should have been caught when loading. Was the " | 226 << "of file. This should have been caught when loading. Was the " |
| 209 << "file modified?"; | 227 << "file modified?"; |
| 210 return false; | 228 return false; |
| 211 } | 229 } |
| 212 | 230 |
| 213 size_t length = next_entry->file_offset - target->file_offset; | 231 size_t length = next_entry->file_offset - target->file_offset; |
| 214 data->set(reinterpret_cast<const char*>(mmap_->data() + target->file_offset), | 232 data->set(reinterpret_cast<const char*>(data_ + target->file_offset), |
| 215 length); | 233 length); |
| 216 return true; | 234 return true; |
| 217 } | 235 } |
| 218 | 236 |
| 219 base::RefCountedStaticMemory* DataPack::GetStaticMemory( | 237 base::RefCountedStaticMemory* DataPack::GetStaticMemory( |
| 220 uint16_t resource_id) const { | 238 uint16_t resource_id) const { |
| 221 base::StringPiece piece; | 239 base::StringPiece piece; |
| 222 if (!GetStringPiece(resource_id, &piece)) | 240 if (!GetStringPiece(resource_id, &piece)) |
| 223 return NULL; | 241 return NULL; |
| 224 | 242 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 235 | 253 |
| 236 bool DataPack::HasOnlyMaterialDesignAssets() const { | 254 bool DataPack::HasOnlyMaterialDesignAssets() const { |
| 237 return has_only_material_design_assets_; | 255 return has_only_material_design_assets_; |
| 238 } | 256 } |
| 239 | 257 |
| 240 #if DCHECK_IS_ON() | 258 #if DCHECK_IS_ON() |
| 241 void DataPack::CheckForDuplicateResources( | 259 void DataPack::CheckForDuplicateResources( |
| 242 const ScopedVector<ResourceHandle>& packs) { | 260 const ScopedVector<ResourceHandle>& packs) { |
| 243 for (size_t i = 0; i < resource_count_ + 1; ++i) { | 261 for (size_t i = 0; i < resource_count_ + 1; ++i) { |
| 244 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( | 262 const DataPackEntry* entry = reinterpret_cast<const DataPackEntry*>( |
| 245 mmap_->data() + kHeaderLength + (i * sizeof(DataPackEntry))); | 263 data_ + kHeaderLength + (i * sizeof(DataPackEntry))); |
| 246 const uint16_t resource_id = entry->resource_id; | 264 const uint16_t resource_id = entry->resource_id; |
| 247 const float resource_scale = GetScaleForScaleFactor(scale_factor_); | 265 const float resource_scale = GetScaleForScaleFactor(scale_factor_); |
| 248 for (const ResourceHandle* handle : packs) { | 266 for (const ResourceHandle* handle : packs) { |
| 249 if (HasOnlyMaterialDesignAssets() != | 267 if (HasOnlyMaterialDesignAssets() != |
| 250 handle->HasOnlyMaterialDesignAssets()) { | 268 handle->HasOnlyMaterialDesignAssets()) { |
| 251 continue; | 269 continue; |
| 252 } | 270 } |
| 253 if (GetScaleForScaleFactor(handle->GetScaleFactor()) != resource_scale) | 271 if (GetScaleForScaleFactor(handle->GetScaleFactor()) != resource_scale) |
| 254 continue; | 272 continue; |
| 255 DCHECK(!handle->HasResource(resource_id)) << "Duplicate resource " | 273 DCHECK(!handle->HasResource(resource_id)) << "Duplicate resource " |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 return false; | 363 return false; |
| 346 } | 364 } |
| 347 } | 365 } |
| 348 | 366 |
| 349 base::CloseFile(file); | 367 base::CloseFile(file); |
| 350 | 368 |
| 351 return true; | 369 return true; |
| 352 } | 370 } |
| 353 | 371 |
| 354 } // namespace ui | 372 } // namespace ui |
| OLD | NEW |