Chromium Code Reviews| Index: ui/base/resource/resource_bundle.cc |
| =================================================================== |
| --- ui/base/resource/resource_bundle.cc (revision 133962) |
| +++ ui/base/resource/resource_bundle.cc (working copy) |
| @@ -24,8 +24,6 @@ |
| #include "ui/base/ui_base_switches.h" |
| #include "ui/gfx/codec/jpeg_codec.h" |
| #include "ui/gfx/codec/png_codec.h" |
| -#include "ui/gfx/font.h" |
| -#include "ui/gfx/image/image.h" |
| namespace ui { |
| @@ -48,9 +46,9 @@ |
| // static |
| std::string ResourceBundle::InitSharedInstanceWithLocale( |
| - const std::string& pref_locale) { |
| + const std::string& pref_locale, Delegate* delegate) { |
| DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| - g_shared_instance_ = new ResourceBundle(); |
| + g_shared_instance_ = new ResourceBundle(delegate); |
| g_shared_instance_->LoadCommonResources(); |
| return g_shared_instance_->LoadLocaleResources(pref_locale); |
| @@ -59,7 +57,7 @@ |
| // static |
| void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { |
| DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; |
| - g_shared_instance_ = new ResourceBundle(); |
| + g_shared_instance_ = new ResourceBundle(NULL); |
| g_shared_instance_->LoadTestResources(path); |
| } |
| @@ -84,7 +82,6 @@ |
| return *g_shared_instance_; |
| } |
| -// static |
| bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { |
| return !GetLocaleFilePath(locale).empty(); |
| } |
| @@ -100,22 +97,30 @@ |
| } |
| #if !defined(OS_MACOSX) |
| -// static |
| FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { |
| + if (app_locale.empty()) |
| + return FilePath(); |
| + |
| FilePath locale_file_path; |
| + |
| #if defined(OS_ANDROID) |
| PathService::Get(base::DIR_ANDROID_APP_DATA, &locale_file_path); |
| locale_file_path = locale_file_path.Append(FILE_PATH_LITERAL("paks")); |
| #else |
| PathService::Get(ui::DIR_LOCALES, &locale_file_path); |
| #endif |
| - if (locale_file_path.empty()) |
| - return locale_file_path; |
| - if (app_locale.empty()) |
| + |
| + if (!locale_file_path.empty()) |
| + locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
| + |
| + if (delegate_ && |
| + !delegate_->GetPathForLocalePack(app_locale, &locale_file_path)) { |
| return FilePath(); |
| - locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); |
| + } |
| + |
| if (!file_util::PathExists(locale_file_path)) |
| return FilePath(); |
| + |
| return locale_file_path; |
| } |
| #endif |
| @@ -126,7 +131,7 @@ |
| std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); |
| FilePath locale_file_path = GetOverriddenPakPath(); |
| if (locale_file_path.empty()) { |
| - CommandLine *command_line = CommandLine::ForCurrentProcess(); |
| + CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| if (command_line->HasSwitch(switches::kLocalePak)) { |
| locale_file_path = |
| command_line->GetSwitchValuePath(switches::kLocalePak); |
| @@ -137,7 +142,6 @@ |
| if (locale_file_path.empty()) { |
| // It's possible that there is no locale.pak. |
| - NOTREACHED(); |
| return std::string(); |
| } |
| @@ -168,46 +172,6 @@ |
| locale_resources_data_.reset(); |
| } |
| -string16 ResourceBundle::GetLocalizedString(int message_id) { |
| - // Ensure that ReloadLocaleResources() doesn't drop the resources while |
| - // we're using them. |
| - base::AutoLock lock_scope(*locale_resources_data_lock_); |
| - |
| - // If for some reason we were unable to load the resources , return an empty |
| - // string (better than crashing). |
| - if (!locale_resources_data_.get()) { |
| - LOG(WARNING) << "locale resources are not loaded"; |
| - return string16(); |
| - } |
| - |
| - base::StringPiece data; |
| - if (!locale_resources_data_->GetStringPiece(message_id, &data)) { |
| - // Fall back on the main data pack (shouldn't be any strings here except in |
| - // unittests). |
| - data = GetRawDataResource(message_id); |
| - if (data.empty()) { |
| - NOTREACHED() << "unable to find resource: " << message_id; |
| - return string16(); |
| - } |
| - } |
| - |
| - // Strings should not be loaded from a data pack that contains binary data. |
| - ResourceHandle::TextEncodingType encoding = |
| - locale_resources_data_->GetTextEncodingType(); |
| - DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) |
| - << "requested localized string from binary pack file"; |
| - |
| - // Data pack encodes strings as either UTF8 or UTF16. |
| - string16 msg; |
| - if (encoding == ResourceHandle::UTF16) { |
| - msg = string16(reinterpret_cast<const char16*>(data.data()), |
| - data.length() / 2); |
| - } else if (encoding == ResourceHandle::UTF8) { |
| - msg = UTF8ToUTF16(data); |
| - } |
| - return msg; |
| -} |
| - |
| void ResourceBundle::OverrideLocalePakForTest(const FilePath& pak_path) { |
| overridden_pak_path_ = pak_path; |
| } |
| @@ -237,34 +201,43 @@ |
| return *found->second; |
| } |
| - DCHECK(!data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; |
| - ScopedVector<const SkBitmap> bitmaps; |
| - for (size_t i = 0; i < data_packs_.size(); ++i) { |
| - SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); |
| - if (bitmap) |
| - bitmaps.push_back(bitmap); |
| - } |
| + scoped_ptr<gfx::Image> image; |
| + if (delegate_) |
| + image.reset(delegate_->GetImageNamed(resource_id).release()); |
| - if (bitmaps.empty()) { |
| - LOG(WARNING) << "Unable to load image with id " << resource_id; |
| - NOTREACHED(); // Want to assert in debug mode. |
| - // The load failed to retrieve the image; show a debugging red square. |
| - return *GetEmptyImage(); |
| + if (!image.get()) { |
| + DCHECK(!data_packs_.empty()) << "Missing call to SetResourcesDataDLL?"; |
|
tony
2012/05/01 15:56:48
Nit: This could also mean that the delegate blocke
Marshall
2012/05/01 17:29:41
Good question. I think these checks can be useful.
|
| + ScopedVector<const SkBitmap> bitmaps; |
| + for (size_t i = 0; i < data_packs_.size(); ++i) { |
| + SkBitmap* bitmap = LoadBitmap(*data_packs_[i], resource_id); |
| + if (bitmap) |
| + bitmaps.push_back(bitmap); |
| + } |
| + |
| + if (bitmaps.empty()) { |
| + LOG(WARNING) << "Unable to load image with id " << resource_id; |
| + NOTREACHED(); // Want to assert in debug mode. |
| + // The load failed to retrieve the image; show a debugging red square. |
| + return *GetEmptyImage(); |
| + } |
| + |
| + std::vector<const SkBitmap*> tmp_bitmaps; |
| + bitmaps.release(&tmp_bitmaps); |
| + |
| + // Takes ownership of bitmaps. |
| + image.reset(new gfx::Image(tmp_bitmaps)); |
| } |
| // The load was successful, so cache the image. |
| base::AutoLock lock_scope(*images_and_fonts_lock_); |
| // Another thread raced the load and has already cached the image. |
| - if (images_.count(resource_id)) |
| + if (images_.count(resource_id)) { |
| return *images_[resource_id]; |
| + } |
| - std::vector<const SkBitmap*> tmp_bitmaps; |
| - bitmaps.release(&tmp_bitmaps); |
| - // Takes ownership of bitmaps. |
| - gfx::Image* image = new gfx::Image(tmp_bitmaps); |
| - images_[resource_id] = image; |
| - return *image; |
| + images_[resource_id] = image.release(); |
| + return *images_[resource_id]; |
| } |
| gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |
| @@ -273,19 +246,24 @@ |
| base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( |
| int resource_id) const { |
| - for (size_t i = 0; i < data_packs_.size(); ++i) { |
| - base::RefCountedStaticMemory* bytes = |
| - data_packs_[i]->GetStaticMemory(resource_id); |
| - if (bytes) |
| - return bytes; |
| + scoped_refptr<base::RefCountedStaticMemory> bytes; |
| + if (delegate_) |
| + bytes = delegate_->LoadDataResourceBytes(resource_id); |
| + |
| + if (!bytes) { |
| + for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) |
| + bytes = data_packs_[i]->GetStaticMemory(resource_id); |
| } |
| - return NULL; |
| + return bytes; |
| } |
| base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { |
| + base::StringPiece data; |
| + if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) |
| + return data; |
| + |
| DCHECK(locale_resources_data_.get()); |
| - base::StringPiece data; |
| if (locale_resources_data_->GetStringPiece(resource_id, &data)) |
| return data; |
| @@ -297,6 +275,50 @@ |
| return base::StringPiece(); |
| } |
| +string16 ResourceBundle::GetLocalizedString(int message_id) { |
| + string16 string; |
| + if (delegate_ && delegate_->GetLocalizedString(message_id, &string)) |
| + return string; |
| + |
| + // Ensure that ReloadLocaleResources() doesn't drop the resources while |
| + // we're using them. |
| + base::AutoLock lock_scope(*locale_resources_data_lock_); |
| + |
| + // If for some reason we were unable to load the resources , return an empty |
| + // string (better than crashing). |
| + if (!locale_resources_data_.get()) { |
| + LOG(WARNING) << "locale resources are not loaded"; |
| + return string16(); |
| + } |
| + |
| + base::StringPiece data; |
| + if (!locale_resources_data_->GetStringPiece(message_id, &data)) { |
| + // Fall back on the main data pack (shouldn't be any strings here except in |
| + // unittests). |
| + data = GetRawDataResource(message_id); |
| + if (data.empty()) { |
| + NOTREACHED() << "unable to find resource: " << message_id; |
| + return string16(); |
| + } |
| + } |
| + |
| + // Strings should not be loaded from a data pack that contains binary data. |
| + ResourceHandle::TextEncodingType encoding = |
| + locale_resources_data_->GetTextEncodingType(); |
| + DCHECK(encoding == ResourceHandle::UTF16 || encoding == ResourceHandle::UTF8) |
| + << "requested localized string from binary pack file"; |
| + |
| + // Data pack encodes strings as either UTF8 or UTF16. |
| + string16 msg; |
| + if (encoding == ResourceHandle::UTF16) { |
| + msg = string16(reinterpret_cast<const char16*>(data.data()), |
| + data.length() / 2); |
| + } else if (encoding == ResourceHandle::UTF8) { |
| + msg = UTF8ToUTF16(data); |
| + } |
| + return msg; |
| +} |
| + |
| const gfx::Font& ResourceBundle::GetFont(FontStyle style) { |
| { |
| base::AutoLock lock_scope(*images_and_fonts_lock_); |
| @@ -326,8 +348,9 @@ |
| LoadFontsIfNecessary(); |
| } |
| -ResourceBundle::ResourceBundle() |
| - : images_and_fonts_lock_(new base::Lock), |
| +ResourceBundle::ResourceBundle(Delegate* delegate) |
| + : delegate_(delegate), |
| + images_and_fonts_lock_(new base::Lock), |
| locale_resources_data_lock_(new base::Lock) { |
| } |
| @@ -342,33 +365,67 @@ |
| images_.clear(); |
| } |
| +void ResourceBundle::AddCommonDataPack(const std::string& pack_name, |
| + const FilePath& path) { |
| + FilePath pack_path = path; |
| + if (!delegate_ || delegate_->GetPathForResourcePack(pack_name, &pack_path)) |
| + AddDataPack(pack_path); |
| +} |
| + |
| void ResourceBundle::LoadFontsIfNecessary() { |
| images_and_fonts_lock_->AssertAcquired(); |
| if (!base_font_.get()) { |
| - base_font_.reset(new gfx::Font()); |
| + if (delegate_) |
| + base_font_.reset(delegate_->GetFont(BaseFont).release()); |
| + if (!base_font_.get()) |
| + base_font_.reset(new gfx::Font()); |
| - bold_font_.reset(new gfx::Font()); |
| - *bold_font_ = |
| - base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); |
| + if (delegate_) |
| + bold_font_.reset(delegate_->GetFont(BoldFont).release()); |
| + if (!bold_font_.get()) { |
| + bold_font_.reset(new gfx::Font()); |
| + *bold_font_ = |
| + base_font_->DeriveFont(0, base_font_->GetStyle() | gfx::Font::BOLD); |
| + } |
| - small_font_.reset(new gfx::Font()); |
| - *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); |
| + if (delegate_) |
| + small_font_.reset(delegate_->GetFont(SmallFont).release()); |
| + if (!small_font_.get()) { |
| + small_font_.reset(new gfx::Font()); |
| + *small_font_ = base_font_->DeriveFont(kSmallFontSizeDelta); |
| + } |
| - medium_font_.reset(new gfx::Font()); |
| - *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); |
| + if (delegate_) |
| + medium_font_.reset(delegate_->GetFont(MediumFont).release()); |
| + if (!medium_font_.get()) { |
| + medium_font_.reset(new gfx::Font()); |
| + *medium_font_ = base_font_->DeriveFont(kMediumFontSizeDelta); |
| + } |
| - medium_bold_font_.reset(new gfx::Font()); |
| - *medium_bold_font_ = |
| - base_font_->DeriveFont(kMediumFontSizeDelta, |
| - base_font_->GetStyle() | gfx::Font::BOLD); |
| + if (delegate_) |
| + medium_bold_font_.reset(delegate_->GetFont(MediumBoldFont).release()); |
| + if (!medium_bold_font_.get()) { |
| + medium_bold_font_.reset(new gfx::Font()); |
| + *medium_bold_font_ = |
| + base_font_->DeriveFont(kMediumFontSizeDelta, |
| + base_font_->GetStyle() | gfx::Font::BOLD); |
| + } |
| - large_font_.reset(new gfx::Font()); |
| - *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); |
| + if (delegate_) |
| + large_font_.reset(delegate_->GetFont(LargeFont).release()); |
| + if (!large_font_.get()) { |
| + large_font_.reset(new gfx::Font()); |
| + *large_font_ = base_font_->DeriveFont(kLargeFontSizeDelta); |
| + } |
| - large_bold_font_.reset(new gfx::Font()); |
| - *large_bold_font_ = |
| - base_font_->DeriveFont(kLargeFontSizeDelta, |
| - base_font_->GetStyle() | gfx::Font::BOLD); |
| + if (delegate_) |
| + large_bold_font_.reset(delegate_->GetFont(LargeBoldFont).release()); |
| + if (!large_bold_font_.get()) { |
| + large_bold_font_.reset(new gfx::Font()); |
| + *large_bold_font_ = |
| + base_font_->DeriveFont(kLargeFontSizeDelta, |
| + base_font_->GetStyle() | gfx::Font::BOLD); |
| + } |
| } |
| } |