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/layout.h" |
21 #include "ui/base/ui_export.h" | 22 #include "ui/base/ui_export.h" |
22 #include "ui/gfx/font.h" | 23 #include "ui/gfx/font.h" |
23 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
24 #include "ui/gfx/native_widget_types.h" | 25 #include "ui/gfx/native_widget_types.h" |
25 | 26 |
26 class SkBitmap; | 27 class SkBitmap; |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 class Lock; | 30 class Lock; |
30 class RefCountedStaticMemory; | 31 class RefCountedStaticMemory; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 63 |
63 // Delegate class that allows interception of pack file loading and resource | 64 // Delegate class that allows interception of pack file loading and resource |
64 // requests. The methods of this class may be called on multiple threads. | 65 // requests. The methods of this class may be called on multiple threads. |
65 class Delegate { | 66 class Delegate { |
66 public: | 67 public: |
67 // Called before a resource pack file is loaded. Return the full path for | 68 // Called before a resource pack file is loaded. Return the full path for |
68 // the pack file to continue loading or an empty value to cancel loading. | 69 // the pack file to continue loading or an empty value to cancel loading. |
69 // |pack_path| will contain the complete default path for the pack file if | 70 // |pack_path| will contain the complete default path for the pack file if |
70 // known or just the pack file name otherwise. | 71 // known or just the pack file name otherwise. |
71 virtual FilePath GetPathForResourcePack(const FilePath& pack_path, | 72 virtual FilePath GetPathForResourcePack(const FilePath& pack_path, |
72 float scale_factor) = 0; | 73 ScaleFactor scale_factor) = 0; |
73 | 74 |
74 // Called before a locale pack file is loaded. Return the full path for | 75 // Called before a locale pack file is loaded. Return the full path for |
75 // the pack file to continue loading or an empty value to cancel loading. | 76 // the pack file to continue loading or an empty value to cancel loading. |
76 // |pack_path| will contain the complete default path for the pack file if | 77 // |pack_path| will contain the complete default path for the pack file if |
77 // known or just the pack file name otherwise. | 78 // known or just the pack file name otherwise. |
78 virtual FilePath GetPathForLocalePack(const FilePath& pack_path, | 79 virtual FilePath GetPathForLocalePack(const FilePath& pack_path, |
79 const std::string& locale) = 0; | 80 const std::string& locale) = 0; |
80 | 81 |
81 // Return an image resource or an empty value to attempt retrieval of the | 82 // Return an image resource or an empty value to attempt retrieval of the |
82 // default resource. | 83 // default resource. |
83 virtual gfx::Image GetImageNamed(int resource_id) = 0; | 84 virtual gfx::Image GetImageNamed(int resource_id) = 0; |
84 | 85 |
85 // Return an image resource or an empty value to attempt retrieval of the | 86 // Return an image resource or an empty value to attempt retrieval of the |
86 // default resource. | 87 // default resource. |
87 virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0; | 88 virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0; |
88 | 89 |
89 // Return a static memory resource or NULL to attempt retrieval of the | 90 // Return a static memory resource or NULL to attempt retrieval of the |
90 // default resource. | 91 // default resource. |
91 virtual base::RefCountedStaticMemory* LoadDataResourceBytes( | 92 virtual base::RefCountedStaticMemory* LoadDataResourceBytes( |
92 int resource_id) = 0; | 93 int resource_id, |
| 94 ScaleFactor scale_factor) = 0; |
93 | 95 |
94 // Retrieve a raw data resource. Return true if a resource was provided or | 96 // Retrieve a raw data resource. Return true if a resource was provided or |
95 // false to attempt retrieval of the default resource. | 97 // false to attempt retrieval of the default resource. |
96 virtual bool GetRawDataResource(int resource_id, | 98 virtual bool GetRawDataResource(int resource_id, |
| 99 ScaleFactor scale_factor, |
97 base::StringPiece* value) = 0; | 100 base::StringPiece* value) = 0; |
98 | 101 |
99 // Retrieve a localized string. Return true if a string was provided or | 102 // Retrieve a localized string. Return true if a string was provided or |
100 // false to attempt retrieval of the default string. | 103 // false to attempt retrieval of the default string. |
101 virtual bool GetLocalizedString(int message_id, string16* value) = 0; | 104 virtual bool GetLocalizedString(int message_id, string16* value) = 0; |
102 | 105 |
103 // Return a font resource or NULL to attempt retrieval of the default | 106 // Return a font resource or NULL to attempt retrieval of the default |
104 // resource. | 107 // resource. |
105 virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0; | 108 virtual scoped_ptr<gfx::Font> GetFont(FontStyle style) = 0; |
106 | 109 |
(...skipping 23 matching lines...) Expand all Loading... |
130 // Check if the .pak for the given locale exists. | 133 // Check if the .pak for the given locale exists. |
131 bool LocaleDataPakExists(const std::string& locale); | 134 bool LocaleDataPakExists(const std::string& locale); |
132 | 135 |
133 // Registers additional data pack files with this ResourceBundle. When | 136 // Registers additional data pack files with this ResourceBundle. When |
134 // looking for a DataResource, we will search these files after searching the | 137 // looking for a DataResource, we will search these files after searching the |
135 // main module. |path| should be the complete path to the pack file if known | 138 // main module. |path| should be the complete path to the pack file if known |
136 // or just the pack file name otherwise (the delegate may optionally override | 139 // or just the pack file name otherwise (the delegate may optionally override |
137 // this value). |scale_factor| is the scale of images in this resource pak | 140 // this value). |scale_factor| is the scale of images in this resource pak |
138 // relative to the images in the 1x resource pak. This method is not thread | 141 // relative to the images in the 1x resource pak. This method is not thread |
139 // safe! You should call it immediately after calling InitSharedInstance. | 142 // safe! You should call it immediately after calling InitSharedInstance. |
140 void AddDataPack(const FilePath& path, float scale_factor); | 143 void AddDataPack(const FilePath& path, ScaleFactor scale_factor); |
141 | 144 |
142 // Changes the locale for an already-initialized ResourceBundle, returning the | 145 // Changes the locale for an already-initialized ResourceBundle, returning the |
143 // name of the newly-loaded locale. Future calls to get strings will return | 146 // name of the newly-loaded locale. Future calls to get strings will return |
144 // the strings for this new locale. This has no effect on existing or future | 147 // the strings for this new locale. This has no effect on existing or future |
145 // image resources. |locale_resources_data_| is protected by a lock for the | 148 // image resources. |locale_resources_data_| is protected by a lock for the |
146 // duration of the swap, as GetLocalizedString() may be concurrently invoked | 149 // duration of the swap, as GetLocalizedString() may be concurrently invoked |
147 // on another thread. | 150 // on another thread. |
148 std::string ReloadLocaleResources(const std::string& pref_locale); | 151 std::string ReloadLocaleResources(const std::string& pref_locale); |
149 | 152 |
150 // Gets the bitmap with the specified resource_id from the current module | 153 // Gets the bitmap with the specified resource_id from the current module |
(...skipping 23 matching lines...) Expand all Loading... |
174 // Note that if the same resource has already been loaded in GetImageNamed(), | 177 // Note that if the same resource has already been loaded in GetImageNamed(), |
175 // gfx::Image will perform a conversion, rather than using the native image | 178 // gfx::Image will perform a conversion, rather than using the native image |
176 // loading code of ResourceBundle. | 179 // loading code of ResourceBundle. |
177 // | 180 // |
178 // If |rtl| is RTL_ENABLED then the image is flipped in RTL locales. | 181 // If |rtl| is RTL_ENABLED then the image is flipped in RTL locales. |
179 gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl); | 182 gfx::Image& GetNativeImageNamed(int resource_id, ImageRTL rtl); |
180 | 183 |
181 // Same as GetNativeImageNamed() except that RTL is not enabled. | 184 // Same as GetNativeImageNamed() except that RTL is not enabled. |
182 gfx::Image& GetNativeImageNamed(int resource_id); | 185 gfx::Image& GetNativeImageNamed(int resource_id); |
183 | 186 |
184 // Loads the raw bytes of a data resource into |bytes|, | 187 // Loads the raw bytes of a data resource nearest the scale factor |
185 // without doing any processing or interpretation of | 188 // |scale_factor| into |bytes|, without doing any processing or interpretation |
186 // the resource. Returns whether we successfully read the resource. | 189 // of the resource. Use ResourceHandle::SCALE_FACTOR_NONE for non-image |
187 base::RefCountedStaticMemory* LoadDataResourceBytes(int resource_id) const; | 190 // resources. Returns NULL if we fail to read the resource. |
| 191 base::RefCountedStaticMemory* LoadDataResourceBytes( |
| 192 int resource_id, |
| 193 ScaleFactor scale_factor) const; |
188 | 194 |
189 // Return the contents of a resource in a StringPiece given the resource id. | 195 // Return the contents of a resource in a StringPiece given the resource id |
190 base::StringPiece GetRawDataResource(int resource_id) const; | 196 // nearest the scale factor |scale_factor|. |
| 197 // Use ResourceHanlde::SCALE_FACTOR_NONE for non-image resources. |
| 198 base::StringPiece GetRawDataResource(int resource_id, |
| 199 ScaleFactor scale_factor) const; |
191 | 200 |
192 // Get a localized string given a message id. Returns an empty | 201 // Get a localized string given a message id. Returns an empty |
193 // string if the message_id is not found. | 202 // string if the message_id is not found. |
194 string16 GetLocalizedString(int message_id); | 203 string16 GetLocalizedString(int message_id); |
195 | 204 |
196 // Returns the font for the specified style. | 205 // Returns the font for the specified style. |
197 const gfx::Font& GetFont(FontStyle style); | 206 const gfx::Font& GetFont(FontStyle style); |
198 | 207 |
199 // Resets and reloads the cached fonts. This is useful when the fonts of the | 208 // Resets and reloads the cached fonts. This is useful when the fonts of the |
200 // system have changed, for example, when the locale has changed. | 209 // system have changed, for example, when the locale has changed. |
201 void ReloadFonts(); | 210 void ReloadFonts(); |
202 | 211 |
203 // Overrides the path to the pak file from which the locale resources will be | 212 // Overrides the path to the pak file from which the locale resources will be |
204 // loaded. Pass an empty path to undo. | 213 // loaded. Pass an empty path to undo. |
205 void OverrideLocalePakForTest(const FilePath& pak_path); | 214 void OverrideLocalePakForTest(const FilePath& pak_path); |
206 | 215 |
207 private: | 216 private: |
208 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForResourcePack); | 217 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForResourcePack); |
209 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForLocalePack); | 218 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForLocalePack); |
210 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetImageNamed); | 219 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetImageNamed); |
211 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetNativeImageNamed); | 220 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetNativeImageNamed); |
212 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateLoadDataResourceBytes); | 221 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateLoadDataResourceBytes); |
213 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource); | 222 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource); |
214 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString); | 223 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString); |
215 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont); | 224 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont); |
| 225 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, GetRawDataResource); |
216 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); | 226 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); |
217 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists); | 227 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists); |
218 | 228 |
219 // Ctor/dtor are private, since we're a singleton. | 229 // Ctor/dtor are private, since we're a singleton. |
220 explicit ResourceBundle(Delegate* delegate); | 230 explicit ResourceBundle(Delegate* delegate); |
221 ~ResourceBundle(); | 231 ~ResourceBundle(); |
222 | 232 |
223 // Free skia_images_. | 233 // Free skia_images_. |
224 void FreeImages(); | 234 void FreeImages(); |
225 | 235 |
226 // Load the main resources. | 236 // Load the main resources. |
227 void LoadCommonResources(); | 237 void LoadCommonResources(); |
228 | 238 |
229 // Try to load the locale specific strings from an external data module. | 239 // Try to load the locale specific strings from an external data module. |
230 // Returns the locale that is loaded. | 240 // Returns the locale that is loaded. |
231 std::string LoadLocaleResources(const std::string& pref_locale); | 241 std::string LoadLocaleResources(const std::string& pref_locale); |
232 | 242 |
233 // Load test resources in given path. | 243 // Load test resources in given paths. If either path is empty an empty |
234 void LoadTestResources(const FilePath& path); | 244 // resource pack is loaded. |
| 245 void LoadTestResources(const FilePath& path, const FilePath& locale_path); |
235 | 246 |
236 // Unload the locale specific strings and prepares to load new ones. See | 247 // Unload the locale specific strings and prepares to load new ones. See |
237 // comments for ReloadLocaleResources(). | 248 // comments for ReloadLocaleResources(). |
238 void UnloadLocaleResources(); | 249 void UnloadLocaleResources(); |
239 | 250 |
240 // Initialize all the gfx::Font members if they haven't yet been initialized. | 251 // Initialize all the gfx::Font members if they haven't yet been initialized. |
241 void LoadFontsIfNecessary(); | 252 void LoadFontsIfNecessary(); |
242 | 253 |
243 // Returns the full pathname of the locale file to load. May return an empty | 254 // Returns the full pathname of the locale file to load. May return an empty |
244 // string if no locale data files are found. | 255 // string if no locale data files are found. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 303 |
293 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); | 304 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); |
294 }; | 305 }; |
295 | 306 |
296 } // namespace ui | 307 } // namespace ui |
297 | 308 |
298 // TODO(beng): Someday, maybe, get rid of this. | 309 // TODO(beng): Someday, maybe, get rid of this. |
299 using ui::ResourceBundle; | 310 using ui::ResourceBundle; |
300 | 311 |
301 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 312 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
OLD | NEW |