[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 1 | // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_COMMON_RESOURCE_BUNDLE_H__ |
| 6 | #define CHROME_COMMON_RESOURCE_BUNDLE_H__ |
| 7 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
| 10 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include <windows.h> |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 12 | #endif |
| 13 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
| 16 | |
| 17 | #include "base/basictypes.h" |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 18 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 19 | #include "base/lock.h" |
| 20 | #include "base/scoped_ptr.h" |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
[email protected] | 4b0d2dd | 2009-02-26 12:41:40 | [diff] [blame] | 23 | #if defined(OS_LINUX) || defined(OS_MACOSX) |
[email protected] | 1a14188 | 2009-01-13 21:35:32 | [diff] [blame] | 24 | namespace base { |
| 25 | class DataPack; |
| 26 | }; |
[email protected] | 4b0d2dd | 2009-02-26 12:41:40 | [diff] [blame] | 27 | #endif |
| 28 | #if defined(OS_LINUX) |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame] | 29 | typedef struct _GdkPixbuf GdkPixbuf; |
[email protected] | 1a14188 | 2009-01-13 21:35:32 | [diff] [blame] | 30 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | class ChromeFont; |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 32 | class Extension; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | class SkBitmap; |
| 34 | class StringPiece; |
| 35 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 36 | // ResourceBundle is a central facility to load images and other resources, |
| 37 | // such as theme graphics. |
| 38 | // Every resource is loaded only once. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | class ResourceBundle { |
| 40 | public: |
| 41 | // An enumeration of the various font styles used throughout Chrome. |
| 42 | // The following holds true for the font sizes: |
| 43 | // Small <= Base <= Medium <= MediumBold <= Large. |
| 44 | enum FontStyle { |
| 45 | SmallFont, |
| 46 | BaseFont, |
| 47 | MediumFont, |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 48 | // NOTE: depending upon the locale, this may *not* result in a bold font. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | MediumBoldFont, |
| 50 | LargeFont, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | }; |
| 52 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 53 | // Initialize the ResourceBundle for this process. |
[email protected] | 4b0d2dd | 2009-02-26 12:41:40 | [diff] [blame] | 54 | // NOTE: Mac ignores this and always loads up resources for the language |
| 55 | // defined by the Cocoa UI (ie-NSBundle does the langange work). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | static void InitSharedInstance(const std::wstring& pref_locale); |
| 57 | |
| 58 | // Delete the ResourceBundle for this process if it exists. |
| 59 | static void CleanupSharedInstance(); |
| 60 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 61 | // Return the global resource loader instance. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | static ResourceBundle& GetSharedInstance(); |
| 63 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 64 | // Load the data file that contains theme resources if present. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | void LoadThemeResources(); |
| 66 | |
| 67 | // Gets the bitmap with the specified resource_id, first by looking into the |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 68 | // theme data, than in the current module data if applicable. |
| 69 | // Returns a pointer to a shared instance of the SkBitmap in the given out |
| 70 | // parameter. This shared bitmap is owned by the resource bundle and should |
| 71 | // not be freed. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | // |
| 73 | // The bitmap is assumed to exist. This function will log in release, and |
| 74 | // assert in debug mode if it does not. On failure, this will return a |
| 75 | // pointer to a shared empty placeholder bitmap so it will be visible what |
| 76 | // is missing. |
| 77 | SkBitmap* GetBitmapNamed(int resource_id); |
| 78 | |
| 79 | // Loads the raw bytes of an image resource into |bytes|, |
| 80 | // without doing any processing or interpretation of |
| 81 | // the resource. Returns whether we successfully read the resource. |
| 82 | bool LoadImageResourceBytes(int resource_id, |
| 83 | std::vector<unsigned char>* bytes); |
| 84 | |
| 85 | // Loads the raw bytes of a data resource into |bytes|, |
| 86 | // without doing any processing or interpretation of |
| 87 | // the resource. Returns whether we successfully read the resource. |
| 88 | bool LoadDataResourceBytes(int resource_id, |
| 89 | std::vector<unsigned char>* bytes); |
| 90 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | // Return the contents of a file in a string given the resource id. |
| 92 | // This will copy the data from the resource and return it as a string. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 93 | // TODO(port): deprecate this and replace with GetRawDataResource to avoid |
| 94 | // needless copying. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | std::string GetDataResource(int resource_id); |
| 96 | |
| 97 | // Like GetDataResource(), but avoids copying the resource. Instead, it |
| 98 | // returns a StringPiece which points into the actual resource in the image. |
| 99 | StringPiece GetRawDataResource(int resource_id); |
| 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | // Get a localized string given a message id. Returns an empty |
| 102 | // string if the message_id is not found. |
[email protected] | 26749715 | 2009-04-14 19:31:09 | [diff] [blame] | 103 | string16 GetLocalizedString(int message_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | |
| 105 | // Returns the font for the specified style. |
| 106 | ChromeFont GetFont(FontStyle style); |
| 107 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 108 | #if defined(OS_WIN) |
| 109 | // Loads and returns an icon from the theme dll. |
| 110 | HICON LoadThemeIcon(int icon_id); |
| 111 | |
| 112 | // Loads and returns the global accelerators. |
| 113 | HACCEL GetGlobalAccelerators(); |
| 114 | |
| 115 | // Loads and returns a cursor from the app module. |
| 116 | HCURSOR LoadCursor(int cursor_id); |
[email protected] | 9146221 | 2009-04-10 15:09:39 | [diff] [blame] | 117 | #elif defined(TOOLKIT_GTK) |
[email protected] | e6ba540 | 2009-02-20 19:15:02 | [diff] [blame] | 118 | // Load a theme image as a GdkPixbuf. |
| 119 | GdkPixbuf* LoadPixbuf(int resource_id); |
| 120 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 122 | // Sets an Extension object that can handle theme resource requests. |
| 123 | void SetThemeExtension(const Extension& e); |
| 124 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | private: |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 126 | // We define a DataHandle typedef to abstract across how data is stored |
| 127 | // across platforms. |
| 128 | #if defined(OS_WIN) |
| 129 | // Windows stores resources in DLLs, which are managed by HINSTANCE. |
| 130 | typedef HINSTANCE DataHandle; |
[email protected] | 4b0d2dd | 2009-02-26 12:41:40 | [diff] [blame] | 131 | #elif defined(OS_LINUX) || defined(OS_MACOSX) |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 132 | // Linux uses base::DataPack. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 133 | typedef base::DataPack* DataHandle; |
| 134 | #endif |
| 135 | |
| 136 | // Ctor/dtor are private, since we're a singleton. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | ResourceBundle(); |
| 138 | ~ResourceBundle(); |
| 139 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 140 | // Free skia_images_. |
| 141 | void FreeImages(); |
| 142 | |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 143 | // Try to load the main resources and the locale specific strings from an |
| 144 | // external data module. |
| 145 | void LoadResources(const std::wstring& pref_locale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 147 | // Initialize all the ChromeFont members if they haven't yet been initialized. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | void LoadFontsIfNecessary(); |
| 149 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 150 | // Returns the full pathname of the locale file to load. May return an empty |
| 151 | // string if no locale data files are found. |
| 152 | FilePath GetLocaleFilePath(const std::wstring& pref_locale); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | |
| 154 | // Loads the raw bytes of a resource from |module| into |bytes|, |
| 155 | // without doing any processing or interpretation of |
| 156 | // the resource. Returns whether we successfully read the resource. |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 157 | static bool LoadResourceBytes(DataHandle module, |
| 158 | int resource_id, |
| 159 | std::vector<unsigned char>* bytes); |
| 160 | |
| 161 | // Creates and returns a new SkBitmap given the data file to look in and the |
| 162 | // resource id. It's up to the caller to free the returned bitmap when |
| 163 | // done. |
| 164 | static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 165 | |
| 166 | // Class level lock. Used to protect internal data structures that may be |
| 167 | // accessed from other threads (e.g., skia_images_). |
| 168 | Lock lock_; |
| 169 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 170 | // Handles for data sources. |
[email protected] | 2aad8bb | 2009-02-04 19:00:50 | [diff] [blame] | 171 | DataHandle resources_data_; |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 172 | DataHandle locale_resources_data_; |
| 173 | DataHandle theme_data_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | |
| 175 | // Cached images. The ResourceBundle caches all retrieved bitmaps and keeps |
| 176 | // ownership of the pointers. |
| 177 | typedef std::map<int, SkBitmap*> SkImageMap; |
| 178 | SkImageMap skia_images_; |
| 179 | |
| 180 | // The various fonts used. Cached to avoid repeated GDI creation/destruction. |
| 181 | scoped_ptr<ChromeFont> base_font_; |
| 182 | scoped_ptr<ChromeFont> small_font_; |
| 183 | scoped_ptr<ChromeFont> medium_font_; |
| 184 | scoped_ptr<ChromeFont> medium_bold_font_; |
| 185 | scoped_ptr<ChromeFont> large_font_; |
| 186 | scoped_ptr<ChromeFont> web_font_; |
| 187 | |
[email protected] | 11c2fe8 | 2009-01-13 01:52:59 | [diff] [blame] | 188 | static ResourceBundle* g_shared_instance_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | |
[email protected] | 07c00d99 | 2009-03-04 20:27:04 | [diff] [blame] | 190 | scoped_ptr<Extension> theme_extension_; |
| 191 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | DISALLOW_EVIL_CONSTRUCTORS(ResourceBundle); |
| 193 | }; |
| 194 | |
| 195 | #endif // CHROME_COMMON_RESOURCE_BUNDLE_H__ |