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 { |
|
tony
2012/05/02 20:41:20
Are delegates expected to deal with thread safety
Marshall
2012/05/03 19:10:54
Yes, the delegate will need to deal with thread sa
| |
| 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, | |
| 71 float scale_factor) = 0; | |
|
tony
2012/05/02 20:41:20
I would keep pack_name as a FilePath. What if we
Marshall
2012/05/03 19:10:54
Done.
| |
| 72 | |
| 73 // Retrieve the full path for the specified |locale|. |pack_path| will | |
| 74 // initially contain the default path for the pack file. Change it in this | |
| 75 // callback if a different path is desired. Return true to continue loading | |
| 76 // the pack file or false to cancel the load. | |
| 77 virtual bool GetPathForLocalePack(const std::string& locale, | |
| 78 FilePath* pack_path) = 0; | |
|
tony
2012/05/02 20:41:20
I would just return a FilePath (empty to cancel th
Marshall
2012/05/03 19:10:54
Done.
| |
| 79 | |
| 80 // Return an image resource or NULL to attempt retrieval of the default | |
| 81 // resource. | |
| 82 virtual scoped_ptr<gfx::Image> GetImageNamed(int resource_id) = 0; | |
| 83 | |
| 84 // Return an image resource or NULL to attempt retrieval of the default | |
| 85 // resource. | |
| 86 virtual scoped_ptr<gfx::Image> GetNativeImageNamed(int resource_id, | |
| 87 ImageRTL rtl) = 0; | |
| 88 | |
| 89 // Return a static memory resource or NULL to attempt retrieval of the | |
| 90 // default resource. | |
| 91 virtual base::RefCountedStaticMemory* LoadDataResourceBytes( | |
| 92 int resource_id) = 0; | |
| 93 | |
| 94 // Retrieve a raw data resource. Return true if a resource was provided or | |
| 95 // false to attempt retrieval of the default resource. | |
| 96 virtual bool GetRawDataResource(int resource_id, | |
| 97 base::StringPiece* value) = 0; | |
| 98 | |
| 99 // Retrieve a localized string. Return true if a string was provided or | |
| 100 // false to attempt retrieval of the default string. | |
| 101 virtual bool GetLocalizedString(int message_id, string16* value) = 0; | |
| 102 | |
| 103 // Return a font resource or NULL to attempt retrieval of the default | |
| 104 // resource. | |
| 105 virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0; | |
| 106 | |
| 107 protected: | |
| 108 virtual ~Delegate() {} | |
| 109 }; | |
| 110 | |
| 111 // Initialize the ResourceBundle for this process. Does not take ownership of | |
| 112 // the |delegate| value. Returns the language selected. | |
| 68 // NOTE: Mac ignores this and always loads up resources for the language | 113 // 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). | 114 // defined by the Cocoa UI (i.e., NSBundle does the language work). |
| 70 static std::string InitSharedInstanceWithLocale( | 115 static std::string InitSharedInstanceWithLocale( |
| 71 const std::string& pref_locale); | 116 const std::string& pref_locale, Delegate* delegate); |
| 72 | 117 |
| 73 // Initialize the ResourceBundle using given data pack path for testing. | 118 // Initialize the ResourceBundle using given data pack path for testing. |
| 74 static void InitSharedInstanceWithPakFile(const FilePath& path); | 119 static void InitSharedInstanceWithPakFile(const FilePath& path); |
| 75 | 120 |
| 76 // Delete the ResourceBundle for this process if it exists. | 121 // Delete the ResourceBundle for this process if it exists. |
| 77 static void CleanupSharedInstance(); | 122 static void CleanupSharedInstance(); |
| 78 | 123 |
| 79 // Returns true after the global resource loader instance has been created. | 124 // Returns true after the global resource loader instance has been created. |
| 80 static bool HasSharedInstance(); | 125 static bool HasSharedInstance(); |
| 81 | 126 |
| 82 // Return the global resource loader instance. | 127 // Return the global resource loader instance. |
| 83 static ResourceBundle& GetSharedInstance(); | 128 static ResourceBundle& GetSharedInstance(); |
| 84 | 129 |
| 85 // Check if the .pak for the given locale exists. | 130 // Check if the .pak for the given locale exists. |
| 86 static bool LocaleDataPakExists(const std::string& locale); | 131 bool LocaleDataPakExists(const std::string& locale); |
|
tony
2012/05/02 20:41:20
Does this need to be public?
Marshall
2012/05/03 19:10:54
It's used by l10n_util.cc currently.
| |
| 87 | 132 |
| 88 // Registers additional data pack files with the global ResourceBundle. When | 133 // Registers additional data pack files with the ResourceBundle. When looking |
| 89 // looking for a DataResource, we will search these files after searching the | 134 // for a DataResource, we will search these files after searching the main |
| 90 // main module. |scale_factor| is the scale of images in this resource pak | 135 // module. |pack_name| is the name of the pack file (minus the extension) |
| 91 // relative to the images in the 1x resource pak. This method is not thread | 136 // that will be passed to the delegate. |scale_factor| is the scale of images |
| 92 // safe! You should call it immediately after calling InitSharedInstance. | 137 // in this resource pak relative to the images in the 1x resource pak. This |
| 93 void AddDataPack(const FilePath& path, float scale_factor); | 138 // method is not thread safe! You should call it immediately after calling |
| 139 // InitSharedInstance. | |
| 140 void AddDataPack(const std::string& pack_name, | |
|
tony
2012/05/02 20:41:20
I would leave this interface unchanged and and spl
Marshall
2012/05/03 19:10:54
Done.
| |
| 141 const FilePath& path, | |
| 142 float scale_factor); | |
| 94 | 143 |
| 95 // Changes the locale for an already-initialized ResourceBundle, returning the | 144 // Changes the locale for an already-initialized ResourceBundle, returning the |
| 96 // name of the newly-loaded locale. Future calls to get strings will return | 145 // name of the newly-loaded locale. Future calls to get strings will return |
| 97 // the strings for this new locale. This has no effect on existing or future | 146 // the strings for this new locale. This has no effect on existing or future |
| 98 // image resources. |locale_resources_data_| is protected by a lock for the | 147 // image resources. |locale_resources_data_| is protected by a lock for the |
| 99 // duration of the swap, as GetLocalizedString() may be concurrently invoked | 148 // duration of the swap, as GetLocalizedString() may be concurrently invoked |
| 100 // on another thread. | 149 // on another thread. |
| 101 std::string ReloadLocaleResources(const std::string& pref_locale); | 150 std::string ReloadLocaleResources(const std::string& pref_locale); |
| 102 | 151 |
| 103 // Gets the bitmap with the specified resource_id from the current module | 152 // Gets the bitmap with the specified resource_id from the current module |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 191 |
| 143 // Resets and reloads the cached fonts. This is useful when the fonts of the | 192 // Resets and reloads the cached fonts. This is useful when the fonts of the |
| 144 // system have changed, for example, when the locale has changed. | 193 // system have changed, for example, when the locale has changed. |
| 145 void ReloadFonts(); | 194 void ReloadFonts(); |
| 146 | 195 |
| 147 // Overrides the path to the pak file from which the locale resources will be | 196 // Overrides the path to the pak file from which the locale resources will be |
| 148 // loaded. Pass an empty path to undo. | 197 // loaded. Pass an empty path to undo. |
| 149 void OverrideLocalePakForTest(const FilePath& pak_path); | 198 void OverrideLocalePakForTest(const FilePath& pak_path); |
| 150 | 199 |
| 151 private: | 200 private: |
| 201 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForResourcePack); | |
| 202 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForLocalePack); | |
| 203 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetImageNamed); | |
| 204 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetNativeImageNamed); | |
| 205 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateLoadDataResourceBytes); | |
| 206 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource); | |
| 207 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString); | |
| 208 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont); | |
| 152 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); | 209 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); |
| 210 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists); | |
| 153 | 211 |
| 154 // Ctor/dtor are private, since we're a singleton. | 212 // Ctor/dtor are private, since we're a singleton. |
| 155 ResourceBundle(); | 213 explicit ResourceBundle(Delegate* delegate); |
| 156 ~ResourceBundle(); | 214 ~ResourceBundle(); |
| 157 | 215 |
| 158 // Free skia_images_. | 216 // Free skia_images_. |
| 159 void FreeImages(); | 217 void FreeImages(); |
| 160 | 218 |
| 161 // Load the main resources. | 219 // Load the main resources. |
| 162 void LoadCommonResources(); | 220 void LoadCommonResources(); |
| 163 | 221 |
| 164 // Try to load the locale specific strings from an external data module. | 222 // Try to load the locale specific strings from an external data module. |
| 165 // Returns the locale that is loaded. | 223 // Returns the locale that is loaded. |
| 166 std::string LoadLocaleResources(const std::string& pref_locale); | 224 std::string LoadLocaleResources(const std::string& pref_locale); |
| 167 | 225 |
| 168 // Load test resources in given path. | 226 // Load test resources in given path. |
| 169 void LoadTestResources(const FilePath& path); | 227 void LoadTestResources(const FilePath& path); |
| 170 | 228 |
| 171 // Unload the locale specific strings and prepares to load new ones. See | 229 // Unload the locale specific strings and prepares to load new ones. See |
| 172 // comments for ReloadLocaleResources(). | 230 // comments for ReloadLocaleResources(). |
| 173 void UnloadLocaleResources(); | 231 void UnloadLocaleResources(); |
| 174 | 232 |
| 175 // Initialize all the gfx::Font members if they haven't yet been initialized. | 233 // Initialize all the gfx::Font members if they haven't yet been initialized. |
| 176 void LoadFontsIfNecessary(); | 234 void LoadFontsIfNecessary(); |
| 177 | 235 |
| 178 // Returns the full pathname of the locale file to load. May return an empty | 236 // Returns the full pathname of the locale file to load. May return an empty |
| 179 // string if no locale data files are found. | 237 // string if no locale data files are found. |
| 180 static FilePath GetLocaleFilePath(const std::string& app_locale); | 238 FilePath GetLocaleFilePath(const std::string& app_locale); |
| 181 | 239 |
| 182 // Creates and returns a new SkBitmap given the data file to look in and the | 240 // Creates and returns a new SkBitmap given the data file to look in and the |
| 183 // resource id. It's up to the caller to free the returned bitmap when | 241 // resource id. It's up to the caller to free the returned bitmap when |
| 184 // done. | 242 // done. |
| 185 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); | 243 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); |
| 186 | 244 |
| 187 // Returns an empty image for when a resource cannot be loaded. This is a | 245 // Returns an empty image for when a resource cannot be loaded. This is a |
| 188 // bright red bitmap. | 246 // bright red bitmap. |
| 189 gfx::Image* GetEmptyImage(); | 247 gfx::Image* GetEmptyImage(); |
| 190 | 248 |
| 191 const FilePath& GetOverriddenPakPath(); | 249 const FilePath& GetOverriddenPakPath(); |
| 192 | 250 |
| 251 // This pointer is guaranteed to outlive the ResourceBundle instance. | |
|
tony
2012/05/02 20:41:20
I would add that it can be NULL.
Marshall
2012/05/03 19:10:54
Done.
| |
| 252 Delegate* delegate_; | |
| 253 | |
| 193 // Protects |images_| and font-related members. | 254 // Protects |images_| and font-related members. |
| 194 scoped_ptr<base::Lock> images_and_fonts_lock_; | 255 scoped_ptr<base::Lock> images_and_fonts_lock_; |
| 195 | 256 |
| 196 // Protects |locale_resources_data_|. | 257 // Protects |locale_resources_data_|. |
| 197 scoped_ptr<base::Lock> locale_resources_data_lock_; | 258 scoped_ptr<base::Lock> locale_resources_data_lock_; |
| 198 | 259 |
| 199 // Handles for data sources. | 260 // Handles for data sources. |
| 200 scoped_ptr<ResourceHandle> locale_resources_data_; | 261 scoped_ptr<ResourceHandle> locale_resources_data_; |
| 201 ScopedVector<ResourceHandle> data_packs_; | 262 ScopedVector<ResourceHandle> data_packs_; |
| 202 | 263 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 221 | 282 |
| 222 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); | 283 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); |
| 223 }; | 284 }; |
| 224 | 285 |
| 225 } // namespace ui | 286 } // namespace ui |
| 226 | 287 |
| 227 // TODO(beng): Someday, maybe, get rid of this. | 288 // TODO(beng): Someday, maybe, get rid of this. |
| 228 using ui::ResourceBundle; | 289 using ui::ResourceBundle; |
| 229 | 290 |
| 230 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 291 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
| OLD | NEW |