| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string16.h" | 11 #include "base/string16.h" |
| 12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/data_pack.h" | 15 #include "ui/base/resource/data_pack.h" |
| 16 #include "ui/base/ui_base_switches.h" | 16 #include "ui/base/ui_base_switches.h" |
| 17 #include "ui/gfx/font.h" | 17 #include "ui/gfx/font.h" |
| 18 | 18 |
| 19 namespace ui { | 19 namespace ui { |
| 20 | 20 |
| 21 namespace { | |
| 22 | |
| 23 DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { | |
| 24 DataPack* resources_pak = new DataPack; | |
| 25 bool success = resources_pak->Load(resources_pak_path); | |
| 26 if (!success) { | |
| 27 delete resources_pak; | |
| 28 resources_pak = NULL; | |
| 29 } | |
| 30 return resources_pak; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 ResourceBundle::~ResourceBundle() { | 21 ResourceBundle::~ResourceBundle() { |
| 36 FreeImages(); | 22 FreeImages(); |
| 37 UnloadLocaleResources(); | 23 UnloadLocaleResources(); |
| 38 STLDeleteContainerPointers(data_packs_.begin(), | 24 STLDeleteContainerPointers(data_packs_.begin(), |
| 39 data_packs_.end()); | 25 data_packs_.end()); |
| 40 delete resources_data_; | 26 delete resources_data_; |
| 41 resources_data_ = NULL; | 27 resources_data_ = NULL; |
| 42 } | 28 } |
| 43 | 29 |
| 44 void ResourceBundle::UnloadLocaleResources() { | |
| 45 delete locale_resources_data_; | |
| 46 locale_resources_data_ = NULL; | |
| 47 } | |
| 48 | |
| 49 // static | 30 // static |
| 50 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( | 31 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
| 51 DataHandle module, int resource_id) { | 32 DataHandle module, int resource_id) { |
| 52 DCHECK(module); | 33 DCHECK(module); |
| 53 return module->GetStaticMemory(resource_id); | 34 return module->GetStaticMemory(resource_id); |
| 54 } | 35 } |
| 55 | 36 |
| 56 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 37 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
| 57 DCHECK(resources_data_); | 38 DCHECK(resources_data_); |
| 58 base::StringPiece data; | 39 base::StringPiece data; |
| 59 if (!resources_data_->GetStringPiece(resource_id, &data)) { | 40 if (!resources_data_->GetStringPiece(resource_id, &data)) { |
| 60 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { | 41 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { |
| 61 for (size_t i = 0; i < data_packs_.size(); ++i) { | 42 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 62 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 43 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 63 return data; | 44 return data; |
| 64 } | 45 } |
| 65 | 46 |
| 66 return base::StringPiece(); | 47 return base::StringPiece(); |
| 67 } | 48 } |
| 68 } | 49 } |
| 69 return data; | 50 return data; |
| 70 } | 51 } |
| 71 | 52 |
| 72 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
| 73 // If for some reason we were unable to load a resource pak, return an empty | |
| 74 // string (better than crashing). | |
| 75 if (!locale_resources_data_) { | |
| 76 LOG(WARNING) << "locale resources are not loaded"; | |
| 77 return string16(); | |
| 78 } | |
| 79 | |
| 80 base::StringPiece data; | |
| 81 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
| 82 // Fall back on the main data pack (shouldn't be any strings here except in | |
| 83 // unittests). | |
| 84 data = GetRawDataResource(message_id); | |
| 85 if (data.empty()) { | |
| 86 NOTREACHED() << "unable to find resource: " << message_id; | |
| 87 return string16(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // Data pack encodes strings as UTF16. | |
| 92 DCHECK_EQ(data.length() % 2, 0U); | |
| 93 string16 msg(reinterpret_cast<const char16*>(data.data()), | |
| 94 data.length() / 2); | |
| 95 return msg; | |
| 96 } | |
| 97 | |
| 98 void ResourceBundle::LoadCommonResources() { | 53 void ResourceBundle::LoadCommonResources() { |
| 99 DCHECK(!resources_data_) << "chrome.pak already loaded"; | 54 DCHECK(!resources_data_) << "chrome.pak already loaded"; |
| 100 FilePath resources_file_path = GetResourcesFilePath(); | 55 FilePath resources_file_path = GetResourcesFilePath(); |
| 101 CHECK(!resources_file_path.empty()) << "chrome.pak not found"; | 56 CHECK(!resources_file_path.empty()) << "chrome.pak not found"; |
| 102 resources_data_ = LoadResourcesDataPak(resources_file_path); | 57 resources_data_ = LoadResourcesDataPak(resources_file_path); |
| 103 CHECK(resources_data_) << "failed to load chrome.pak"; | 58 CHECK(resources_data_) << "failed to load chrome.pak"; |
| 104 | 59 |
| 105 FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath(); | 60 FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath(); |
| 106 if (!large_icon_resources_file_path.empty()) { | 61 if (!large_icon_resources_file_path.empty()) { |
| 107 large_icon_resources_data_ = | 62 large_icon_resources_data_ = |
| 108 LoadResourcesDataPak(large_icon_resources_file_path); | 63 LoadResourcesDataPak(large_icon_resources_file_path); |
| 109 CHECK(large_icon_resources_data_) << | 64 CHECK(large_icon_resources_data_) << |
| 110 "failed to load theme_resources_large.pak"; | 65 "failed to load theme_resources_large.pak"; |
| 111 } | 66 } |
| 112 } | 67 } |
| 113 | 68 |
| 114 std::string ResourceBundle::LoadLocaleResources( | |
| 115 const std::string& pref_locale) { | |
| 116 DCHECK(!locale_resources_data_) << "locale.pak already loaded"; | |
| 117 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | |
| 118 FilePath locale_file_path; | |
| 119 CommandLine *command_line = CommandLine::ForCurrentProcess(); | |
| 120 if (command_line->HasSwitch(switches::kLocalePak)) { | |
| 121 locale_file_path = | |
| 122 command_line->GetSwitchValuePath(switches::kLocalePak); | |
| 123 } else { | |
| 124 locale_file_path = GetLocaleFilePath(app_locale); | |
| 125 } | |
| 126 if (locale_file_path.empty()) { | |
| 127 // It's possible that there is no locale.pak. | |
| 128 NOTREACHED(); | |
| 129 return std::string(); | |
| 130 } | |
| 131 locale_resources_data_ = LoadResourcesDataPak(locale_file_path); | |
| 132 CHECK(locale_resources_data_) << "failed to load locale.pak"; | |
| 133 return app_locale; | |
| 134 } | |
| 135 | |
| 136 void ResourceBundle::LoadTestResources(const FilePath& path) { | 69 void ResourceBundle::LoadTestResources(const FilePath& path) { |
| 137 DCHECK(!resources_data_) << "resource already loaded"; | 70 DCHECK(!resources_data_) << "resource already loaded"; |
| 138 | 71 |
| 139 // Use the given resource pak for both common and localized resources. | 72 // Use the given resource pak for both common and localized resources. |
| 140 resources_data_ = LoadResourcesDataPak(path); | 73 resources_data_ = LoadResourcesDataPak(path); |
| 141 locale_resources_data_ = LoadResourcesDataPak(path); | 74 locale_resources_data_.reset(LoadResourcesDataPak(path)); |
| 142 } | 75 } |
| 143 | 76 |
| 144 } // namespace ui | 77 } // namespace ui |
| OLD | NEW |