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

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
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 {
67 // selected. 67 public:
68 // Retrieve the full path for the specified |pack_name|. |pack_path| will
69 // initially contain the default path for the pack file. Change it in this
70 // callback if a different path is desired. Return true to continue loading
71 // the pack file or false to cancel the load.
72 virtual bool GetPathForResourcePack(const std::string& pack_name,
73 FilePath* pack_path) = 0;
74
75 // Retrieve the full path for the specified |locale|. |pack_path| will
76 // initially contain the default path for the pack file. Change it in this
77 // callback if a different path is desired. Return true to continue loading
78 // the pack file or false to cancel the load.
79 virtual bool GetPathForLocalePack(const std::string& locale,
80 FilePath* pack_path) = 0;
81
82 // Return an image resource or NULL to attempt retrieval of the default
83 // resource. The returned value will be owned by ResourceBundle.
84 virtual gfx::Image* GetImageNamed(int resource_id) = 0;
sail 2012/04/30 21:14:43 can you have this return a scoped_ptr. Then you ca
Marshall 2012/04/30 21:50:10 Done.
85
86 // Return an image resource or NULL to attempt retrieval of the default
87 // resource. The returned value will be owned by ResourceBundle.
88 virtual gfx::Image* GetNativeImageNamed(int resource_id, ImageRTL rtl) = 0;
89
90 // Return a static memory resource or NULL to attempt retrieval of the
91 // default resource.
92 virtual base::RefCountedStaticMemory* LoadDataResourceBytes(
93 int resource_id) = 0;
94
95 // Retrieve a raw data resource. Return true if a resource was provided or
96 // false to attempt retrieval of the default resource.
97 virtual bool GetRawDataResource(int resource_id,
98 base::StringPiece* value) = 0;
99
100 // Retrieve a localized string. Return true if a string was provided or
101 // false to attempt retrieval of the default string.
102 virtual bool GetLocalizedString(int message_id, string16* value) = 0;
103
104 // Return a font resource or NULL to attempt retrieval of the default
105 // resource. The returned value will be owned by ResourceBundle.
106 virtual gfx::Font* GetFont(FontStyle style) = 0;
107
108 protected:
109 virtual ~Delegate() {}
110 };
111
112 // Initialize the ResourceBundle for this process. Does not take ownership of
113 // the |delegate| value. Returns the language selected.
68 // NOTE: Mac ignores this and always loads up resources for the language 114 // 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). 115 // defined by the Cocoa UI (i.e., NSBundle does the language work).
70 static std::string InitSharedInstanceWithLocale( 116 static std::string InitSharedInstanceWithLocale(
71 const std::string& pref_locale); 117 const std::string& pref_locale, Delegate* delegate);
72 118
73 // Initialize the ResourceBundle using given data pack path for testing. 119 // Initialize the ResourceBundle using given data pack path for testing.
74 static void InitSharedInstanceWithPakFile(const FilePath& path); 120 static void InitSharedInstanceWithPakFile(const FilePath& path);
75 121
76 // Delete the ResourceBundle for this process if it exists. 122 // Delete the ResourceBundle for this process if it exists.
77 static void CleanupSharedInstance(); 123 static void CleanupSharedInstance();
78 124
79 // Returns true after the global resource loader instance has been created. 125 // Returns true after the global resource loader instance has been created.
80 static bool HasSharedInstance(); 126 static bool HasSharedInstance();
81 127
82 // Return the global resource loader instance. 128 // Return the global resource loader instance.
83 static ResourceBundle& GetSharedInstance(); 129 static ResourceBundle& GetSharedInstance();
84 130
85 // Check if the .pak for the given locale exists. 131 // Check if the .pak for the given locale exists.
86 static bool LocaleDataPakExists(const std::string& locale); 132 bool LocaleDataPakExists(const std::string& locale);
87 133
88 // Registers additional data pack files with the global ResourceBundle. When 134 // Registers additional data pack files with the global ResourceBundle. When
89 // looking for a DataResource, we will search these files after searching the 135 // 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 136 // main module. This method is not thread safe! You should call it
91 // immediately after calling InitSharedInstance. 137 // immediately after calling InitSharedInstance.
92 void AddDataPack(const FilePath& path); 138 void AddDataPack(const FilePath& path);
93 139
94 // Changes the locale for an already-initialized ResourceBundle, returning the 140 // Changes the locale for an already-initialized ResourceBundle, returning the
95 // name of the newly-loaded locale. Future calls to get strings will return 141 // 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 142 // 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(); 190 void ReloadFonts();
145 191
146 // Overrides the path to the pak file from which the locale resources will be 192 // Overrides the path to the pak file from which the locale resources will be
147 // loaded. Pass an empty path to undo. 193 // loaded. Pass an empty path to undo.
148 void OverrideLocalePakForTest(const FilePath& pak_path); 194 void OverrideLocalePakForTest(const FilePath& pak_path);
149 195
150 private: 196 private:
151 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes); 197 FRIEND_TEST_ALL_PREFIXES(ResourceBundle, LoadDataResourceBytes);
152 198
153 // Ctor/dtor are private, since we're a singleton. 199 // Ctor/dtor are private, since we're a singleton.
154 ResourceBundle(); 200 explicit ResourceBundle(Delegate* delegate);
155 ~ResourceBundle(); 201 ~ResourceBundle();
156 202
157 // Free skia_images_. 203 // Free skia_images_.
158 void FreeImages(); 204 void FreeImages();
159 205
160 // Load the main resources. 206 // Load the main resources.
161 void LoadCommonResources(); 207 void LoadCommonResources();
162 208
209 // Add a common data pack to the ResourceBundle. Gives the delegate an
210 // opportunity to modify the load.
211 void AddCommonDataPack(const std::string& pack_name, const FilePath& path);
sail 2012/04/30 21:14:43 do you really need AddDataPack and AddCommonDataPa
Marshall 2012/04/30 21:50:10 I think both are needed. AddDataPack() is used ext
sail 2012/04/30 22:33:26 But why does external vs internal matter? Why woul
Marshall 2012/05/01 17:29:41 Removed the additional method.
212
163 // Try to load the locale specific strings from an external data module. 213 // Try to load the locale specific strings from an external data module.
164 // Returns the locale that is loaded. 214 // Returns the locale that is loaded.
165 std::string LoadLocaleResources(const std::string& pref_locale); 215 std::string LoadLocaleResources(const std::string& pref_locale);
166 216
167 // Load test resources in given path. 217 // Load test resources in given path.
168 void LoadTestResources(const FilePath& path); 218 void LoadTestResources(const FilePath& path);
169 219
170 // Unload the locale specific strings and prepares to load new ones. See 220 // Unload the locale specific strings and prepares to load new ones. See
171 // comments for ReloadLocaleResources(). 221 // comments for ReloadLocaleResources().
172 void UnloadLocaleResources(); 222 void UnloadLocaleResources();
173 223
174 // Initialize all the gfx::Font members if they haven't yet been initialized. 224 // Initialize all the gfx::Font members if they haven't yet been initialized.
175 void LoadFontsIfNecessary(); 225 void LoadFontsIfNecessary();
176 226
177 // Returns the full pathname of the locale file to load. May return an empty 227 // Returns the full pathname of the locale file to load. May return an empty
178 // string if no locale data files are found. 228 // string if no locale data files are found.
179 static FilePath GetLocaleFilePath(const std::string& app_locale); 229 FilePath GetLocaleFilePath(const std::string& app_locale);
180 230
181 // Creates and returns a new SkBitmap given the data file to look in and the 231 // 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 232 // resource id. It's up to the caller to free the returned bitmap when
183 // done. 233 // done.
184 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id); 234 SkBitmap* LoadBitmap(const ResourceHandle& dll_inst, int resource_id);
185 235
186 // Returns an empty image for when a resource cannot be loaded. This is a 236 // Returns an empty image for when a resource cannot be loaded. This is a
187 // bright red bitmap. 237 // bright red bitmap.
188 gfx::Image* GetEmptyImage(); 238 gfx::Image* GetEmptyImage();
189 239
190 const FilePath& GetOverriddenPakPath(); 240 const FilePath& GetOverriddenPakPath();
191 241
242 // This pointer is guaranteed to outlive the ResourceBundle instance.
243 Delegate* delegate_;
244
192 // Protects |images_| and font-related members. 245 // Protects |images_| and font-related members.
193 scoped_ptr<base::Lock> images_and_fonts_lock_; 246 scoped_ptr<base::Lock> images_and_fonts_lock_;
194 247
195 // Protects |locale_resources_data_|. 248 // Protects |locale_resources_data_|.
196 scoped_ptr<base::Lock> locale_resources_data_lock_; 249 scoped_ptr<base::Lock> locale_resources_data_lock_;
197 250
198 // Handles for data sources. 251 // Handles for data sources.
199 scoped_ptr<ResourceHandle> locale_resources_data_; 252 scoped_ptr<ResourceHandle> locale_resources_data_;
200 ScopedVector<ResourceHandle> data_packs_; 253 ScopedVector<ResourceHandle> data_packs_;
201 254
(...skipping 18 matching lines...) Expand all
220 273
221 DISALLOW_COPY_AND_ASSIGN(ResourceBundle); 274 DISALLOW_COPY_AND_ASSIGN(ResourceBundle);
222 }; 275 };
223 276
224 } // namespace ui 277 } // namespace ui
225 278
226 // TODO(beng): Someday, maybe, get rid of this. 279 // TODO(beng): Someday, maybe, get rid of this.
227 using ui::ResourceBundle; 280 using ui::ResourceBundle;
228 281
229 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_ 282 #endif // UI_BASE_RESOURCE_RESOURCE_BUNDLE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698