diff options
author | hayato@chromium.org <hayato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-03 05:25:11 +0000 |
---|---|---|
committer | hayato@chromium.org <hayato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-03 05:25:11 +0000 |
commit | 32caba271be320eaba3ef4cee0a5806de0b2cbca (patch) | |
tree | 0ecbe2fdd97e243a20fe009144e3ee1746744a91 | |
parent | 717fcd89505d0e318b052c24a60fb1b64a6de480 (diff) | |
download | chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.zip chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.tar.gz chromium_src-32caba271be320eaba3ef4cee0a5806de0b2cbca.tar.bz2 |
Share code between Mac and Linux in ResourceBundle.
BUG=25891
TEST=None
Review URL: http://codereview.chromium.org/442002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33668 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | app/app.gyp | 3 | ||||
-rw-r--r-- | app/resource_bundle.h | 13 | ||||
-rw-r--r-- | app/resource_bundle_linux.cc | 95 | ||||
-rw-r--r-- | app/resource_bundle_mac.mm | 93 | ||||
-rw-r--r-- | app/resource_bundle_posix.cc | 98 | ||||
-rw-r--r-- | app/resource_bundle_win.cc | 1 |
6 files changed, 141 insertions, 162 deletions
diff --git a/app/app.gyp b/app/app.gyp index 4ddfb8a..65a12d5 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -124,9 +124,10 @@ 'os_exchange_data.h', 'resource_bundle.cc', 'resource_bundle.h', - 'resource_bundle_win.cc', 'resource_bundle_linux.cc', 'resource_bundle_mac.mm', + 'resource_bundle_posix.cc', + 'resource_bundle_win.cc', 'slide_animation.cc', 'slide_animation.h', 'sql/connection.cc', diff --git a/app/resource_bundle.h b/app/resource_bundle.h index 5c3afb2..550fb72 100644 --- a/app/resource_bundle.h +++ b/app/resource_bundle.h @@ -163,6 +163,11 @@ class ResourceBundle { // Free skia_images_. void FreeImages(); +#if defined(USE_X11) + // Free gdkPixbufs_. + void FreeGdkPixBufs(); +#endif + // Try to load the main resources and the locale specific strings from an // external data module. void LoadResources(const std::wstring& pref_locale); @@ -170,9 +175,15 @@ class ResourceBundle { // Initialize all the gfx::Font members if they haven't yet been initialized. void LoadFontsIfNecessary(); +#if defined(USE_BASE_DATA_PACK) + // Returns the full pathname of the main resources file to load. May return + // an empty string if no main resources data files are found. + static FilePath GetResourcesFilePath(); +#endif + // Returns the full pathname of the locale file to load. May return an empty // string if no locale data files are found. - FilePath GetLocaleFilePath(const std::wstring& pref_locale); + static FilePath GetLocaleFilePath(const std::wstring& pref_locale); // Returns a handle to bytes from the resource |module|, without doing any // processing or interpretation of the resource. Returns whether we diff --git a/app/resource_bundle_linux.cc b/app/resource_bundle_linux.cc index ca23792..e8c53d3 100644 --- a/app/resource_bundle_linux.cc +++ b/app/resource_bundle_linux.cc @@ -56,97 +56,36 @@ GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { } // namespace -ResourceBundle::~ResourceBundle() { - FreeImages(); - // Free GdkPixbufs. +void ResourceBundle::FreeGdkPixBufs() { for (GdkPixbufMap::iterator i = gdk_pixbufs_.begin(); i != gdk_pixbufs_.end(); i++) { g_object_unref(i->second); } gdk_pixbufs_.clear(); - - delete locale_resources_data_; - locale_resources_data_ = NULL; - delete resources_data_; - resources_data_ = NULL; } -void ResourceBundle::LoadResources(const std::wstring& pref_locale) { - FilePath resources_data_path; - PathService::Get(base::DIR_EXE, &resources_data_path); - resources_data_path = resources_data_path.Append( - FILE_PATH_LITERAL("chrome.pak")); - DCHECK(resources_data_ == NULL) << "resource data already loaded!"; - resources_data_ = new base::DataPack; - bool success = resources_data_->Load(resources_data_path); - DCHECK(success) << "failed to load chrome.pak"; - - DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!"; - const FilePath& locale_path = GetLocaleFilePath(pref_locale); - if (locale_path.value().empty()) { - // It's possible that there are no locale dlls found, in which case we just - // return. - NOTREACHED(); - return; - } - - locale_resources_data_ = new base::DataPack; - success = locale_resources_data_->Load(locale_path); - DCHECK(success) << "failed to load locale pak file"; +// static +FilePath ResourceBundle::GetResourcesFilePath() { + FilePath resources_file_path; + PathService::Get(base::DIR_EXE, &resources_file_path); + if (resources_file_path.empty()) + return resources_file_path; + return resources_file_path.Append(FILE_PATH_LITERAL("chrome.pak")); } +// static FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { - FilePath locale_path; - PathService::Get(app::DIR_LOCALES, &locale_path); - + FilePath locale_file_path; + PathService::Get(app::DIR_LOCALES, &locale_file_path); + if (locale_file_path.empty()) + return locale_file_path; const std::string app_locale = l10n_util::GetApplicationLocale(pref_locale); if (app_locale.empty()) return FilePath(); - - return locale_path.AppendASCII(app_locale + ".pak"); -} - -// static -RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( - DataHandle module, int resource_id) { - DCHECK(module); - return module->GetStaticMemory(resource_id); -} - -base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { - DCHECK(resources_data_); - base::StringPiece data; - if (!resources_data_->GetStringPiece(resource_id, &data)) { - if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { - return base::StringPiece(); - } - } - return data; -} - -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_) { - 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. - string16 msg(reinterpret_cast<const char16*>(data.data()), - data.length() / 2); - return msg; + locale_file_path = locale_file_path.AppendASCII(app_locale + ".pak"); + if (!file_util::PathExists(locale_file_path)) + return FilePath(); + return locale_file_path; } GdkPixbuf* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { diff --git a/app/resource_bundle_mac.mm b/app/resource_bundle_mac.mm index 547c84b..94ef31a 100644 --- a/app/resource_bundle_mac.mm +++ b/app/resource_bundle_mac.mm @@ -6,104 +6,33 @@ #import <Foundation/Foundation.h> -#include "app/gfx/font.h" -#include "app/l10n_util.h" -#include "base/base_paths.h" -#include "base/data_pack.h" #include "base/file_path.h" -#include "base/file_util.h" #include "base/logging.h" #include "base/mac_util.h" -#include "base/path_service.h" -#include "base/string_piece.h" -#include "base/string_util.h" #include "skia/ext/skia_utils_mac.h" -ResourceBundle::~ResourceBundle() { - FreeImages(); - - delete locale_resources_data_; - locale_resources_data_ = NULL; - delete resources_data_; - resources_data_ = NULL; -} - namespace { -base::DataPack *LoadResourceDataPack(NSString *name) { - base::DataPack *resource_pack = NULL; - +FilePath GetResourcesPakFilePath(NSString* name) { NSString *resource_path = [mac_util::MainAppBundle() pathForResource:name ofType:@"pak"]; - if (resource_path) { - FilePath resources_pak_path([resource_path fileSystemRepresentation]); - resource_pack = new base::DataPack; - bool success = resource_pack->Load(resources_pak_path); - if (!success) { - delete resource_pack; - resource_pack = NULL; - } - } - - return resource_pack; + if (!resource_path) + return FilePath(); + return FilePath([resource_path fileSystemRepresentation]); } } // namespace -void ResourceBundle::LoadResources(const std::wstring& pref_locale) { - DLOG_IF(WARNING, pref_locale.size() != 0) - << "ignoring requested locale in favor of NSBundle's selection"; - - DCHECK(resources_data_ == NULL) << "resource data already loaded!"; - resources_data_ = LoadResourceDataPack(@"chrome"); - DCHECK(resources_data_) << "failed to load chrome.pak"; - - DCHECK(locale_resources_data_ == NULL) << "locale data already loaded!"; - locale_resources_data_ = LoadResourceDataPack(@"locale"); - DCHECK(locale_resources_data_) << "failed to load locale.pak"; -} - // static -RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( - DataHandle module, int resource_id) { - DCHECK(module); - return module->GetStaticMemory(resource_id); -} - -base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { - DCHECK(resources_data_); - base::StringPiece data; - if (!resources_data_->GetStringPiece(resource_id, &data)) { - if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { - return base::StringPiece(); - } - } - return data; +FilePath ResourceBundle::GetResourcesFilePath() { + return GetResourcesPakFilePath(@"chrome"); } -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_) { - 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. - string16 msg(reinterpret_cast<const char16*>(data.data()), - data.length() / 2); - return msg; +// static +FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { + DLOG_IF(WARNING, !pref_locale.empty()) + << "ignoring requested locale in favor of NSBundle's selection"; + return GetResourcesPakFilePath(@"locale"); } NSImage* ResourceBundle::GetNSImageNamed(int resource_id) { diff --git a/app/resource_bundle_posix.cc b/app/resource_bundle_posix.cc new file mode 100644 index 0000000..ec6893a --- /dev/null +++ b/app/resource_bundle_posix.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "app/resource_bundle.h" + +#include "app/gfx/font.h" +#include "base/data_pack.h" +#include "base/logging.h" +#include "base/string16.h" +#include "base/string_piece.h" + +namespace { + +base::DataPack* LoadResourcesDataPak(FilePath resources_pak_path) { + base::DataPack* resources_pak = new base::DataPack; + bool success = resources_pak->Load(resources_pak_path); + if (!success) { + delete resources_pak; + resources_pak = NULL; + } + return resources_pak; +} + +} // namespace + +ResourceBundle::~ResourceBundle() { + FreeImages(); +#if defined(OS_LINUX) + FreeGdkPixBufs(); +#endif + delete locale_resources_data_; + locale_resources_data_ = NULL; + delete resources_data_; + resources_data_ = NULL; +} + +// static +RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( + DataHandle module, int resource_id) { + DCHECK(module); + return module->GetStaticMemory(resource_id); +} + +base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) { + DCHECK(resources_data_); + base::StringPiece data; + if (!resources_data_->GetStringPiece(resource_id, &data)) { + if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { + return base::StringPiece(); + } + } + 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::LoadResources(const std::wstring& pref_locale) { + DCHECK(!resources_data_) << "chrome.pak already loaded"; + FilePath resources_file_path = GetResourcesFilePath(); + DCHECK(!resources_file_path.empty()) << "chrome.pak not found"; + resources_data_ = LoadResourcesDataPak(resources_file_path); + DCHECK(resources_data_) << "failed to load chrome.pak"; + + DCHECK(!locale_resources_data_) << "locale.pak already loaded"; + FilePath locale_file_path = GetLocaleFilePath(pref_locale); + if (locale_file_path.empty()) { + // It's possible that there is no locale.pak. + NOTREACHED(); + return; + } + locale_resources_data_ = LoadResourcesDataPak(locale_file_path); + DCHECK(locale_resources_data_) << "failed to load locale.pak"; +} diff --git a/app/resource_bundle_win.cc b/app/resource_bundle_win.cc index ec306ef..9a0f7eb 100644 --- a/app/resource_bundle_win.cc +++ b/app/resource_bundle_win.cc @@ -58,6 +58,7 @@ void ResourceBundle::LoadResources(const std::wstring& pref_locale) { "unable to load generated resources"; } +// static FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { FilePath locale_path; PathService::Get(app::DIR_LOCALES, &locale_path); |