Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 5 #ifndef UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
| 6 #define UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 6 #define UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
| 19 #include "base/string16.h" | 19 #include "base/string16.h" |
| 20 #include "base/string_piece.h" | 20 #include "base/string_piece.h" |
| 21 #include "ui/base/ui_export.h" | 21 #include "ui/base/ui_export.h" |
| 22 #include "ui/gfx/font.h" | |
| 23 #include "ui/gfx/image/image.h" | |
| 22 #include "ui/gfx/native_widget_types.h" | 24 #include "ui/gfx/native_widget_types.h" |
| 23 | 25 |
| 24 class SkBitmap; | 26 class SkBitmap; |
| 25 | 27 |
| 26 namespace base { | 28 namespace base { |
| 27 class Lock; | 29 class Lock; |
| 28 class RefCountedStaticMemory; | 30 class RefCountedStaticMemory; |
| 29 } | 31 } |
| 30 | 32 |
| 31 namespace gfx { | |
| 32 class Font; | |
| 33 class Image; | |
| 34 } | |
| 35 | |
| 36 namespace ui { | 33 namespace ui { |
| 37 | 34 |
| 38 class ResourceHandle; | 35 class ResourceHandle; |
| 39 | 36 |
| 40 // ResourceBundle is a central facility to load images and other resources, | 37 // ResourceBundle is a central facility to load images and other resources, |
| 41 // such as theme graphics. Every resource is loaded only once. | 38 // such as theme graphics. Every resource is loaded only once. |
| 42 class UI_EXPORT ResourceBundle { | 39 class UI_EXPORT ResourceBundle { |
| 43 public: | 40 public: |
| 44 // An enumeration of the various font styles used throughout Chrome. | 41 // An enumeration of the various font styles used throughout Chrome. |
| 45 // The following holds true for the font sizes: | 42 // The following holds true for the font sizes: |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 56 LargeBoldFont, | 53 LargeBoldFont, |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 enum ImageRTL { | 56 enum ImageRTL { |
| 60 // Images are flipped in RTL locales. | 57 // Images are flipped in RTL locales. |
| 61 RTL_ENABLED, | 58 RTL_ENABLED, |
| 62 // Images are never flipped. | 59 // Images are never flipped. |
| 63 RTL_DISABLED, | 60 RTL_DISABLED, |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 // Initialize the ResourceBundle for this process. Returns the language | 63 class Delegate { |
| 67 // selected. | 64 public: |
| 65 // Retrieve the full path for the specified |pack_name|. |pack_path| will | |
| 66 // initially contain the default path for the pack file. Change it in this | |
| 67 // callback if a different path is desired. Return true to continue loading | |
| 68 // the pack file or false to cancel the load. | |
| 69 virtual bool GetPathForResourcePack(const std::string& pack_name, | |
| 70 FilePath* pack_path) = 0; | |
| 71 | |
| 72 // Retrieve the full path for the specified |locale|. |pack_path| will | |
| 73 // initially contain the default path for the pack file. Change it in this | |
| 74 // callback if a different path is desired. Return true to continue loading | |
| 75 // the pack file or false to cancel the load. | |
| 76 virtual bool GetPathForLocalePack(const std::string& locale, | |
| 77 FilePath* pack_path) = 0; | |
| 78 | |
| 79 // Return an image resource or NULL to attempt retrieval of the default | |
| 80 // resource. | |
| 81 virtual scoped_ptr<gfx::Image> GetImageNamed(int resource_id) = 0; | |
| 82 | |
| 83 // Return an image resource or NULL to attempt retrieval of the default | |
| 84 // resource. | |
| 85 virtual scoped_ptr<gfx::Image> GetNativeImageNamed(int resource_id, | |
| 86 ImageRTL rtl) = 0; | |
| 87 | |
| 88 // Return a static memory resource or NULL to attempt retrieval of the | |
| 89 // default resource. | |
| 90 virtual base::RefCountedStaticMemory* LoadDataResourceBytes( | |
| 91 int resource_id) = 0; | |
| 92 | |
| 93 // Retrieve a raw data resource. Return true if a resource was provided or | |
| 94 // false to attempt retrieval of the default resource. | |
| 95 virtual bool GetRawDataResource(int resource_id, | |
| 96 base::StringPiece* value) = 0; | |
| 97 | |
| 98 // Retrieve a localized string. Return true if a string was provided or | |
| 99 // false to attempt retrieval of the default string. | |
| 100 virtual bool GetLocalizedString(int message_id, string16* value) = 0; | |
| 101 | |
| 102 // Return a font resource or NULL to attempt retrieval of the default | |
| 103 // resource. | |
| 104 virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0; | |
| 105 | |
| 106 protected: | |
| 107 virtual ~Delegate() {} | |
| 108 }; | |
| 109 | |
| 110 // Initialize the ResourceBundle for this process. Does not take ownership of | |
| 111 // the |delegate| value. Returns the language selected. | |
| 68 // NOTE: Mac ignores this and always loads up resources for the language | 112 // NOTE: Mac ignores this and always loads up resources for the language |
| 69 // defined by the Cocoa UI (i.e., NSBundle does the language work). | 113 // defined by the Cocoa UI (i.e., NSBundle does the language work). |
| 70 static std::string InitSharedInstanceWithLocale( | 114 static std::string InitSharedInstanceWithLocale( |
| 71 const std::string& pref_locale); | 115 const std::string& pref_locale, Delegate* delegate); |
| 72 | 116 |
| 73 // Initialize the ResourceBundle using given data pack path for testing. | 117 // Initialize the ResourceBundle using given data pack path for testing. |
| 74 static void InitSharedInstanceWithPakFile(const FilePath& path); | 118 static void InitSharedInstanceWithPakFile(const FilePath& path); |
| 75 | 119 |
| 76 // Delete the ResourceBundle for this process if it exists. | 120 // Delete the ResourceBundle for this process if it exists. |
| 77 static void CleanupSharedInstance(); | 121 static void CleanupSharedInstance(); |
| 78 | 122 |
| 79 // Returns true after the global resource loader instance has been created. | 123 // Returns true after the global resource loader instance has been created. |
| 80 static bool HasSharedInstance(); | 124 static bool HasSharedInstance(); |
| 81 | 125 |
| 82 // Return the global resource loader instance. | 126 // Return the global resource loader instance. |
| 83 static ResourceBundle& GetSharedInstance(); | 127 static ResourceBundle& GetSharedInstance(); |
| 84 | 128 |
| 85 // Check if the .pak for the given locale exists. | 129 // Check if the .pak for the given locale exists. |
| 86 static bool LocaleDataPakExists(const std::string& locale); | 130 bool LocaleDataPakExists(const std::string& locale); |
| 87 | 131 |
| 88 // Registers additional data pack files with the global ResourceBundle. When | 132 // Registers additional data pack files with the global ResourceBundle. This |
|
tony
2012/05/01 15:56:48
Nit: Remove the word global since this operates on
Marshall
2012/05/01 17:29:41
Done.
| |
| 89 // looking for a DataResource, we will search these files after searching the | 133 // method is intended for use by ResourceBundle consumers and adds the data |
| 90 // main module. This method is not thread safe! You should call it | 134 // pack to the ResourceBundle without calling the ResourceBundle delegate. |
| 91 // immediately after calling InitSharedInstance. | 135 // These files will be seached in the order added when looking for resources. |
| 136 // This method is not thread safe and should be called immediately after | |
| 137 // calling the appropriate InitSharedInstance*() method. | |
| 92 void AddDataPack(const FilePath& path); | 138 void AddDataPack(const FilePath& path); |
| 93 | 139 |
| 94 // Changes the locale for an already-initialized ResourceBundle, returning the | 140 // Changes the locale for an already-initialized ResourceBundle, returning the |
| 95 // name of the newly-loaded locale. Future calls to get strings will return | 141 // name of the newly-loaded locale. Future calls to get strings will return |
| 96 // the strings for this new locale. This has no effect on existing or future | 142 // the strings for this new locale. This has no effect on existing or future |
| 97 // image resources. |locale_resources_data_| is protected by a lock for the | 143 // image resources. |locale_resources_data_| is protected by a lock for the |
| 98 // duration of the swap, as GetLocalizedString() may be concurrently invoked | 144 // duration of the swap, as GetLocalizedString() may be concurrently invoked |
| 99 // on another thread. | 145 // on another thread. |
| 100 std::string ReloadLocaleResources(const std::string& pref_locale); | 146 std::string ReloadLocaleResources(const std::string& pref_locale); |
| 101 | 147 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 void ReloadFonts(); | 190 void ReloadFonts(); |
| 145 | 191 |
| 146 // Overrides the path to the pak file from which the locale resources will be | 192 // Overrides the path to the pak file from which the locale resources will be |
| 147 // loaded. Pass an empty path to undo. | 193 // loaded. Pass an empty path to undo. |
| 148 void OverrideLocalePakForTest(const FilePath& pak_path); | 194 void OverrideLocalePakForTest(const FilePath& pak_path); |
| 149 | 195 |
| 150 private: | 196 private: |
| 151 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); | 197 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); |
| 152 | 198 |
| 153 // Ctor/dtor are private, since we're a singleton. | 199 // Ctor/dtor are private, since we're a singleton. |
| 154 ResourceBundle(); | 200 explicit ResourceBundle(Delegate* delegate); |
| 155 ~ResourceBundle(); | 201 ~ResourceBundle(); |
| 156 | 202 |
| 157 // Free skia_images_. | 203 // Free skia_images_. |
| 158 void FreeImages(); | 204 void FreeImages(); |
| 159 | 205 |
| 160 // Load the main resources. | 206 // Load the main resources. |
| 161 void LoadCommonResources(); | 207 void LoadCommonResources(); |
| 162 | 208 |
| 209 // Registers additional data pack files with the global ResourceBundle. This | |
|
tony
2012/05/01 15:56:48
Nit: Remove global here too?
Marshall
2012/05/01 17:29:41
Done.
| |
| 210 // method is intended for use when ResourceBundle loads pack files internally | |
| 211 // and will give the delegate an opportunity to modify or cancel the load. | |
| 212 // This method is not thread safe and should only be called from the platform | |
| 213 // LoadCommonResources() implementation. | |
| 214 void AddCommonDataPack(const std::string& pack_name, const FilePath& path); | |
| 215 | |
| 163 // Try to load the locale specific strings from an external data module. | 216 // Try to load the locale specific strings from an external data module. |
| 164 // Returns the locale that is loaded. | 217 // Returns the locale that is loaded. |
| 165 std::string LoadLocaleResources(const std::string& pref_locale); | 218 std::string LoadLocaleResources(const std::string& pref_locale); |
| 166 | 219 |
| 167 // Load test resources in given path. | 220 // Load test resources in given path. |
| 168 void LoadTestResources(const FilePath& path); | 221 void LoadTestResources(const FilePath& path); |
| 169 | 222 |
| 170 // Unload the locale specific strings and prepares to load new ones. See | 223 // Unload the locale specific strings and prepares to load new ones. See |
| 171 // comments for ReloadLocaleResources(). | 224 // comments for ReloadLocaleResources(). |
| 172 void UnloadLocaleResources(); | 225 void UnloadLocaleResources(); |
| 173 | 226 |
| 174 // Initialize all the gfx::Font members if they haven't yet been initialized. | 227 // Initialize all the gfx::Font members if they haven't yet been initialized. |
| 175 void LoadFontsIfNecessary(); | 228 void LoadFontsIfNecessary(); |
| 176 | 229 |
| 177 // Returns the full pathname of the locale file to load. May return an empty | 230 // Returns the full pathname of the locale file to load. May return an empty |
| 178 // string if no locale data files are found. | 231 // string if no locale data files are found. |
| 179 static FilePath GetLocaleFilePath(const std::string& app_locale); | 232 FilePath GetLocaleFilePath(const std::string& app_locale); |
| 180 | 233 |
| 181 // Creates and returns a new SkBitmap given the data file to look in and the | 234 // Creates and returns a new SkBitmap given the data file to look in and the |
| 182 // resource id. It's up to the caller to free the returned bitmap when | 235 // resource id. It's up to the caller to free the returned bitmap when |
| 183 // done. | 236 // done. |
| 184 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); | 237 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); |
| 185 | 238 |
| 186 // Returns an empty image for when a resource cannot be loaded. This is a | 239 // Returns an empty image for when a resource cannot be loaded. This is a |
| 187 // bright red bitmap. | 240 // bright red bitmap. |
| 188 gfx::Image* GetEmptyImage(); | 241 gfx::Image* GetEmptyImage(); |
| 189 | 242 |
| 190 const FilePath& GetOverriddenPakPath(); | 243 const FilePath& GetOverriddenPakPath(); |
| 191 | 244 |
| 245 // This pointer is guaranteed to outlive the ResourceBundle instance. | |
| 246 Delegate* delegate_; | |
| 247 | |
| 192 // Protects |images_| and font-related members. | 248 // Protects |images_| and font-related members. |
| 193 scoped_ptr<base::Lock> images_and_fonts_lock_; | 249 scoped_ptr<base::Lock> images_and_fonts_lock_; |
| 194 | 250 |
| 195 // Protects |locale_resources_data_|. | 251 // Protects |locale_resources_data_|. |
| 196 scoped_ptr<base::Lock> locale_resources_data_lock_; | 252 scoped_ptr<base::Lock> locale_resources_data_lock_; |
| 197 | 253 |
| 198 // Handles for data sources. | 254 // Handles for data sources. |
| 199 scoped_ptr<ResourceHandle> locale_resources_data_; | 255 scoped_ptr<ResourceHandle> locale_resources_data_; |
| 200 ScopedVector<ResourceHandle> data_packs_; | 256 ScopedVector<ResourceHandle> data_packs_; |
| 201 | 257 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 220 | 276 |
| 221 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); | 277 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); |
| 222 }; | 278 }; |
| 223 | 279 |
| 224 } // namespace ui | 280 } // namespace ui |
| 225 | 281 |
| 226 // TODO(beng): Someday, maybe, get rid of this. | 282 // TODO(beng): Someday, maybe, get rid of this. |
| 227 using ui::ResourceBundle; | 283 using ui::ResourceBundle; |
| 228 | 284 |
| 229 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 285 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
| OLD | NEW |