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