Home | History | Annotate | Download | only in resource
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_
      6 #define UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/files/file_path.h"
     13 #include "base/files/memory_mapped_file.h"
     14 #include "base/gtest_prod_util.h"
     15 #include "base/memory/scoped_ptr.h"
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/strings/string16.h"
     18 #include "base/strings/string_piece.h"
     19 #include "build/build_config.h"
     20 #include "ui/base/layout.h"
     21 #include "ui/base/ui_base_export.h"
     22 #include "ui/gfx/font_list.h"
     23 #include "ui/gfx/image/image.h"
     24 #include "ui/gfx/native_widget_types.h"
     25 
     26 class SkBitmap;
     27 
     28 namespace base {
     29 class File;
     30 class Lock;
     31 class RefCountedStaticMemory;
     32 }
     33 
     34 namespace ui {
     35 
     36 class DataPack;
     37 class ResourceHandle;
     38 
     39 // ResourceBundle is a central facility to load images and other resources,
     40 // such as theme graphics. Every resource is loaded only once.
     41 class UI_BASE_EXPORT ResourceBundle {
     42  public:
     43   // An enumeration of the various font styles used throughout Chrome.
     44   // The following holds true for the font sizes:
     45   // Small <= SmallBold <= Base <= Bold <= Medium <= MediumBold <= Large.
     46   enum FontStyle {
     47     // NOTE: depending upon the locale, using one of the *BoldFont below
     48     // may *not* actually result in a bold font.
     49     SmallFont,
     50     SmallBoldFont,
     51     BaseFont,
     52     BoldFont,
     53     MediumFont,
     54     MediumBoldFont,
     55     LargeFont,
     56     LargeBoldFont,
     57   };
     58 
     59   enum ImageRTL {
     60     // Images are flipped in RTL locales.
     61     RTL_ENABLED,
     62     // Images are never flipped.
     63     RTL_DISABLED,
     64   };
     65 
     66   // Delegate class that allows interception of pack file loading and resource
     67   // requests. The methods of this class may be called on multiple threads.
     68   class Delegate {
     69    public:
     70     // Called before a resource pack file is loaded. Return the full path for
     71     // the pack file to continue loading or an empty value to cancel loading.
     72     // |pack_path| will contain the complete default path for the pack file if
     73     // known or just the pack file name otherwise.
     74     virtual base::FilePath GetPathForResourcePack(
     75         const base::FilePath& pack_path,
     76         ScaleFactor scale_factor) = 0;
     77 
     78     // Called before a locale pack file is loaded. Return the full path for
     79     // the pack file to continue loading or an empty value to cancel loading.
     80     // |pack_path| will contain the complete default path for the pack file if
     81     // known or just the pack file name otherwise.
     82     virtual base::FilePath GetPathForLocalePack(
     83         const base::FilePath& pack_path,
     84         const std::string& locale) = 0;
     85 
     86     // Return an image resource or an empty value to attempt retrieval of the
     87     // default resource.
     88     virtual gfx::Image GetImageNamed(int resource_id) = 0;
     89 
     90     // Return an image resource or an empty value to attempt retrieval of the
     91     // default resource.
     92     virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0;
     93 
     94     // Return a static memory resource or NULL to attempt retrieval of the
     95     // default resource.
     96     virtual base::RefCountedStaticMemory* LoadDataResourceBytes(
     97         int resource_id,
     98         ScaleFactor scale_factor) = 0;
     99 
    100     // Retrieve a raw data resource. Return true if a resource was provided or
    101     // false to attempt retrieval of the default resource.
    102     virtual bool GetRawDataResource(int resource_id,
    103                                     ScaleFactor scale_factor,
    104                                     base::StringPiece* value) = 0;
    105 
    106     // Retrieve a localized string. Return true if a string was provided or
    107     // false to attempt retrieval of the default string.
    108     virtual bool GetLocalizedString(int message_id, base::string16* value) = 0;
    109 
    110     // Returns a font or NULL to attempt retrieval of the default resource.
    111     virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0;
    112 
    113    protected:
    114     virtual ~Delegate() {}
    115   };
    116 
    117   // Initialize the ResourceBundle for this process. Does not take ownership of
    118   // the |delegate| value. Returns the language selected.
    119   // NOTE: Mac ignores this and always loads up resources for the language
    120   // defined by the Cocoa UI (i.e., NSBundle does the language work).
    121   //
    122   // TODO(sergeyu): This method also loads common resources (i.e. chrome.pak).
    123   // There is no way to specify which resource files are loaded, i.e. names of
    124   // the files are hardcoded in ResourceBundle. Fix it to allow to specify which
    125   // files are loaded (e.g. add a new method in Delegate).
    126   static std::string InitSharedInstanceWithLocale(
    127       const std::string& pref_locale, Delegate* delegate);
    128 
    129   // Same as InitSharedInstanceWithLocale(), but loads only localized resources,
    130   // without default resource packs.
    131   static std::string InitSharedInstanceLocaleOnly(
    132       const std::string& pref_locale, Delegate* delegate);
    133 
    134   // Initialize the ResourceBundle using the given file region. If |region| is
    135   // MemoryMappedFile::Region::kWholeFile, the entire |pak_file| is used.
    136   // |should_load_common_resources| controls whether or not LoadCommonResources
    137   // is called.
    138   // This allows the use of this function in a sandbox without local file
    139   // access (as on Android).
    140   static void InitSharedInstanceWithPakFileRegion(
    141       base::File pak_file,
    142       const base::MemoryMappedFile::Region& region,
    143       bool should_load_common_resources);
    144 
    145   // Initialize the ResourceBundle using given data pack path for testing.
    146   static void InitSharedInstanceWithPakPath(const base::FilePath& path);
    147 
    148   // Delete the ResourceBundle for this process if it exists.
    149   static void CleanupSharedInstance();
    150 
    151   // Returns true after the global resource loader instance has been created.
    152   static bool HasSharedInstance();
    153 
    154   // Return the global resource loader instance.
    155   static ResourceBundle& GetSharedInstance();
    156 
    157   // Check if the .pak for the given locale exists.
    158   bool LocaleDataPakExists(const std::string& locale);
    159 
    160   // Registers additional data pack files with this ResourceBundle.  When
    161   // looking for a DataResource, we will search these files after searching the
    162   // main module. |path| should be the complete path to the pack file if known
    163   // or just the pack file name otherwise (the delegate may optionally override
    164   // this value). |scale_factor| is the scale of images in this resource pak
    165   // relative to the images in the 1x resource pak. This method is not thread
    166   // safe! You should call it immediately after calling InitSharedInstance.
    167   void AddDataPackFromPath(const base::FilePath& path,
    168                            ScaleFactor scale_factor);
    169 
    170   // Same as above but using an already open file.
    171   void AddDataPackFromFile(base::File file, ScaleFactor scale_factor);
    172 
    173   // Same as above but using only a region (offset + size) of the file.
    174   void AddDataPackFromFileRegion(base::File file,
    175                                  const base::MemoryMappedFile::Region& region,
    176                                  ScaleFactor scale_factor);
    177 
    178   // Same as AddDataPackFromPath but does not log an error if the pack fails to
    179   // load.
    180   void AddOptionalDataPackFromPath(const base::FilePath& path,
    181                                    ScaleFactor scale_factor);
    182 
    183   // Changes the locale for an already-initialized ResourceBundle, returning the
    184   // name of the newly-loaded locale.  Future calls to get strings will return
    185   // the strings for this new locale.  This has no effect on existing or future
    186   // image resources.  |locale_resources_data_| is protected by a lock for the
    187   // duration of the swap, as GetLocalizedString() may be concurrently invoked
    188   // on another thread.
    189   std::string ReloadLocaleResources(const std::string& pref_locale);
    190 
    191   // Gets image with the specified resource_id from the current module data.
    192   // Returns a pointer to a shared instance of gfx::ImageSkia. This shared
    193   // instance is owned by the resource bundle and should not be freed.
    194   // TODO(pkotwicz): Make method return const gfx::ImageSkia*
    195   //
    196   // NOTE: GetNativeImageNamed is preferred for cross-platform gfx::Image use.
    197   gfx::ImageSkia* GetImageSkiaNamed(int resource_id);
    198 
    199   // Gets an image resource from the current module data. This will load the
    200   // image in Skia format by default. The ResourceBundle owns this.
    201   gfx::Image& GetImageNamed(int resource_id);
    202 
    203   // Similar to GetImageNamed, but rather than loading the image in Skia format,
    204   // it will load in the native platform type. This can avoid conversion from
    205   // one image type to another. ResourceBundle owns the result.
    206   //
    207   // Note that if the same resource has already been loaded in GetImageNamed(),
    208   // gfx::Image will perform a conversion, rather than using the native image
    209   // loading code of ResourceBundle.
    210   //
    211   // If |rtl| is RTL_ENABLED then the image is flipped in RTL locales.
    212   gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl);
    213 
    214   // Same as GetNativeImageNamed() except that RTL is not enabled.
    215   gfx::Image& GetNativeImageNamed(int resource_id);
    216 
    217   // Loads the raw bytes of a scale independent data resource.
    218   base::RefCountedStaticMemory* LoadDataResourceBytes(int resource_id) const;
    219 
    220   // Loads the raw bytes of a data resource nearest the scale factor
    221   // |scale_factor| into |bytes|, without doing any processing or
    222   // interpretation of the resource. Use ResourceHandle::SCALE_FACTOR_NONE
    223   // for scale independent image resources (such as wallpaper).
    224   // Returns NULL if we fail to read the resource.
    225   base::RefCountedStaticMemory* LoadDataResourceBytesForScale(
    226       int resource_id,
    227       ScaleFactor scale_factor) const;
    228 
    229   // Return the contents of a scale independent resource in a
    230   // StringPiece given the resource id
    231   base::StringPiece GetRawDataResource(int resource_id) const;
    232 
    233   // Return the contents of a resource in a StringPiece given the resource id
    234   // nearest the scale factor |scale_factor|.
    235   // Use ResourceHandle::SCALE_FACTOR_NONE for scale independent image resources
    236   // (such as wallpaper).
    237   base::StringPiece GetRawDataResourceForScale(int resource_id,
    238                                                ScaleFactor scale_factor) const;
    239 
    240   // Get a localized string given a message id.  Returns an empty
    241   // string if the message_id is not found.
    242   base::string16 GetLocalizedString(int message_id);
    243 
    244   // Returns the font list for the specified style.
    245   const gfx::FontList& GetFontList(FontStyle style);
    246 
    247   // Returns the font for the specified style.
    248   const gfx::Font& GetFont(FontStyle style);
    249 
    250   // Resets and reloads the cached fonts.  This is useful when the fonts of the
    251   // system have changed, for example, when the locale has changed.
    252   void ReloadFonts();
    253 
    254   // Overrides the path to the pak file from which the locale resources will be
    255   // loaded. Pass an empty path to undo.
    256   void OverrideLocalePakForTest(const base::FilePath& pak_path);
    257 
    258   // Returns the full pathname of the locale file to load.  May return an empty
    259   // string if no locale data files are found and |test_file_exists| is true.
    260   // Used on Android to load the local file in the browser process and pass it
    261   // to the sandboxed renderer process.
    262   base::FilePath GetLocaleFilePath(const std::string& app_locale,
    263                                    bool test_file_exists);
    264 
    265   // Returns the maximum scale factor currently loaded.
    266   // Returns SCALE_FACTOR_100P if no resource is loaded.
    267   ScaleFactor GetMaxScaleFactor() const;
    268 
    269  protected:
    270   // Returns true if |scale_factor| is supported by this platform.
    271   static bool IsScaleFactorSupported(ScaleFactor scale_factor);
    272 
    273  private:
    274   FRIEND_TEST_ALL_PREFIXES(ResourceBundleTest, DelegateGetPathForLocalePack);
    275   FRIEND_TEST_ALL_PREFIXES(ResourceBundleTest, DelegateGetImageNamed);
    276   FRIEND_TEST_ALL_PREFIXES(ResourceBundleTest, DelegateGetNativeImageNamed);
    277 
    278   friend class ResourceBundleImageTest;
    279   friend class ResourceBundleTest;
    280 
    281   class ResourceBundleImageSource;
    282   friend class ResourceBundleImageSource;
    283 
    284   // Ctor/dtor are private, since we're a singleton.
    285   explicit ResourceBundle(Delegate* delegate);
    286   ~ResourceBundle();
    287 
    288   // Shared initialization.
    289   static void InitSharedInstance(Delegate* delegate);
    290 
    291   // Free skia_images_.
    292   void FreeImages();
    293 
    294   // Load the main resources.
    295   void LoadCommonResources();
    296 
    297   // Implementation for AddDataPackFromPath and AddOptionalDataPackFromPath, if
    298   // the pack is not |optional| logs an error on failure to load.
    299   void AddDataPackFromPathInternal(const base::FilePath& path,
    300                                    ScaleFactor scale_factor,
    301                                    bool optional);
    302 
    303   // Inserts |data_pack| to |data_pack_| and updates |max_scale_factor_|
    304   // accordingly.
    305   void AddDataPack(DataPack* data_pack);
    306 
    307   // Try to load the locale specific strings from an external data module.
    308   // Returns the locale that is loaded.
    309   std::string LoadLocaleResources(const std::string& pref_locale);
    310 
    311   // Load test resources in given paths. If either path is empty an empty
    312   // resource pack is loaded.
    313   void LoadTestResources(const base::FilePath& path,
    314                          const base::FilePath& locale_path);
    315 
    316   // Unload the locale specific strings and prepares to load new ones. See
    317   // comments for ReloadLocaleResources().
    318   void UnloadLocaleResources();
    319 
    320   // Initializes all the gfx::FontList members if they haven't yet been
    321   // initialized.
    322   void LoadFontsIfNecessary();
    323 
    324   // Returns a FontList or NULL by calling Delegate::GetFont and converting
    325   // scoped_ptr<gfx::Font> to scoped_ptr<gfx::FontList>.
    326   scoped_ptr<gfx::FontList> GetFontListFromDelegate(FontStyle style);
    327 
    328   // Fills the |bitmap| given the data file to look in and the |resource_id|.
    329   // Returns false if the resource does not exist.
    330   //
    331   // If the call succeeds, |fell_back_to_1x| indicates whether Chrome's custom
    332   // csCl PNG chunk is present (added by GRIT if it falls back to a 100% image).
    333   bool LoadBitmap(const ResourceHandle& data_handle,
    334                   int resource_id,
    335                   SkBitmap* bitmap,
    336                   bool* fell_back_to_1x) const;
    337 
    338   // Fills the |bitmap| given the |resource_id| and |scale_factor|.
    339   // Returns false if the resource does not exist. This may fall back to
    340   // the data pack with SCALE_FACTOR_NONE, and when this happens,
    341   // |scale_factor| will be set to SCALE_FACTOR_100P.
    342   bool LoadBitmap(int resource_id,
    343                   ScaleFactor* scale_factor,
    344                   SkBitmap* bitmap,
    345                   bool* fell_back_to_1x) const;
    346 
    347   // Returns true if missing scaled resources should be visually indicated when
    348   // drawing the fallback (e.g., by tinting the image).
    349   static bool ShouldHighlightMissingScaledResources();
    350 
    351   // Returns true if the data in |buf| is a PNG that has the special marker
    352   // added by GRIT that indicates that the image is actually 1x data.
    353   static bool PNGContainsFallbackMarker(const unsigned char* buf, size_t size);
    354 
    355   // A wrapper for PNGCodec::Decode that returns information about custom
    356   // chunks. For security reasons we can't alter PNGCodec to return this
    357   // information. Our PNG files are preprocessed by GRIT, and any special chunks
    358   // should occur immediately after the IHDR chunk.
    359   static bool DecodePNG(const unsigned char* buf,
    360                         size_t size,
    361                         SkBitmap* bitmap,
    362                         bool* fell_back_to_1x);
    363 
    364   // Returns an empty image for when a resource cannot be loaded. This is a
    365   // bright red bitmap.
    366   gfx::Image& GetEmptyImage();
    367 
    368   const base::FilePath& GetOverriddenPakPath();
    369 
    370   // This pointer is guaranteed to outlive the ResourceBundle instance and may
    371   // be NULL.
    372   Delegate* delegate_;
    373 
    374   // Protects |images_| and font-related members.
    375   scoped_ptr<base::Lock> images_and_fonts_lock_;
    376 
    377   // Protects |locale_resources_data_|.
    378   scoped_ptr<base::Lock> locale_resources_data_lock_;
    379 
    380   // Handles for data sources.
    381   scoped_ptr<ResourceHandle> locale_resources_data_;
    382   ScopedVector<ResourceHandle> data_packs_;
    383 
    384   // The maximum scale factor currently loaded.
    385   ScaleFactor max_scale_factor_;
    386 
    387   // Cached images. The ResourceBundle caches all retrieved images and keeps
    388   // ownership of the pointers.
    389   typedef std::map<int, gfx::Image> ImageMap;
    390   ImageMap images_;
    391 
    392   gfx::Image empty_image_;
    393 
    394   // The various font lists used. Cached to avoid repeated GDI
    395   // creation/destruction.
    396   scoped_ptr<gfx::FontList> base_font_list_;
    397   scoped_ptr<gfx::FontList> bold_font_list_;
    398   scoped_ptr<gfx::FontList> small_font_list_;
    399   scoped_ptr<gfx::FontList> small_bold_font_list_;
    400   scoped_ptr<gfx::FontList> medium_font_list_;
    401   scoped_ptr<gfx::FontList> medium_bold_font_list_;
    402   scoped_ptr<gfx::FontList> large_font_list_;
    403   scoped_ptr<gfx::FontList> large_bold_font_list_;
    404   scoped_ptr<gfx::FontList> web_font_list_;
    405 
    406   base::FilePath overridden_pak_path_;
    407 
    408   DISALLOW_COPY_AND_ASSIGN(ResourceBundle);
    409 };
    410 
    411 }  // namespace ui
    412 
    413 // TODO(beng): Someday, maybe, get rid of this.
    414 using ui::ResourceBundle;
    415 
    416 #endif  // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_
    417