Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_THEMES_THEME_SERVICE_H_ |
| 6 | #define CHROME_BROWSER_THEMES_THEME_SERVICE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include "base/compiler_specific.h" |
| 14 | #include "base/memory/ref_counted.h" |
| 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/threading/non_thread_safe.h" |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 17 | #include "components/browser_context_keyed_service/browser_context_keyed_service.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
| 19 | #include "content/public/browser/notification_registrar.h" |
| 20 | #include "ui/base/theme_provider.h" |
| 21 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 22 | class CustomThemeSupplier; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 23 | class BrowserThemePack; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 24 | class ThemeSyncableService; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 25 | class Profile; |
| 26 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 27 | namespace base { |
| 28 | class FilePath; |
| 29 | } |
| 30 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | namespace color_utils { |
| 32 | struct HSL; |
| 33 | } |
| 34 | |
| 35 | namespace extensions { |
| 36 | class Extension; |
| 37 | } |
| 38 | |
| 39 | namespace gfx { |
| 40 | class Image; |
| 41 | } |
| 42 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 43 | namespace theme_service_internal { |
| 44 | class ThemeServiceTest; |
| 45 | } |
| 46 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 47 | namespace ui { |
| 48 | class ResourceBundle; |
| 49 | } |
| 50 | |
| 51 | #ifdef __OBJC__ |
| 52 | @class NSString; |
| 53 | // Sent whenever the browser theme changes. Object => NSValue wrapping the |
| 54 | // ThemeService that changed. |
| 55 | extern "C" NSString* const kBrowserThemeDidChangeNotification; |
| 56 | #endif // __OBJC__ |
| 57 | |
| 58 | class ThemeService : public base::NonThreadSafe, |
| 59 | public content::NotificationObserver, |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 60 | public BrowserContextKeyedService, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 61 | public ui::ThemeProvider { |
| 62 | public: |
| 63 | // Public constants used in ThemeService and its subclasses: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 64 | static const char* kDefaultThemeID; |
| 65 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 66 | ThemeService(); |
| 67 | virtual ~ThemeService(); |
| 68 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | virtual void Init(Profile* profile); |
| 70 | |
| 71 | // Returns a cross platform image for an id. |
| 72 | // |
| 73 | // TODO(erg): Make this part of the ui::ThemeProvider and the main way to get |
| 74 | // theme properties out of the theme provider since it's cross platform. |
| 75 | virtual gfx::Image GetImageNamed(int id) const; |
| 76 | |
| 77 | // Overridden from ui::ThemeProvider: |
| 78 | virtual gfx::ImageSkia* GetImageSkiaNamed(int id) const OVERRIDE; |
| 79 | virtual SkColor GetColor(int id) const OVERRIDE; |
| 80 | virtual bool GetDisplayProperty(int id, int* result) const OVERRIDE; |
| 81 | virtual bool ShouldUseNativeFrame() const OVERRIDE; |
| 82 | virtual bool HasCustomImage(int id) const OVERRIDE; |
| 83 | virtual base::RefCountedMemory* GetRawData( |
| 84 | int id, |
| 85 | ui::ScaleFactor scale_factor) const OVERRIDE; |
| 86 | #if defined(OS_MACOSX) |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 87 | virtual NSImage* GetNSImageNamed(int id) const OVERRIDE; |
| 88 | virtual NSColor* GetNSImageColorNamed(int id) const OVERRIDE; |
| 89 | virtual NSColor* GetNSColor(int id) const OVERRIDE; |
| 90 | virtual NSColor* GetNSColorTint(int id) const OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | virtual NSGradient* GetNSGradient(int id) const OVERRIDE; |
| 92 | #elif defined(OS_POSIX) && !defined(TOOLKIT_VIEWS) && !defined(OS_ANDROID) |
| 93 | // This mismatch between what this class defines and whether or not it |
| 94 | // overrides ui::ThemeProvider is http://crbug.com/105040 . |
| 95 | // GdkPixbufs returned by GetPixbufNamed and GetRTLEnabledPixbufNamed are |
| 96 | // shared instances owned by the theme provider and should not be freed. |
| 97 | virtual GdkPixbuf* GetRTLEnabledPixbufNamed(int id) const OVERRIDE; |
| 98 | #endif |
| 99 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 100 | // Overridden from content::NotificationObserver: |
| 101 | virtual void Observe(int type, |
| 102 | const content::NotificationSource& source, |
| 103 | const content::NotificationDetails& details) OVERRIDE; |
| 104 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 105 | // Set the current theme to the theme defined in |extension|. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 106 | // |extension| must already be added to this profile's |
| 107 | // ExtensionService. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 108 | virtual void SetTheme(const extensions::Extension* extension); |
| 109 | |
| 110 | // Reset the theme to default. |
| 111 | virtual void UseDefaultTheme(); |
| 112 | |
| 113 | // Set the current theme to the native theme. On some platforms, the native |
| 114 | // theme is the default theme. |
| 115 | virtual void SetNativeTheme(); |
| 116 | |
| 117 | // Whether we're using the chrome default theme. Virtual so linux can check |
| 118 | // if we're using the GTK theme. |
| 119 | virtual bool UsingDefaultTheme() const; |
| 120 | |
| 121 | // Whether we're using the native theme (which may or may not be the |
| 122 | // same as the default theme). |
| 123 | virtual bool UsingNativeTheme() const; |
| 124 | |
| 125 | // Gets the id of the last installed theme. (The theme may have been further |
| 126 | // locally customized.) |
| 127 | virtual std::string GetThemeID() const; |
| 128 | |
| 129 | // This class needs to keep track of the number of theme infobars so that we |
| 130 | // clean up unused themes. |
| 131 | void OnInfobarDisplayed(); |
| 132 | |
| 133 | // Decrements the number of theme infobars. If the last infobar has been |
| 134 | // destroyed, uninstalls all themes that aren't the currently selected. |
| 135 | void OnInfobarDestroyed(); |
| 136 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 137 | // Remove preference values for themes that are no longer in use. |
| 138 | void RemoveUnusedThemes(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 139 | |
| 140 | // Returns the syncable service for syncing theme. The returned service is |
| 141 | // owned by |this| object. |
| 142 | virtual ThemeSyncableService* GetThemeSyncableService() const; |
| 143 | |
| 144 | // Save the images to be written to disk, mapping file path to id. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 145 | typedef std::map<base::FilePath, int> ImagesDiskCache; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 146 | |
| 147 | protected: |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 148 | // Set a custom default theme instead of the normal default theme. |
| 149 | virtual void SetCustomDefaultTheme( |
| 150 | scoped_refptr<CustomThemeSupplier> theme_supplier); |
| 151 | |
| 152 | // Returns true if the ThemeService should use the native theme on startup. |
| 153 | virtual bool ShouldInitWithNativeTheme() const; |
| 154 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 155 | // Get the specified tint - |id| is one of the TINT_* enum values. |
| 156 | color_utils::HSL GetTint(int id) const; |
| 157 | |
| 158 | // Clears all the override fields and saves the dictionary. |
| 159 | virtual void ClearAllThemeData(); |
| 160 | |
| 161 | // Load theme data from preferences. |
| 162 | virtual void LoadThemePrefs(); |
| 163 | |
| 164 | // Let all the browser views know that themes have changed. |
| 165 | virtual void NotifyThemeChanged(); |
| 166 | |
| 167 | #if defined(OS_MACOSX) |
| 168 | // Let all the browser views know that themes have changed in a platform way. |
| 169 | virtual void NotifyPlatformThemeChanged(); |
| 170 | #endif // OS_MACOSX |
| 171 | |
| 172 | // Clears the platform-specific caches. Do not call directly; it's called |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 173 | // from ClearAllThemeData(). |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 174 | virtual void FreePlatformCaches(); |
| 175 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 176 | Profile* profile() const { return profile_; } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 177 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 178 | void set_ready() { ready_ = true; } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 179 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 180 | const CustomThemeSupplier* get_theme_supplier() const { |
| 181 | return theme_supplier_.get(); |
| 182 | } |
| 183 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 184 | // True if the theme service is ready to be used. |
| 185 | // TODO(pkotwicz): Add DCHECKS to the theme service's getters once |
| 186 | // ThemeSource no longer uses the ThemeService when it is not ready. |
| 187 | bool ready_; |
| 188 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 189 | private: |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 190 | friend class theme_service_internal::ThemeServiceTest; |
| 191 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 192 | // Replaces the current theme supplier with a new one and calls |
| 193 | // StopUsingTheme() or StartUsingTheme() as appropriate. |
| 194 | void SwapThemeSupplier(scoped_refptr<CustomThemeSupplier> theme_supplier); |
| 195 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 196 | // Migrate the theme to the new theme pack schema by recreating the data pack |
| 197 | // from the extension. |
| 198 | void MigrateTheme(); |
| 199 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 200 | // Saves the filename of the cached theme pack. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 201 | void SavePackName(const base::FilePath& pack_path); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 202 | |
| 203 | // Save the id of the last theme installed. |
| 204 | void SaveThemeID(const std::string& id); |
| 205 | |
| 206 | // Implementation of SetTheme() (and the fallback from LoadThemePrefs() in |
| 207 | // case we don't have a theme pack). |
| 208 | void BuildFromExtension(const extensions::Extension* extension); |
| 209 | |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 210 | // Returns true if the profile belongs to a managed user. |
| 211 | bool IsManagedUser() const; |
| 212 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 213 | // Sets the current theme to the managed user theme. Should only be used for |
| 214 | // managed user profiles. |
| 215 | void SetManagedUserTheme(); |
| 216 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 217 | #if defined(TOOLKIT_GTK) |
| 218 | // Loads an image and flips it horizontally if |rtl_enabled| is true. |
| 219 | GdkPixbuf* GetPixbufImpl(int id, bool rtl_enabled) const; |
| 220 | #endif |
| 221 | |
| 222 | #if defined(TOOLKIT_GTK) |
| 223 | typedef std::map<int, GdkPixbuf*> GdkPixbufMap; |
| 224 | mutable GdkPixbufMap gdk_pixbufs_; |
| 225 | #elif defined(OS_MACOSX) |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 226 | // |nsimage_cache_| retains the images it has cached. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 227 | typedef std::map<int, NSImage*> NSImageMap; |
| 228 | mutable NSImageMap nsimage_cache_; |
| 229 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 230 | // |nscolor_cache_| retains the colors it has cached. |
| 231 | typedef std::map<int, NSColor*> NSColorMap; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 232 | mutable NSColorMap nscolor_cache_; |
| 233 | |
| 234 | typedef std::map<int, NSGradient*> NSGradientMap; |
| 235 | mutable NSGradientMap nsgradient_cache_; |
| 236 | #endif |
| 237 | |
| 238 | ui::ResourceBundle& rb_; |
| 239 | Profile* profile_; |
| 240 | |
Ben Murdoch | 558790d | 2013-07-30 15:19:42 +0100 | [diff] [blame] | 241 | scoped_refptr<CustomThemeSupplier> theme_supplier_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 242 | |
| 243 | // The number of infobars currently displayed. |
| 244 | int number_of_infobars_; |
| 245 | |
| 246 | content::NotificationRegistrar registrar_; |
| 247 | |
| 248 | scoped_ptr<ThemeSyncableService> theme_syncable_service_; |
| 249 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 250 | DISALLOW_COPY_AND_ASSIGN(ThemeService); |
| 251 | }; |
| 252 | |
| 253 | #endif // CHROME_BROWSER_THEMES_THEME_SERVICE_H_ |