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 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 }; | 64 }; |
65 | 65 |
66 // Delegate class that allows interception of pack file loading and resource | 66 // Delegate class that allows interception of pack file loading and resource |
67 // requests. The methods of this class may be called on multiple threads. | 67 // requests. The methods of this class may be called on multiple threads. |
68 class Delegate { | 68 class Delegate { |
69 public: | 69 public: |
70 // Called before a resource pack file is loaded. Return the full path for | 70 // Called before a resource pack file is loaded. Return the full path for |
71 // the pack file to continue loading or an empty value to cancel loading. | 71 // the pack file to continue loading or an empty value to cancel loading. |
72 // |pack_path| will contain the complete default path for the pack file if | 72 // |pack_path| will contain the complete default path for the pack file if |
73 // known or just the pack file name otherwise. | 73 // known or just the pack file name otherwise. |
74 virtual FilePath GetPathForResourcePack(const FilePath& pack_path, | 74 virtual base::FilePath GetPathForResourcePack( |
75 ScaleFactor scale_factor) = 0; | 75 const base::FilePath& pack_path, |
| 76 ScaleFactor scale_factor) = 0; |
76 | 77 |
77 // Called before a locale pack file is loaded. Return the full path for | 78 // Called before a locale pack file is loaded. Return the full path for |
78 // the pack file to continue loading or an empty value to cancel loading. | 79 // the pack file to continue loading or an empty value to cancel loading. |
79 // |pack_path| will contain the complete default path for the pack file if | 80 // |pack_path| will contain the complete default path for the pack file if |
80 // known or just the pack file name otherwise. | 81 // known or just the pack file name otherwise. |
81 virtual FilePath GetPathForLocalePack(const FilePath& pack_path, | 82 virtual base::FilePath GetPathForLocalePack( |
82 const std::string& locale) = 0; | 83 const base::FilePath& pack_path, |
| 84 const std::string& locale) = 0; |
83 | 85 |
84 // 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 |
85 // default resource. | 87 // default resource. |
86 virtual gfx::Image GetImageNamed(int resource_id) = 0; | 88 virtual gfx::Image GetImageNamed(int resource_id) = 0; |
87 | 89 |
88 // Return an image resource or an empty value to attempt retrieval of the | 90 // Return an image resource or an empty value to attempt retrieval of the |
89 // default resource. | 91 // default resource. |
90 virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0; | 92 virtual gfx::Image GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0; |
91 | 93 |
92 // Return a static memory resource or NULL to attempt retrieval of the | 94 // Return a static memory resource or NULL to attempt retrieval of the |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 const std::string& pref_locale, Delegate* delegate); | 133 const std::string& pref_locale, Delegate* delegate); |
132 | 134 |
133 // Initialize the ResourceBundle using given file. The second argument | 135 // Initialize the ResourceBundle using given file. The second argument |
134 // controls whether or not ResourceBundle::LoadCommonResources is called. | 136 // controls whether or not ResourceBundle::LoadCommonResources is called. |
135 // This allows the use of this function in a sandbox without local file | 137 // This allows the use of this function in a sandbox without local file |
136 // access (as on Android). | 138 // access (as on Android). |
137 static void InitSharedInstanceWithPakFile( | 139 static void InitSharedInstanceWithPakFile( |
138 base::PlatformFile file, bool should_load_common_resources); | 140 base::PlatformFile file, bool should_load_common_resources); |
139 | 141 |
140 // Initialize the ResourceBundle using given data pack path for testing. | 142 // Initialize the ResourceBundle using given data pack path for testing. |
141 static void InitSharedInstanceWithPakPath(const FilePath& path); | 143 static void InitSharedInstanceWithPakPath(const base::FilePath& path); |
142 | 144 |
143 // Delete the ResourceBundle for this process if it exists. | 145 // Delete the ResourceBundle for this process if it exists. |
144 static void CleanupSharedInstance(); | 146 static void CleanupSharedInstance(); |
145 | 147 |
146 // Returns true after the global resource loader instance has been created. | 148 // Returns true after the global resource loader instance has been created. |
147 static bool HasSharedInstance(); | 149 static bool HasSharedInstance(); |
148 | 150 |
149 // Return the global resource loader instance. | 151 // Return the global resource loader instance. |
150 static ResourceBundle& GetSharedInstance(); | 152 static ResourceBundle& GetSharedInstance(); |
151 | 153 |
152 // Check if the .pak for the given locale exists. | 154 // Check if the .pak for the given locale exists. |
153 bool LocaleDataPakExists(const std::string& locale); | 155 bool LocaleDataPakExists(const std::string& locale); |
154 | 156 |
155 // Registers additional data pack files with this ResourceBundle. When | 157 // Registers additional data pack files with this ResourceBundle. When |
156 // looking for a DataResource, we will search these files after searching the | 158 // looking for a DataResource, we will search these files after searching the |
157 // main module. |path| should be the complete path to the pack file if known | 159 // main module. |path| should be the complete path to the pack file if known |
158 // or just the pack file name otherwise (the delegate may optionally override | 160 // or just the pack file name otherwise (the delegate may optionally override |
159 // this value). |scale_factor| is the scale of images in this resource pak | 161 // this value). |scale_factor| is the scale of images in this resource pak |
160 // relative to the images in the 1x resource pak. This method is not thread | 162 // relative to the images in the 1x resource pak. This method is not thread |
161 // safe! You should call it immediately after calling InitSharedInstance. | 163 // safe! You should call it immediately after calling InitSharedInstance. |
162 void AddDataPackFromPath(const FilePath& path, ScaleFactor scale_factor); | 164 void AddDataPackFromPath(const base::FilePath& path, |
| 165 ScaleFactor scale_factor); |
163 | 166 |
164 // Same as above but using an already open file. | 167 // Same as above but using an already open file. |
165 void AddDataPackFromFile(base::PlatformFile file, ScaleFactor scale_factor); | 168 void AddDataPackFromFile(base::PlatformFile file, ScaleFactor scale_factor); |
166 | 169 |
167 // Same as AddDataPackFromPath but does not log an error if the pack fails to | 170 // Same as AddDataPackFromPath but does not log an error if the pack fails to |
168 // load. | 171 // load. |
169 void AddOptionalDataPackFromPath(const FilePath& path, | 172 void AddOptionalDataPackFromPath(const base::FilePath& path, |
170 ScaleFactor scale_factor); | 173 ScaleFactor scale_factor); |
171 | 174 |
172 // Changes the locale for an already-initialized ResourceBundle, returning the | 175 // Changes the locale for an already-initialized ResourceBundle, returning the |
173 // name of the newly-loaded locale. Future calls to get strings will return | 176 // name of the newly-loaded locale. Future calls to get strings will return |
174 // the strings for this new locale. This has no effect on existing or future | 177 // the strings for this new locale. This has no effect on existing or future |
175 // image resources. |locale_resources_data_| is protected by a lock for the | 178 // image resources. |locale_resources_data_| is protected by a lock for the |
176 // duration of the swap, as GetLocalizedString() may be concurrently invoked | 179 // duration of the swap, as GetLocalizedString() may be concurrently invoked |
177 // on another thread. | 180 // on another thread. |
178 std::string ReloadLocaleResources(const std::string& pref_locale); | 181 std::string ReloadLocaleResources(const std::string& pref_locale); |
179 | 182 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 | 235 |
233 // Returns the font for the specified style. | 236 // Returns the font for the specified style. |
234 const gfx::Font& GetFont(FontStyle style); | 237 const gfx::Font& GetFont(FontStyle style); |
235 | 238 |
236 // Resets and reloads the cached fonts. This is useful when the fonts of the | 239 // Resets and reloads the cached fonts. This is useful when the fonts of the |
237 // system have changed, for example, when the locale has changed. | 240 // system have changed, for example, when the locale has changed. |
238 void ReloadFonts(); | 241 void ReloadFonts(); |
239 | 242 |
240 // Overrides the path to the pak file from which the locale resources will be | 243 // Overrides the path to the pak file from which the locale resources will be |
241 // loaded. Pass an empty path to undo. | 244 // loaded. Pass an empty path to undo. |
242 void OverrideLocalePakForTest(const FilePath& pak_path); | 245 void OverrideLocalePakForTest(const base::FilePath& pak_path); |
243 | 246 |
244 // Returns the full pathname of the locale file to load. May return an empty | 247 // Returns the full pathname of the locale file to load. May return an empty |
245 // string if no locale data files are found and |test_file_exists| is true. | 248 // string if no locale data files are found and |test_file_exists| is true. |
246 // Used on Android to load the local file in the browser process and pass it | 249 // Used on Android to load the local file in the browser process and pass it |
247 // to the sandboxed renderer process. | 250 // to the sandboxed renderer process. |
248 FilePath GetLocaleFilePath(const std::string& app_locale, | 251 base::FilePath GetLocaleFilePath(const std::string& app_locale, |
249 bool test_file_exists); | 252 bool test_file_exists); |
250 | 253 |
251 // Returns the maximum scale factor currently loaded. | 254 // Returns the maximum scale factor currently loaded. |
252 // Returns SCALE_FACTOR_100P if no resource is loaded. | 255 // Returns SCALE_FACTOR_100P if no resource is loaded. |
253 ScaleFactor max_scale_factor() const { | 256 ScaleFactor max_scale_factor() const { |
254 return max_scale_factor_; | 257 return max_scale_factor_; |
255 } | 258 } |
256 | 259 |
257 private: | 260 private: |
258 FRIEND_TEST_ALL_PREFIXES(ResourceBundleTest, DelegateGetPathForLocalePack); | 261 FRIEND_TEST_ALL_PREFIXES(ResourceBundleTest, DelegateGetPathForLocalePack); |
(...skipping 11 matching lines...) Expand all Loading... |
270 ~ResourceBundle(); | 273 ~ResourceBundle(); |
271 | 274 |
272 // Free skia_images_. | 275 // Free skia_images_. |
273 void FreeImages(); | 276 void FreeImages(); |
274 | 277 |
275 // Load the main resources. | 278 // Load the main resources. |
276 void LoadCommonResources(); | 279 void LoadCommonResources(); |
277 | 280 |
278 // Implementation for AddDataPackFromPath and AddOptionalDataPackFromPath, if | 281 // Implementation for AddDataPackFromPath and AddOptionalDataPackFromPath, if |
279 // the pack is not |optional| logs an error on failure to load. | 282 // the pack is not |optional| logs an error on failure to load. |
280 void AddDataPackFromPathInternal(const FilePath& path, | 283 void AddDataPackFromPathInternal(const base::FilePath& path, |
281 ScaleFactor scale_factor, | 284 ScaleFactor scale_factor, |
282 bool optional); | 285 bool optional); |
283 | 286 |
284 // Inserts |data_pack| to |data_pack_| and updates |max_scale_factor_| | 287 // Inserts |data_pack| to |data_pack_| and updates |max_scale_factor_| |
285 // accordingly. | 288 // accordingly. |
286 void AddDataPack(DataPack* data_pack); | 289 void AddDataPack(DataPack* data_pack); |
287 | 290 |
288 // Try to load the locale specific strings from an external data module. | 291 // Try to load the locale specific strings from an external data module. |
289 // Returns the locale that is loaded. | 292 // Returns the locale that is loaded. |
290 std::string LoadLocaleResources(const std::string& pref_locale); | 293 std::string LoadLocaleResources(const std::string& pref_locale); |
291 | 294 |
292 // Load test resources in given paths. If either path is empty an empty | 295 // Load test resources in given paths. If either path is empty an empty |
293 // resource pack is loaded. | 296 // resource pack is loaded. |
294 void LoadTestResources(const FilePath& path, const FilePath& locale_path); | 297 void LoadTestResources(const base::FilePath& path, |
| 298 const base::FilePath& locale_path); |
295 | 299 |
296 // Unload the locale specific strings and prepares to load new ones. See | 300 // Unload the locale specific strings and prepares to load new ones. See |
297 // comments for ReloadLocaleResources(). | 301 // comments for ReloadLocaleResources(). |
298 void UnloadLocaleResources(); | 302 void UnloadLocaleResources(); |
299 | 303 |
300 // Initialize all the gfx::Font members if they haven't yet been initialized. | 304 // Initialize all the gfx::Font members if they haven't yet been initialized. |
301 void LoadFontsIfNecessary(); | 305 void LoadFontsIfNecessary(); |
302 | 306 |
303 // Fills the |bitmap| given the data file to look in and the |resource_id|. | 307 // Fills the |bitmap| given the data file to look in and the |resource_id|. |
304 // Returns false if the resource does not exist. | 308 // Returns false if the resource does not exist. |
(...skipping 28 matching lines...) Expand all Loading... |
333 // should occur immediately after the IHDR chunk. | 337 // should occur immediately after the IHDR chunk. |
334 static bool DecodePNG(const unsigned char* buf, | 338 static bool DecodePNG(const unsigned char* buf, |
335 size_t size, | 339 size_t size, |
336 SkBitmap* bitmap, | 340 SkBitmap* bitmap, |
337 bool* fell_back_to_1x); | 341 bool* fell_back_to_1x); |
338 | 342 |
339 // Returns an empty image for when a resource cannot be loaded. This is a | 343 // Returns an empty image for when a resource cannot be loaded. This is a |
340 // bright red bitmap. | 344 // bright red bitmap. |
341 gfx::Image& GetEmptyImage(); | 345 gfx::Image& GetEmptyImage(); |
342 | 346 |
343 const FilePath& GetOverriddenPakPath(); | 347 const base::FilePath& GetOverriddenPakPath(); |
344 | 348 |
345 // This pointer is guaranteed to outlive the ResourceBundle instance and may | 349 // This pointer is guaranteed to outlive the ResourceBundle instance and may |
346 // be NULL. | 350 // be NULL. |
347 Delegate* delegate_; | 351 Delegate* delegate_; |
348 | 352 |
349 // Protects |images_| and font-related members. | 353 // Protects |images_| and font-related members. |
350 scoped_ptr<base::Lock> images_and_fonts_lock_; | 354 scoped_ptr<base::Lock> images_and_fonts_lock_; |
351 | 355 |
352 // Protects |locale_resources_data_|. | 356 // Protects |locale_resources_data_|. |
353 scoped_ptr<base::Lock> locale_resources_data_lock_; | 357 scoped_ptr<base::Lock> locale_resources_data_lock_; |
(...skipping 16 matching lines...) Expand all Loading... |
370 scoped_ptr<gfx::Font> base_font_; | 374 scoped_ptr<gfx::Font> base_font_; |
371 scoped_ptr<gfx::Font> bold_font_; | 375 scoped_ptr<gfx::Font> bold_font_; |
372 scoped_ptr<gfx::Font> small_font_; | 376 scoped_ptr<gfx::Font> small_font_; |
373 scoped_ptr<gfx::Font> small_bold_font_; | 377 scoped_ptr<gfx::Font> small_bold_font_; |
374 scoped_ptr<gfx::Font> medium_font_; | 378 scoped_ptr<gfx::Font> medium_font_; |
375 scoped_ptr<gfx::Font> medium_bold_font_; | 379 scoped_ptr<gfx::Font> medium_bold_font_; |
376 scoped_ptr<gfx::Font> large_font_; | 380 scoped_ptr<gfx::Font> large_font_; |
377 scoped_ptr<gfx::Font> large_bold_font_; | 381 scoped_ptr<gfx::Font> large_bold_font_; |
378 scoped_ptr<gfx::Font> web_font_; | 382 scoped_ptr<gfx::Font> web_font_; |
379 | 383 |
380 FilePath overridden_pak_path_; | 384 base::FilePath overridden_pak_path_; |
381 | 385 |
382 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); | 386 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); |
383 }; | 387 }; |
384 | 388 |
385 } // namespace ui | 389 } // namespace ui |
386 | 390 |
387 // TODO(beng): Someday, maybe, get rid of this. | 391 // TODO(beng): Someday, maybe, get rid of this. |
388 using ui::ResourceBundle; | 392 using ui::ResourceBundle; |
389 | 393 |
390 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ | 394 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ |
OLD | NEW |