diff options
Diffstat (limited to 'ui/base/resource')
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 88 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 8 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_dummy.cc | 3 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_linux.cc | 14 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_posix.cc | 69 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_win.cc | 93 |
6 files changed, 108 insertions, 167 deletions
diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index 8322566..6a68f20 100644 --- a/ui/base/resource/resource_bundle.cc +++ b/ui/base/resource/resource_bundle.cc @@ -4,13 +4,19 @@ #include "ui/base/resource/resource_bundle.h" +#include "base/command_line.h" +#include "base/file_util.h" #include "base/logging.h" +#include "base/path_service.h" #include "base/stl_util.h" #include "base/string_piece.h" #include "base/synchronization/lock.h" #include "build/build_config.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/data_pack.h" +#include "ui/base/ui_base_paths.h" +#include "ui/base/ui_base_switches.h" #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" @@ -73,6 +79,17 @@ void ResourceBundle::InitSharedInstanceForTest(const FilePath& path) { } /* static */ +DataPack* ResourceBundle::LoadResourcesDataPak(const FilePath& path) { + DataPack* datapack = new DataPack; + bool success = datapack->Load(path); + if (!success) { + delete datapack; + datapack = NULL; + } + return datapack; +} + +/* static */ std::string ResourceBundle::ReloadSharedInstance( const std::string& pref_locale) { DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; @@ -102,6 +119,74 @@ ResourceBundle& ResourceBundle::GetSharedInstance() { return *g_shared_instance_; } +#if !defined(OS_MACOSX) +/* static */ +FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { + FilePath locale_file_path; + PathService::Get(ui::DIR_LOCALES, &locale_file_path); + if (locale_file_path.empty()) + return locale_file_path; + if (app_locale.empty()) + 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 + +std::string ResourceBundle::LoadLocaleResources( + const std::string& pref_locale) { + DCHECK(!locale_resources_data_.get()) << "locale.pak already loaded"; + std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); + FilePath locale_file_path; + CommandLine *command_line = CommandLine::ForCurrentProcess(); + if (command_line->HasSwitch(switches::kLocalePak)) { + locale_file_path = + command_line->GetSwitchValuePath(switches::kLocalePak); + } else { + locale_file_path = GetLocaleFilePath(app_locale); + } + if (locale_file_path.empty()) { + // It's possible that there is no locale.pak. + NOTREACHED(); + return std::string(); + } + locale_resources_data_.reset(LoadResourcesDataPak(locale_file_path)); + CHECK(locale_resources_data_.get()) << "failed to load locale.pak"; + return app_locale; +} + +void ResourceBundle::UnloadLocaleResources() { + locale_resources_data_.reset(); +} + +string16 ResourceBundle::GetLocalizedString(int message_id) { + // If for some reason we were unable to load a resource pak, 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(); + } + } + + // Data pack encodes strings as UTF16. + DCHECK_EQ(data.length() % 2, 0U); + string16 msg(reinterpret_cast<const char16*>(data.data()), + data.length() / 2); + return msg; +} + SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { const SkBitmap* bitmap = static_cast<const SkBitmap*>(GetImageNamed(resource_id)); @@ -194,8 +279,7 @@ void ResourceBundle::ReloadFonts() { ResourceBundle::ResourceBundle() : lock_(new base::Lock), resources_data_(NULL), - large_icon_resources_data_(NULL), - locale_resources_data_(NULL) { + large_icon_resources_data_(NULL) { } void ResourceBundle::FreeImages() { diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index 37d3ef1..7076a6e 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -83,6 +83,10 @@ class UI_EXPORT ResourceBundle { // Initialize the ResourceBundle using given data pack path for testing. static void InitSharedInstanceForTest(const FilePath& path); + // Load a .pak file. Returns NULL if we fail to load |path|. The caller + // is responsible for deleting up this pointer. + static DataPack* LoadResourcesDataPak(const FilePath& path); + // Changes the locale for an already-initialized ResourceBundle. Future // calls to get strings will return the strings for this new locale. This // has no effect on existing or future image resources. This has no effect @@ -271,7 +275,9 @@ class UI_EXPORT ResourceBundle { // Handles for data sources. DataHandle resources_data_; DataHandle large_icon_resources_data_; - DataHandle locale_resources_data_; +#if !defined(NACL_WIN64) + scoped_ptr<DataPack> locale_resources_data_; +#endif // References to extra data packs loaded via AddDataPackToSharedInstance. std::vector<LoadedDataPack*> data_packs_; diff --git a/ui/base/resource/resource_bundle_dummy.cc b/ui/base/resource/resource_bundle_dummy.cc index 8a2ad08..529e63a 100644 --- a/ui/base/resource/resource_bundle_dummy.cc +++ b/ui/base/resource/resource_bundle_dummy.cc @@ -45,8 +45,7 @@ ResourceBundle& ResourceBundle::GetSharedInstance() { ResourceBundle::ResourceBundle() : lock_(new base::Lock), - resources_data_(NULL), - locale_resources_data_(NULL) { + resources_data_(NULL) { } ResourceBundle::~ResourceBundle() { diff --git a/ui/base/resource/resource_bundle_linux.cc b/ui/base/resource/resource_bundle_linux.cc index d1eb654..0ea8b0d 100644 --- a/ui/base/resource/resource_bundle_linux.cc +++ b/ui/base/resource/resource_bundle_linux.cc @@ -71,20 +71,6 @@ FilePath ResourceBundle::GetLargeIconResourcesFilePath() { return FilePath(); } -// static -FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { - FilePath locale_file_path; - PathService::Get(ui::DIR_LOCALES, &locale_file_path); - if (locale_file_path.empty()) - return locale_file_path; - if (app_locale.empty()) - 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; -} - gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { return *GetPixbufImpl(resource_id, false); } diff --git a/ui/base/resource/resource_bundle_posix.cc b/ui/base/resource/resource_bundle_posix.cc index d23a5b1..2669b68 100644 --- a/ui/base/resource/resource_bundle_posix.cc +++ b/ui/base/resource/resource_bundle_posix.cc @@ -18,20 +18,6 @@ namespace ui { -namespace { - -DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { - DataPack* resources_pak = new DataPack; - bool success = resources_pak->Load(resources_pak_path); - if (!success) { - delete resources_pak; - resources_pak = NULL; - } - return resources_pak; -} - -} // namespace - ResourceBundle::~ResourceBundle() { FreeImages(); UnloadLocaleResources(); @@ -41,11 +27,6 @@ ResourceBundle::~ResourceBundle() { resources_data_ = NULL; } -void ResourceBundle::UnloadLocaleResources() { - delete locale_resources_data_; - locale_resources_data_ = NULL; -} - // static RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( DataHandle module, int resource_id) { @@ -69,32 +50,6 @@ base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { return data; } -string16 ResourceBundle::GetLocalizedString(int message_id) { - // If for some reason we were unable to load a resource pak, return an empty - // string (better than crashing). - if (!locale_resources_data_) { - 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(); - } - } - - // Data pack encodes strings as UTF16. - DCHECK_EQ(data.length() % 2, 0U); - string16 msg(reinterpret_cast<const char16*>(data.data()), - data.length() / 2); - return msg; -} - void ResourceBundle::LoadCommonResources() { DCHECK(!resources_data_) << "chrome.pak already loaded"; FilePath resources_file_path = GetResourcesFilePath(); @@ -111,34 +66,12 @@ void ResourceBundle::LoadCommonResources() { } } -std::string ResourceBundle::LoadLocaleResources( - const std::string& pref_locale) { - DCHECK(!locale_resources_data_) << "locale.pak already loaded"; - std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); - FilePath locale_file_path; - CommandLine *command_line = CommandLine::ForCurrentProcess(); - if (command_line->HasSwitch(switches::kLocalePak)) { - locale_file_path = - command_line->GetSwitchValuePath(switches::kLocalePak); - } else { - locale_file_path = GetLocaleFilePath(app_locale); - } - if (locale_file_path.empty()) { - // It's possible that there is no locale.pak. - NOTREACHED(); - return std::string(); - } - locale_resources_data_ = LoadResourcesDataPak(locale_file_path); - CHECK(locale_resources_data_) << "failed to load locale.pak"; - return app_locale; -} - void ResourceBundle::LoadTestResources(const FilePath& path) { DCHECK(!resources_data_) << "resource already loaded"; // Use the given resource pak for both common and localized resources. resources_data_ = LoadResourcesDataPak(path); - locale_resources_data_ = LoadResourcesDataPak(path); + locale_resources_data_.reset(LoadResourcesDataPak(path)); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_win.cc b/ui/base/resource/resource_bundle_win.cc index d5cbaad..0024f5d 100644 --- a/ui/base/resource/resource_bundle_win.cc +++ b/ui/base/resource/resource_bundle_win.cc @@ -7,17 +7,13 @@ #include <atlbase.h> #include "base/debug/stack_trace.h" -#include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" #include "base/resource_util.h" #include "base/stl_util.h" #include "base/string_piece.h" -#include "base/synchronization/lock.h" #include "base/win/windows_version.h" -#include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/data_pack.h" -#include "ui/base/ui_base_paths.h" #include "ui/gfx/font.h" namespace ui { @@ -55,50 +51,11 @@ void ResourceBundle::LoadCommonResources() { } } -std::string ResourceBundle::LoadLocaleResources( - const std::string& pref_locale) { - DCHECK(NULL == locale_resources_data_) << "locale dll already loaded"; - const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); - const FilePath& locale_path = GetLocaleFilePath(app_locale); - if (locale_path.value().empty()) { - // It's possible that there are no locale dlls found, in which case we just - // return. - NOTREACHED(); - return std::string(); - } - - // The dll should only have resources, not executable code. - locale_resources_data_ = LoadLibraryEx(locale_path.value().c_str(), NULL, - GetDataDllLoadFlags()); - DCHECK(locale_resources_data_ != NULL) << - "unable to load generated resources"; - return app_locale; -} - void ResourceBundle::LoadTestResources(const FilePath& path) { // On Windows, the test resources are normally compiled into the binary // itself. } -void ResourceBundle::UnloadLocaleResources() { - if (locale_resources_data_) { - BOOL rv = FreeLibrary(locale_resources_data_); - DCHECK(rv); - locale_resources_data_ = NULL; - } -} - -// static -FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { - FilePath locale_path; - PathService::Get(ui::DIR_LOCALES, &locale_path); - - if (app_locale.empty()) - return FilePath(); - - return locale_path.AppendASCII(app_locale + ".dll"); -} - // static RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( DataHandle module, int resource_id) { @@ -125,20 +82,26 @@ HICON ResourceBundle::LoadThemeIcon(int icon_id) { base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { void* data_ptr; size_t data_size; + base::StringPiece data; if (base::GetDataResourceFromModule(resources_data_, resource_id, &data_ptr, &data_size)) { return base::StringPiece(static_cast<const char*>(data_ptr), data_size); - } else if (locale_resources_data_ && - base::GetDataResourceFromModule(locale_resources_data_, - resource_id, - &data_ptr, - &data_size)) { - return base::StringPiece(static_cast<const char*>(data_ptr), data_size); + } else if (locale_resources_data_.get() && + locale_resources_data_->GetStringPiece(resource_id, &data)) { + return data; + } + + // TODO(tony): Remove this ATL code once we remove the strings in + // chrome.dll. + const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage( + resources_data_, resource_id); + if (image) { + return base::StringPiece(reinterpret_cast<const char*>(image->achString), + image->nLength * 2); } - base::StringPiece data; for (size_t i = 0; i < data_packs_.size(); ++i) { if (data_packs_[i]->GetStringPiece(resource_id, &data)) return data; @@ -152,36 +115,6 @@ HCURSOR ResourceBundle::LoadCursor(int cursor_id) { return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); } -string16 ResourceBundle::GetLocalizedString(int message_id) { - // If for some reason we were unable to load a resource dll, return an empty - // string (better than crashing). - if (!locale_resources_data_) { - base::debug::StackTrace().PrintBacktrace(); // See http://crbug.com/21925. - LOG(WARNING) << "locale resources are not loaded"; - return string16(); - } - - DCHECK(IS_INTRESOURCE(message_id)); - - // Get a reference directly to the string resource. - HINSTANCE hinstance = locale_resources_data_; - const ATLSTRINGRESOURCEIMAGE* image = AtlGetStringResourceImage(hinstance, - message_id); - if (!image) { - // Fall back on the current module (shouldn't be any strings here except - // in unittests). - image = AtlGetStringResourceImage(resources_data_, message_id); - if (!image) { - // See http://crbug.com/21925. - base::debug::StackTrace().PrintBacktrace(); - NOTREACHED() << "unable to find resource: " << message_id; - return string16(); - } - } - // Copy into a string16 and return. - return string16(image->achString, image->nLength); -} - // Windows only uses SkBitmap for gfx::Image, so this is the same as // GetImageNamed. gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { |