diff options
author | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 23:41:53 +0000 |
---|---|---|
committer | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-12 23:41:53 +0000 |
commit | ec015c8457dd6978ad0d86fd04738aa4a12867a4 (patch) | |
tree | ddd3873f745e968db94c1cc7b3dae16ab27545e6 /ui/base | |
parent | 7ad2d26cf0d1eb496f3372ad02ae21b93778fe78 (diff) | |
download | chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.zip chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.tar.gz chromium_src-ec015c8457dd6978ad0d86fd04738aa4a12867a4.tar.bz2 |
Metro/HiDPI: Cleanup ResourceBundle
For Metro and HiDPI support we need to conditionally load resource pak files. For example, on Windows I'd like to have something like this:
..ResourceBundle::LoadCommonResources() {
....if (IsMetroMode()) {
......switch (DPIScale()) {
........case 1.0: AddDataPack("theme_resources_metro_1_0x.pak"); break;
........case 1.4: AddDataPack("theme_resources_metro_1_4x.pak"); break;
........case 1.8: AddDataPack("theme_resources_metro_1_8x.pak"); break;
......}
....} else {
......if (DPIScale() < 1.5)
........AddDataPack("theme_resources_desktop_1_0x.pak");
......else
........AddDataPack("theme_resources_desktop_2_0x.pak");
....}
..}
As a first step this CL does the following:
- create a new ResourceHandle abstract class that can be backed by a DLL on Windows.
- manage all resource packs through the data_packs_ vector
- clean up ResourceBundle interface by removing all the #ifdefs
BUG=114311
TEST=Built and ran on Mac, Linux, Windows, Linux Arua.
Review URL: http://codereview.chromium.org/10051012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r-- | ui/base/resource/data_pack.cc | 6 | ||||
-rw-r--r-- | ui/base/resource/data_pack.h | 32 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 179 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 127 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_android.cc | 16 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_aurax11.cc | 7 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_gtk.cc | 26 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_linux.cc | 17 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_mac.mm | 86 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_posix.cc | 60 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_unittest.cc | 4 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_win.cc | 102 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_win.h | 27 | ||||
-rw-r--r-- | ui/base/resource/resource_data_dll_win.cc | 51 | ||||
-rw-r--r-- | ui/base/resource/resource_data_dll_win.h | 36 | ||||
-rw-r--r-- | ui/base/resource/resource_handle.h | 44 |
16 files changed, 367 insertions, 453 deletions
diff --git a/ui/base/resource/data_pack.cc b/ui/base/resource/data_pack.cc index be44afa..ccd062c 100644 --- a/ui/base/resource/data_pack.cc +++ b/ui/base/resource/data_pack.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -178,6 +178,10 @@ RefCountedStaticMemory* DataPack::GetStaticMemory(uint16 resource_id) const { reinterpret_cast<const unsigned char*>(piece.data()), piece.length()); } +ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { + return text_encoding_type_; +} + // static bool DataPack::WritePack(const FilePath& path, const std::map<uint16, base::StringPiece>& resources, diff --git a/ui/base/resource/data_pack.h b/ui/base/resource/data_pack.h index a8bca0e..11adf99 100644 --- a/ui/base/resource/data_pack.h +++ b/ui/base/resource/data_pack.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -16,6 +16,7 @@ #include "base/memory/scoped_ptr.h" #include "base/string_piece.h" #include "ui/base/ui_export.h" +#include "ui/base/resource/resource_handle.h" class FilePath; class RefCountedStaticMemory; @@ -26,31 +27,14 @@ class MemoryMappedFile; namespace ui { -class UI_EXPORT DataPack { +class UI_EXPORT DataPack : public ResourceHandle { public: - // What type of encoding the text resources use. - enum TextEncodingType { - BINARY, - UTF8, - UTF16 - }; - DataPack(); - ~DataPack(); + virtual ~DataPack(); // Load a pack file from |path|, returning false on error. bool Load(const FilePath& path); - // Get resource by id |resource_id|, filling in |data|. - // The data is owned by the DataPack object and should not be modified. - // Returns false if the resource id isn't found. - bool GetStringPiece(uint16 resource_id, base::StringPiece* data) const; - - // Like GetStringPiece(), but returns a reference to memory. This interface - // is used for image data, while the StringPiece interface is usually used - // for localization strings. - RefCountedStaticMemory* GetStaticMemory(uint16 resource_id) const; - // Writes a pack file containing |resources| to |path|. If there are any // text resources to be written, their encoding must already agree to the // |textEncodingType| specified. If no text resources are present, please @@ -59,8 +43,12 @@ class UI_EXPORT DataPack { const std::map<uint16, base::StringPiece>& resources, TextEncodingType textEncodingType); - // Get the encoding type of text resources. - TextEncodingType GetTextEncodingType() const { return text_encoding_type_; } + // ResourceHandle implementation: + virtual bool GetStringPiece(uint16 resource_id, + base::StringPiece* data) const OVERRIDE; + virtual RefCountedStaticMemory* GetStaticMemory( + uint16 resource_id) const OVERRIDE; + virtual TextEncodingType GetTextEncodingType() const OVERRIDE; private: // The memory-mapped data. diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index 5f44fa2..881bdd7 100644 --- a/ui/base/resource/resource_bundle.cc +++ b/ui/base/resource/resource_bundle.cc @@ -4,6 +4,8 @@ #include "ui/base/resource/resource_bundle.h" +#include <vector> + #include "base/command_line.h" #include "base/file_util.h" #include "base/logging.h" @@ -62,23 +64,6 @@ void ResourceBundle::InitSharedInstanceWithPakFile(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 -void ResourceBundle::AddDataPackToSharedInstance(const FilePath& path) { - DCHECK(g_shared_instance_ != NULL) << "ResourceBundle not initialized"; - g_shared_instance_->data_packs_.push_back(new LoadedDataPack(path)); -} - -// static void ResourceBundle::CleanupSharedInstance() { if (g_shared_instance_) { delete g_shared_instance_; @@ -103,6 +88,16 @@ bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { return !GetLocaleFilePath(locale).empty(); } +void ResourceBundle::AddDataPack(const FilePath& path) { + scoped_ptr<DataPack> data_pack(new DataPack()); + if (data_pack->Load(path)) { + data_packs_.push_back(data_pack.release()); + } else { + LOG(ERROR) << "Failed to load " << path.value() + << "\nSome features may not be available."; + } +} + #if !defined(OS_MACOSX) // static FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { @@ -138,18 +133,22 @@ std::string ResourceBundle::LoadLocaleResources( 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)); - if (!locale_resources_data_.get()) { + + scoped_ptr<DataPack> data_pack(new DataPack()); + if (!data_pack->Load(locale_file_path)) { UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", logging::GetLastSystemErrorCode(), 16000); NOTREACHED() << "failed to load locale.pak"; return std::string(); } + + locale_resources_data_.reset(data_pack.release()); return app_locale; } @@ -181,16 +180,17 @@ string16 ResourceBundle::GetLocalizedString(int message_id) { } // Strings should not be loaded from a data pack that contains binary data. - DCHECK(locale_resources_data_->GetTextEncodingType() == DataPack::UTF16 || - locale_resources_data_->GetTextEncodingType() == DataPack::UTF8) + 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 (locale_resources_data_->GetTextEncodingType() == DataPack::UTF16) { + if (encoding == ResourceHandle::UTF16) { msg = string16(reinterpret_cast<const char16*>(data.data()), data.length() / 2); - } else if (locale_resources_data_->GetTextEncodingType() == DataPack::UTF8) { + } else if (encoding == ResourceHandle::UTF8) { msg = UTF8ToUTF16(data); } return msg; @@ -225,49 +225,64 @@ gfx::Image& ResourceBundle::GetImageNamed(int resource_id) { return *found->second; } - DCHECK(resources_data_) << "Missing call to SetResourcesDataDLL?"; - scoped_ptr<SkBitmap> bitmap(LoadBitmap(resources_data_, resource_id)); - if (bitmap.get()) { - // Check if there's a large version of the image as well. - scoped_ptr<SkBitmap> large_bitmap; - if (large_icon_resources_data_) - large_bitmap.reset(LoadBitmap(large_icon_resources_data_, resource_id)); - - // The load was successful, so cache the image. - base::AutoLock lock_scope(*images_and_fonts_lock_); + 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); + } - // Another thread raced the load and has already cached the image. - if (images_.count(resource_id)) - return *images_[resource_id]; - - std::vector<const SkBitmap*> bitmaps; - bitmaps.push_back(bitmap.release()); - if (large_bitmap.get()) - bitmaps.push_back(large_bitmap.release()); - gfx::Image* image = new gfx::Image(bitmaps); - images_[resource_id] = image; - return *image; + 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(); } - // The load failed to retrieve the image; show a debugging red square. - LOG(WARNING) << "Unable to load image with id " << resource_id; - NOTREACHED(); // Want to assert in debug mode. - return *GetEmptyImage(); + // 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)) + 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; +} + +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { + return GetNativeImageNamed(resource_id, RTL_DISABLED); } RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( int resource_id) const { - RefCountedStaticMemory* bytes = - LoadResourceBytes(resources_data_, resource_id); - - // Check all our additional data packs for the resources if it wasn't loaded - // from our main source. - for (std::vector<LoadedDataPack*>::const_iterator it = data_packs_.begin(); - !bytes && it != data_packs_.end(); ++it) { - bytes = (*it)->GetStaticMemory(resource_id); + for (size_t i = 0; i < data_packs_.size(); ++i) { + RefCountedStaticMemory* bytes = + data_packs_[i]->GetStaticMemory(resource_id); + if (bytes) + return bytes; } - return bytes; + return NULL; +} + +base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { + DCHECK(locale_resources_data_.get()); + base::StringPiece data; + if (locale_resources_data_->GetStringPiece(resource_id, &data)) + return data; + + for (size_t i = 0; i < data_packs_.size(); ++i) { + if (data_packs_[i]->GetStringPiece(resource_id, &data)) + return data; + } + + return base::StringPiece(); } const gfx::Font& ResourceBundle::GetFont(FontStyle style) { @@ -301,9 +316,12 @@ void ResourceBundle::ReloadFonts() { ResourceBundle::ResourceBundle() : images_and_fonts_lock_(new base::Lock), - locale_resources_data_lock_(new base::Lock), - resources_data_(NULL), - large_icon_resources_data_(NULL) { + locale_resources_data_lock_(new base::Lock) { +} + +ResourceBundle::~ResourceBundle() { + FreeImages(); + UnloadLocaleResources(); } void ResourceBundle::FreeImages() { @@ -342,10 +360,10 @@ void ResourceBundle::LoadFontsIfNecessary() { } } -// static -SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) { +SkBitmap* ResourceBundle::LoadBitmap(const ResourceHandle& data_handle, + int resource_id) { scoped_refptr<RefCountedMemory> memory( - LoadResourceBytes(data_handle, resource_id)); + data_handle.GetStaticMemory(resource_id)); if (!memory) return NULL; @@ -379,39 +397,4 @@ gfx::Image* ResourceBundle::GetEmptyImage() { return empty_image; } -// LoadedDataPack ------------------------------------------------------------- - -ResourceBundle::LoadedDataPack::LoadedDataPack(const FilePath& path) - : path_(path) { - // Always preload the data packs so we can maintain constness. - Load(); -} - -ResourceBundle::LoadedDataPack::~LoadedDataPack() { -} - -void ResourceBundle::LoadedDataPack::Load() { - DCHECK(!data_pack_.get()); - data_pack_.reset(new ui::DataPack); - bool success = data_pack_->Load(path_); - LOG_IF(ERROR, !success) << "Failed to load " << path_.value() - << "\nSome features may not be available."; - if (!success) - data_pack_.reset(); -} - -bool ResourceBundle::LoadedDataPack::GetStringPiece( - int resource_id, base::StringPiece* data) const { - if (!data_pack_.get()) - return false; - return data_pack_->GetStringPiece(static_cast<uint32>(resource_id), data); -} - -RefCountedStaticMemory* ResourceBundle::LoadedDataPack::GetStaticMemory( - int resource_id) const { - if (!data_pack_.get()) - return NULL; - return data_pack_->GetStaticMemory(resource_id); -} - } // namespace ui diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index 668823b..e91cab1 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -8,19 +8,15 @@ #include "build/build_config.h" -#if defined(OS_WIN) -#include <windows.h> -#endif - #include <map> #include <string> -#include <vector> #include "base/basictypes.h" #include "base/file_path.h" #include "base/gtest_prod_util.h" #include "base/memory/ref_counted_memory.h" #include "base/memory/scoped_ptr.h" +#include "base/memory/scoped_vector.h" #include "base/string16.h" #include "base/string_piece.h" #include "ui/base/ui_export.h" @@ -37,17 +33,12 @@ class Font; class Image; } -#if defined(USE_X11) -typedef struct _GdkPixbuf GdkPixbuf; -#endif - namespace ui { -class DataPack; +class ResourceHandle; // ResourceBundle is a central facility to load images and other resources, -// such as theme graphics. -// Every resource is loaded only once. +// such as theme graphics. Every resource is loaded only once. class UI_EXPORT ResourceBundle { public: // An enumeration of the various font styles used throughout Chrome. @@ -65,6 +56,13 @@ class UI_EXPORT ResourceBundle { LargeBoldFont, }; + enum ImageRTL { + // Images are flipped in RTL locales. + RTL_ENABLED, + // Images are never flipped. + RTL_DISABLED, + }; + // Initialize the ResourceBundle for this process. Returns the language // selected. // NOTE: Mac ignores this and always loads up resources for the language @@ -75,16 +73,6 @@ class UI_EXPORT ResourceBundle { // Initialize the ResourceBundle using given data pack path for testing. static void InitSharedInstanceWithPakFile(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); - - // Registers additional data pack files with the global ResourceBundle. When - // looking for a DataResource, we will search these files after searching the - // main module. This method is not thread safe! You should call it - // immediately after calling InitSharedInstance. - static void AddDataPackToSharedInstance(const FilePath& path); - // Delete the ResourceBundle for this process if it exists. static void CleanupSharedInstance(); @@ -97,6 +85,12 @@ class UI_EXPORT ResourceBundle { // Check if the .pak for the given locale exists. static bool LocaleDataPakExists(const std::string& locale); + // Registers additional data pack files with the global ResourceBundle. When + // looking for a DataResource, we will search these files after searching the + // main module. This method is not thread safe! You should call it + // immediately after calling InitSharedInstance. + void AddDataPack(const FilePath& path); + // Changes the locale for an already-initialized ResourceBundle, returning the // name of the newly-loaded locale. Future calls to get strings will return // the strings for this new locale. This has no effect on existing or future @@ -123,6 +117,11 @@ class UI_EXPORT ResourceBundle { // Note that if the same resource has already been loaded in GetImageNamed(), // gfx::Image will perform a conversion, rather than using the native image // loading code of ResourceBundle. + // + // If |rtl| is RTL_ENABLED then the image is flipped in RTL locales. + gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl); + + // Same as GetNativeImageNamed() except that RTL is not enabled. gfx::Image& GetNativeImageNamed(int resource_id); // Loads the raw bytes of a data resource into |bytes|, @@ -148,68 +147,9 @@ class UI_EXPORT ResourceBundle { // loaded. Pass an empty path to undo. void OverrideLocalePakForTest(const FilePath& pak_path); -#if defined(OS_WIN) - // NOTE: This needs to be called before initializing the shared instance if - // your resources are not stored in the executable. - static void SetResourcesDataDLL(HINSTANCE handle); - - // Loads and returns an icon from the app module. - HICON LoadThemeIcon(int icon_id); - - // Loads and returns a cursor from the app module. - HCURSOR LoadCursor(int cursor_id); -#elif defined(TOOLKIT_GTK) - // Gets the GdkPixbuf with the specified resource_id from the main data pak - // file. Returns a pointer to a shared instance of the GdkPixbuf. This - // shared GdkPixbuf is owned by the resource bundle and should not be freed. - // - // The bitmap is assumed to exist. This function will log in release, and - // assert in debug mode if it does not. On failure, this will return a - // pointer to a shared empty placeholder bitmap so it will be visible what - // is missing. - // This function flips it in RTL locales. - GdkPixbuf* GetRTLEnabledPixbufNamed(int resource_id); - - // Same as above, but returns a gfx::Image wrapping the GdkPixbuf. - gfx::Image& GetRTLEnabledImageNamed(int resource_id); - - private: - // Shared implementation for the above two functions. - gfx::Image* GetPixbufImpl(int resource_id, bool rtl_enabled); - - public: -#endif - private: FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); - // Helper class for managing data packs. - class LoadedDataPack { - public: - explicit LoadedDataPack(const FilePath& path); - ~LoadedDataPack(); - bool GetStringPiece(int resource_id, base::StringPiece* data) const; - RefCountedStaticMemory* GetStaticMemory(int resource_id) const; - - private: - void Load(); - - scoped_ptr<DataPack> data_pack_; - FilePath path_; - - DISALLOW_COPY_AND_ASSIGN(LoadedDataPack); - }; - - // We define a DataHandle typedef to abstract across how data is stored - // across platforms. -#if defined(OS_WIN) - // Windows stores resources in DLLs, which are managed by HINSTANCE. - typedef HINSTANCE DataHandle; -#elif defined(OS_POSIX) - // Everyone else uses base::DataPack. - typedef DataPack* DataHandle; -#endif - // Ctor/dtor are private, since we're a singleton. ResourceBundle(); ~ResourceBundle(); @@ -234,29 +174,14 @@ class UI_EXPORT ResourceBundle { // Initialize all the gfx::Font members if they haven't yet been initialized. void LoadFontsIfNecessary(); -#if defined(OS_POSIX) - // 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(); - - static FilePath GetLargeIconResourcesFilePath(); -#endif - // Returns the full pathname of the locale file to load. May return an empty // string if no locale data files are found. static FilePath GetLocaleFilePath(const std::string& app_locale); - // Returns a handle to bytes from the resource |module|, without doing any - // processing or interpretation of the resource. Returns whether we - // successfully read the resource. Caller does not own the data returned - // through this method and must not modify the data pointed to by |bytes|. - static RefCountedStaticMemory* LoadResourceBytes(DataHandle module, - int resource_id); - // Creates and returns a new SkBitmap given the data file to look in and the // resource id. It's up to the caller to free the returned bitmap when // done. - static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); + SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); // Returns an empty image for when a resource cannot be loaded. This is a // bright red bitmap. @@ -271,12 +196,8 @@ class UI_EXPORT ResourceBundle { scoped_ptr<base::Lock> locale_resources_data_lock_; // Handles for data sources. - DataHandle resources_data_; - DataHandle large_icon_resources_data_; - scoped_ptr<DataPack> locale_resources_data_; - - // References to extra data packs loaded via AddDataPackToSharedInstance. - std::vector<LoadedDataPack*> data_packs_; + scoped_ptr<ResourceHandle> locale_resources_data_; + ScopedVector<ResourceHandle> data_packs_; // Cached images. The ResourceBundle caches all retrieved images and keeps // ownership of the pointers. diff --git a/ui/base/resource/resource_bundle_android.cc b/ui/base/resource/resource_bundle_android.cc index 14a81f3d..2959d5b 100644 --- a/ui/base/resource/resource_bundle_android.cc +++ b/ui/base/resource/resource_bundle_android.cc @@ -14,22 +14,18 @@ namespace ui { -// static -FilePath ResourceBundle::GetResourcesFilePath() { +void ResourceBundle::LoadCommonResources() { FilePath data_path; PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); DCHECK(!data_path.empty()); - return data_path.Append(FILE_PATH_LITERAL("paks/chrome.pak")); + data_path = data_path.AppendASCII("paks").AppendASCII("chrome.pak"); + AddDataPack(data_path); } -gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { + // Flipped image is not used on Android. + DCHECK_EQ(rtl, RTL_DISABLED); return GetImageNamed(resource_id); } -// static -FilePath ResourceBundle::GetLargeIconResourcesFilePath() { - // Not supported. - return FilePath(); -} - } diff --git a/ui/base/resource/resource_bundle_aurax11.cc b/ui/base/resource/resource_bundle_aurax11.cc index 8c082fa..fae2add 100644 --- a/ui/base/resource/resource_bundle_aurax11.cc +++ b/ui/base/resource/resource_bundle_aurax11.cc @@ -1,14 +1,17 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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 "ui/base/resource/resource_bundle.h" +#include "base/logging.h" #include "ui/gfx/image/image.h" namespace ui { -gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { + // Flipped image is not used on ChromeOS. + DCHECK_EQ(rtl, RTL_DISABLED); return GetImageNamed(resource_id); } diff --git a/ui/base/resource/resource_bundle_gtk.cc b/ui/base/resource/resource_bundle_gtk.cc index 7442a74..f397307 100644 --- a/ui/base/resource/resource_bundle_gtk.cc +++ b/ui/base/resource/resource_bundle_gtk.cc @@ -50,25 +50,21 @@ GdkPixbuf* LoadPixbuf(RefCountedStaticMemory* data, bool rtl_enabled) { } // namespace -gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { - return *GetPixbufImpl(resource_id, false); -} - -gfx::Image* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { // Use the negative |resource_id| for the key for BIDI-aware images. - int key = rtl_enabled ? -resource_id : resource_id; + int key = rtl == RTL_ENABLED ? -resource_id : resource_id; // Check to see if the image is already in the cache. { base::AutoLock lock_scope(*images_and_fonts_lock_); ImageMap::const_iterator found = images_.find(key); if (found != images_.end()) - return found->second; + return *found->second; } scoped_refptr<RefCountedStaticMemory> data( LoadDataResourceBytes(resource_id)); - GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl_enabled); + GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED); // The load was successful, so cache the image. if (pixbuf) { @@ -77,25 +73,17 @@ gfx::Image* ResourceBundle::GetPixbufImpl(int resource_id, bool rtl_enabled) { // Another thread raced the load and has already cached the image. if (images_.count(key)) { g_object_unref(pixbuf); - return images_[key]; + return *images_[key]; } gfx::Image* image = new gfx::Image(pixbuf); // Takes ownership. images_[key] = image; - return image; + return *image; } LOG(WARNING) << "Unable to pixbuf with id " << resource_id; NOTREACHED(); // Want to assert in debug mode. - return GetEmptyImage(); -} - -GdkPixbuf* ResourceBundle::GetRTLEnabledPixbufNamed(int resource_id) { - return GetPixbufImpl(resource_id, true)->ToGdkPixbuf(); -} - -gfx::Image& ResourceBundle::GetRTLEnabledImageNamed(int resource_id) { - return *GetPixbufImpl(resource_id, true); + return *GetEmptyImage(); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_linux.cc b/ui/base/resource/resource_bundle_linux.cc index f1f1b9e..ec6678d 100644 --- a/ui/base/resource/resource_bundle_linux.cc +++ b/ui/base/resource/resource_bundle_linux.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -9,17 +9,10 @@ namespace ui { -// static -FilePath ResourceBundle::GetResourcesFilePath() { - FilePath resources_file_path; - PathService::Get(ui::FILE_RESOURCES_PAK, &resources_file_path); - return resources_file_path; -} - -// static -FilePath ResourceBundle::GetLargeIconResourcesFilePath() { - // Not supported. - return FilePath(); +void ResourceBundle::LoadCommonResources() { + FilePath path; + PathService::Get(ui::FILE_RESOURCES_PAK, &path); + AddDataPack(path); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_mac.mm b/ui/base/resource/resource_bundle_mac.mm index dd890d0..5ace3de 100644 --- a/ui/base/resource/resource_bundle_mac.mm +++ b/ui/base/resource/resource_bundle_mac.mm @@ -13,6 +13,7 @@ #include "base/memory/scoped_nsobject.h" #include "base/synchronization/lock.h" #include "base/sys_string_conversions.h" +#include "ui/base/resource/resource_handle.h" #include "ui/gfx/image/image.h" namespace ui { @@ -41,18 +42,10 @@ FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { } // namespace -// static -FilePath ResourceBundle::GetResourcesFilePath() { - return GetResourcesPakFilePath(@"chrome", nil); -} - -// static -FilePath ResourceBundle::GetLargeIconResourcesFilePath() { - // Only load the large resource pak when running on 10.7 or later. +void ResourceBundle::LoadCommonResources() { + AddDataPack(GetResourcesPakFilePath(@"chrome", nil)); if (base::mac::IsOSLionOrLater()) - return GetResourcesPakFilePath(@"theme_resources_2x", nil); - else - return FilePath(); + AddDataPack(GetResourcesPakFilePath(@"theme_resources_2x", nil)); } // static @@ -70,7 +63,10 @@ FilePath ResourceBundle::GetLocaleFilePath(const std::string& app_locale) { return GetResourcesPakFilePath(@"locale", mac_locale); } -gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { + // Flipped images are not used on Mac. + DCHECK_EQ(rtl, RTL_DISABLED); + // Check to see if the image is already in the cache. { base::AutoLock lock(*images_and_fonts_lock_); @@ -86,49 +82,41 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { } } - // Load the raw data from the resource pack. - scoped_refptr<RefCountedStaticMemory> data( - LoadDataResourceBytes(resource_id)); - - // Create a data object from the raw bytes. - scoped_nsobject<NSData> ns_data([[NSData alloc] initWithBytes:data->front() - length:data->size()]); - - // Create the image from the data. The gfx::Image will take ownership of this. - scoped_nsobject<NSImage> ns_image([[NSImage alloc] initWithData:ns_data]); - - // Cache the converted image. - if (ns_image.get()) { - // Load a high resolution version of the icon if available. - if (large_icon_resources_data_) { - scoped_refptr<RefCountedStaticMemory> large_data( - LoadResourceBytes(large_icon_resources_data_, resource_id)); - if (large_data.get()) { - scoped_nsobject<NSData> ns_large_data( - [[NSData alloc] initWithBytes:large_data->front() - length:large_data->size()]); - NSImageRep* image_rep = - [NSBitmapImageRep imageRepWithData:ns_large_data]; - if (image_rep) - [ns_image addRepresentation:image_rep]; - } + scoped_nsobject<NSImage> ns_image; + for (size_t i = 0; i < data_packs_.size(); ++i) { + scoped_refptr<RefCountedStaticMemory> data( + data_packs_[i]->GetStaticMemory(resource_id)); + if (!data.get()) + continue; + + scoped_nsobject<NSData> ns_data( + [[NSData alloc] initWithBytes:data->front() + length:data->size()]); + if (!ns_image.get()) { + ns_image.reset([[NSImage alloc] initWithData:ns_data]); + } else { + NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; + if (image_rep) + [ns_image addRepresentation:image_rep]; } + } - base::AutoLock lock(*images_and_fonts_lock_); + if (!ns_image.get()) { + LOG(WARNING) << "Unable to load image with id " << resource_id; + NOTREACHED(); // Want to assert in debug mode. + return *GetEmptyImage(); + } - // Another thread raced the load and has already cached the image. - if (images_.count(resource_id)) { - return *images_[resource_id]; - } + base::AutoLock lock(*images_and_fonts_lock_); - gfx::Image* image = new gfx::Image(ns_image.release()); - images_[resource_id] = image; - return *image; + // Another thread raced the load and has already cached the image. + if (images_.count(resource_id)) { + return *images_[resource_id]; } - LOG(WARNING) << "Unable to load image with id " << resource_id; - NOTREACHED(); // Want to assert in debug mode. - return *GetEmptyImage(); + gfx::Image* image = new gfx::Image(ns_image.release()); + images_[resource_id] = image; + return *image; } } // namespace ui diff --git a/ui/base/resource/resource_bundle_posix.cc b/ui/base/resource/resource_bundle_posix.cc index 184f7b2..1e919d6 100644 --- a/ui/base/resource/resource_bundle_posix.cc +++ b/ui/base/resource/resource_bundle_posix.cc @@ -6,7 +6,6 @@ #include "base/file_path.h" #include "base/logging.h" -#include "base/stl_util.h" #include "base/string16.h" #include "base/string_piece.h" #include "base/synchronization/lock.h" @@ -16,60 +15,15 @@ namespace ui { -ResourceBundle::~ResourceBundle() { - FreeImages(); - UnloadLocaleResources(); - STLDeleteContainerPointers(data_packs_.begin(), - data_packs_.end()); - 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) const { - DCHECK(resources_data_); - base::StringPiece data; - if (!resources_data_->GetStringPiece(resource_id, &data)) { - if (!locale_resources_data_->GetStringPiece(resource_id, &data)) { - for (size_t i = 0; i < data_packs_.size(); ++i) { - if (data_packs_[i]->GetStringPiece(resource_id, &data)) - return data; - } - - return base::StringPiece(); - } - } - return data; -} - -void ResourceBundle::LoadCommonResources() { - DCHECK(!resources_data_) << "chrome.pak already loaded"; - FilePath resources_file_path = GetResourcesFilePath(); - CHECK(!resources_file_path.empty()) << "chrome.pak not found"; - resources_data_ = LoadResourcesDataPak(resources_file_path); - CHECK(resources_data_) << "failed to load chrome.pak"; - - FilePath large_icon_resources_file_path = GetLargeIconResourcesFilePath(); - if (!large_icon_resources_file_path.empty()) { - large_icon_resources_data_ = - LoadResourcesDataPak(large_icon_resources_file_path); - CHECK(large_icon_resources_data_) << - "failed to load theme_resources_2x.pak"; - } -} - 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_.reset(LoadResourcesDataPak(path)); + scoped_ptr<DataPack> data_pack(new DataPack()); + if (data_pack->Load(path)) + data_packs_.push_back(data_pack.release()); + + data_pack.reset(new DataPack()); + if (data_pack->Load(path)) + locale_resources_data_.reset(data_pack.release()); } } // namespace ui diff --git a/ui/base/resource/resource_bundle_unittest.cc b/ui/base/resource/resource_bundle_unittest.cc index 83a53d5..f98d8a7 100644 --- a/ui/base/resource/resource_bundle_unittest.cc +++ b/ui/base/resource/resource_bundle_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -39,7 +39,7 @@ TEST(ResourceBundle, LoadDataResourceBytes) { EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); // Give a .pak file that doesn't exist so we will fail to load it. - resource_bundle.AddDataPackToSharedInstance(FilePath( + resource_bundle.AddDataPack(FilePath( FILE_PATH_LITERAL("non-existant-file.pak"))); EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); } diff --git a/ui/base/resource/resource_bundle_win.cc b/ui/base/resource/resource_bundle_win.cc index 3efdd3b..b295b50 100644 --- a/ui/base/resource/resource_bundle_win.cc +++ b/ui/base/resource/resource_bundle_win.cc @@ -1,19 +1,12 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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 "ui/base/resource/resource_bundle.h" +#include "ui/base/resource/resource_bundle_win.h" -#include "base/debug/stack_trace.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 "base/win/resource_util.h" -#include "base/win/windows_version.h" -#include "ui/base/resource/data_pack.h" -#include "ui/gfx/font.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/base/resource/resource_data_dll_win.h" namespace ui { @@ -21,33 +14,17 @@ namespace { HINSTANCE resources_data_dll; -// Returns the flags that should be passed to LoadLibraryEx. -DWORD GetDataDllLoadFlags() { - if (base::win::GetVersion() >= base::win::VERSION_VISTA) - return LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE | LOAD_LIBRARY_AS_IMAGE_RESOURCE; - - return DONT_RESOLVE_DLL_REFERENCES; +HINSTANCE GetCurrentResourceDLL() { + if (resources_data_dll) + return resources_data_dll; + return GetModuleHandle(NULL); } } // end anonymous namespace -ResourceBundle::~ResourceBundle() { - FreeImages(); - UnloadLocaleResources(); - STLDeleteContainerPointers(data_packs_.begin(), - data_packs_.end()); - resources_data_ = NULL; -} - void ResourceBundle::LoadCommonResources() { - // As a convenience, set resources_data_ to the current resource module. - DCHECK(NULL == resources_data_) << "common resources already loaded"; - - if (resources_data_dll) { - resources_data_ = resources_data_dll; - } else { - resources_data_ = GetModuleHandle(NULL); - } + // As a convenience, add the current resource module as a data packs. + data_packs_.push_back(new ResourceDataDLL(GetCurrentResourceDLL())); } void ResourceBundle::LoadTestResources(const FilePath& path) { @@ -55,60 +32,21 @@ void ResourceBundle::LoadTestResources(const FilePath& path) { // itself. } -// static -RefCountedStaticMemory* ResourceBundle::LoadResourceBytes( - DataHandle module, int resource_id) { - void* data_ptr; - size_t data_size; - if (base::win::GetDataResourceFromModule(module, resource_id, &data_ptr, - &data_size)) { - return new RefCountedStaticMemory( - reinterpret_cast<const unsigned char*>(data_ptr), data_size); - } else { - return NULL; - } -} +gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { + // Flipped image is not used on Windows. + DCHECK_EQ(rtl, RTL_DISABLED); -// static -void ResourceBundle::SetResourcesDataDLL(HINSTANCE handle) { - resources_data_dll = handle; -} - -HICON ResourceBundle::LoadThemeIcon(int icon_id) { - return ::LoadIcon(resources_data_, MAKEINTRESOURCE(icon_id)); -} - -base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { - void* data_ptr; - size_t data_size; - base::StringPiece data; - if (base::win::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_.get() && - locale_resources_data_->GetStringPiece(resource_id, &data)) { - return data; - } - - for (size_t i = 0; i < data_packs_.size(); ++i) { - if (data_packs_[i]->GetStringPiece(resource_id, &data)) - return data; - } - - return base::StringPiece(); + // Windows only uses SkBitmap for gfx::Image, so this is the same as + // GetImageNamed. + return GetImageNamed(resource_id); } -// Loads and returns a cursor from the current module. -HCURSOR ResourceBundle::LoadCursor(int cursor_id) { - return ::LoadCursor(resources_data_, MAKEINTRESOURCE(cursor_id)); +void SetResourcesDataDLL(HINSTANCE handle) { + resources_data_dll = handle; } -// Windows only uses SkBitmap for gfx::Image, so this is the same as -// GetImageNamed. -gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { - return GetImageNamed(resource_id); +HICON LoadThemeIconFromResourcesDataDLL(int icon_id) { + return ::LoadIcon(GetCurrentResourceDLL(), MAKEINTRESOURCE(icon_id)); } } // namespace ui; diff --git a/ui/base/resource/resource_bundle_win.h b/ui/base/resource/resource_bundle_win.h new file mode 100644 index 0000000..207b90c --- /dev/null +++ b/ui/base/resource/resource_bundle_win.h @@ -0,0 +1,27 @@ +// Copyright (c) 2012 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. + +#ifndef UI_BASE_RESOURCE_RESOURCE_BUNDLE_WIN_ +#define UI_BASE_RESOURCE_RESOURCE_BUNDLE_WIN_ +#pragma once + +#include "build/build_config.h" + +#include <windows.h> + +#include "base/basictypes.h" +#include "ui/base/ui_export.h" + +namespace ui { + +// NOTE: This needs to be called before initializing ResourceBundle if your +// resources are not stored in the executable. +UI_EXPORT void SetResourcesDataDLL(HINSTANCE handle); + +// Loads and returns an icon from the app module. +UI_EXPORT HICON LoadThemeIconFromResourcesDataDLL(int icon_id); + +} // namespace ui + +#endif // UI_BASE_RESOURCE_RESOURCE_DATA_DLL_WIN_H_ diff --git a/ui/base/resource/resource_data_dll_win.cc b/ui/base/resource/resource_data_dll_win.cc new file mode 100644 index 0000000..16f6a25 --- /dev/null +++ b/ui/base/resource/resource_data_dll_win.cc @@ -0,0 +1,51 @@ +// Copyright (c) 2012 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 "ui/base/resource/resource_data_dll_win.h" + +#include "base/logging.h" +#include "base/memory/ref_counted_memory.h" +#include "base/win/resource_util.h" + +namespace ui { + +ResourceDataDLL::ResourceDataDLL(HINSTANCE module) : module_(module) { + DCHECK(module_); +} + +ResourceDataDLL::~ResourceDataDLL() { +} + +bool ResourceDataDLL::GetStringPiece(uint16 resource_id, + base::StringPiece* data) const { + DCHECK(data); + void* data_ptr; + size_t data_size; + if (base::win::GetDataResourceFromModule(module_, + resource_id, + &data_ptr, + &data_size)) { + data->set(static_cast<const char*>(data_ptr), data_size); + return true; + } + return false; +} + +RefCountedStaticMemory* ResourceDataDLL::GetStaticMemory( + uint16 resource_id) const { + void* data_ptr; + size_t data_size; + if (base::win::GetDataResourceFromModule(module_, resource_id, &data_ptr, + &data_size)) { + return new RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(data_ptr), data_size); + } + return NULL; +} + +ResourceHandle::TextEncodingType ResourceDataDLL::GetTextEncodingType() const { + return BINARY; +} + +} // namespace ui diff --git a/ui/base/resource/resource_data_dll_win.h b/ui/base/resource/resource_data_dll_win.h new file mode 100644 index 0000000..e7b3cf1 --- /dev/null +++ b/ui/base/resource/resource_data_dll_win.h @@ -0,0 +1,36 @@ +// Copyright (c) 2012 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. + +#ifndef UI_BASE_RESOURCE_RESOURCE_DATA_DLL_WIN_H_ +#define UI_BASE_RESOURCE_RESOURCE_DATA_DLL_WIN_H_ +#pragma once + +#include <windows.h> + +#include "base/compiler_specific.h" +#include "ui/base/resource/resource_handle.h" + +namespace ui { + +class ResourceDataDLL : public ResourceHandle { + public: + explicit ResourceDataDLL(HINSTANCE module); + virtual ~ResourceDataDLL(); + + // ResourceHandle implementation: + virtual bool GetStringPiece(uint16 resource_id, + base::StringPiece* data) const OVERRIDE; + virtual RefCountedStaticMemory* GetStaticMemory( + uint16 resource_id) const OVERRIDE; + virtual TextEncodingType GetTextEncodingType() const OVERRIDE; + + private: + const HINSTANCE module_; + + DISALLOW_COPY_AND_ASSIGN(ResourceDataDLL); +}; + +} // namespace ui + +#endif // UI_BASE_RESOURCE_RESOURCE_DATA_DLL_WIN_H_ diff --git a/ui/base/resource/resource_handle.h b/ui/base/resource/resource_handle.h new file mode 100644 index 0000000..45c1969 --- /dev/null +++ b/ui/base/resource/resource_handle.h @@ -0,0 +1,44 @@ +// Copyright (c) 2012 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. + +#ifndef UI_BASE_RESOURCE_RESOURCE_HANDLE_H_ +#define UI_BASE_RESOURCE_RESOURCE_HANDLE_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/string_piece.h" +#include "ui/base/ui_export.h" + +class RefCountedStaticMemory; + +namespace ui { + +class UI_EXPORT ResourceHandle { + public: + // What type of encoding the text resources use. + enum TextEncodingType { + BINARY, + UTF8, + UTF16 + }; + + virtual ~ResourceHandle() {} + + // Get resource by id |resource_id|, filling in |data|. + // The data is owned by the DataPack object and should not be modified. + // Returns false if the resource id isn't found. + virtual bool GetStringPiece(uint16 resource_id, + base::StringPiece* data) const = 0; + + // Like GetStringPiece(), but returns a reference to memory. + // Caller owns the returned object. + virtual RefCountedStaticMemory* GetStaticMemory(uint16 resource_id) const = 0; + + // Get the encoding type of text resources. + virtual TextEncodingType GetTextEncodingType() const = 0; +}; + +} // namespace ui + +#endif // UI_BASE_RESOURCE_RESOURCE_HANDLE_H_ |