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.
5 #ifndef CHROME_BROWSER_THEMES_THEME_SERVICE_H_
6 #define CHROME_BROWSER_THEMES_THEME_SERVICE_H_
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/non_thread_safe.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "ui/base/theme_provider.h"
23 class CustomThemeSupplier
;
24 class BrowserThemePack
;
25 class ThemeSyncableService
;
32 namespace color_utils
{
36 namespace extensions
{
44 namespace theme_service_internal
{
45 class ThemeServiceTest
;
54 // Sent whenever the browser theme changes. Object => NSValue wrapping the
55 // ThemeService that changed.
56 extern "C" NSString
* const kBrowserThemeDidChangeNotification
;
59 class ThemeService
: public base::NonThreadSafe
,
60 public content::NotificationObserver
,
62 public ui::ThemeProvider
{
64 // Public constants used in ThemeService and its subclasses:
65 static const char* kDefaultThemeID
;
68 virtual ~ThemeService();
70 virtual void Init(Profile
* profile
);
72 // Returns a cross platform image for an id.
74 // TODO(erg): Make this part of the ui::ThemeProvider and the main way to get
75 // theme properties out of the theme provider since it's cross platform.
76 virtual gfx::Image
GetImageNamed(int id
) const;
78 // Overridden from ui::ThemeProvider:
79 virtual bool UsingSystemTheme() const override
;
80 virtual gfx::ImageSkia
* GetImageSkiaNamed(int id
) const override
;
81 virtual SkColor
GetColor(int id
) const override
;
82 virtual int GetDisplayProperty(int id
) const override
;
83 virtual bool ShouldUseNativeFrame() const override
;
84 virtual bool HasCustomImage(int id
) const override
;
85 virtual base::RefCountedMemory
* GetRawData(
87 ui::ScaleFactor scale_factor
) const override
;
88 #if defined(OS_MACOSX)
89 virtual NSImage
* GetNSImageNamed(int id
) const override
;
90 virtual NSColor
* GetNSImageColorNamed(int id
) const override
;
91 virtual NSColor
* GetNSColor(int id
) const override
;
92 virtual NSColor
* GetNSColorTint(int id
) const override
;
93 virtual NSGradient
* GetNSGradient(int id
) const override
;
96 // Overridden from content::NotificationObserver:
97 virtual void Observe(int type
,
98 const content::NotificationSource
& source
,
99 const content::NotificationDetails
& details
) override
;
101 // Set the current theme to the theme defined in |extension|.
102 // |extension| must already be added to this profile's
104 virtual void SetTheme(const extensions::Extension
* extension
);
106 // Reset the theme to default.
107 virtual void UseDefaultTheme();
109 // Set the current theme to the system theme. On some platforms, the system
110 // theme is the default theme.
111 virtual void UseSystemTheme();
113 // Returns true if the default theme and system theme are not the same on
115 virtual bool IsSystemThemeDistinctFromDefaultTheme() const;
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;
121 // Gets the id of the last installed theme. (The theme may have been further
122 // locally customized.)
123 virtual std::string
GetThemeID() const;
125 // This class needs to keep track of the number of theme infobars so that we
126 // clean up unused themes.
127 void OnInfobarDisplayed();
129 // Decrements the number of theme infobars. If the last infobar has been
130 // destroyed, uninstalls all themes that aren't the currently selected.
131 void OnInfobarDestroyed();
133 // Uninstall theme extensions which are no longer in use. |ignore_infobars| is
134 // whether unused themes should be removed despite a theme infobar being
136 void RemoveUnusedThemes(bool ignore_infobars
);
138 // Returns the syncable service for syncing theme. The returned service is
139 // owned by |this| object.
140 virtual ThemeSyncableService
* GetThemeSyncableService() const;
142 // Save the images to be written to disk, mapping file path to id.
143 typedef std::map
<base::FilePath
, int> ImagesDiskCache
;
146 // Set a custom default theme instead of the normal default theme.
147 virtual void SetCustomDefaultTheme(
148 scoped_refptr
<CustomThemeSupplier
> theme_supplier
);
150 // Returns true if the ThemeService should use the system theme on startup.
151 virtual bool ShouldInitWithSystemTheme() const;
153 // Get the specified tint - |id| is one of the TINT_* enum values.
154 color_utils::HSL
GetTint(int id
) const;
156 // Clears all the override fields and saves the dictionary.
157 virtual void ClearAllThemeData();
159 // Load theme data from preferences.
160 virtual void LoadThemePrefs();
162 // Let all the browser views know that themes have changed.
163 virtual void NotifyThemeChanged();
165 #if defined(OS_MACOSX)
166 // Let all the browser views know that themes have changed in a platform way.
167 virtual void NotifyPlatformThemeChanged();
170 // Clears the platform-specific caches. Do not call directly; it's called
171 // from ClearAllThemeData().
172 virtual void FreePlatformCaches();
174 Profile
* profile() const { return profile_
; }
176 void set_ready() { ready_
= true; }
178 const CustomThemeSupplier
* get_theme_supplier() const {
179 return theme_supplier_
.get();
182 // True if the theme service is ready to be used.
183 // TODO(pkotwicz): Add DCHECKS to the theme service's getters once
184 // ThemeSource no longer uses the ThemeService when it is not ready.
188 friend class theme_service_internal::ThemeServiceTest
;
190 // Called when the extension service is ready.
191 void OnExtensionServiceReady();
193 // Migrate the theme to the new theme pack schema by recreating the data pack
194 // from the extension.
197 // Replaces the current theme supplier with a new one and calls
198 // StopUsingTheme() or StartUsingTheme() as appropriate.
199 void SwapThemeSupplier(scoped_refptr
<CustomThemeSupplier
> theme_supplier
);
201 // Saves the filename of the cached theme pack.
202 void SavePackName(const base::FilePath
& pack_path
);
204 // Save the id of the last theme installed.
205 void SaveThemeID(const std::string
& id
);
207 // Implementation of SetTheme() (and the fallback from LoadThemePrefs() in
208 // case we don't have a theme pack).
209 void BuildFromExtension(const extensions::Extension
* extension
);
211 #if defined(ENABLE_MANAGED_USERS)
212 // Returns true if the profile belongs to a supervised user.
213 bool IsSupervisedUser() const;
215 // Sets the current theme to the supervised user theme. Should only be used
216 // for supervised user profiles.
217 void SetSupervisedUserTheme();
220 #if defined(OS_MACOSX)
221 // |nsimage_cache_| retains the images it has cached.
222 typedef std::map
<int, NSImage
*> NSImageMap
;
223 mutable NSImageMap nsimage_cache_
;
225 // |nscolor_cache_| retains the colors it has cached.
226 typedef std::map
<int, NSColor
*> NSColorMap
;
227 mutable NSColorMap nscolor_cache_
;
229 typedef std::map
<int, NSGradient
*> NSGradientMap
;
230 mutable NSGradientMap nsgradient_cache_
;
233 ui::ResourceBundle
& rb_
;
236 scoped_refptr
<CustomThemeSupplier
> theme_supplier_
;
238 // The id of the theme extension which has just been installed but has not
239 // been loaded yet. The theme extension with |installed_pending_load_id_| may
240 // never be loaded if the install is due to updating a disabled theme.
241 // |pending_install_id_| should be set to |kDefaultThemeID| if there are no
242 // recently installed theme extensions
243 std::string installed_pending_load_id_
;
245 // The number of infobars currently displayed.
246 int number_of_infobars_
;
248 content::NotificationRegistrar registrar_
;
250 scoped_ptr
<ThemeSyncableService
> theme_syncable_service_
;
252 base::WeakPtrFactory
<ThemeService
> weak_ptr_factory_
;
254 DISALLOW_COPY_AND_ASSIGN(ThemeService
);
257 #endif // CHROME_BROWSER_THEMES_THEME_SERVICE_H_