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 |
21 ResourceBundle::~ResourceBundle() { | 35 ResourceBundle::~ResourceBundle() { |
22 FreeImages(); | 36 FreeImages(); |
23 UnloadLocaleResources(); | 37 UnloadLocaleResources(); |
24 STLDeleteContainerPointers(data_packs_.begin(), | 38 STLDeleteContainerPointers(data_packs_.begin(), |
25 data_packs_.end()); | 39 data_packs_.end()); |
26 delete resources_data_; | 40 delete resources_data_; |
27 resources_data_ = NULL; | 41 resources_data_ = NULL; |
28 } | 42 } |
29 | 43 |
| 44 void ResourceBundle::UnloadLocaleResources() { |
| 45 delete locale_resources_data_; |
| 46 locale_resources_data_ = NULL; |
| 47 } |
| 48 |
30 // static | 49 // static |
31 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( | 50 RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( |
32 DataHandle module, int resource_id) { | 51 DataHandle module, int resource_id) { |
33 DCHECK(module); | 52 DCHECK(module); |
34 return module->GetStaticMemory(resource_id); | 53 return module->GetStaticMemory(resource_id); |
35 } | 54 } |
36 | 55 |
37 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 56 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
38 DCHECK(resources_data_); | 57 DCHECK(resources_data_); |
39 base::StringPiece data; | 58 base::StringPiece data; |
40 if (!resources_data_->GetStringPiece(resource_id, &data)) { | 59 if (!resources_data_->GetStringPiece(resource_id, &data)) { |
41 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { | 60 if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { |
42 for (size_t i = 0; i < data_packs_.size(); ++i) { | 61 for (size_t i = 0; i < data_packs_.size(); ++i) { |
43 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 62 if (data_packs_[i]->GetStringPiece(resource_id, &data)) |
44 return data; | 63 return data; |
45 } | 64 } |
46 | 65 |
47 return base::StringPiece(); | 66 return base::StringPiece(); |
48 } | 67 } |
49 } | 68 } |
50 return data; | 69 return data; |
51 } | 70 } |
52 | 71 |
| 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 |
53 void ResourceBundle::LoadCommonResources() { | 98 void ResourceBundle::LoadCommonResources() { |
54 DCHECK(!resources_data_) << "chrome.pak already loaded"; | 99 DCHECK(!resources_data_) << "chrome.pak already loaded"; |
55 FilePath resources_file_path = GetResourcesFilePath(); | 100 FilePath resources_file_path = GetResourcesFilePath(); |
56 CHECK(!resources_file_path.empty()) << "chrome.pak not found"; | 101 CHECK(!resources_file_path.empty()) << "chrome.pak not found"; |
57 resources_data_ = LoadResourcesDataPak(resources_file_path); | 102 resources_data_ = LoadResourcesDataPak(resources_file_path); |
58 CHECK(resources_data_) << "failed to load chrome.pak"; | 103 CHECK(resources_data_) << "failed to load chrome.pak"; |
59 | 104 |
60 FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath(); | 105 FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath(); |
61 if (!large_icon_resources_file_path.empty()) { | 106 if (!large_icon_resources_file_path.empty()) { |
62 large_icon_resources_data_ = | 107 large_icon_resources_data_ = |
63 LoadResourcesDataPak(large_icon_resources_file_path); | 108 LoadResourcesDataPak(large_icon_resources_file_path); |
64 CHECK(large_icon_resources_data_) << | 109 CHECK(large_icon_resources_data_) << |
65 "failed to load theme_resources_large.pak"; | 110 "failed to load theme_resources_large.pak"; |
66 } | 111 } |
67 } | 112 } |
68 | 113 |
| 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 |
69 void ResourceBundle::LoadTestResources(const FilePath& path) { | 136 void ResourceBundle::LoadTestResources(const FilePath& path) { |
70 DCHECK(!resources_data_) << "resource already loaded"; | 137 DCHECK(!resources_data_) << "resource already loaded"; |
71 | 138 |
72 // Use the given resource pak for both common and localized resources. | 139 // Use the given resource pak for both common and localized resources. |
73 resources_data_ = LoadResourcesDataPak(path); | 140 resources_data_ = LoadResourcesDataPak(path); |
74 locale_resources_data_.reset(LoadResourcesDataPak(path)); | 141 locale_resources_data_ = LoadResourcesDataPak(path); |
75 } | 142 } |
76 | 143 |
77 } // namespace ui | 144 } // namespace ui |
OLD | NEW |