Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: ui/base/resource/resource_bundle.h

Issue 10270023: Add new ResourceBundle::Delegate interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/base/l10n/l10n_util.cc ('k') | ui/base/resource/resource_bundle.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 // Delegate class that allows interception of pack file loading and resource
67 // selected. 64 // requests. The methods of this class may be called on multiple threads.
65 class Delegate {
66 public:
67 // 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 // |pack_path| will contain the complete default path for the pack file if
70 // known or just the pack file name otherwise.
71 virtual FilePath GetPathForResourcePack(const FilePath& pack_path,
72 float scale_factor) = 0;
73
74 // 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 // |pack_path| will contain the complete default path for the pack file if
77 // known or just the pack file name otherwise.
78 virtual FilePath GetPathForLocalePack(const FilePath& pack_path,
79 const std::string& locale) = 0;
80
81 // Return an image resource or an empty value to attempt retrieval of the
82 // default resource.
83 virtual gfx::Image GetImageNamed(int resource_id) = 0;
84
85 // Return an image resource or an empty value to attempt retrieval of the
86 // default resource.
87 virtual gfx::Image GetNativeImageNamed(int resource_id, 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);
87 132
88 // Registers additional data pack files with the global ResourceBundle. When 133 // Registers additional data pack files with this ResourceBundle. When
89 // looking for a DataResource, we will search these files after searching the 134 // looking for a DataResource, we will search these files after searching the
90 // main module. |scale_factor| is the scale of images in this resource pak 135 // 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
137 // this value). |scale_factor| is the scale of images in this resource pak
91 // relative to the images in the 1x resource pak. This method is not thread 138 // relative to the images in the 1x resource pak. This method is not thread
92 // safe! You should call it immediately after calling InitSharedInstance. 139 // safe! You should call it immediately after calling InitSharedInstance.
93 void AddDataPack(const FilePath& path, float scale_factor); 140 void AddDataPack(const FilePath& path, float scale_factor);
94 141
95 // Changes the locale for an already-initialized ResourceBundle, returning the 142 // Changes the locale for an already-initialized ResourceBundle, returning the
96 // name of the newly-loaded locale. Future calls to get strings will return 143 // 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 144 // 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 145 // image resources. |locale_resources_data_| is protected by a lock for the
99 // duration of the swap, as GetLocalizedString() may be concurrently invoked 146 // duration of the swap, as GetLocalizedString() may be concurrently invoked
100 // on another thread. 147 // on another thread.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 189
143 // Resets and reloads the cached fonts. This is useful when the fonts of the 190 // 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. 191 // system have changed, for example, when the locale has changed.
145 void ReloadFonts(); 192 void ReloadFonts();
146 193
147 // Overrides the path to the pak file from which the locale resources will be 194 // Overrides the path to the pak file from which the locale resources will be
148 // loaded. Pass an empty path to undo. 195 // loaded. Pass an empty path to undo.
149 void OverrideLocalePakForTest(const FilePath& pak_path); 196 void OverrideLocalePakForTest(const FilePath& pak_path);
150 197
151 private: 198 private:
199 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForResourcePack);
200 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetPathForLocalePack);
201 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetImageNamed);
202 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetNativeImageNamed);
203 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateLoadDataResourceBytes);
204 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetRawDataResource);
205 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetLocalizedString);
206 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, DelegateGetFont);
152 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); 207 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes);
208 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LocaleDataPakExists);
153 209
154 // Ctor/dtor are private, since we're a singleton. 210 // Ctor/dtor are private, since we're a singleton.
155 ResourceBundle(); 211 explicit ResourceBundle(Delegate* delegate);
156 ~ResourceBundle(); 212 ~ResourceBundle();
157 213
158 // Free skia_images_. 214 // Free skia_images_.
159 void FreeImages(); 215 void FreeImages();
160 216
161 // Load the main resources. 217 // Load the main resources.
162 void LoadCommonResources(); 218 void LoadCommonResources();
163 219
164 // Try to load the locale specific strings from an external data module. 220 // Try to load the locale specific strings from an external data module.
165 // Returns the locale that is loaded. 221 // Returns the locale that is loaded.
166 std::string LoadLocaleResources(const std::string& pref_locale); 222 std::string LoadLocaleResources(const std::string& pref_locale);
167 223
168 // Load test resources in given path. 224 // Load test resources in given path.
169 void LoadTestResources(const FilePath& path); 225 void LoadTestResources(const FilePath& path);
170 226
171 // Unload the locale specific strings and prepares to load new ones. See 227 // Unload the locale specific strings and prepares to load new ones. See
172 // comments for ReloadLocaleResources(). 228 // comments for ReloadLocaleResources().
173 void UnloadLocaleResources(); 229 void UnloadLocaleResources();
174 230
175 // Initialize all the gfx::Font members if they haven't yet been initialized. 231 // Initialize all the gfx::Font members if they haven't yet been initialized.
176 void LoadFontsIfNecessary(); 232 void LoadFontsIfNecessary();
177 233
178 // Returns the full pathname of the locale file to load. May return an empty 234 // Returns the full pathname of the locale file to load. May return an empty
179 // string if no locale data files are found. 235 // string if no locale data files are found.
180 static FilePath GetLocaleFilePath(const std::string& app_locale); 236 FilePath GetLocaleFilePath(const std::string& app_locale);
181 237
182 // Creates and returns a new SkBitmap given the data file to look in and the 238 // 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 239 // resource id. It's up to the caller to free the returned bitmap when
184 // done. 240 // done.
185 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); 241 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id);
186 242
187 // Returns an empty image for when a resource cannot be loaded. This is a 243 // Returns an empty image for when a resource cannot be loaded. This is a
188 // bright red bitmap. 244 // bright red bitmap.
189 gfx::Image* GetEmptyImage(); 245 gfx::Image& GetEmptyImage();
190 246
191 const FilePath& GetOverriddenPakPath(); 247 const FilePath& GetOverriddenPakPath();
192 248
249 // This pointer is guaranteed to outlive the ResourceBundle instance and may
250 // be NULL.
251 Delegate* delegate_;
252
193 // Protects |images_| and font-related members. 253 // Protects |images_| and font-related members.
194 scoped_ptr<base::Lock> images_and_fonts_lock_; 254 scoped_ptr<base::Lock> images_and_fonts_lock_;
195 255
196 // Protects |locale_resources_data_|. 256 // Protects |locale_resources_data_|.
197 scoped_ptr<base::Lock> locale_resources_data_lock_; 257 scoped_ptr<base::Lock> locale_resources_data_lock_;
198 258
199 // Handles for data sources. 259 // Handles for data sources.
200 scoped_ptr<ResourceHandle> locale_resources_data_; 260 scoped_ptr<ResourceHandle> locale_resources_data_;
201 ScopedVector<ResourceHandle> data_packs_; 261 ScopedVector<ResourceHandle> data_packs_;
202 262
203 // Cached images. The ResourceBundle caches all retrieved images and keeps 263 // Cached images. The ResourceBundle caches all retrieved images and keeps
204 // ownership of the pointers. 264 // ownership of the pointers.
205 typedef std::map<int, gfx::Image*> ImageMap; 265 typedef std::map<int, gfx::Image> ImageMap;
206 ImageMap images_; 266 ImageMap images_;
207 267
268 gfx::Image empty_image_;
269
208 // The various fonts used. Cached to avoid repeated GDI creation/destruction. 270 // The various fonts used. Cached to avoid repeated GDI creation/destruction.
209 scoped_ptr<gfx::Font> base_font_; 271 scoped_ptr<gfx::Font> base_font_;
210 scoped_ptr<gfx::Font> bold_font_; 272 scoped_ptr<gfx::Font> bold_font_;
211 scoped_ptr<gfx::Font> small_font_; 273 scoped_ptr<gfx::Font> small_font_;
212 scoped_ptr<gfx::Font> medium_font_; 274 scoped_ptr<gfx::Font> medium_font_;
213 scoped_ptr<gfx::Font> medium_bold_font_; 275 scoped_ptr<gfx::Font> medium_bold_font_;
214 scoped_ptr<gfx::Font> large_font_; 276 scoped_ptr<gfx::Font> large_font_;
215 scoped_ptr<gfx::Font> large_bold_font_; 277 scoped_ptr<gfx::Font> large_bold_font_;
216 scoped_ptr<gfx::Font> web_font_; 278 scoped_ptr<gfx::Font> web_font_;
217 279
218 static ResourceBundle* g_shared_instance_; 280 static ResourceBundle* g_shared_instance_;
219 281
220 FilePath overridden_pak_path_; 282 FilePath overridden_pak_path_;
221 283
222 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); 284 DISALLOW_COPY_AND_ASSIGN(ResourceBundle);
223 }; 285 };
224 286
225 } // namespace ui 287 } // namespace ui
226 288
227 // TODO(beng): Someday, maybe, get rid of this. 289 // TODO(beng): Someday, maybe, get rid of this.
228 using ui::ResourceBundle; 290 using ui::ResourceBundle;
229 291
230 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ 292 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_
OLDNEW
« no previous file with comments | « ui/base/l10n/l10n_util.cc ('k') | ui/base/resource/resource_bundle.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698