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

Side by Side Diff: chrome/browser/browser_theme_provider.h

Issue 499004: Try 2: Completely redo how themes are stored on disk and processed at install time. (Closed)
Patch Set: Created 11 years 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_ 5 #ifndef CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_
6 #define CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_ 6 #define CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "app/gfx/color_utils.h" 12 #include "app/gfx/color_utils.h"
13 #include "app/resource_bundle.h" 13 #include "app/resource_bundle.h"
14 #include "app/theme_provider.h" 14 #include "app/theme_provider.h"
15 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "base/lock.h" 16 #include "base/lock.h"
17 #include "base/non_thread_safe.h" 17 #include "base/non_thread_safe.h"
18 #include "base/ref_counted.h" 18 #include "base/ref_counted.h"
19 19
20 class Extension; 20 class Extension;
21 class Profile; 21 class Profile;
22 class DictionaryValue; 22 class DictionaryValue;
23 class PrefService; 23 class PrefService;
24 class BrowserThemeProviderTest; 24 class BrowserThemeProviderTest;
25 class BrowserThemePack;
25 26
26 class BrowserThemeProvider : public NonThreadSafe, 27 class BrowserThemeProvider : public NonThreadSafe,
27 public ThemeProvider { 28 public ThemeProvider {
28 public: 29 public:
29 // Public constants used in BrowserThemeProvider and its subclasses: 30 // Public constants used in BrowserThemeProvider and its subclasses:
30 31
31 // Strings used by themes to identify colors for different parts of our UI.
32 static const char* kColorFrame;
33 static const char* kColorFrameInactive;
34 static const char* kColorFrameIncognito;
35 static const char* kColorFrameIncognitoInactive;
36 static const char* kColorToolbar;
37 static const char* kColorTabText;
38 static const char* kColorBackgroundTabText;
39 static const char* kColorBookmarkText;
40 static const char* kColorNTPBackground;
41 static const char* kColorNTPText;
42 static const char* kColorNTPLink;
43 static const char* kColorNTPLinkUnderline;
44 static const char* kColorNTPHeader;
45 static const char* kColorNTPSection;
46 static const char* kColorNTPSectionText;
47 static const char* kColorNTPSectionLink;
48 static const char* kColorNTPSectionLinkUnderline;
49 static const char* kColorControlBackground;
50 static const char* kColorButtonBackground;
51
52 // Strings used by themes to identify tints to apply to different parts of
53 // our UI. The frame tints apply to the frame color and produce the
54 // COLOR_FRAME* colors.
55 static const char* kTintButtons;
56 static const char* kTintFrame;
57 static const char* kTintFrameInactive;
58 static const char* kTintFrameIncognito;
59 static const char* kTintFrameIncognitoInactive;
60 static const char* kTintBackgroundTab;
61
62 // Strings used by themes to identify miscellaneous numerical properties.
63 static const char* kDisplayPropertyNTPAlignment;
64 static const char* kDisplayPropertyNTPTiling;
65 static const char* kDisplayPropertyNTPInverseLogo;
66
67 // Strings used in alignment properties. 32 // Strings used in alignment properties.
68 static const char* kAlignmentTop; 33 static const char* kAlignmentTop;
69 static const char* kAlignmentBottom; 34 static const char* kAlignmentBottom;
70 static const char* kAlignmentLeft; 35 static const char* kAlignmentLeft;
71 static const char* kAlignmentRight; 36 static const char* kAlignmentRight;
72 37
73 // Strings used in tiling properties. 38 // Strings used in tiling properties.
74 static const char* kTilingNoRepeat; 39 static const char* kTilingNoRepeat;
75 static const char* kTilingRepeatX; 40 static const char* kTilingRepeatX;
76 static const char* kTilingRepeatY; 41 static const char* kTilingRepeatY;
77 static const char* kTilingRepeat; 42 static const char* kTilingRepeat;
78 43
79 // Default colors.
80 static const SkColor kDefaultColorFrame;
81 static const SkColor kDefaultColorFrameInactive;
82 static const SkColor kDefaultColorFrameIncognito;
83 static const SkColor kDefaultColorFrameIncognitoInactive;
84 static const SkColor kDefaultColorToolbar;
85 static const SkColor kDefaultColorTabText;
86 static const SkColor kDefaultColorBackgroundTabText;
87 static const SkColor kDefaultColorBookmarkText;
88 static const SkColor kDefaultColorNTPBackground;
89 static const SkColor kDefaultColorNTPText;
90 static const SkColor kDefaultColorNTPLink;
91 static const SkColor kDefaultColorNTPHeader;
92 static const SkColor kDefaultColorNTPSection;
93 static const SkColor kDefaultColorNTPSectionText;
94 static const SkColor kDefaultColorNTPSectionLink;
95 static const SkColor kDefaultColorControlBackground;
96 static const SkColor kDefaultColorButtonBackground;
97
98 static const color_utils::HSL kDefaultTintButtons;
99 static const color_utils::HSL kDefaultTintFrame;
100 static const color_utils::HSL kDefaultTintFrameInactive;
101 static const color_utils::HSL kDefaultTintFrameIncognito;
102 static const color_utils::HSL kDefaultTintFrameIncognitoInactive;
103 static const color_utils::HSL kDefaultTintBackgroundTab;
104
105 static const char* kDefaultThemeID; 44 static const char* kDefaultThemeID;
106 45
107 // Returns true if the image is themeable. Safe to call on any thread. 46 // Returns true if the image is themeable. Safe to call on any thread.
108 static bool IsThemeableImage(int resource_id); 47 static bool IsThemeableImage(int resource_id);
109 48
110
111 BrowserThemeProvider(); 49 BrowserThemeProvider();
112 virtual ~BrowserThemeProvider(); 50 virtual ~BrowserThemeProvider();
113 51
114 enum { 52 enum {
115 COLOR_FRAME, 53 COLOR_FRAME,
116 COLOR_FRAME_INACTIVE, 54 COLOR_FRAME_INACTIVE,
117 COLOR_FRAME_INCOGNITO, 55 COLOR_FRAME_INCOGNITO,
118 COLOR_FRAME_INCOGNITO_INACTIVE, 56 COLOR_FRAME_INCOGNITO_INACTIVE,
119 COLOR_TOOLBAR, 57 COLOR_TOOLBAR,
120 COLOR_TAB_TEXT, 58 COLOR_TAB_TEXT,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 virtual void UseDefaultTheme(); 121 virtual void UseDefaultTheme();
184 122
185 // Set the current theme to the native theme. On some platforms, the native 123 // Set the current theme to the native theme. On some platforms, the native
186 // theme is the default theme. 124 // theme is the default theme.
187 virtual void SetNativeTheme() { UseDefaultTheme(); } 125 virtual void SetNativeTheme() { UseDefaultTheme(); }
188 126
189 // Gets the id of the last installed theme. (The theme may have been further 127 // Gets the id of the last installed theme. (The theme may have been further
190 // locally customized.) 128 // locally customized.)
191 std::string GetThemeID() const; 129 std::string GetThemeID() const;
192 130
193 // Reads the image data from the theme file into the specified vector. Returns
194 // true on success.
195 RefCountedMemory* ReadThemeFileData(int id) const;
196
197 // Convert a bitfield alignment into a string like "top left". Public so that 131 // Convert a bitfield alignment into a string like "top left". Public so that
198 // it can be used to generate CSS values. Takes a bitfield of AlignmentMasks. 132 // it can be used to generate CSS values. Takes a bitfield of AlignmentMasks.
199 static std::string AlignmentToString(int alignment); 133 static std::string AlignmentToString(int alignment);
200 134
201 // Parse alignments from something like "top left" into a bitfield of 135 // Parse alignments from something like "top left" into a bitfield of
202 // AlignmentMasks 136 // AlignmentMasks
203 static int StringToAlignment(const std::string& alignment); 137 static int StringToAlignment(const std::string& alignment);
204 138
205 // Convert a tiling value into a string like "no-repeat". Public 139 // Convert a tiling value into a string like "no-repeat". Public
206 // so that it can be used to generate CSS values. Takes a Tiling. 140 // so that it can be used to generate CSS values. Takes a Tiling.
207 static std::string TilingToString(int tiling); 141 static std::string TilingToString(int tiling);
208 142
209 // Parse tiling values from something like "no-repeat" into a Tiling value. 143 // Parse tiling values from something like "no-repeat" into a Tiling value.
210 static int StringToTiling(const std::string& tiling); 144 static int StringToTiling(const std::string& tiling);
211 145
212 // Lock on write to themed_image_cache_ in UI thread; lock on all cache 146 // Returns the default tint for the given tint |id| TINT_* enum value.
213 // access in File thread. This allows the File thread and UI thread to 147 static color_utils::HSL GetDefaultTint(int id);
214 // both read themed images at the same time, while preventing simultaneous 148
215 // File thread read and UI thread write. 149 // Returns the default color for the given color |id| COLOR_* enum value.
216 static Lock themed_image_cache_lock_; 150 static SkColor GetDefaultColor(int id);
151
152 // Returns true and sets the default property to |result| for valid display
tony 2009/12/14 20:19:57 Nit: This comment makes it sounds like default pro
153 // properties.
154 static bool GetDefaultDisplayProperty(int it, int* result);
217 155
218 // Save the images to be written to disk, mapping file path to id. 156 // Save the images to be written to disk, mapping file path to id.
219 typedef std::map<FilePath, int> ImagesDiskCache; 157 typedef std::map<FilePath, int> ImagesDiskCache;
220 158
221 // Cached images. We cache all retrieved and generated bitmaps and keep
222 // track of the pointers. We own these and will delete them when we're done
223 // using them.
224 typedef std::map<int, SkBitmap*> ImageCache;
225
226 protected: 159 protected:
227 // Sets an individual color value.
228 void SetColor(const char* id, const SkColor& color);
229
230 // Sets an individual tint value.
231 void SetTint(const char* id, const color_utils::HSL& tint);
232
233 // Get the specified tint - |id| is one of the TINT_* enum values. 160 // Get the specified tint - |id| is one of the TINT_* enum values.
234 color_utils::HSL GetTint(int id) const; 161 color_utils::HSL GetTint(int id) const;
235 162
236 // Generate any frame colors that weren't specified.
237 void GenerateFrameColors();
238
239 // Generate any frame images that weren't specified. The resulting images
240 // will be stored in our cache and written to disk. If images have already
241 // been generated and cached, load them from disk.
242 void GenerateFrameImages() const;
243
244 // Generate any tab images that weren't specified. The resulting images
245 // will be stored in our cache.
246 void GenerateTabImages() const;
247
248 // Clears all the override fields and saves the dictionary. 163 // Clears all the override fields and saves the dictionary.
249 void ClearAllThemeData(); 164 virtual void ClearAllThemeData();
250 165
251 // Load theme data from preferences. 166 // Load theme data from preferences.
252 virtual void LoadThemePrefs(); 167 virtual void LoadThemePrefs();
253 168
254 // Let all the browser views know that themes have changed. 169 // Let all the browser views know that themes have changed.
255 virtual void NotifyThemeChanged(); 170 virtual void NotifyThemeChanged();
256 171
257 // Loads a bitmap from the theme, which may be tinted or
258 // otherwise modified, or an application default.
259 virtual SkBitmap* LoadThemeBitmap(int id) const;
260
261 // Save the modified bitmap at image_cache_[id].
262 virtual void SaveThemeBitmap(std::string resource_name, int id) const;
263
264 // Clears the platform-specific caches. Do not call directly; it's called 172 // Clears the platform-specific caches. Do not call directly; it's called
265 // from ClearCaches(). 173 // from ClearCaches().
266 virtual void FreePlatformCaches(); 174 virtual void FreePlatformCaches();
267 175
268 // The implementation of GenerateTabBackgroundBitmap(). That function also
269 // must be locked and touches caches; this function only deals with image
270 // generation.
271 SkBitmap* GenerateTabBackgroundBitmapImpl(int id) const;
272
273 Profile* profile() { return profile_; } 176 Profile* profile() { return profile_; }
274 177
275 // Subclasses may need us to not use the on-disk image cache. The GTK
276 // interface needs to generate some images itself.
277 void force_process_images() { process_images_ = true; }
278
279 private: 178 private:
280 friend class BrowserThemeProviderTest; 179 friend class BrowserThemeProviderTest;
281 180
282 typedef std::map<const int, std::string> ImageMap; 181 // Saves the filename of the cached theme pack.
283 typedef std::map<const std::string, SkColor> ColorMap; 182 void SavePackName(const FilePath& pack_path);
284 typedef std::map<const std::string, color_utils::HSL> TintMap;
285 typedef std::map<const std::string, int> DisplayPropertyMap;
286 typedef std::map<const int, scoped_refptr<RefCountedMemory> > RawDataMap;
287 typedef std::map<const int, std::string> ResourceNameMap;
288
289 // Returns the string key for the given tint |id| TINT_* enum value.
290 const std::string GetTintKey(int id) const;
291
292 // Returns the default tint for the given tint |id| TINT_* enum value.
293 color_utils::HSL GetDefaultTint(int id) const;
294
295 // Returns the string key for the given color |id| COLOR_* enum value.
296 const std::string GetColorKey(int id) const;
297
298 // Returns the default color for the given color |id| COLOR_* enum value.
299 SkColor GetDefaultColor(int id) const;
300
301 // Tint |bitmap| with the tint specified by |hsl_id|
302 SkBitmap TintBitmap(const SkBitmap& bitmap, int hsl_id) const;
303
304 // The following load data from specified dictionaries (either from
305 // preferences or from an extension manifest) and update our theme
306 // data appropriately.
307 // Allow any ResourceBundle image to be overridden. |images| should
308 // contain keys defined in ThemeResourceMap, and values as paths to
309 // the images on-disk.
310 void SetImageData(DictionaryValue* images, FilePath images_path);
311
312 // Set our theme colors. The keys of |colors| are any of the kColor*
313 // constants, and the values are a three-item list containing 8-bit
314 // RGB values.
315 void SetColorData(DictionaryValue* colors);
316
317 // Set tint data for our images and colors. The keys of |tints| are
318 // any of the kTint* contstants, and the values are a three-item list
319 // containing real numbers in the range 0-1 (and -1 for 'null').
320 void SetTintData(DictionaryValue* tints);
321
322 // Set miscellaneous display properties. While these can be defined as
323 // strings, they are currently stored as integers.
324 void SetDisplayPropertyData(DictionaryValue* display_properties);
325
326 // Create any images that aren't pregenerated (e.g. background tab images).
327 SkBitmap* GenerateTabBackgroundBitmap(int id) const;
328
329 // Save our data - when saving images we need the original dictionary
330 // from the extension because it contains the text ids that we want to save.
331 void SaveImageData(DictionaryValue* images) const;
332 void SaveColorData() const;
333 void SaveTintData() const;
334 void SaveDisplayPropertyData() const;
335
336 // Save the paths of data we have written to disk in prefs.
337 void SaveCachedImageData() const;
338 183
339 // Save the id of the last theme installed. 184 // Save the id of the last theme installed.
340 void SaveThemeID(const std::string& id); 185 void SaveThemeID(const std::string& id);
341 186
342 // Frees generated images and clears the image cache. 187 // Implementation of SetTheme() (and the fallback from LoadThemePrefs() in
343 void ClearCaches(); 188 // case we don't have a theme pack).
189 void BuildFromExtension(Extension* extension);
344 190
345 // Remove preference values for themes that are no longer in use. 191 // Remove preference values for themes that are no longer in use.
346 void RemoveUnusedThemes(); 192 void RemoveUnusedThemes();
347 193
348 // Encode image at image_cache_[id] as PNG and write to disk.
349 void WriteImagesToDisk() const;
350
351 // Do we have a custom frame image or custom tints?
352 bool ShouldTintFrames() const;
353
354 #if defined(OS_LINUX) 194 #if defined(OS_LINUX)
355 // Loads an image and flips it horizontally if |rtl_enabled| is true. 195 // Loads an image and flips it horizontally if |rtl_enabled| is true.
356 GdkPixbuf* GetPixbufImpl(int id, bool rtl_enabled) const; 196 GdkPixbuf* GetPixbufImpl(int id, bool rtl_enabled) const;
357 #endif 197 #endif
358 198
359 mutable ImageCache image_cache_;
360
361 // Keep images generated for theme cache in their own place, so we can lock
362 // them on WRITE from UI thread and READ from file thread. Read from UI
363 // thread will be allowed unlocked, because no other thread has write
364 // access to the cache.
365 mutable ImageCache themed_image_cache_;
366
367 #if defined(OS_LINUX) 199 #if defined(OS_LINUX)
368 typedef std::map<int, GdkPixbuf*> GdkPixbufMap; 200 typedef std::map<int, GdkPixbuf*> GdkPixbufMap;
369 mutable GdkPixbufMap gdk_pixbufs_; 201 mutable GdkPixbufMap gdk_pixbufs_;
370 #elif defined(OS_MACOSX) 202 #elif defined(OS_MACOSX)
371 typedef std::map<int, NSImage*> NSImageMap; 203 typedef std::map<int, NSImage*> NSImageMap;
372 mutable NSImageMap nsimage_cache_; 204 mutable NSImageMap nsimage_cache_;
373 typedef std::map<int, NSColor*> NSColorMap; 205 typedef std::map<int, NSColor*> NSColorMap;
374 mutable NSColorMap nscolor_cache_; 206 mutable NSColorMap nscolor_cache_;
375 #endif 207 #endif
376 208
377 mutable ImagesDiskCache images_disk_cache_;
378
379 ResourceBundle& rb_; 209 ResourceBundle& rb_;
380 Profile* profile_; 210 Profile* profile_;
381 211
382 ImageMap images_; 212 scoped_refptr<BrowserThemePack> theme_pack_;
383 ColorMap colors_;
384 TintMap tints_;
385 mutable RawDataMap raw_data_;
386 DisplayPropertyMap display_properties_;
387
388 // Reverse of theme_resources_map, so we can cache images properly.
389 ResourceNameMap resource_names_;
390
391 // If true, process all images; if false, just load from disk.
392 bool process_images_;
393
394 // Where we will store our generated images.
395 FilePath image_dir_;
396 213
397 DISALLOW_COPY_AND_ASSIGN(BrowserThemeProvider); 214 DISALLOW_COPY_AND_ASSIGN(BrowserThemeProvider);
398 }; 215 };
399 216
400 #endif // CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_ 217 #endif // CHROME_BROWSER_BROWSER_THEME_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698