Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Unified Diff: ui/base/resource/resource_bundle.h

Issue 10270023: Add new ResourceBundle::Delegate interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/base/resource/resource_bundle.h
===================================================================
--- ui/base/resource/resource_bundle.h (revision 134688)
+++ ui/base/resource/resource_bundle.h (working copy)
@@ -19,6 +19,8 @@
#include "base/string16.h"
#include "base/string_piece.h"
#include "ui/base/ui_export.h"
+#include "ui/gfx/font.h"
tony 2012/05/03 20:35:42 Nit: I think we can still forward declare gfx::Fon
Marshall 2012/05/04 14:41:03 When using scoped_ptr with a forward declared type
tony 2012/05/04 17:10:51 Which header files did you see this in? Should th
Marshall 2012/05/04 17:28:41 Here is the complete error message: 1>C:\svn\chro
+#include "ui/gfx/image/image.h"
#include "ui/gfx/native_widget_types.h"
class SkBitmap;
@@ -28,11 +30,6 @@
class RefCountedStaticMemory;
}
-namespace gfx {
-class Font;
-class Image;
-}
-
namespace ui {
class ResourceHandle;
@@ -63,12 +60,60 @@
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 @@
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);
@@ -109,7 +156,7 @@
// Gets an image resource from the current module data. This will load the
// image in Skia format by default. The ResourceBundle owns this.
- gfx::Image& GetImageNamed(int resource_id);
+ const gfx::Image& GetImageNamed(int resource_id);
// Similar to GetImageNamed, but rather than loading the image in Skia format,
// it will load in the native platform type. This can avoid conversion from
@@ -120,10 +167,10 @@
// 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);
+ const gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl);
// Same as GetNativeImageNamed() except that RTL is not enabled.
- gfx::Image& GetNativeImageNamed(int resource_id);
+ const gfx::Image& GetNativeImageNamed(int resource_id);
// Loads the raw bytes of a data resource into |bytes|,
// without doing any processing or interpretation of
@@ -149,10 +196,19 @@
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 @@
// 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 @@
// Returns an empty image for when a resource cannot be loaded. This is a
// bright red bitmap.
- gfx::Image* GetEmptyImage();
+ const 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,7 +262,7 @@
// 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_;
// The various fonts used. Cached to avoid repeated GDI creation/destruction.

Powered by Google App Engine
This is Rietveld 408576698