| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 APP_RESOURCE_BUNDLE_H_ | |
| 6 #define APP_RESOURCE_BUNDLE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "build/build_config.h" | |
| 10 | |
| 11 #if defined(OS_WIN) | |
| 12 #include <windows.h> | |
| 13 #endif | |
| 14 | |
| 15 #include <map> | |
| 16 #include <string> | |
| 17 #include <vector> | |
| 18 | |
| 19 #include "base/basictypes.h" | |
| 20 #include "base/file_path.h" | |
| 21 #include "base/ref_counted_memory.h" | |
| 22 #include "base/scoped_ptr.h" | |
| 23 #include "base/string16.h" | |
| 24 #include "gfx/native_widget_types.h" | |
| 25 | |
| 26 namespace app { | |
| 27 class DataPack; | |
| 28 } | |
| 29 namespace base { | |
| 30 class Lock; | |
| 31 } | |
| 32 namespace gfx { | |
| 33 class Font; | |
| 34 } | |
| 35 class SkBitmap; | |
| 36 typedef uint32 SkColor; | |
| 37 namespace base { | |
| 38 class StringPiece; | |
| 39 } | |
| 40 | |
| 41 #if defined(OS_MACOSX) | |
| 42 #ifdef __OBJC__ | |
| 43 @class NSImage; | |
| 44 #else | |
| 45 class NSImage; | |
| 46 #endif // __OBJC__ | |
| 47 #endif // defined(OS_MACOSX) | |
| 48 | |
| 49 #if defined(USE_X11) | |
| 50 typedef struct _GdkPixbuf GdkPixbuf; | |
| 51 #endif | |
| 52 | |
| 53 // ResourceBundle is a central facility to load images and other resources, | |
| 54 // such as theme graphics. | |
| 55 // Every resource is loaded only once. | |
| 56 class ResourceBundle { | |
| 57 public: | |
| 58 // An enumeration of the various font styles used throughout Chrome. | |
| 59 // The following holds true for the font sizes: | |
| 60 // Small <= Base <= Bold <= Medium <= MediumBold <= Large. | |
| 61 enum FontStyle { | |
| 62 SmallFont, | |
| 63 BaseFont, | |
| 64 BoldFont, | |
| 65 MediumFont, | |
| 66 // NOTE: depending upon the locale, this may *not* result in a bold font. | |
| 67 MediumBoldFont, | |
| 68 LargeFont, | |
| 69 }; | |
| 70 | |
| 71 // Initialize the ResourceBundle for this process. Returns the language | |
| 72 // selected. | |
| 73 // NOTE: Mac ignores this and always loads up resources for the language | |
| 74 // defined by the Cocoa UI (ie-NSBundle does the langange work). | |
| 75 static std::string InitSharedInstance(const std::string& pref_locale); | |
| 76 | |
| 77 // Changes the locale for an already-initialized ResourceBundle. Future | |
| 78 // calls to get strings will return the strings for this new locale. This | |
| 79 // has no effect on existing or future image resources. This has no effect | |
| 80 // on existing or future image resources, and thus does not use the lock to | |
| 81 // guarantee thread-safety, since all string access is expected to happen on | |
| 82 // the UI thread. | |
| 83 static std::string ReloadSharedInstance(const std::string& pref_locale); | |
| 84 | |
| 85 // Registers additional data pack files with the global ResourceBundle. When | |
| 86 // looking for a DataResource, we will search these files after searching the | |
| 87 // main module. This method is not thread safe! You should call it | |
| 88 // immediately after calling InitSharedInstance. | |
| 89 static void AddDataPackToSharedInstance(const FilePath& path); | |
| 90 | |
| 91 // Delete the ResourceBundle for this process if it exists. | |
| 92 static void CleanupSharedInstance(); | |
| 93 | |
| 94 // Return the global resource loader instance. | |
| 95 static ResourceBundle& GetSharedInstance(); | |
| 96 | |
| 97 // Gets the bitmap with the specified resource_id from the current module | |
| 98 // data. Returns a pointer to a shared instance of the SkBitmap. This shared | |
| 99 // bitmap is owned by the resource bundle and should not be freed. | |
| 100 SkBitmap* GetBitmapNamed(int resource_id); | |
| 101 | |
| 102 // Loads the raw bytes of a data resource into |bytes|, | |
| 103 // without doing any processing or interpretation of | |
| 104 // the resource. Returns whether we successfully read the resource. | |
| 105 RefCountedStaticMemory* LoadDataResourceBytes(int resource_id) const; | |
| 106 | |
| 107 // Return the contents of a resource in a StringPiece given the resource id. | |
| 108 base::StringPiece GetRawDataResource(int resource_id) const; | |
| 109 | |
| 110 // Get a localized string given a message id. Returns an empty | |
| 111 // string if the message_id is not found. | |
| 112 string16 GetLocalizedString(int message_id); | |
| 113 | |
| 114 // Returns the font for the specified style. | |
| 115 const gfx::Font& GetFont(FontStyle style); | |
| 116 | |
| 117 // Returns the gfx::NativeImage, the native platform type, named resource. | |
| 118 // Internally, this makes use of GetNSImageNamed(), GetPixbufNamed(), or | |
| 119 // GetBitmapNamed() depending on the platform (see gfx/native_widget_types.h). | |
| 120 // NOTE: On Mac the returned resource is autoreleased. | |
| 121 gfx::NativeImage GetNativeImageNamed(int resource_id); | |
| 122 | |
| 123 #if defined(OS_WIN) | |
| 124 // Loads and returns an icon from the app module. | |
| 125 HICON LoadThemeIcon(int icon_id); | |
| 126 | |
| 127 // Loads and returns a cursor from the app module. | |
| 128 HCURSOR LoadCursor(int cursor_id); | |
| 129 #elif defined(OS_MACOSX) | |
| 130 private: | |
| 131 // Wrapper for GetBitmapNamed. Converts the bitmap to an autoreleased NSImage. | |
| 132 // TODO(rsesek): Move implementation into GetNativeImageNamed(). | |
| 133 NSImage* GetNSImageNamed(int resource_id); | |
| 134 public: | |
| 135 #elif defined(USE_X11) | |
| 136 // Gets the GdkPixbuf with the specified resource_id from the main data pak | |
| 137 // file. Returns a pointer to a shared instance of the GdkPixbuf. This | |
| 138 // shared GdkPixbuf is owned by the resource bundle and should not be freed. | |
| 139 // | |
| 140 // The bitmap is assumed to exist. This function will log in release, and | |
| 141 // assert in debug mode if it does not. On failure, this will return a | |
| 142 // pointer to a shared empty placeholder bitmap so it will be visible what | |
| 143 // is missing. | |
| 144 GdkPixbuf* GetPixbufNamed(int resource_id); | |
| 145 | |
| 146 // As above, but flips it in RTL locales. | |
| 147 GdkPixbuf* GetRTLEnabledPixbufNamed(int resource_id); | |
| 148 | |
| 149 private: | |
| 150 // Shared implementation for the above two functions. | |
| 151 GdkPixbuf* GetPixbufImpl(int resource_id, bool rtl_enabled); | |
| 152 | |
| 153 public: | |
| 154 #endif | |
| 155 | |
| 156 // TODO(glen): Move these into theme provider (dialogs still depend on | |
| 157 // ResourceBundle). | |
| 158 static const SkColor frame_color; | |
| 159 static const SkColor frame_color_inactive; | |
| 160 static const SkColor frame_color_app_panel; | |
| 161 static const SkColor frame_color_app_panel_inactive; | |
| 162 static const SkColor frame_color_incognito; | |
| 163 static const SkColor frame_color_incognito_inactive; | |
| 164 static const SkColor toolbar_color; | |
| 165 static const SkColor toolbar_separator_color; | |
| 166 | |
| 167 private: | |
| 168 // Helper class for managing data packs. | |
| 169 class LoadedDataPack { | |
| 170 public: | |
| 171 explicit LoadedDataPack(const FilePath& path); | |
| 172 ~LoadedDataPack(); | |
| 173 bool GetStringPiece(int resource_id, base::StringPiece* data) const; | |
| 174 RefCountedStaticMemory* GetStaticMemory(int resource_id) const; | |
| 175 | |
| 176 private: | |
| 177 void Load(); | |
| 178 | |
| 179 scoped_ptr<app::DataPack> data_pack_; | |
| 180 FilePath path_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(LoadedDataPack); | |
| 183 }; | |
| 184 | |
| 185 // We define a DataHandle typedef to abstract across how data is stored | |
| 186 // across platforms. | |
| 187 #if defined(OS_WIN) | |
| 188 // Windows stores resources in DLLs, which are managed by HINSTANCE. | |
| 189 typedef HINSTANCE DataHandle; | |
| 190 #elif defined(USE_BASE_DATA_PACK) | |
| 191 // Linux uses base::DataPack. | |
| 192 typedef app::DataPack* DataHandle; | |
| 193 #endif | |
| 194 | |
| 195 // Ctor/dtor are private, since we're a singleton. | |
| 196 ResourceBundle(); | |
| 197 ~ResourceBundle(); | |
| 198 | |
| 199 // Free skia_images_. | |
| 200 void FreeImages(); | |
| 201 | |
| 202 #if defined(USE_X11) | |
| 203 // Free gdkPixbufs_. | |
| 204 void FreeGdkPixBufs(); | |
| 205 #endif | |
| 206 | |
| 207 // Load the main resources. | |
| 208 void LoadCommonResources(); | |
| 209 | |
| 210 // Try to load the locale specific strings from an external data module. | |
| 211 // Returns the locale that is loaded. | |
| 212 std::string LoadLocaleResources(const std::string& pref_locale); | |
| 213 | |
| 214 // Unload the locale specific strings and prepares to load new ones. See | |
| 215 // comments for ReloadSharedInstance(). | |
| 216 void UnloadLocaleResources(); | |
| 217 | |
| 218 // Initialize all the gfx::Font members if they haven't yet been initialized. | |
| 219 void LoadFontsIfNecessary(); | |
| 220 | |
| 221 #if defined(USE_BASE_DATA_PACK) | |
| 222 // Returns the full pathname of the main resources file to load. May return | |
| 223 // an empty string if no main resources data files are found. | |
| 224 static FilePath GetResourcesFilePath(); | |
| 225 #endif | |
| 226 | |
| 227 // Returns the full pathname of the locale file to load. May return an empty | |
| 228 // string if no locale data files are found. | |
| 229 static FilePath GetLocaleFilePath(const std::string& app_locale); | |
| 230 | |
| 231 // Returns a handle to bytes from the resource |module|, without doing any | |
| 232 // processing or interpretation of the resource. Returns whether we | |
| 233 // successfully read the resource. Caller does not own the data returned | |
| 234 // through this method and must not modify the data pointed to by |bytes|. | |
| 235 static RefCountedStaticMemory* LoadResourceBytes(DataHandle module, | |
| 236 int resource_id); | |
| 237 | |
| 238 // Creates and returns a new SkBitmap given the data file to look in and the | |
| 239 // resource id. It's up to the caller to free the returned bitmap when | |
| 240 // done. | |
| 241 static SkBitmap* LoadBitmap(DataHandle dll_inst, int resource_id); | |
| 242 | |
| 243 // Class level lock. Used to protect internal data structures that may be | |
| 244 // accessed from other threads (e.g., skia_images_). | |
| 245 scoped_ptr<base::Lock> lock_; | |
| 246 | |
| 247 // Handles for data sources. | |
| 248 DataHandle resources_data_; | |
| 249 DataHandle locale_resources_data_; | |
| 250 | |
| 251 // References to extra data packs loaded via AddDataPackToSharedInstance. | |
| 252 std::vector<LoadedDataPack*> data_packs_; | |
| 253 | |
| 254 // Cached images. The ResourceBundle caches all retrieved bitmaps and keeps | |
| 255 // ownership of the pointers. | |
| 256 typedef std::map<int, SkBitmap*> SkImageMap; | |
| 257 SkImageMap skia_images_; | |
| 258 #if defined(USE_X11) | |
| 259 typedef std::map<int, GdkPixbuf*> GdkPixbufMap; | |
| 260 GdkPixbufMap gdk_pixbufs_; | |
| 261 #endif | |
| 262 | |
| 263 // The various fonts used. Cached to avoid repeated GDI creation/destruction. | |
| 264 scoped_ptr<gfx::Font> base_font_; | |
| 265 scoped_ptr<gfx::Font> bold_font_; | |
| 266 scoped_ptr<gfx::Font> small_font_; | |
| 267 scoped_ptr<gfx::Font> medium_font_; | |
| 268 scoped_ptr<gfx::Font> medium_bold_font_; | |
| 269 scoped_ptr<gfx::Font> large_font_; | |
| 270 scoped_ptr<gfx::Font> web_font_; | |
| 271 | |
| 272 static ResourceBundle* g_shared_instance_; | |
| 273 | |
| 274 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); | |
| 275 }; | |
| 276 | |
| 277 #endif // APP_RESOURCE_BUNDLE_H_ | |
| OLD | NEW |