| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_piece.h" | 16 #include "base/string_piece.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/layout.h" |
| 22 #include "ui/base/resource/data_pack.h" | 23 #include "ui/base/resource/data_pack.h" |
| 23 #include "ui/base/ui_base_paths.h" | 24 #include "ui/base/ui_base_paths.h" |
| 24 #include "ui/base/ui_base_switches.h" | 25 #include "ui/base/ui_base_switches.h" |
| 25 #include "ui/gfx/codec/jpeg_codec.h" | 26 #include "ui/gfx/codec/jpeg_codec.h" |
| 26 #include "ui/gfx/codec/png_codec.h" | 27 #include "ui/gfx/codec/png_codec.h" |
| 27 #include "ui/gfx/image/image_skia.h" | 28 #include "ui/gfx/image/image_skia.h" |
| 28 | 29 |
| 29 namespace ui { | 30 namespace ui { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 53 | 54 |
| 54 g_shared_instance_->LoadCommonResources(); | 55 g_shared_instance_->LoadCommonResources(); |
| 55 return g_shared_instance_->LoadLocaleResources(pref_locale); | 56 return g_shared_instance_->LoadLocaleResources(pref_locale); |
| 56 } | 57 } |
| 57 | 58 |
| 58 // static | 59 // static |
| 59 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { | 60 void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { |
| 60 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; | 61 DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| 61 g_shared_instance_ = new ResourceBundle(NULL); | 62 g_shared_instance_ = new ResourceBundle(NULL); |
| 62 | 63 |
| 63 g_shared_instance_->LoadTestResources(path); | 64 g_shared_instance_->LoadTestResources(path, path); |
| 64 } | 65 } |
| 65 | 66 |
| 66 // static | 67 // static |
| 67 void ResourceBundle::CleanupSharedInstance() { | 68 void ResourceBundle::CleanupSharedInstance() { |
| 68 if (g_shared_instance_) { | 69 if (g_shared_instance_) { |
| 69 delete g_shared_instance_; | 70 delete g_shared_instance_; |
| 70 g_shared_instance_ = NULL; | 71 g_shared_instance_ = NULL; |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 74 // static | 75 // static |
| 75 bool ResourceBundle::HasSharedInstance() { | 76 bool ResourceBundle::HasSharedInstance() { |
| 76 return g_shared_instance_ != NULL; | 77 return g_shared_instance_ != NULL; |
| 77 } | 78 } |
| 78 | 79 |
| 79 // static | 80 // static |
| 80 ResourceBundle& ResourceBundle::GetSharedInstance() { | 81 ResourceBundle& ResourceBundle::GetSharedInstance() { |
| 81 // Must call InitSharedInstance before this function. | 82 // Must call InitSharedInstance before this function. |
| 82 CHECK(g_shared_instance_ != NULL); | 83 CHECK(g_shared_instance_ != NULL); |
| 83 return *g_shared_instance_; | 84 return *g_shared_instance_; |
| 84 } | 85 } |
| 85 | 86 |
| 86 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { | 87 bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
| 87 return !GetLocaleFilePath(locale).empty(); | 88 return !GetLocaleFilePath(locale).empty(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { | 91 void ResourceBundle::AddDataPack(const FilePath& path, |
| 92 ScaleFactor scale_factor) { |
| 91 // Do not pass an empty |path| value to this method. If the absolute path is | 93 // Do not pass an empty |path| value to this method. If the absolute path is |
| 92 // unknown pass just the pack file name. | 94 // unknown pass just the pack file name. |
| 93 DCHECK(!path.empty()); | 95 DCHECK(!path.empty()); |
| 94 | 96 |
| 95 FilePath pack_path = path; | 97 FilePath pack_path = path; |
| 96 if (delegate_) | 98 if (delegate_) |
| 97 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); | 99 pack_path = delegate_->GetPathForResourcePack(pack_path, scale_factor); |
| 98 | 100 |
| 99 // Don't try to load empty values or values that are not absolute paths. | 101 // Don't try to load empty values or values that are not absolute paths. |
| 100 if (pack_path.empty() || !pack_path.IsAbsolute()) | 102 if (pack_path.empty() || !pack_path.IsAbsolute()) |
| 101 return; | 103 return; |
| 102 | 104 |
| 103 scoped_ptr<DataPack> data_pack( | 105 scoped_ptr<DataPack> data_pack( |
| 104 new DataPack(ResourceHandle::kScaleFactor100x)); | 106 new DataPack(scale_factor)); |
| 105 if (data_pack->Load(pack_path)) { | 107 if (data_pack->Load(pack_path)) { |
| 106 data_packs_.push_back(data_pack.release()); | 108 data_packs_.push_back(data_pack.release()); |
| 107 } else { | 109 } else { |
| 108 LOG(ERROR) << "Failed to load " << pack_path.value() | 110 LOG(ERROR) << "Failed to load " << pack_path.value() |
| 109 << "\nSome features may not be available."; | 111 << "\nSome features may not be available."; |
| 110 } | 112 } |
| 111 } | 113 } |
| 112 | 114 |
| 113 #if !defined(OS_MACOSX) | 115 #if !defined(OS_MACOSX) |
| 114 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { | 116 FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 locale_file_path = GetLocaleFilePath(app_locale); | 159 locale_file_path = GetLocaleFilePath(app_locale); |
| 158 } | 160 } |
| 159 } | 161 } |
| 160 | 162 |
| 161 if (locale_file_path.empty()) { | 163 if (locale_file_path.empty()) { |
| 162 // It's possible that there is no locale.pak. | 164 // It's possible that there is no locale.pak. |
| 163 return std::string(); | 165 return std::string(); |
| 164 } | 166 } |
| 165 | 167 |
| 166 scoped_ptr<DataPack> data_pack( | 168 scoped_ptr<DataPack> data_pack( |
| 167 new DataPack(ResourceHandle::kScaleFactor100x)); | 169 new DataPack(SCALE_FACTOR_100P)); |
| 168 if (!data_pack->Load(locale_file_path)) { | 170 if (!data_pack->Load(locale_file_path)) { |
| 169 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", | 171 UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", |
| 170 logging::GetLastSystemErrorCode(), 16000); | 172 logging::GetLastSystemErrorCode(), 16000); |
| 171 NOTREACHED() << "failed to load locale.pak"; | 173 NOTREACHED() << "failed to load locale.pak"; |
| 172 return std::string(); | 174 return std::string(); |
| 173 } | 175 } |
| 174 | 176 |
| 175 locale_resources_data_.reset(data_pack.release()); | 177 locale_resources_data_.reset(data_pack.release()); |
| 176 return app_locale; | 178 return app_locale; |
| 177 } | 179 } |
| 178 | 180 |
| 179 void ResourceBundle::LoadTestResources(const FilePath& path) { | 181 void ResourceBundle::LoadTestResources(const FilePath& path, |
| 182 const FilePath& locale_path) { |
| 180 // Use the given resource pak for both common and localized resources. | 183 // Use the given resource pak for both common and localized resources. |
| 181 scoped_ptr<DataPack> data_pack( | 184 scoped_ptr<DataPack> data_pack( |
| 182 new DataPack(ResourceHandle::kScaleFactor100x)); | 185 new DataPack(SCALE_FACTOR_100P)); |
| 183 if (data_pack->Load(path)) | 186 if (!path.empty() && data_pack->Load(path)) |
| 184 data_packs_.push_back(data_pack.release()); | 187 data_packs_.push_back(data_pack.release()); |
| 185 | 188 |
| 186 data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); | 189 data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); |
| 187 if (data_pack->Load(path)) | 190 if (!locale_path.empty() && data_pack->Load(locale_path)) { |
| 188 locale_resources_data_.reset(data_pack.release()); | 191 locale_resources_data_.reset(data_pack.release()); |
| 192 } else { |
| 193 locale_resources_data_.reset( |
| 194 new DataPack(ui::SCALE_FACTOR_NONE)); |
| 195 } |
| 189 } | 196 } |
| 190 | 197 |
| 191 void ResourceBundle::UnloadLocaleResources() { | 198 void ResourceBundle::UnloadLocaleResources() { |
| 192 locale_resources_data_.reset(); | 199 locale_resources_data_.reset(); |
| 193 } | 200 } |
| 194 | 201 |
| 195 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { | 202 void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { |
| 196 overridden_pak_path_ = pak_path; | 203 overridden_pak_path_ = pak_path; |
| 197 } | 204 } |
| 198 | 205 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 image = delegate_->GetImageNamed(resource_id); | 237 image = delegate_->GetImageNamed(resource_id); |
| 231 | 238 |
| 232 if (image.IsEmpty()) { | 239 if (image.IsEmpty()) { |
| 233 DCHECK(!delegate_ && !data_packs_.empty()) << | 240 DCHECK(!delegate_ && !data_packs_.empty()) << |
| 234 "Missing call to SetResourcesDataDLL?"; | 241 "Missing call to SetResourcesDataDLL?"; |
| 235 gfx::ImageSkia image_skia; | 242 gfx::ImageSkia image_skia; |
| 236 for (size_t i = 0; i < data_packs_.size(); ++i) { | 243 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 237 scoped_ptr<SkBitmap> bitmap(LoadBitmap(*data_packs_[i], resource_id)); | 244 scoped_ptr<SkBitmap> bitmap(LoadBitmap(*data_packs_[i], resource_id)); |
| 238 if (bitmap.get()) { | 245 if (bitmap.get()) { |
| 239 #if defined(USE_AURA) | 246 #if defined(USE_AURA) |
| 240 image_skia.AddBitmapForScale(*bitmap, data_packs_[i]->GetScaleFactor()); | 247 // TODO(flackr): Modify ImageSkia to use ScaleFactor enum. |
| 248 image_skia.AddBitmapForScale(*bitmap, |
| 249 ui::GetScaleFactorScale(data_packs_[i]->GetScaleFactor())); |
| 241 #else | 250 #else |
| 242 image_skia.AddBitmapForScale(*bitmap, 1.0f); | 251 image_skia.AddBitmapForScale(*bitmap, 1.0f); |
| 243 #endif | 252 #endif |
| 244 } | 253 } |
| 245 } | 254 } |
| 246 | 255 |
| 247 if (image_skia.empty()) { | 256 if (image_skia.empty()) { |
| 248 LOG(WARNING) << "Unable to load image with id " << resource_id; | 257 LOG(WARNING) << "Unable to load image with id " << resource_id; |
| 249 NOTREACHED(); // Want to assert in debug mode. | 258 NOTREACHED(); // Want to assert in debug mode. |
| 250 // The load failed to retrieve the image; show a debugging red square. | 259 // The load failed to retrieve the image; show a debugging red square. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 263 | 272 |
| 264 images_[resource_id] = image; | 273 images_[resource_id] = image; |
| 265 return images_[resource_id]; | 274 return images_[resource_id]; |
| 266 } | 275 } |
| 267 | 276 |
| 268 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { | 277 gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
| 269 return GetNativeImageNamed(resource_id, RTL_DISABLED); | 278 return GetNativeImageNamed(resource_id, RTL_DISABLED); |
| 270 } | 279 } |
| 271 | 280 |
| 272 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( | 281 base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( |
| 273 int resource_id) const { | 282 int resource_id, |
| 283 ScaleFactor scale_factor) const { |
| 274 base::RefCountedStaticMemory* bytes = NULL; | 284 base::RefCountedStaticMemory* bytes = NULL; |
| 275 if (delegate_) | 285 if (delegate_) |
| 276 bytes = delegate_->LoadDataResourceBytes(resource_id); | 286 bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); |
| 277 | 287 |
| 278 if (!bytes) { | 288 if (!bytes) { |
| 279 for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) | 289 base::StringPiece data = GetRawDataResource(resource_id, scale_factor); |
| 280 bytes = data_packs_[i]->GetStaticMemory(resource_id); | 290 if (!data.empty()) { |
| 291 bytes = new base::RefCountedStaticMemory( |
| 292 reinterpret_cast<const unsigned char*>(data.data()), data.length()); |
| 293 } |
| 281 } | 294 } |
| 282 | 295 |
| 283 return bytes; | 296 return bytes; |
| 284 } | 297 } |
| 285 | 298 |
| 286 base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { | 299 base::StringPiece ResourceBundle::GetRawDataResource( |
| 300 int resource_id, |
| 301 ScaleFactor scale_factor) const { |
| 287 base::StringPiece data; | 302 base::StringPiece data; |
| 288 if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) | 303 if (delegate_ && |
| 304 delegate_->GetRawDataResource(resource_id, scale_factor, &data)) |
| 289 return data; | 305 return data; |
| 290 | 306 |
| 291 DCHECK(locale_resources_data_.get()); | 307 DCHECK(locale_resources_data_.get()); |
| 292 if (locale_resources_data_->GetStringPiece(resource_id, &data)) | 308 if (locale_resources_data_->GetStringPiece(resource_id, &data)) |
| 293 return data; | 309 return data; |
| 294 | 310 |
| 295 for (size_t i = 0; i < data_packs_.size(); ++i) { | 311 if (scale_factor != ui::SCALE_FACTOR_100P) { |
| 296 if (data_packs_[i]->GetStringPiece(resource_id, &data)) | 312 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 313 if (data_packs_[i]->GetScaleFactor() == scale_factor && |
| 314 data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 315 return data; |
| 316 } |
| 317 } |
| 318 for (size_t i = 0; i < data_packs_.size(); i++) { |
| 319 if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && |
| 320 data_packs_[i]->GetStringPiece(resource_id, &data)) |
| 297 return data; | 321 return data; |
| 298 } | 322 } |
| 299 | 323 |
| 300 return base::StringPiece(); | 324 return base::StringPiece(); |
| 301 } | 325 } |
| 302 | 326 |
| 303 string16 ResourceBundle::GetLocalizedString(int message_id) { | 327 string16 ResourceBundle::GetLocalizedString(int message_id) { |
| 304 string16 string; | 328 string16 string; |
| 305 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) | 329 if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) |
| 306 return string; | 330 return string; |
| 307 | 331 |
| 308 // Ensure that ReloadLocaleResources() doesn't drop the resources while | 332 // Ensure that ReloadLocaleResources() doesn't drop the resources while |
| 309 // we're using them. | 333 // we're using them. |
| 310 base::AutoLock lock_scope(*locale_resources_data_lock_); | 334 base::AutoLock lock_scope(*locale_resources_data_lock_); |
| 311 | 335 |
| 312 // If for some reason we were unable to load the resources , return an empty | 336 // If for some reason we were unable to load the resources , return an empty |
| 313 // string (better than crashing). | 337 // string (better than crashing). |
| 314 if (!locale_resources_data_.get()) { | 338 if (!locale_resources_data_.get()) { |
| 315 LOG(WARNING) << "locale resources are not loaded"; | 339 LOG(WARNING) << "locale resources are not loaded"; |
| 316 return string16(); | 340 return string16(); |
| 317 } | 341 } |
| 318 | 342 |
| 319 base::StringPiece data; | 343 base::StringPiece data; |
| 320 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { | 344 if (!locale_resources_data_->GetStringPiece(message_id, &data)) { |
| 321 // Fall back on the main data pack (shouldn't be any strings here except in | 345 // Fall back on the main data pack (shouldn't be any strings here except in |
| 322 // unittests). | 346 // unittests). |
| 323 data = GetRawDataResource(message_id); | 347 data = GetRawDataResource(message_id, ui::SCALE_FACTOR_NONE); |
| 324 if (data.empty()) { | 348 if (data.empty()) { |
| 325 NOTREACHED() << "unable to find resource: " << message_id; | 349 NOTREACHED() << "unable to find resource: " << message_id; |
| 326 return string16(); | 350 return string16(); |
| 327 } | 351 } |
| 328 } | 352 } |
| 329 | 353 |
| 330 // Strings should not be loaded from a data pack that contains binary data. | 354 // Strings should not be loaded from a data pack that contains binary data. |
| 331 ResourceHandle::TextEncodingType encoding = | 355 ResourceHandle::TextEncodingType encoding = |
| 332 locale_resources_data_->GetTextEncodingType(); | 356 locale_resources_data_->GetTextEncodingType(); |
| 333 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) | 357 DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 SkBitmap bitmap; | 494 SkBitmap bitmap; |
| 471 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); | 495 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 32, 32); |
| 472 bitmap.allocPixels(); | 496 bitmap.allocPixels(); |
| 473 bitmap.eraseARGB(255, 255, 0, 0); | 497 bitmap.eraseARGB(255, 255, 0, 0); |
| 474 empty_image_ = gfx::Image(bitmap); | 498 empty_image_ = gfx::Image(bitmap); |
| 475 } | 499 } |
| 476 return empty_image_; | 500 return empty_image_; |
| 477 } | 501 } |
| 478 | 502 |
| 479 } // namespace ui | 503 } // namespace ui |
| OLD | NEW |