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" | |
8 #include "base/file_util.h" | |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "base/path_service.h" | |
11 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
12 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
13 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
14 #include "build/build_config.h" | 11 #include "build/build_config.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "ui/base/l10n/l10n_util.h" | |
17 #include "ui/base/resource/data_pack.h" | 13 #include "ui/base/resource/data_pack.h" |
18 #include "ui/base/ui_base_paths.h" | |
19 #include "ui/base/ui_base_switches.h" | |
20 #include "ui/gfx/codec/png_codec.h" | 14 #include "ui/gfx/codec/png_codec.h" |
21 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
22 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
23 | 17 |
24 namespace ui { | 18 namespace ui { |
25 | 19 |
26 namespace { | 20 namespace { |
27 | 21 |
28 // Font sizes relative to base font. | 22 // Font sizes relative to base font. |
29 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) | 23 #if defined(OS_CHROMEOS) && defined(CROS_FONTS_USING_BCI) |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 66 |
73 /* static */ | 67 /* static */ |
74 void ResourceBundle::InitSharedInstanceForTest(const FilePath& path) { | 68 void ResourceBundle::InitSharedInstanceForTest(const FilePath& path) { |
75 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 69 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
76 g_shared_instance_ = new ResourceBundle(); | 70 g_shared_instance_ = new ResourceBundle(); |
77 | 71 |
78 g_shared_instance_->LoadTestResources(path); | 72 g_shared_instance_->LoadTestResources(path); |
79 } | 73 } |
80 | 74 |
81 /* static */ | 75 /* static */ |
82 DataPack* ResourceBundle::LoadResourcesDataPak(const FilePath& path) { | |
83 DataPack* datapack = new DataPack; | |
84 bool success = datapack->Load(path); | |
85 if (!success) { | |
86 delete datapack; | |
87 datapack = NULL; | |
88 } | |
89 return datapack; | |
90 } | |
91 | |
92 /* static */ | |
93 std::string ResourceBundle::ReloadSharedInstance( | 76 std::string ResourceBundle::ReloadSharedInstance( |
94 const std::string& pref_locale) { | 77 const std::string& pref_locale) { |
95 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; | 78 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; |
96 | 79 |
97 g_shared_instance_->UnloadLocaleResources(); | 80 g_shared_instance_->UnloadLocaleResources(); |
98 return g_shared_instance_->LoadLocaleResources(pref_locale); | 81 return g_shared_instance_->LoadLocaleResources(pref_locale); |
99 } | 82 } |
100 | 83 |
101 /* static */ | 84 /* static */ |
102 void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) { | 85 void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) { |
103 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; | 86 DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; |
104 g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path)); | 87 g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path)); |
105 } | 88 } |
106 | 89 |
107 /* static */ | 90 /* static */ |
108 void ResourceBundle::CleanupSharedInstance() { | 91 void ResourceBundle::CleanupSharedInstance() { |
109 if (g_shared_instance_) { | 92 if (g_shared_instance_) { |
110 delete g_shared_instance_; | 93 delete g_shared_instance_; |
111 g_shared_instance_ = NULL; | 94 g_shared_instance_ = NULL; |
112 } | 95 } |
113 } | 96 } |
114 | 97 |
115 /* static */ | 98 /* static */ |
116 ResourceBundle& ResourceBundle::GetSharedInstance() { | 99 ResourceBundle& ResourceBundle::GetSharedInstance() { |
117 // Must call InitSharedInstance before this function. | 100 // Must call InitSharedInstance before this function. |
118 CHECK(g_shared_instance_ != NULL); | 101 CHECK(g_shared_instance_ != NULL); |
119 return *g_shared_instance_; | 102 return *g_shared_instance_; |
120 } | 103 } |
121 | 104 |
122 #if !defined(OS_MACOSX) | |
123 /* static */ | |
124 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | |
125 FilePath locale_file_path; | |
126 PathService::Get(ui::DIR_LOCALES, &locale_file_path); | |
127 if (locale_file_path.empty()) | |
128 return locale_file_path; | |
129 if (app_locale.empty()) | |
130 return FilePath(); | |
131 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | |
132 if (!file_util::PathExists(locale_file_path)) | |
133 return FilePath(); | |
134 return locale_file_path; | |
135 } | |
136 #endif | |
137 | |
138 std::string ResourceBundle::LoadLocaleResources( | |
139 const std::string& pref_locale) { | |
140 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; | |
141 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | |
142 FilePath locale_file_path; | |
143 CommandLine *command_line = CommandLine::ForCurrentProcess(); | |
144 if (command_line->HasSwitch(switches::kLocalePak)) { | |
145 locale_file_path = | |
146 command_line->GetSwitchValuePath(switches::kLocalePak); | |
147 } else { | |
148 locale_file_path = GetLocaleFilePath(app_locale); | |
149 } | |
150 if (locale_file_path.empty()) { | |
151 // It's possible that there is no locale.pak. | |
152 NOTREACHED(); | |
153 return std::string(); | |
154 } | |
155 locale_resources_data_.reset(LoadResourcesDataPak(locale_file_path)); | |
156 CHECK(locale_resources_data_.get()) << "failed to load locale.pak"; | |
157 return app_locale; | |
158 } | |
159 | |
160 void ResourceBundle::UnloadLocaleResources() { | |
161 locale_resources_data_.reset(); | |
162 } | |
163 | |
164 string16 ResourceBundle::GetLocalizedString(int message_id) { | |
165 // If for some reason we were unable to load a resource pak, return an empty | |
166 // string (better than crashing). | |
167 if (!locale_resources_data_.get()) { | |
168 LOG(WARNING) << "locale resources are not loaded"; | |
169 return string16(); | |
170 } | |
171 | |
172 base::StringPiece data; | |
173 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | |
174 // Fall back on the main data pack (shouldn't be any strings here except in | |
175 // unittests). | |
176 data = GetRawDataResource(message_id); | |
177 if (data.empty()) { | |
178 NOTREACHED() << "unable to find resource: " << message_id; | |
179 return string16(); | |
180 } | |
181 } | |
182 | |
183 // Data pack encodes strings as UTF16. | |
184 DCHECK_EQ(data.length() % 2, 0U); | |
185 string16 msg(reinterpret_cast<const char16*>(data.data()), | |
186 data.length() / 2); | |
187 return msg; | |
188 } | |
189 | |
190 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { | 105 SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { |
191 const SkBitmap* bitmap = | 106 const SkBitmap* bitmap = |
192 static_cast<const SkBitmap*>(GetImageNamed(resource_id)); | 107 static_cast<const SkBitmap*>(GetImageNamed(resource_id)); |
193 return const_cast<SkBitmap*>(bitmap); | 108 return const_cast<SkBitmap*>(bitmap); |
194 } | 109 } |
195 | 110 |
196 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { | 111 gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { |
197 // Check to see if the image is already in the cache. | 112 // Check to see if the image is already in the cache. |
198 { | 113 { |
199 base::AutoLock lock_scope(*lock_); | 114 base::AutoLock lock_scope(*lock_); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 | 187 |
273 void ResourceBundle::ReloadFonts() { | 188 void ResourceBundle::ReloadFonts() { |
274 base::AutoLock lock_scope(*lock_); | 189 base::AutoLock lock_scope(*lock_); |
275 base_font_.reset(); | 190 base_font_.reset(); |
276 LoadFontsIfNecessary(); | 191 LoadFontsIfNecessary(); |
277 } | 192 } |
278 | 193 |
279 ResourceBundle::ResourceBundle() | 194 ResourceBundle::ResourceBundle() |
280 : lock_(new base::Lock), | 195 : lock_(new base::Lock), |
281 resources_data_(NULL), | 196 resources_data_(NULL), |
282 large_icon_resources_data_(NULL) { | 197 large_icon_resources_data_(NULL), |
| 198 locale_resources_data_(NULL) { |
283 } | 199 } |
284 | 200 |
285 void ResourceBundle::FreeImages() { | 201 void ResourceBundle::FreeImages() { |
286 STLDeleteContainerPairSecondPointers(images_.begin(), | 202 STLDeleteContainerPairSecondPointers(images_.begin(), |
287 images_.end()); | 203 images_.end()); |
288 images_.clear(); | 204 images_.clear(); |
289 } | 205 } |
290 | 206 |
291 void ResourceBundle::LoadFontsIfNecessary() { | 207 void ResourceBundle::LoadFontsIfNecessary() { |
292 lock_->AssertAcquired(); | 208 lock_->AssertAcquired(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 } | 295 } |
380 | 296 |
381 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( | 297 RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( |
382 int resource_id) const { | 298 int resource_id) const { |
383 if (!data_pack_.get()) | 299 if (!data_pack_.get()) |
384 return NULL; | 300 return NULL; |
385 return data_pack_->GetStaticMemory(resource_id); | 301 return data_pack_->GetStaticMemory(resource_id); |
386 } | 302 } |
387 | 303 |
388 } // namespace ui | 304 } // namespace ui |
OLD | NEW |