summaryrefslogtreecommitdiffstats
path: root/ui/base/resource/resource_bundle.h
diff options
context:
space:
mode:
authormarshall@chromium.org <marshall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 15:57:47 +0000
committermarshall@chromium.org <marshall@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 15:57:47 +0000
commitfb317a5d27a76cb0cc21c2eb8312fbfaabd811c6 (patch)
treefd0fe106112e04a0250ba7554d39d005d17e2e2c /ui/base/resource/resource_bundle.h
parent91bea1dd2206ae7cd506eb4e40ba7445852d6cf7 (diff)
downloadchromium_src-fb317a5d27a76cb0cc21c2eb8312fbfaabd811c6.zip
chromium_src-fb317a5d27a76cb0cc21c2eb8312fbfaabd811c6.tar.gz
chromium_src-fb317a5d27a76cb0cc21c2eb8312fbfaabd811c6.tar.bz2
Add new ResourceBundle::Delegate interface.
BUG=125351 TEST=ResourceBundle.* Review URL: https://chromiumcodereview.appspot.com/10270023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136039 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/resource/resource_bundle.h')
-rw-r--r--ui/base/resource/resource_bundle.h92
1 files changed, 77 insertions, 15 deletions
diff --git a/ui/base/resource/resource_bundle.h b/ui/base/resource/resource_bundle.h
index 7b06e5e9..41d2634 100644
--- a/ui/base/resource/resource_bundle.h
+++ b/ui/base/resource/resource_bundle.h
@@ -19,6 +19,8 @@
#include "base/string16.h"
#include "base/string_piece.h"
#include "ui/base/ui_export.h"
+#include "ui/gfx/font.h"
+#include "ui/gfx/image/image.h"
#include "ui/gfx/native_widget_types.h"
class SkBitmap;
@@ -28,11 +30,6 @@ class Lock;
class RefCountedStaticMemory;
}
-namespace gfx {
-class Font;
-class Image;
-}
-
namespace ui {
class ResourceHandle;
@@ -63,12 +60,60 @@ class UI_EXPORT ResourceBundle {
RTL_DISABLED,
};
- // Initialize the ResourceBundle for this process. Returns the language
- // selected.
+ // Delegate class that allows interception of pack file loading and resource
+ // requests. The methods of this class may be called on multiple threads.
+ class Delegate {
+ public:
+ // Called before a resource pack file is loaded. Return the full path for
+ // the pack file to continue loading or an empty value to cancel loading.
+ // |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;
+
+ // 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.
+ // |pack_path| will contain the complete default path for the pack file if
+ // known or just the pack file name otherwise.
+ virtual FilePath GetPathForLocalePack(const FilePath& pack_path,
+ const std::string& locale) = 0;
+
+ // Return an image resource or an empty value to attempt retrieval of the
+ // default resource.
+ virtual gfx::Image GetImageNamed(int resource_id) = 0;
+
+ // Return an image resource or an empty value to attempt retrieval of the
+ // default resource.
+ virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0;
+
+ // Return a static memory resource or NULL to attempt retrieval of the
+ // default resource.
+ virtual base::RefCountedStaticMemory* LoadDataResourceBytes(
+ int resource_id) = 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,
+ base::StringPiece* value) = 0;
+
+ // Retrieve a localized string. Return true if a string was provided or
+ // false to attempt retrieval of the default string.
+ virtual bool GetLocalizedString(int message_id, string16* value) = 0;
+
+ // Return a font resource or NULL to attempt retrieval of the default
+ // resource.
+ virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0;
+
+ protected:
+ virtual ~Delegate() {}
+ };
+
+ // Initialize the ResourceBundle for this process. Does not take ownership of
+ // the |delegate| value. Returns the language selected.
// NOTE: Mac ignores this and always loads up resources for the language
// defined by the Cocoa UI (i.e., NSBundle does the language work).
static std::string InitSharedInstanceWithLocale(
- const std::string& pref_locale);
+ const std::string& pref_locale, Delegate* delegate);
// Initialize the ResourceBundle using given data pack path for testing.
static void InitSharedInstanceWithPakFile(const FilePath& path);
@@ -83,11 +128,13 @@ class UI_EXPORT ResourceBundle {
static ResourceBundle& GetSharedInstance();
// Check if the .pak for the given locale exists.
- static bool LocaleDataPakExists(const std::string& locale);
+ bool LocaleDataPakExists(const std::string& locale);
- // Registers additional data pack files with the global ResourceBundle. When
+ // Registers additional data pack files with this ResourceBundle. When
// looking for a DataResource, we will search these files after searching the
- // main module. |scale_factor| is the scale of images in this resource pak
+ // main module. |path| should be the complete path to the pack file if known
+ // or just the pack file name otherwise (the delegate may optionally override
+ // 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);
@@ -149,10 +196,19 @@ class UI_EXPORT ResourceBundle {
void OverrideLocalePakForTest(const FilePath& pak_path);
private:
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForResourcePack);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForLocalePack);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetImageNamed);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetNativeImageNamed);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateLoadDataResourceBytes);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont);
FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes);
+ FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists);
// Ctor/dtor are private, since we're a singleton.
- ResourceBundle();
+ explicit ResourceBundle(Delegate* delegate);
~ResourceBundle();
// Free skia_images_.
@@ -177,7 +233,7 @@ class UI_EXPORT ResourceBundle {
// 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);
+ FilePath GetLocaleFilePath(const std::string& app_locale);
// 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
@@ -186,10 +242,14 @@ class UI_EXPORT ResourceBundle {
// Returns an empty image for when a resource cannot be loaded. This is a
// bright red bitmap.
- gfx::Image* GetEmptyImage();
+ gfx::Image& GetEmptyImage();
const FilePath& GetOverriddenPakPath();
+ // This pointer is guaranteed to outlive the ResourceBundle instance and may
+ // be NULL.
+ Delegate* delegate_;
+
// Protects |images_| and font-related members.
scoped_ptr<base::Lock> images_and_fonts_lock_;
@@ -202,9 +262,11 @@ class UI_EXPORT ResourceBundle {
// Cached images. The ResourceBundle caches all retrieved images and keeps
// ownership of the pointers.
- typedef std::map<int, gfx::Image*> ImageMap;
+ typedef std::map<int, gfx::Image> ImageMap;
ImageMap images_;
+ gfx::Image empty_image_;
+
// The various fonts used. Cached to avoid repeated GDI creation/destruction.
scoped_ptr<gfx::Font> base_font_;
scoped_ptr<gfx::Font> bold_font_;