diff options
author | flackr@chromium.org <flackr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-17 21:05:40 +0000 |
---|---|---|
committer | flackr@chromium.org <flackr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-05-17 21:05:40 +0000 |
commit | bebfb2ed2025f1e0f99dbcf3aa9af48cc6e8ac0f (patch) | |
tree | 2858dc6858da0f4c70b7d4fa2d11ba02ca83fde6 /ui/base/resource | |
parent | c145c83565c8c019f97d0280d12c623352e1c58e (diff) | |
download | chromium_src-bebfb2ed2025f1e0f99dbcf3aa9af48cc6e8ac0f.zip chromium_src-bebfb2ed2025f1e0f99dbcf3aa9af48cc6e8ac0f.tar.gz chromium_src-bebfb2ed2025f1e0f99dbcf3aa9af48cc6e8ac0f.tar.bz2 |
Select theme resources from ResourceBundle at requested scale factor.
Return the best match for a requested scale factor when fetching raw image data from ResourceBundle.
TBR=aa,abodenha
BUG=123611
TEST=ResourceBundle.LoadImageResourceBytes
Review URL: https://chromiumcodereview.appspot.com/10387010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@137734 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/resource')
-rw-r--r-- | ui/base/resource/data_pack.cc | 9 | ||||
-rw-r--r-- | ui/base/resource/data_pack.h | 10 | ||||
-rw-r--r-- | ui/base/resource/data_pack_literal.cc | 21 | ||||
-rw-r--r-- | ui/base/resource/data_pack_unittest.cc | 9 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.cc | 58 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle.h | 33 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_android.cc | 6 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_aurax11.cc | 10 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_gtk.cc | 11 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_mac.mm | 12 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_unittest.cc | 82 | ||||
-rw-r--r-- | ui/base/resource/resource_bundle_win.cc | 12 | ||||
-rw-r--r-- | ui/base/resource/resource_data_dll_win.cc | 13 | ||||
-rw-r--r-- | ui/base/resource/resource_data_dll_win.h | 3 | ||||
-rw-r--r-- | ui/base/resource/resource_handle.cc | 12 | ||||
-rw-r--r-- | ui/base/resource/resource_handle.h | 10 |
16 files changed, 213 insertions, 98 deletions
diff --git a/ui/base/resource/data_pack.cc b/ui/base/resource/data_pack.cc index 0fa97a8..e1d8c14 100644 --- a/ui/base/resource/data_pack.cc +++ b/ui/base/resource/data_pack.cc @@ -61,7 +61,7 @@ enum LoadErrors { namespace ui { -DataPack::DataPack(float scale_factor) +DataPack::DataPack(ui::ScaleFactor scale_factor) : resource_count_(0), text_encoding_type_(BINARY), scale_factor_(scale_factor) { @@ -145,6 +145,11 @@ bool DataPack::Load(const FilePath& path) { return true; } +bool DataPack::HasResource(uint16 resource_id) const { + return !!bsearch(&resource_id, mmap_->data() + kHeaderLength, resource_count_, + sizeof(DataPackEntry), DataPackEntry::CompareById); +} + bool DataPack::GetStringPiece(uint16 resource_id, base::StringPiece* data) const { // It won't be hard to make this endian-agnostic, but it's not worth @@ -186,7 +191,7 @@ ResourceHandle::TextEncodingType DataPack::GetTextEncodingType() const { return text_encoding_type_; } -float DataPack::GetScaleFactor() const { +ui::ScaleFactor DataPack::GetScaleFactor() const { return scale_factor_; } diff --git a/ui/base/resource/data_pack.h b/ui/base/resource/data_pack.h index 0ac5efe..3c03253 100644 --- a/ui/base/resource/data_pack.h +++ b/ui/base/resource/data_pack.h @@ -15,8 +15,9 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "base/string_piece.h" -#include "ui/base/ui_export.h" +#include "ui/base/layout.h" #include "ui/base/resource/resource_handle.h" +#include "ui/base/ui_export.h" class FilePath; @@ -32,7 +33,7 @@ namespace ui { class UI_EXPORT DataPack : public ResourceHandle { public: - DataPack(float scale_factor); + DataPack(ui::ScaleFactor scale_factor); virtual ~DataPack(); // Load a pack file from |path|, returning false on error. @@ -47,12 +48,13 @@ class UI_EXPORT DataPack : public ResourceHandle { TextEncodingType textEncodingType); // ResourceHandle implementation: + virtual bool HasResource(uint16 resource_id) const OVERRIDE; virtual bool GetStringPiece(uint16 resource_id, base::StringPiece* data) const OVERRIDE; virtual base::RefCountedStaticMemory* GetStaticMemory( uint16 resource_id) const OVERRIDE; virtual TextEncodingType GetTextEncodingType() const OVERRIDE; - virtual float GetScaleFactor() const OVERRIDE; + virtual ui::ScaleFactor GetScaleFactor() const OVERRIDE; private: // The memory-mapped data. @@ -66,7 +68,7 @@ class UI_EXPORT DataPack : public ResourceHandle { // The scale of the image in this resource pack relative to images in the 1x // resource pak. - float scale_factor_; + ui::ScaleFactor scale_factor_; DISALLOW_COPY_AND_ASSIGN(DataPack); }; diff --git a/ui/base/resource/data_pack_literal.cc b/ui/base/resource/data_pack_literal.cc index 510d07a..05e610b 100644 --- a/ui/base/resource/data_pack_literal.cc +++ b/ui/base/resource/data_pack_literal.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. @@ -21,4 +21,23 @@ extern const char kSamplePakContents[] = { extern const size_t kSamplePakSize = sizeof(kSamplePakContents); +extern const char kSamplePakContents2x[] = { + 0x04, 0x00, 0x00, 0x00, // header(version + 0x01, 0x00, 0x00, 0x00, // no. entries + 0x01, // encoding) + 0x04, 0x00, 0x15, 0x00, 0x00, 0x00, // index entry 4 + 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, // extra entry for the size of last + 't', 'h', 'i', 's', ' ', 'i', 's', ' ', 'i', 'd', ' ', '4', ' ', '2', 'x' +}; + +extern const size_t kSamplePakSize2x = sizeof(kSamplePakContents2x); + +extern const char kEmptyPakContents[] = { + 0x04, 0x00, 0x00, 0x00, // header(version + 0x00, 0x00, 0x00, 0x00, // no. entries + 0x01 +}; + +extern const size_t kEmptyPakSize = sizeof(kEmptyPakContents); + } // namespace ui diff --git a/ui/base/resource/data_pack_unittest.cc b/ui/base/resource/data_pack_unittest.cc index 36be6fd..1ac2145 100644 --- a/ui/base/resource/data_pack_unittest.cc +++ b/ui/base/resource/data_pack_unittest.cc @@ -31,12 +31,14 @@ TEST(DataPackTest, Load) { static_cast<int>(kSamplePakSize)); // Load the file through the data pack API. - DataPack pack(ResourceHandle::kScaleFactor100x); + DataPack pack(SCALE_FACTOR_100P); ASSERT_TRUE(pack.Load(data_path)); base::StringPiece data; + ASSERT_TRUE(pack.HasResource(4)); ASSERT_TRUE(pack.GetStringPiece(4, &data)); EXPECT_EQ("this is id 4", data); + ASSERT_TRUE(pack.HasResource(6)); ASSERT_TRUE(pack.GetStringPiece(6, &data)); EXPECT_EQ("this is id 6", data); @@ -47,6 +49,7 @@ TEST(DataPackTest, Load) { EXPECT_EQ(0U, data.length()); // Try looking up an invalid key. + ASSERT_FALSE(pack.HasResource(140)); ASSERT_FALSE(pack.GetStringPiece(140, &data)); } @@ -63,7 +66,7 @@ TEST(DataPackTest, LoadFileWithTruncatedHeader) { data_path = data_path.Append(FILE_PATH_LITERAL( "ui/base/test/data/data_pack_unittest/truncated-header.pak")); - DataPack pack(ResourceHandle::kScaleFactor100x); + DataPack pack(SCALE_FACTOR_100P); ASSERT_FALSE(pack.Load(data_path)); } @@ -87,7 +90,7 @@ TEST_P(DataPackTest, Write) { ASSERT_TRUE(DataPack::WritePack(file, resources, GetParam())); // Now try to read the data back in. - DataPack pack(ResourceHandle::kScaleFactor100x); + DataPack pack(SCALE_FACTOR_100P); ASSERT_TRUE(pack.Load(file)); EXPECT_EQ(pack.GetTextEncodingType(), GetParam()); diff --git a/ui/base/resource/resource_bundle.cc b/ui/base/resource/resource_bundle.cc index c5fa973..bc77d53 100644 --- a/ui/base/resource/resource_bundle.cc +++ b/ui/base/resource/resource_bundle.cc @@ -19,6 +19,7 @@ #include "build/build_config.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/base/layout.h" #include "ui/base/resource/data_pack.h" #include "ui/base/ui_base_paths.h" #include "ui/base/ui_base_switches.h" @@ -60,7 +61,7 @@ void ResourceBundle::InitSharedInstanceWithPakFile(const FilePath& path) { DCHECK(g_shared_instance_ == NULL) << "ResourceBundle initialized twice"; g_shared_instance_ = new ResourceBundle(NULL); - g_shared_instance_->LoadTestResources(path); + g_shared_instance_->LoadTestResources(path, path); } // static @@ -87,7 +88,8 @@ bool ResourceBundle::LocaleDataPakExists(const std::string& locale) { return !GetLocaleFilePath(locale).empty(); } -void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { +void ResourceBundle::AddDataPack(const FilePath& path, + ScaleFactor scale_factor) { // Do not pass an empty |path| value to this method. If the absolute path is // unknown pass just the pack file name. DCHECK(!path.empty()); @@ -101,7 +103,7 @@ void ResourceBundle::AddDataPack(const FilePath& path, float scale_factor) { return; scoped_ptr<DataPack> data_pack( - new DataPack(ResourceHandle::kScaleFactor100x)); + new DataPack(scale_factor)); if (data_pack->Load(pack_path)) { data_packs_.push_back(data_pack.release()); } else { @@ -164,7 +166,7 @@ std::string ResourceBundle::LoadLocaleResources( } scoped_ptr<DataPack> data_pack( - new DataPack(ResourceHandle::kScaleFactor100x)); + new DataPack(SCALE_FACTOR_100P)); if (!data_pack->Load(locale_file_path)) { UMA_HISTOGRAM_ENUMERATION("ResourceBundle.LoadLocaleResourcesError", logging::GetLastSystemErrorCode(), 16000); @@ -176,16 +178,21 @@ std::string ResourceBundle::LoadLocaleResources( return app_locale; } -void ResourceBundle::LoadTestResources(const FilePath& path) { +void ResourceBundle::LoadTestResources(const FilePath& path, + const FilePath& locale_path) { // Use the given resource pak for both common and localized resources. scoped_ptr<DataPack> data_pack( - new DataPack(ResourceHandle::kScaleFactor100x)); - if (data_pack->Load(path)) + new DataPack(SCALE_FACTOR_100P)); + if (!path.empty() && data_pack->Load(path)) data_packs_.push_back(data_pack.release()); - data_pack.reset(new DataPack(ResourceHandle::kScaleFactor100x)); - if (data_pack->Load(path)) + data_pack.reset(new DataPack(ui::SCALE_FACTOR_NONE)); + if (!locale_path.empty() && data_pack->Load(locale_path)) { locale_resources_data_.reset(data_pack.release()); + } else { + locale_resources_data_.reset( + new DataPack(ui::SCALE_FACTOR_NONE)); + } } void ResourceBundle::UnloadLocaleResources() { @@ -270,30 +277,45 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id) { } base::RefCountedStaticMemory* ResourceBundle::LoadDataResourceBytes( - int resource_id) const { + int resource_id, + ScaleFactor scale_factor) const { base::RefCountedStaticMemory* bytes = NULL; if (delegate_) - bytes = delegate_->LoadDataResourceBytes(resource_id); + bytes = delegate_->LoadDataResourceBytes(resource_id, scale_factor); if (!bytes) { - for (size_t i = 0; i < data_packs_.size() && !bytes; ++i) - bytes = data_packs_[i]->GetStaticMemory(resource_id); + base::StringPiece data = GetRawDataResource(resource_id, scale_factor); + if (!data.empty()) { + bytes = new base::RefCountedStaticMemory( + reinterpret_cast<const unsigned char*>(data.data()), data.length()); + } } return bytes; } -base::StringPiece ResourceBundle::GetRawDataResource(int resource_id) const { +base::StringPiece ResourceBundle::GetRawDataResource( + int resource_id, + ScaleFactor scale_factor) const { base::StringPiece data; - if (delegate_ && delegate_->GetRawDataResource(resource_id, &data)) + if (delegate_ && + delegate_->GetRawDataResource(resource_id, scale_factor, &data)) return data; DCHECK(locale_resources_data_.get()); 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)) + if (scale_factor != ui::SCALE_FACTOR_100P) { + for (size_t i = 0; i < data_packs_.size(); i++) { + if (data_packs_[i]->GetScaleFactor() == scale_factor && + data_packs_[i]->GetStringPiece(resource_id, &data)) + return data; + } + } + for (size_t i = 0; i < data_packs_.size(); i++) { + if (data_packs_[i]->GetScaleFactor() == ui::SCALE_FACTOR_100P && + data_packs_[i]->GetStringPiece(resource_id, &data)) return data; } @@ -320,7 +342,7 @@ string16 ResourceBundle::GetLocalizedString(int message_id) { 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); + data = GetRawDataResource(message_id, ui::SCALE_FACTOR_NONE); if (data.empty()) { NOTREACHED() << "unable to find resource: " << message_id; return string16(); diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h index df25f31..62d8fd6 100644 --- a/ui/base/resource/resource_bundle.h +++ b/ui/base/resource/resource_bundle.h @@ -18,6 +18,7 @@ #include "base/memory/scoped_vector.h" #include "base/string16.h" #include "base/string_piece.h" +#include "ui/base/layout.h" #include "ui/base/ui_export.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" @@ -69,7 +70,7 @@ class UI_EXPORT ResourceBundle { // |pack_path| will contain the complete default path for the pack file if // known or just the pack file name otherwise. virtual FilePath GetPathForResourcePack(const FilePath& pack_path, - float scale_factor) = 0; + ScaleFactor scale_factor) = 0; // Called before a locale pack file is loaded. Return the full path for // the pack file to continue loading or an empty value to cancel loading. @@ -89,11 +90,13 @@ class UI_EXPORT ResourceBundle { // Return a static memory resource or NULL to attempt retrieval of the // default resource. virtual base::RefCountedStaticMemory* LoadDataResourceBytes( - int resource_id) = 0; + int resource_id, + ScaleFactor scale_factor) = 0; // Retrieve a raw data resource. Return true if a resource was provided or // false to attempt retrieval of the default resource. virtual bool GetRawDataResource(int resource_id, + ScaleFactor scale_factor, base::StringPiece* value) = 0; // Retrieve a localized string. Return true if a string was provided or @@ -137,7 +140,7 @@ class UI_EXPORT ResourceBundle { // this value). |scale_factor| is the scale of images in this resource pak // relative to the images in the 1x resource pak. This method is not thread // safe! You should call it immediately after calling InitSharedInstance. - void AddDataPack(const FilePath& path, float scale_factor); + void AddDataPack(const FilePath& path, ScaleFactor scale_factor); // Changes the locale for an already-initialized ResourceBundle, returning the // name of the newly-loaded locale. Future calls to get strings will return @@ -181,13 +184,19 @@ class UI_EXPORT ResourceBundle { // 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|, - // without doing any processing or interpretation of - // the resource. Returns whether we successfully read the resource. - base::RefCountedStaticMemory* LoadDataResourceBytes(int resource_id) const; + // Loads the raw bytes of a data resource nearest the scale factor + // |scale_factor| into |bytes|, without doing any processing or interpretation + // of the resource. Use ResourceHandle::SCALE_FACTOR_NONE for non-image + // resources. Returns NULL if we fail to read the resource. + base::RefCountedStaticMemory* LoadDataResourceBytes( + int resource_id, + ScaleFactor scale_factor) const; - // Return the contents of a resource in a StringPiece given the resource id. - base::StringPiece GetRawDataResource(int resource_id) const; + // Return the contents of a resource in a StringPiece given the resource id + // nearest the scale factor |scale_factor|. + // Use ResourceHanlde::SCALE_FACTOR_NONE for non-image resources. + base::StringPiece GetRawDataResource(int resource_id, + ScaleFactor scale_factor) const; // Get a localized string given a message id. Returns an empty // string if the message_id is not found. @@ -213,6 +222,7 @@ class UI_EXPORT ResourceBundle { FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource); FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString); FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont); + FRIEND_TEST_ALL_PREFIXES(ResourceBundle, GetRawDataResource); FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists); @@ -230,8 +240,9 @@ class UI_EXPORT ResourceBundle { // Returns the locale that is loaded. std::string LoadLocaleResources(const std::string& pref_locale); - // Load test resources in given path. - void LoadTestResources(const FilePath& path); + // Load test resources in given paths. If either path is empty an empty + // resource pack is loaded. + void LoadTestResources(const FilePath& path, const FilePath& locale_path); // Unload the locale specific strings and prepares to load new ones. See // comments for ReloadLocaleResources(). diff --git a/ui/base/resource/resource_bundle_android.cc b/ui/base/resource/resource_bundle_android.cc index d770d3e..b8d30d4 100644 --- a/ui/base/resource/resource_bundle_android.cc +++ b/ui/base/resource/resource_bundle_android.cc @@ -30,11 +30,11 @@ namespace ui { void ResourceBundle::LoadCommonResources() { AddDataPack(GetResourcesPakFilePath("chrome.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); } gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { diff --git a/ui/base/resource/resource_bundle_aurax11.cc b/ui/base/resource/resource_bundle_aurax11.cc index 6f63dff..535f455 100644 --- a/ui/base/resource/resource_bundle_aurax11.cc +++ b/ui/base/resource/resource_bundle_aurax11.cc @@ -28,18 +28,18 @@ namespace ui { void ResourceBundle::LoadCommonResources() { AddDataPack(GetResourcesPakFilePath("chrome.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) { AddDataPack(GetResourcesPakFilePath("theme_resources_touch_1x.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_touch.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); } else { AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); } } diff --git a/ui/base/resource/resource_bundle_gtk.cc b/ui/base/resource/resource_bundle_gtk.cc index 1c64e4d..d969bc0 100644 --- a/ui/base/resource/resource_bundle_gtk.cc +++ b/ui/base/resource/resource_bundle_gtk.cc @@ -8,10 +8,11 @@ #include "base/logging.h" #include "base/memory/ref_counted_memory.h" #include "base/path_service.h" -#include "ui/base/resource/resource_handle.h" #include "base/synchronization/lock.h" #include "third_party/skia/include/core/SkBitmap.h" #include "ui/base/gtk/scoped_gobject.h" +#include "ui/base/layout.h" +#include "ui/base/resource/resource_handle.h" #include "ui/base/ui_base_paths.h" #include "ui/gfx/image/image.h" @@ -65,11 +66,11 @@ FilePath GetResourcesPakFilePath(const std::string& pak_name) { void ResourceBundle::LoadCommonResources() { AddDataPack(GetResourcesPakFilePath("chrome.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); } gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { @@ -89,7 +90,7 @@ gfx::Image& ResourceBundle::GetNativeImageNamed(int resource_id, ImageRTL rtl) { if (image.IsEmpty()) { scoped_refptr<base::RefCountedStaticMemory> data( - LoadDataResourceBytes(resource_id)); + LoadDataResourceBytes(resource_id, SCALE_FACTOR_100P)); GdkPixbuf* pixbuf = LoadPixbuf(data.get(), rtl == RTL_ENABLED); if (!pixbuf) { diff --git a/ui/base/resource/resource_bundle_mac.mm b/ui/base/resource/resource_bundle_mac.mm index 8a559c4..07736d8 100644 --- a/ui/base/resource/resource_bundle_mac.mm +++ b/ui/base/resource/resource_bundle_mac.mm @@ -50,22 +50,22 @@ FilePath GetResourcesPakFilePath(NSString* name, NSString* mac_locale) { void ResourceBundle::LoadCommonResources() { AddDataPack(GetResourcesPakFilePath(@"chrome", nil), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard", nil), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard", nil), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); // On Windows and ChromeOS we load either the 1x resource or the 2x resource. // On Mac we load both and let the UI framework decide which one to use. #if defined(ENABLE_HIDPI) if (base::mac::IsOSLionOrLater()) { AddDataPack(GetResourcesPakFilePath(@"theme_resources_2x", nil), - ResourceHandle::kScaleFactor200x); + SCALE_FACTOR_200P); AddDataPack(GetResourcesPakFilePath(@"theme_resources_standard_2x", nil), - ResourceHandle::kScaleFactor200x); + SCALE_FACTOR_200P); AddDataPack(GetResourcesPakFilePath(@"ui_resources_standard_2x", nil), - ResourceHandle::kScaleFactor200x); + SCALE_FACTOR_200P); } #endif } diff --git a/ui/base/resource/resource_bundle_unittest.cc b/ui/base/resource/resource_bundle_unittest.cc index 1d72785..79a8ca9 100644 --- a/ui/base/resource/resource_bundle_unittest.cc +++ b/ui/base/resource/resource_bundle_unittest.cc @@ -14,6 +14,7 @@ #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "ui/base/layout.h" using ::testing::_; using ::testing::Between; @@ -25,6 +26,10 @@ namespace ui { extern const char kSamplePakContents[]; extern const size_t kSamplePakSize; +extern const char kSamplePakContents2x[]; +extern const size_t kSamplePakSize2x; +extern const char kEmptyPakContents[]; +extern const size_t kEmptyPakSize; namespace { @@ -37,19 +42,23 @@ class MockResourceBundleDelegate : public ui::ResourceBundle::Delegate { } MOCK_METHOD2(GetPathForResourcePack, FilePath(const FilePath& pack_path, - float scale_factor)); + ui::ScaleFactor scale_factor)); MOCK_METHOD2(GetPathForLocalePack, FilePath(const FilePath& pack_path, const std::string& locale)); MOCK_METHOD1(GetImageNamed, gfx::Image(int resource_id)); MOCK_METHOD2(GetNativeImageNamed, gfx::Image(int resource_id, ui::ResourceBundle::ImageRTL rtl)); - MOCK_METHOD1(LoadDataResourceBytes, - base::RefCountedStaticMemory*(int resource_id)); - MOCK_METHOD1(GetRawDataResourceMock, base::StringPiece(int resource_id)); + MOCK_METHOD2(LoadDataResourceBytes, + base::RefCountedStaticMemory*(int resource_id, + ui::ScaleFactor scale_factor)); + MOCK_METHOD2(GetRawDataResourceMock, base::StringPiece( + int resource_id, + ui::ScaleFactor scale_factor)); virtual bool GetRawDataResource(int resource_id, + ui::ScaleFactor scale_factor, base::StringPiece* value) OVERRIDE { - *value = GetRawDataResourceMock(resource_id); + *value = GetRawDataResourceMock(resource_id, scale_factor); return true; } MOCK_METHOD1(GetLocalizedStringMock, string16(int message_id)); @@ -71,7 +80,7 @@ TEST(ResourceBundle, DelegateGetPathForResourcePack) { ResourceBundle resource_bundle(&delegate); FilePath pack_path(FILE_PATH_LITERAL("/path/to/test_path.pak")); - double pack_scale_factor = 2.0; + ui::ScaleFactor pack_scale_factor = ui::SCALE_FACTOR_200P; EXPECT_CALL(delegate, GetPathForResourcePack( @@ -152,13 +161,14 @@ TEST(ResourceBundle, DelegateLoadDataResourceBytes) { new base::RefCountedStaticMemory(data, sizeof(data))); int resource_id = 5; + ui::ScaleFactor scale_factor = ui::SCALE_FACTOR_NONE; - EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id)) + EXPECT_CALL(delegate, LoadDataResourceBytes(resource_id, scale_factor)) .Times(1) .WillOnce(Return(static_memory)); scoped_refptr<base::RefCountedStaticMemory> result = - resource_bundle.LoadDataResourceBytes(resource_id); + resource_bundle.LoadDataResourceBytes(resource_id, scale_factor); EXPECT_EQ(static_memory, result); } @@ -172,11 +182,13 @@ TEST(ResourceBundle, DelegateGetRawDataResource) { int resource_id = 5; - EXPECT_CALL(delegate, GetRawDataResourceMock(resource_id)) + EXPECT_CALL(delegate, GetRawDataResourceMock( + resource_id, ui::SCALE_FACTOR_NONE)) .Times(1) .WillOnce(Return(string_piece)); - base::StringPiece result = resource_bundle.GetRawDataResource(resource_id); + base::StringPiece result = resource_bundle.GetRawDataResource( + resource_id, ui::SCALE_FACTOR_NONE); EXPECT_EQ(string_piece.data(), result.data()); } @@ -232,16 +244,58 @@ TEST(ResourceBundle, LoadDataResourceBytes) { static_cast<int>(kSamplePakSize)); // Create a resource bundle from the file. - resource_bundle.LoadTestResources(data_path); + resource_bundle.LoadTestResources(data_path, data_path); const int kUnfoundResourceId = 10000; - EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); + EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( + kUnfoundResourceId, ui::SCALE_FACTOR_NONE)); // Give a .pak file that doesn't exist so we will fail to load it. resource_bundle.AddDataPack( FilePath(FILE_PATH_LITERAL("non-existant-file.pak")), - 1.0); - EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes(kUnfoundResourceId)); + ui::SCALE_FACTOR_NONE); + EXPECT_EQ(NULL, resource_bundle.LoadDataResourceBytes( + kUnfoundResourceId, ui::SCALE_FACTOR_NONE)); + } +} + +TEST(ResourceBundle, GetRawDataResource) { + + // On Windows, the default data is compiled into the binary so this does + // nothing. + ScopedTempDir dir; + ASSERT_TRUE(dir.CreateUniqueTempDir()); + FilePath locale_path = dir.path().Append(FILE_PATH_LITERAL("empty.pak")); + FilePath data_path = dir.path().Append(FILE_PATH_LITERAL("sample.pak")); + FilePath data_2x_path = dir.path().Append(FILE_PATH_LITERAL("sample_2x.pak")); + + { + ResourceBundle resource_bundle(NULL); + // Dump contents into the pak files. + ASSERT_EQ(file_util::WriteFile(locale_path, kEmptyPakContents, + kEmptyPakSize), static_cast<int>(kEmptyPakSize)); + ASSERT_EQ(file_util::WriteFile(data_path, kSamplePakContents, + kSamplePakSize), static_cast<int>(kSamplePakSize)); + ASSERT_EQ(file_util::WriteFile(data_2x_path, kSamplePakContents2x, + kSamplePakSize2x), static_cast<int>(kSamplePakSize2x)); + + // Load the regular and 2x pak files. + resource_bundle.LoadTestResources(data_path, locale_path); + resource_bundle.AddDataPack(data_2x_path, SCALE_FACTOR_200P); + + // Resource ID 4 exists in both 1x and 2x paks, so we expect a different + // result when requesting the 2x scale. + EXPECT_EQ("this is id 4", resource_bundle.GetRawDataResource(4, + SCALE_FACTOR_100P)); + EXPECT_EQ("this is id 4 2x", resource_bundle.GetRawDataResource(4, + SCALE_FACTOR_200P)); + + // Resource ID 6 only exists in the 1x pak so we expect the same resource + // for both scale factor requests. + EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, + SCALE_FACTOR_100P)); + EXPECT_EQ("this is id 6", resource_bundle.GetRawDataResource(6, + SCALE_FACTOR_200P)); } } diff --git a/ui/base/resource/resource_bundle_win.cc b/ui/base/resource/resource_bundle_win.cc index e181e7d..4e525e7 100644 --- a/ui/base/resource/resource_bundle_win.cc +++ b/ui/base/resource/resource_bundle_win.cc @@ -49,21 +49,21 @@ void ResourceBundle::LoadCommonResources() { switch (ui::GetDisplayLayout()) { case ui::LAYOUT_TOUCH: AddDataPack(GetResourcesPakFilePath("theme_resources_touch_1x.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); break; default: if (use_hidpi) { AddDataPack(GetResourcesPakFilePath("theme_resources_2x.pak"), - ResourceHandle::kScaleFactor200x); + SCALE_FACTOR_200P); AddDataPack(GetResourcesPakFilePath("ui_resources_2x.pak"), - ResourceHandle::kScaleFactor200x); + SCALE_FACTOR_200P); } else { AddDataPack(GetResourcesPakFilePath("theme_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); AddDataPack(GetResourcesPakFilePath("ui_resources_standard.pak"), - ResourceHandle::kScaleFactor100x); + SCALE_FACTOR_100P); } break; } diff --git a/ui/base/resource/resource_data_dll_win.cc b/ui/base/resource/resource_data_dll_win.cc index 48e9db1..c6c90c71 100644 --- a/ui/base/resource/resource_data_dll_win.cc +++ b/ui/base/resource/resource_data_dll_win.cc @@ -17,6 +17,15 @@ ResourceDataDLL::ResourceDataDLL(HINSTANCE module) : module_(module) { ResourceDataDLL::~ResourceDataDLL() { } +bool ResourceDataDLL::HasResource(uint16 resource_id) const { + void* data_ptr; + size_t data_size; + return base::win::GetDataResourceFromModule(module_, + resource_id, + &data_ptr, + &data_size); +} + bool ResourceDataDLL::GetStringPiece(uint16 resource_id, base::StringPiece* data) const { DCHECK(data); @@ -48,8 +57,8 @@ ResourceHandle::TextEncodingType ResourceDataDLL::GetTextEncodingType() const { return BINARY; } -float ResourceDataDLL::GetScaleFactor() const { - return 1.0; +ScaleFactor ResourceDataDLL::GetScaleFactor() const { + return ui::SCALE_FACTOR_NONE; } } // namespace ui diff --git a/ui/base/resource/resource_data_dll_win.h b/ui/base/resource/resource_data_dll_win.h index e21e9aa..a38dd54 100644 --- a/ui/base/resource/resource_data_dll_win.h +++ b/ui/base/resource/resource_data_dll_win.h @@ -19,12 +19,13 @@ class ResourceDataDLL : public ResourceHandle { virtual ~ResourceDataDLL(); // ResourceHandle implementation: + virtual bool HasResource(uint16 resource_id) const OVERRIDE; virtual bool GetStringPiece(uint16 resource_id, base::StringPiece* data) const OVERRIDE; virtual base::RefCountedStaticMemory* GetStaticMemory( uint16 resource_id) const OVERRIDE; virtual TextEncodingType GetTextEncodingType() const OVERRIDE; - virtual float GetScaleFactor() const OVERRIDE; + virtual ScaleFactor GetScaleFactor() const OVERRIDE; private: const HINSTANCE module_; diff --git a/ui/base/resource/resource_handle.cc b/ui/base/resource/resource_handle.cc deleted file mode 100644 index 947ff80..0000000 --- a/ui/base/resource/resource_handle.cc +++ /dev/null @@ -1,12 +0,0 @@ -// 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_handle.h" - -namespace ui { - -const float ResourceHandle::kScaleFactor100x = 1.0; -const float ResourceHandle::kScaleFactor200x = 2.0; - -} // namespace ui diff --git a/ui/base/resource/resource_handle.h b/ui/base/resource/resource_handle.h index b40741e..1d26e72 100644 --- a/ui/base/resource/resource_handle.h +++ b/ui/base/resource/resource_handle.h @@ -8,6 +8,7 @@ #include "base/basictypes.h" #include "base/string_piece.h" +#include "ui/base/layout.h" #include "ui/base/ui_export.h" namespace base { @@ -25,12 +26,11 @@ class UI_EXPORT ResourceHandle { UTF16 }; - // The scale factors for image resources. - static const float kScaleFactor100x; - static const float kScaleFactor200x; - virtual ~ResourceHandle() {} + // Returns true if the DataPack contains a resource with id |resource_id|. + virtual bool HasResource(uint16 resource_id) const = 0; + // 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. @@ -47,7 +47,7 @@ class UI_EXPORT ResourceHandle { // The scale of images in this resource pack relative to images in the 1x // resource pak. - virtual float GetScaleFactor() const = 0; + virtual ScaleFactor GetScaleFactor() const = 0; }; } // namespace ui |