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