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

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
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 LargeBoldFont, 56 LargeBoldFont,
57 }; 57 };
58 58
59 enum ImageRTL { 59 enum ImageRTL {
60 // Images are flipped in RTL locales. 60 // Images are flipped in RTL locales.
61 RTL_ENABLED, 61 RTL_ENABLED,
62 // Images are never flipped. 62 // Images are never flipped.
63 RTL_DISABLED, 63 RTL_DISABLED,
64 }; 64 };
65 65
66 // Initialize the ResourceBundle for this process. Returns the language 66 class Delegate {
tfarina 2012/04/30 20:42:33 nit: please, move this to a separate class. ui/ba
Marshall 2012/04/30 20:55:43 What advantage do you see in having a separate res
67 // selected. 67 public:
68 virtual ~Delegate() {}
tfarina 2012/04/30 20:42:33 nit: could you make this virtual destructor protec
Marshall 2012/04/30 20:55:43 Done.
69
70 // Retrieve the full path for the specified |pack_name|. |pack_path| will
71 // initially contain the default path for the pack file. Change it in this
72 // callback if a different path is desired. Return true to continue loading
73 // the pack file or false to cancel the load.
74 virtual bool GetPathForResourcePack(const std::string& pack_name,
75 FilePath* pack_path) = 0;
76
77 // Retrieve the full path for the specified |locale|. |pack_path| will
78 // initially contain the default path for the pack file. Change it in this
79 // callback if a different path is desired. Return true to continue loading
80 // the pack file or false to cancel the load.
81 virtual bool GetPathForLocalePack(const std::string& locale,
82 FilePath* pack_path) = 0;
83
84 // Return an image resource or NULL to attempt retrieval of the default
85 // resource. The returned value will be owned by ResourceBundle.
86 virtual gfx::Image* GetImageNamed(int resource_id) = 0;
87
88 // Return an image resource or NULL to attempt retrieval of the default
89 // resource. The returned value will be owned by ResourceBundle.
90 virtual gfx::Image* GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0;
91
92 // Return a static memory resource or NULL to attempt retrieval of the
93 // default resource.
94 virtual base::RefCountedStaticMemory* LoadDataResourceBytes(
95 int resource_id) = 0;
96
97 // Retrieve a raw data resource. Return true if a resource was provided or
98 // false to attempt retrieval of the default resource.
99 virtual bool GetRawDataResource(int resource_id,
100 base::StringPiece* value) = 0;
101
102 // Retrieve a localized string. Return true if a string was provided or
103 // false to attempt retrieval of the default string.
104 virtual bool GetLocalizedString(int message_id, string16* value) = 0;
105
106 // Return a font resource or NULL to attempt retrieval of the default
107 // resource. The returned value will be owned by ResourceBundle.
108 virtual gfx::Font* GetFont(FontStyle style) = 0;
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 the global 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. This method is not thread safe! You should call it 135 // main module. This method is not thread safe! You should call it
91 // immediately after calling InitSharedInstance. 136 // immediately after calling InitSharedInstance.
92 void AddDataPack(const FilePath& path); 137 void AddDataPack(const FilePath& path);
93 138
94 // Changes the locale for an already-initialized ResourceBundle, returning the 139 // Changes the locale for an already-initialized ResourceBundle, returning the
95 // name of the newly-loaded locale. Future calls to get strings will return 140 // 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 141 // the strings for this new locale. This has no effect on existing or future
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 void ReloadFonts(); 189 void ReloadFonts();
145 190
146 // Overrides the path to the pak file from which the locale resources will be 191 // Overrides the path to the pak file from which the locale resources will be
147 // loaded. Pass an empty path to undo. 192 // loaded. Pass an empty path to undo.
148 void OverrideLocalePakForTest(const FilePath& pak_path); 193 void OverrideLocalePakForTest(const FilePath& pak_path);
149 194
150 private: 195 private:
151 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); 196 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes);
152 197
153 // Ctor/dtor are private, since we're a singleton. 198 // Ctor/dtor are private, since we're a singleton.
154 ResourceBundle(); 199 explicit ResourceBundle(Delegate* delegate);
155 ~ResourceBundle(); 200 ~ResourceBundle();
156 201
157 // Free skia_images_. 202 // Free skia_images_.
158 void FreeImages(); 203 void FreeImages();
159 204
160 // Load the main resources. 205 // Load the main resources.
161 void LoadCommonResources(); 206 void LoadCommonResources();
162 207
208 // Add a common data pack to the ResourceBundle. Gives the delegate an
209 // opportunity to modify the load.
210 void AddCommonDataPack(const std::string& pack_name, const FilePath& path);
211
163 // Try to load the locale specific strings from an external data module. 212 // Try to load the locale specific strings from an external data module.
164 // Returns the locale that is loaded. 213 // Returns the locale that is loaded.
165 std::string LoadLocaleResources(const std::string& pref_locale); 214 std::string LoadLocaleResources(const std::string& pref_locale);
166 215
167 // Load test resources in given path. 216 // Load test resources in given path.
168 void LoadTestResources(const FilePath& path); 217 void LoadTestResources(const FilePath& path);
169 218
170 // Unload the locale specific strings and prepares to load new ones. See 219 // Unload the locale specific strings and prepares to load new ones. See
171 // comments for ReloadLocaleResources(). 220 // comments for ReloadLocaleResources().
172 void UnloadLocaleResources(); 221 void UnloadLocaleResources();
173 222
174 // Initialize all the gfx::Font members if they haven't yet been initialized. 223 // Initialize all the gfx::Font members if they haven't yet been initialized.
175 void LoadFontsIfNecessary(); 224 void LoadFontsIfNecessary();
176 225
177 // Returns the full pathname of the locale file to load. May return an empty 226 // Returns the full pathname of the locale file to load. May return an empty
178 // string if no locale data files are found. 227 // string if no locale data files are found.
179 static FilePath GetLocaleFilePath(const std::string& app_locale); 228 FilePath GetLocaleFilePath(const std::string& app_locale);
180 229
181 // Creates and returns a new SkBitmap given the data file to look in and the 230 // 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 231 // resource id. It's up to the caller to free the returned bitmap when
183 // done. 232 // done.
184 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); 233 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id);
185 234
186 // Returns an empty image for when a resource cannot be loaded. This is a 235 // Returns an empty image for when a resource cannot be loaded. This is a
187 // bright red bitmap. 236 // bright red bitmap.
188 gfx::Image* GetEmptyImage(); 237 gfx::Image* GetEmptyImage();
189 238
190 const FilePath& GetOverriddenPakPath(); 239 const FilePath& GetOverriddenPakPath();
191 240
241 // This pointer is guaranteed to outlive the ResourceBundle instance.
242 Delegate* delegate_;
243
192 // Protects |images_| and font-related members. 244 // Protects |images_| and font-related members.
193 scoped_ptr<base::Lock> images_and_fonts_lock_; 245 scoped_ptr<base::Lock> images_and_fonts_lock_;
194 246
195 // Protects |locale_resources_data_|. 247 // Protects |locale_resources_data_|.
196 scoped_ptr<base::Lock> locale_resources_data_lock_; 248 scoped_ptr<base::Lock> locale_resources_data_lock_;
197 249
198 // Handles for data sources. 250 // Handles for data sources.
199 scoped_ptr<ResourceHandle> locale_resources_data_; 251 scoped_ptr<ResourceHandle> locale_resources_data_;
200 ScopedVector<ResourceHandle> data_packs_; 252 ScopedVector<ResourceHandle> data_packs_;
201 253
(...skipping 18 matching lines...) Expand all
220 272
221 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); 273 DISALLOW_COPY_AND_ASSIGN(ResourceBundle);
222 }; 274 };
223 275
224 } // namespace ui 276 } // namespace ui
225 277
226 // TODO(beng): Someday, maybe, get rid of this. 278 // TODO(beng): Someday, maybe, get rid of this.
227 using ui::ResourceBundle; 279 using ui::ResourceBundle;
228 280
229 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ 281 #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