diff options
Diffstat (limited to 'chrome/common')
-rw-r--r-- | chrome/common/common.scons | 2 | ||||
-rw-r--r-- | chrome/common/resource_bundle.cc | 45 | ||||
-rw-r--r-- | chrome/common/resource_bundle.h | 8 | ||||
-rw-r--r-- | chrome/common/resource_bundle_linux.cc | 92 | ||||
-rw-r--r-- | chrome/common/resource_bundle_win.cc | 26 |
5 files changed, 127 insertions, 46 deletions
diff --git a/chrome/common/common.scons b/chrome/common/common.scons index ae4c0bf..5ee52f0 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -178,6 +178,7 @@ input_files = ChromeFileList([ 'ref_counted_util.h', 'resource_bundle.cc', 'resource_bundle.h', + 'resource_bundle_linux.cc', 'resource_bundle_win.cc', 'resource_dispatcher.cc', 'resource_dispatcher.h', @@ -251,6 +252,7 @@ if not env.Bit('linux'): # Linux specific files input_files.Remove( 'chrome_paths_linux.cc', + 'resource_bundle_linux.cc', 'gfx/chrome_canvas_skia.cc', 'gfx/chrome_font_gtk.cc', 'gfx/chrome_font_skia.cc', diff --git a/chrome/common/resource_bundle.cc b/chrome/common/resource_bundle.cc index 1e2538e..785dd29 100644 --- a/chrome/common/resource_bundle.cc +++ b/chrome/common/resource_bundle.cc @@ -4,26 +4,20 @@ #include "chrome/common/resource_bundle.h" -#if defined(OS_WIN) -#include <atlbase.h> -#endif - #include "base/gfx/png_decoder.h" #include "base/logging.h" +#include "base/string_piece.h" #include "chrome/common/gfx/chrome_font.h" #include "SkBitmap.h" ResourceBundle* ResourceBundle::g_shared_instance_ = NULL; -// TODO(port): InitSharedInstance and CleanupSharedInstance are portable, but -// they need LoadLocaleResource and ~ResourceBundle implemented. -#if defined(OS_WIN) /* static */ void ResourceBundle::InitSharedInstance(const std::wstring& pref_locale) { DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; g_shared_instance_ = new ResourceBundle(); - g_shared_instance_->LoadLocaleResources(pref_locale); + g_shared_instance_->LoadResources(pref_locale); } /* static */ @@ -33,7 +27,6 @@ void ResourceBundle::CleanupSharedInstance() { g_shared_instance_ = NULL; } } -#endif /* static */ ResourceBundle& ResourceBundle::GetSharedInstance() { @@ -43,7 +36,8 @@ ResourceBundle& ResourceBundle::GetSharedInstance() { } ResourceBundle::ResourceBundle() - : locale_resources_data_(NULL), + : resources_data_(NULL), + locale_resources_data_(NULL), theme_data_(NULL) { } @@ -55,8 +49,6 @@ void ResourceBundle::FreeImages() { skia_images_.clear(); } -// TODO(port): LoadBitmap is portable, but it needs a LoadResourceBytes impl. -#if defined(OS_WIN) /* static */ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { std::vector<unsigned char> raw_data, png_data; @@ -77,7 +69,20 @@ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { image_width, image_height); } -#endif + +std::string ResourceBundle::GetDataResource(int resource_id) { + return GetRawDataResource(resource_id).as_string(); +} + +bool ResourceBundle::LoadImageResourceBytes(int resource_id, + std::vector<unsigned char>* bytes) { + return LoadResourceBytes(theme_data_, resource_id, bytes); +} + +bool ResourceBundle::LoadDataResourceBytes(int resource_id, + std::vector<unsigned char>* bytes) { + return LoadResourceBytes(resources_data_, resource_id, bytes); +} SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { // Check to see if we already have the Skia image in the cache. @@ -90,22 +95,12 @@ SkBitmap* ResourceBundle::GetBitmapNamed(int resource_id) { scoped_ptr<SkBitmap> bitmap; - if (theme_data_) { -#if defined(OS_WIN) - // TODO(port): We need to implement LoadBitmap before this can link. - // Try to load the bitmap from the theme data. + if (theme_data_) bitmap.reset(LoadBitmap(theme_data_, resource_id)); -#else - NOTIMPLEMENTED(); -#endif - } -#if defined(OS_WIN) - // TODO(port): refactor to remove this. // If we did not find the bitmap in the theme DLL, try the current one. if (!bitmap.get()) - bitmap.reset(LoadBitmap(_AtlBaseModule.GetModuleInstance(), resource_id)); -#endif + bitmap.reset(LoadBitmap(resources_data_, resource_id)); // We loaded successfully. Cache the Skia version of the bitmap. if (bitmap.get()) { diff --git a/chrome/common/resource_bundle.h b/chrome/common/resource_bundle.h index 7af3ac8..062a3a3 100644 --- a/chrome/common/resource_bundle.h +++ b/chrome/common/resource_bundle.h @@ -117,7 +117,7 @@ class ResourceBundle { // Windows stores resources in DLLs, which are managed by HINSTANCE. typedef HINSTANCE DataHandle; #elif defined(OS_LINUX) - // Linux will use base::DataPack. TODO(evanm): finish this. + // Linux uses base::DataPack. typedef base::DataPack* DataHandle; #endif @@ -128,8 +128,9 @@ class ResourceBundle { // Free skia_images_. void FreeImages(); - // Try to load the locale specific strings from an external data module. - void LoadLocaleResources(const std::wstring& pref_locale); + // Try to load the main resources and the locale specific strings from an + // external data module. + void LoadResources(const std::wstring& pref_locale); // Initialize all the ChromeFont members if they haven't yet been initialized. void LoadFontsIfNecessary(); @@ -155,6 +156,7 @@ class ResourceBundle { Lock lock_; // Handles for data sources. + DataHandle resources_data_; DataHandle locale_resources_data_; DataHandle theme_data_; diff --git a/chrome/common/resource_bundle_linux.cc b/chrome/common/resource_bundle_linux.cc new file mode 100644 index 0000000..1696402 --- /dev/null +++ b/chrome/common/resource_bundle_linux.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2006-2008 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 "chrome/common/resource_bundle.h" + +#include "base/data_pack.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "base/path_service.h" +#include "base/string_piece.h" +#include "base/string_util.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/gfx/chrome_font.h" +#include "chrome/common/l10n_util.h" + +ResourceBundle::~ResourceBundle() { + FreeImages(); + + delete locale_resources_data_; + locale_resources_data_ = NULL; + delete theme_data_; + theme_data_ = NULL; + delete resources_data_; + resources_data_ = NULL; +} + +void ResourceBundle::LoadResources(const std::wstring& pref_locale) { + // TODO(tc): Load the .pak files to locale_resources_data_ and + // resources_data_. + NOTIMPLEMENTED(); +} + +FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { + FilePath locale_path; + PathService::Get(chrome::DIR_LOCALES, &locale_path); + + const std::wstring app_locale = l10n_util::GetApplicationLocale(pref_locale); + if (app_locale.empty()) + return FilePath(); + + return locale_path.Append(WideToASCII(app_locale + L".pak")); +} + +void ResourceBundle::LoadThemeResources() { + // TODO(tc): Load the theme .pak file. + NOTIMPLEMENTED(); +} + +/* static */ +bool ResourceBundle::LoadResourceBytes(DataHandle module, int resource_id, + std::vector<unsigned char>* bytes) { + DCHECK(module); + StringPiece data; + if (!module->Get(resource_id, &data)) + return false; + + bytes->resize(data.length()); + memcpy(&(bytes->front()), data.data(), data.length()); + + return true; +} + +StringPiece ResourceBundle::GetRawDataResource(int resource_id) { + DCHECK(resources_data_); + StringPiece data; + if (!resources_data_->Get(resource_id, &data)) + return StringPiece(); + return data; +} + +std::wstring 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 std::wstring(); + } + + StringPiece data; + if (!locale_resources_data_->Get(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 std::wstring(); + } + } + // Copy into a wstring and return. + return UTF8ToWide(data.as_string()); +} diff --git a/chrome/common/resource_bundle_win.cc b/chrome/common/resource_bundle_win.cc index cebd780..377ba4e 100644 --- a/chrome/common/resource_bundle_win.cc +++ b/chrome/common/resource_bundle_win.cc @@ -42,7 +42,10 @@ ResourceBundle::~ResourceBundle() { } } -void ResourceBundle::LoadLocaleResources(const std::wstring& pref_locale) { +void ResourceBundle::LoadResources(const std::wstring& pref_locale) { + // As a convenience, set resources_data_ to the current module. + resources_data_ = _AtlBaseModule.GetModuleInstance(); + DCHECK(NULL == locale_resources_data_) << "locale dll already loaded"; const FilePath& locale_path = GetLocaleFilePath(pref_locale); if (locale_path.value().empty()) { @@ -64,7 +67,7 @@ FilePath ResourceBundle::GetLocaleFilePath(const std::wstring& pref_locale) { const std::wstring app_locale = l10n_util::GetApplicationLocale(pref_locale); if (app_locale.empty()) - return FilePath(app_locale); + return FilePath(); return locale_path.Append(app_locale + L".dll"); } @@ -81,17 +84,6 @@ void ResourceBundle::LoadThemeResources() { DCHECK(theme_data_ != NULL) << "unable to load " << theme_data_path; } -bool ResourceBundle::LoadImageResourceBytes(int resource_id, - std::vector<unsigned char>* bytes) { - return LoadResourceBytes(theme_data_, resource_id, bytes); -} - -bool ResourceBundle::LoadDataResourceBytes(int resource_id, - std::vector<unsigned char>* bytes) { - return LoadResourceBytes(_AtlBaseModule.GetModuleInstance(), - resource_id, bytes); -} - /* static */ bool ResourceBundle::LoadResourceBytes( DataHandle module, @@ -113,10 +105,6 @@ HICON ResourceBundle::LoadThemeIcon(int icon_id) { return ::LoadIcon(theme_data_, MAKEINTRESOURCE(icon_id)); } -std::string ResourceBundle::GetDataResource(int resource_id) { - return GetRawDataResource(resource_id).as_string(); -} - StringPiece ResourceBundle::GetRawDataResource(int resource_id) { void* data_ptr; size_t data_size; @@ -149,8 +137,10 @@ HCURSOR ResourceBundle::LoadCursor(int cursor_id) { std::wstring 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_) + if (!locale_resources_data_) { + LOG(WARNING) << "locale resources are not loaded"; return std::wstring(); + } DCHECK(IS_INTRESOURCE(message_id)); |