Chromium Code Reviews| 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 <set> | |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 11 #include "base/command_line.h" | |
| 10 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 11 #include "base/files/memory_mapped_file.h" | 13 #include "base/files/memory_mapped_file.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/stl_util.h" | |
| 16 #include "base/strings/string_piece.h" | 19 #include "base/strings/string_piece.h" |
| 17 | 20 |
| 18 // For details of the file layout, see | 21 // For details of the file layout, see |
| 19 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings | 22 // http://dev.chromium.org/developers/design-documents/linuxresourcesandlocalize dstrings |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 | 25 |
| 23 static const uint32_t kFileFormatVersion = 4; | 26 static const uint32_t kFileFormatVersion = 4; |
| 24 // Length of file header: version, entry count and text encoding type. | 27 // Length of file header: version, entry count and text encoding type. |
| 25 static const size_t kHeaderLength = 2 * sizeof(uint32_t) + sizeof(uint8_t); | 28 static const size_t kHeaderLength = 2 * sizeof(uint32_t) + sizeof(uint8_t); |
| 26 | 29 |
| 27 #pragma pack(push,2) | 30 #pragma pack(push, 2) |
| 28 struct DataPackEntry { | 31 struct DataPackEntry { |
| 29 uint16_t resource_id; | 32 uint16_t resource_id; |
| 30 uint32_t file_offset; | 33 uint32_t file_offset; |
| 31 | 34 |
| 32 static int CompareById(const void* void_key, const void* void_entry) { | 35 static int CompareById(const void* void_key, const void* void_entry) { |
| 33 uint16_t key = *reinterpret_cast<const uint16_t*>(void_key); | 36 uint16_t key = *reinterpret_cast<const uint16_t*>(void_key); |
| 34 const DataPackEntry* entry = | 37 const DataPackEntry* entry = |
| 35 reinterpret_cast<const DataPackEntry*>(void_entry); | 38 reinterpret_cast<const DataPackEntry*>(void_entry); |
| 36 if (key < entry->resource_id) { | 39 if (key < entry->resource_id) { |
| 37 return -1; | 40 return -1; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 58 WRONG_ENCODING, | 61 WRONG_ENCODING, |
| 59 INIT_FAILED_FROM_FILE, | 62 INIT_FAILED_FROM_FILE, |
| 60 | 63 |
| 61 LOAD_ERRORS_COUNT, | 64 LOAD_ERRORS_COUNT, |
| 62 }; | 65 }; |
| 63 | 66 |
| 64 void LogDataPackError(LoadErrors error) { | 67 void LogDataPackError(LoadErrors error) { |
| 65 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", error, LOAD_ERRORS_COUNT); | 68 UMA_HISTOGRAM_ENUMERATION("DataPack.Load", error, LOAD_ERRORS_COUNT); |
| 66 } | 69 } |
| 67 | 70 |
| 71 // Prints the given resource id the first time it's loaded if Chrome has been | |
| 72 // started with --print-resource-ids. This output is then used to generate a | |
| 73 // more optimal resource renumbering to improve startup speed. See | |
| 74 // tools/gritsettings/README.md for more info. | |
| 75 void MaybePrintResourceId(uint16_t resource_id) { | |
| 76 // This code is run in other binaries than Chrome which do not initialize the | |
| 77 // CommandLine object. Early return in those cases. | |
| 78 if (!base::CommandLine::InitializedForCurrentProcess()) | |
| 79 return; | |
| 80 | |
| 81 // Note: This switch isn't in ui/base/ui_base_switches.h because ui/base | |
| 82 // depends on ui/base/resource and thus it would cause a circular dependency. | |
| 83 static bool print_resource_ids = | |
| 84 base::CommandLine::ForCurrentProcess()->HasSwitch("print-resource-ids"); | |
| 85 if (!print_resource_ids) | |
| 86 return; | |
| 87 | |
| 88 // Note: This is leaked intentionally. However, it's only allocated if the | |
| 89 // above command line is specified, so it shouldn't affect regular users. | |
| 90 static std::set<uint16_t>* resource_ids_logged = new std::set<uint16_t>(); | |
| 91 if (!base::ContainsKey(*resource_ids_logged, resource_id)) { | |
|
Nico
2017/02/17 21:45:58
is it guaranteed that we only load resources from
Alexei Svitkine (slow)
2017/02/17 22:30:43
Good point. I had originally assumed yes, but look
| |
| 92 printf("Resource=%d\n", resource_id); | |
| 93 resource_ids_logged->insert(resource_id); | |
| 94 } | |
| 95 } | |
| 96 | |
| 68 } // namespace | 97 } // namespace |
| 69 | 98 |
| 70 namespace ui { | 99 namespace ui { |
| 71 | 100 |
| 72 // Abstraction of a data source (memory mapped file or in-memory buffer). | 101 // Abstraction of a data source (memory mapped file or in-memory buffer). |
| 73 class DataPack::DataSource { | 102 class DataPack::DataSource { |
| 74 public: | 103 public: |
| 75 virtual ~DataSource() {} | 104 virtual ~DataSource() {} |
| 76 | 105 |
| 77 virtual size_t GetLength() const = 0; | 106 virtual size_t GetLength() const = 0; |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 // http://crbug.com/371301. | 280 // http://crbug.com/371301. |
| 252 if (next_entry->file_offset > data_source_->GetLength()) { | 281 if (next_entry->file_offset > data_source_->GetLength()) { |
| 253 size_t entry_index = target - reinterpret_cast<const DataPackEntry*>( | 282 size_t entry_index = target - reinterpret_cast<const DataPackEntry*>( |
| 254 data_source_->GetData() + kHeaderLength); | 283 data_source_->GetData() + kHeaderLength); |
| 255 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " | 284 LOG(ERROR) << "Entry #" << entry_index << " in data pack points off end " |
| 256 << "of file. This should have been caught when loading. Was the " | 285 << "of file. This should have been caught when loading. Was the " |
| 257 << "file modified?"; | 286 << "file modified?"; |
| 258 return false; | 287 return false; |
| 259 } | 288 } |
| 260 | 289 |
| 290 MaybePrintResourceId(resource_id); | |
| 261 size_t length = next_entry->file_offset - target->file_offset; | 291 size_t length = next_entry->file_offset - target->file_offset; |
| 262 data->set(reinterpret_cast<const char*>(data_source_->GetData() + | 292 data->set(reinterpret_cast<const char*>(data_source_->GetData() + |
| 263 target->file_offset), | 293 target->file_offset), |
| 264 length); | 294 length); |
| 265 return true; | 295 return true; |
| 266 } | 296 } |
| 267 | 297 |
| 268 base::RefCountedStaticMemory* DataPack::GetStaticMemory( | 298 base::RefCountedStaticMemory* DataPack::GetStaticMemory( |
| 269 uint16_t resource_id) const { | 299 uint16_t resource_id) const { |
| 270 base::StringPiece piece; | 300 base::StringPiece piece; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 386 return false; | 416 return false; |
| 387 } | 417 } |
| 388 } | 418 } |
| 389 | 419 |
| 390 base::CloseFile(file); | 420 base::CloseFile(file); |
| 391 | 421 |
| 392 return true; | 422 return true; |
| 393 } | 423 } |
| 394 | 424 |
| 395 } // namespace ui | 425 } // namespace ui |
| OLD | NEW |