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/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 scoped_ptr<DataPack> data_pack( | 146 scoped_ptr<DataPack> data_pack( |
147 new DataPack(SCALE_FACTOR_100P)); | 147 new DataPack(SCALE_FACTOR_100P)); |
148 if (!data_pack->LoadFromFile(pak_file)) { | 148 if (!data_pack->LoadFromFile(pak_file)) { |
149 NOTREACHED() << "failed to load pak file"; | 149 NOTREACHED() << "failed to load pak file"; |
150 return; | 150 return; |
151 } | 151 } |
152 g_shared_instance_->locale_resources_data_.reset(data_pack.release()); | 152 g_shared_instance_->locale_resources_data_.reset(data_pack.release()); |
153 } | 153 } |
154 | 154 |
155 // static | 155 // static |
156 void ResourceBundle::InitSharedInstanceWithPakPath(const FilePath& path) { | 156 void ResourceBundle::InitSharedInstanceWithPakPath(const base::FilePath& path) { |
157 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 157 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
158 g_shared_instance_ = new ResourceBundle(NULL); | 158 g_shared_instance_ = new ResourceBundle(NULL); |
159 | 159 |
160 g_shared_instance_->LoadTestResources(path, path); | 160 g_shared_instance_->LoadTestResources(path, path); |
161 } | 161 } |
162 | 162 |
163 // static | 163 // static |
164 void ResourceBundle::CleanupSharedInstance() { | 164 void ResourceBundle::CleanupSharedInstance() { |
165 if (g_shared_instance_) { | 165 if (g_shared_instance_) { |
166 delete g_shared_instance_; | 166 delete g_shared_instance_; |
(...skipping 10 matching lines...) Expand all Loading... |
177 ResourceBundle& ResourceBundle::GetSharedInstance() { | 177 ResourceBundle& ResourceBundle::GetSharedInstance() { |
178 // Must call InitSharedInstance before this function. | 178 // Must call InitSharedInstance before this function. |
179 CHECK(g_shared_instance_ != NULL); | 179 CHECK(g_shared_instance_ != NULL); |
180 return *g_shared_instance_; | 180 return *g_shared_instance_; |
181 } | 181 } |
182 | 182 |
183 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 183 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
184 return !GetLocaleFilePath(locale, true).empty(); | 184 return !GetLocaleFilePath(locale, true).empty(); |
185 } | 185 } |
186 | 186 |
187 void ResourceBundle::AddDataPackFromPath(const FilePath& path, | 187 void ResourceBundle::AddDataPackFromPath(const base::FilePath& path, |
188 ScaleFactor scale_factor) { | 188 ScaleFactor scale_factor) { |
189 AddDataPackFromPathInternal(path, scale_factor, false); | 189 AddDataPackFromPathInternal(path, scale_factor, false); |
190 } | 190 } |
191 | 191 |
192 void ResourceBundle::AddOptionalDataPackFromPath(const FilePath& path, | 192 void ResourceBundle::AddOptionalDataPackFromPath(const base::FilePath& path, |
193 ScaleFactor scale_factor) { | 193 ScaleFactor scale_factor) { |
194 AddDataPackFromPathInternal(path, scale_factor, true); | 194 AddDataPackFromPathInternal(path, scale_factor, true); |
195 } | 195 } |
196 | 196 |
197 void ResourceBundle::AddDataPackFromFile(base::PlatformFile file, | 197 void ResourceBundle::AddDataPackFromFile(base::PlatformFile file, |
198 ScaleFactor scale_factor) { | 198 ScaleFactor scale_factor) { |
199 scoped_ptr<DataPack> data_pack( | 199 scoped_ptr<DataPack> data_pack( |
200 new DataPack(scale_factor)); | 200 new DataPack(scale_factor)); |
201 if (data_pack->LoadFromFile(file)) { | 201 if (data_pack->LoadFromFile(file)) { |
202 AddDataPack(data_pack.release()); | 202 AddDataPack(data_pack.release()); |
203 } else { | 203 } else { |
204 LOG(ERROR) << "Failed to load data pack from file." | 204 LOG(ERROR) << "Failed to load data pack from file." |
205 << "\nSome features may not be available."; | 205 << "\nSome features may not be available."; |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 #if !defined(OS_MACOSX) | 209 #if !defined(OS_MACOSX) |
210 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, | 210 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale, |
211 bool test_file_exists) { | 211 bool test_file_exists) { |
212 if (app_locale.empty()) | 212 if (app_locale.empty()) |
213 return FilePath(); | 213 return base::FilePath(); |
214 | 214 |
215 FilePath locale_file_path; | 215 base::FilePath locale_file_path; |
216 | 216 |
217 PathService::Get(ui::DIR_LOCALES, &locale_file_path); | 217 PathService::Get(ui::DIR_LOCALES, &locale_file_path); |
218 | 218 |
219 if (!locale_file_path.empty()) | 219 if (!locale_file_path.empty()) |
220 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); | 220 locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
221 | 221 |
222 if (delegate_) { | 222 if (delegate_) { |
223 locale_file_path = | 223 locale_file_path = |
224 delegate_->GetPathForLocalePack(locale_file_path, app_locale); | 224 delegate_->GetPathForLocalePack(locale_file_path, app_locale); |
225 } | 225 } |
226 | 226 |
227 // Don't try to load empty values or values that are not absolute paths. | 227 // Don't try to load empty values or values that are not absolute paths. |
228 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) | 228 if (locale_file_path.empty() || !locale_file_path.IsAbsolute()) |
229 return FilePath(); | 229 return base::FilePath(); |
230 | 230 |
231 if (test_file_exists && !file_util::PathExists(locale_file_path)) | 231 if (test_file_exists && !file_util::PathExists(locale_file_path)) |
232 return FilePath(); | 232 return base::FilePath(); |
233 | 233 |
234 return locale_file_path; | 234 return locale_file_path; |
235 } | 235 } |
236 #endif | 236 #endif |
237 | 237 |
238 std::string ResourceBundle::LoadLocaleResources( | 238 std::string ResourceBundle::LoadLocaleResources( |
239 const std::string& pref_locale) { | 239 const std::string& pref_locale) { |
240 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; | 240 DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; |
241 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); | 241 std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
242 FilePath locale_file_path = GetOverriddenPakPath(); | 242 base::FilePath locale_file_path = GetOverriddenPakPath(); |
243 if (locale_file_path.empty()) { | 243 if (locale_file_path.empty()) { |
244 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 244 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
245 if (command_line->HasSwitch(switches::kLocalePak)) { | 245 if (command_line->HasSwitch(switches::kLocalePak)) { |
246 locale_file_path = | 246 locale_file_path = |
247 command_line->GetSwitchValuePath(switches::kLocalePak); | 247 command_line->GetSwitchValuePath(switches::kLocalePak); |
248 } else { | 248 } else { |
249 locale_file_path = GetLocaleFilePath(app_locale, true); | 249 locale_file_path = GetLocaleFilePath(app_locale, true); |
250 } | 250 } |
251 } | 251 } |
252 | 252 |
(...skipping 10 matching lines...) Expand all Loading... |
263 logging::GetLastSystemErrorCode(), 16000); | 263 logging::GetLastSystemErrorCode(), 16000); |
264 LOG(ERROR) << "failed to load locale.pak"; | 264 LOG(ERROR) << "failed to load locale.pak"; |
265 NOTREACHED(); | 265 NOTREACHED(); |
266 return std::string(); | 266 return std::string(); |
267 } | 267 } |
268 | 268 |
269 locale_resources_data_.reset(data_pack.release()); | 269 locale_resources_data_.reset(data_pack.release()); |
270 return app_locale; | 270 return app_locale; |
271 } | 271 } |
272 | 272 |
273 void ResourceBundle::LoadTestResources(const FilePath& path, | 273 void ResourceBundle::LoadTestResources(const base::FilePath& path, |
274 const FilePath& locale_path) { | 274 const base::FilePath& locale_path) { |
275 // Use the given resource pak for both common and localized resources. | 275 // Use the given resource pak for both common and localized resources. |
276 scoped_ptr<DataPack> data_pack( | 276 scoped_ptr<DataPack> data_pack( |
277 new DataPack(SCALE_FACTOR_100P)); | 277 new DataPack(SCALE_FACTOR_100P)); |
278 if (!path.empty() && data_pack->LoadFromPath(path)) | 278 if (!path.empty() && data_pack->LoadFromPath(path)) |
279 AddDataPack(data_pack.release()); | 279 AddDataPack(data_pack.release()); |
280 | 280 |
281 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); | 281 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
282 if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) { | 282 if (!locale_path.empty() && data_pack->LoadFromPath(locale_path)) { |
283 locale_resources_data_.reset(data_pack.release()); | 283 locale_resources_data_.reset(data_pack.release()); |
284 } else { | 284 } else { |
285 locale_resources_data_.reset( | 285 locale_resources_data_.reset( |
286 new DataPack(ui::SCALE_FACTOR_NONE)); | 286 new DataPack(ui::SCALE_FACTOR_NONE)); |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 void ResourceBundle::UnloadLocaleResources() { | 290 void ResourceBundle::UnloadLocaleResources() { |
291 locale_resources_data_.reset(); | 291 locale_resources_data_.reset(); |
292 } | 292 } |
293 | 293 |
294 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { | 294 void ResourceBundle::OverrideLocalePakForTest(const base::FilePath& pak_path) { |
295 overridden_pak_path_ = pak_path; | 295 overridden_pak_path_ = pak_path; |
296 } | 296 } |
297 | 297 |
298 const FilePath& ResourceBundle::GetOverriddenPakPath() { | 298 const base::FilePath& ResourceBundle::GetOverriddenPakPath() { |
299 return overridden_pak_path_; | 299 return overridden_pak_path_; |
300 } | 300 } |
301 | 301 |
302 std::string ResourceBundle::ReloadLocaleResources( | 302 std::string ResourceBundle::ReloadLocaleResources( |
303 const std::string& pref_locale) { | 303 const std::string& pref_locale) { |
304 base::AutoLock lock_scope(*locale_resources_data_lock_); | 304 base::AutoLock lock_scope(*locale_resources_data_lock_); |
305 UnloadLocaleResources(); | 305 UnloadLocaleResources(); |
306 return LoadLocaleResources(pref_locale); | 306 return LoadLocaleResources(pref_locale); |
307 } | 307 } |
308 | 308 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 | 501 |
502 ResourceBundle::~ResourceBundle() { | 502 ResourceBundle::~ResourceBundle() { |
503 FreeImages(); | 503 FreeImages(); |
504 UnloadLocaleResources(); | 504 UnloadLocaleResources(); |
505 } | 505 } |
506 | 506 |
507 void ResourceBundle::FreeImages() { | 507 void ResourceBundle::FreeImages() { |
508 images_.clear(); | 508 images_.clear(); |
509 } | 509 } |
510 | 510 |
511 void ResourceBundle::AddDataPackFromPathInternal(const FilePath& path, | 511 void ResourceBundle::AddDataPackFromPathInternal(const base::FilePath& path, |
512 ScaleFactor scale_factor, | 512 ScaleFactor scale_factor, |
513 bool optional) { | 513 bool optional) { |
514 // Do not pass an empty |path| value to this method. If the absolute path is | 514 // Do not pass an empty |path| value to this method. If the absolute path is |
515 // unknown pass just the pack file name. | 515 // unknown pass just the pack file name. |
516 DCHECK(!path.empty()); | 516 DCHECK(!path.empty()); |
517 | 517 |
518 FilePath pack_path = path; | 518 base::FilePath pack_path = path; |
519 if (delegate_) | 519 if (delegate_) |
520 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | 520 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); |
521 | 521 |
522 // Don't try to load empty values or values that are not absolute paths. | 522 // Don't try to load empty values or values that are not absolute paths. |
523 if (pack_path.empty() || !pack_path.IsAbsolute()) | 523 if (pack_path.empty() || !pack_path.IsAbsolute()) |
524 return; | 524 return; |
525 | 525 |
526 scoped_ptr<DataPack> data_pack( | 526 scoped_ptr<DataPack> data_pack( |
527 new DataPack(scale_factor)); | 527 new DataPack(scale_factor)); |
528 if (data_pack->LoadFromPath(pack_path)) { | 528 if (data_pack->LoadFromPath(pack_path)) { |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 // static | 710 // static |
711 bool ResourceBundle::DecodePNG(const unsigned char* buf, | 711 bool ResourceBundle::DecodePNG(const unsigned char* buf, |
712 size_t size, | 712 size_t size, |
713 SkBitmap* bitmap, | 713 SkBitmap* bitmap, |
714 bool* fell_back_to_1x) { | 714 bool* fell_back_to_1x) { |
715 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); | 715 *fell_back_to_1x = PNGContainsFallbackMarker(buf, size); |
716 return gfx::PNGCodec::Decode(buf, size, bitmap); | 716 return gfx::PNGCodec::Decode(buf, size, bitmap); |
717 } | 717 } |
718 | 718 |
719 } // namespace ui | 719 } // namespace ui |
OLD | NEW |