ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / themes / theme_service.h
blobb70fd74c3cdb19ec7b8f273e11b1dbbd4532cba1
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
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;
26 class Profile;
28 namespace base {
29 class FilePath;
32 namespace color_utils {
33 struct HSL;
36 namespace extensions {
37 class Extension;
40 namespace gfx {
41 class Image;
44 namespace theme_service_internal {
45 class ThemeServiceTest;
48 namespace ui {
49 class ResourceBundle;
52 #ifdef __OBJC__
53 @class NSString;
54 // Sent whenever the browser theme changes. Object => NSValue wrapping the
55 // ThemeService that changed.
56 extern "C" NSString* const kBrowserThemeDidChangeNotification;
57 #endif // __OBJC__
59 class ThemeService : public base::NonThreadSafe,
60 public content::NotificationObserver,
61 public KeyedService,
62 public ui::ThemeProvider {
63 public:
64 // Public constants used in ThemeService and its subclasses:
65 static const char* kDefaultThemeID;
67 ThemeService();
68 ~ThemeService() override;
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 bool UsingSystemTheme() const override;
80 gfx::ImageSkia* GetImageSkiaNamed(int id) const override;
81 SkColor GetColor(int id) const override;
82 int GetDisplayProperty(int id) const override;
83 bool ShouldUseNativeFrame() const override;
84 bool HasCustomImage(int id) const override;
85 base::RefCountedMemory* GetRawData(int id, ui::ScaleFactor scale_factor)
86 const override;
87 #if defined(OS_MACOSX)
88 NSImage* GetNSImageNamed(int id) const override;
89 NSColor* GetNSImageColorNamed(int id) const override;
90 NSColor* GetNSColor(int id) const override;
91 NSColor* GetNSColorTint(int id) const override;
92 NSGradient* GetNSGradient(int id) const override;
93 #endif
95 // Overridden from content::NotificationObserver:
96 void Observe(int type,
97 const content::NotificationSource& source,
98 const content::NotificationDetails& details) override;
100 // Set the current theme to the theme defined in |extension|.
101 // |extension| must already be added to this profile's
102 // ExtensionService.
103 virtual void SetTheme(const extensions::Extension* extension);
105 // Reset the theme to default.
106 virtual void UseDefaultTheme();
108 // Set the current theme to the system theme. On some platforms, the system
109 // theme is the default theme.
110 virtual void UseSystemTheme();
112 // Returns true if the default theme and system theme are not the same on
113 // this platform.
114 virtual bool IsSystemThemeDistinctFromDefaultTheme() const;
116 // Whether we're using the chrome default theme. Virtual so linux can check
117 // if we're using the GTK theme.
118 virtual bool UsingDefaultTheme() const;
120 // Gets the id of the last installed theme. (The theme may have been further
121 // locally customized.)
122 virtual std::string GetThemeID() const;
124 // This class needs to keep track of the number of theme infobars so that we
125 // clean up unused themes.
126 void OnInfobarDisplayed();
128 // Decrements the number of theme infobars. If the last infobar has been
129 // destroyed, uninstalls all themes that aren't the currently selected.
130 void OnInfobarDestroyed();
132 // Uninstall theme extensions which are no longer in use. |ignore_infobars| is
133 // whether unused themes should be removed despite a theme infobar being
134 // visible.
135 void RemoveUnusedThemes(bool ignore_infobars);
137 // Returns the syncable service for syncing theme. The returned service is
138 // owned by |this| object.
139 virtual ThemeSyncableService* GetThemeSyncableService() const;
141 // Save the images to be written to disk, mapping file path to id.
142 typedef std::map<base::FilePath, int> ImagesDiskCache;
144 protected:
145 // Set a custom default theme instead of the normal default theme.
146 virtual void SetCustomDefaultTheme(
147 scoped_refptr<CustomThemeSupplier> theme_supplier);
149 // Returns true if the ThemeService should use the system theme on startup.
150 virtual bool ShouldInitWithSystemTheme() const;
152 // Get the specified tint - |id| is one of the TINT_* enum values.
153 color_utils::HSL GetTint(int id) const;
155 // Clears all the override fields and saves the dictionary.
156 virtual void ClearAllThemeData();
158 // Load theme data from preferences.
159 virtual void LoadThemePrefs();
161 // Let all the browser views know that themes have changed.
162 virtual void NotifyThemeChanged();
164 #if defined(OS_MACOSX)
165 // Let all the browser views know that themes have changed in a platform way.
166 virtual void NotifyPlatformThemeChanged();
167 #endif // OS_MACOSX
169 // Clears the platform-specific caches. Do not call directly; it's called
170 // from ClearAllThemeData().
171 virtual void FreePlatformCaches();
173 Profile* profile() const { return profile_; }
175 void set_ready() { ready_ = true; }
177 const CustomThemeSupplier* get_theme_supplier() const {
178 return theme_supplier_.get();
181 // True if the theme service is ready to be used.
182 // TODO(pkotwicz): Add DCHECKS to the theme service's getters once
183 // ThemeSource no longer uses the ThemeService when it is not ready.
184 bool ready_;
186 private:
187 friend class theme_service_internal::ThemeServiceTest;
189 // Called when the extension service is ready.
190 void OnExtensionServiceReady();
192 // Migrate the theme to the new theme pack schema by recreating the data pack
193 // from the extension.
194 void MigrateTheme();
196 // Replaces the current theme supplier with a new one and calls
197 // StopUsingTheme() or StartUsingTheme() as appropriate.
198 void SwapThemeSupplier(scoped_refptr<CustomThemeSupplier> theme_supplier);
200 // Saves the filename of the cached theme pack.
201 void SavePackName(const base::FilePath& pack_path);
203 // Save the id of the last theme installed.
204 void SaveThemeID(const std::string& id);
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);
210 #if defined(ENABLE_SUPERVISED_USERS)
211 // Returns true if the profile belongs to a supervised user.
212 bool IsSupervisedUser() const;
214 // Sets the current theme to the supervised user theme. Should only be used
215 // for supervised user profiles.
216 void SetSupervisedUserTheme();
217 #endif
219 #if defined(OS_MACOSX)
220 // |nsimage_cache_| retains the images it has cached.
221 typedef std::map<int, NSImage*> NSImageMap;
222 mutable NSImageMap nsimage_cache_;
224 // |nscolor_cache_| retains the colors it has cached.
225 typedef std::map<int, NSColor*> NSColorMap;
226 mutable NSColorMap nscolor_cache_;
228 typedef std::map<int, NSGradient*> NSGradientMap;
229 mutable NSGradientMap nsgradient_cache_;
230 #endif
232 ui::ResourceBundle& rb_;
233 Profile* profile_;
235 scoped_refptr<CustomThemeSupplier> theme_supplier_;
237 // The id of the theme extension which has just been installed but has not
238 // been loaded yet. The theme extension with |installed_pending_load_id_| may
239 // never be loaded if the install is due to updating a disabled theme.
240 // |pending_install_id_| should be set to |kDefaultThemeID| if there are no
241 // recently installed theme extensions
242 std::string installed_pending_load_id_;
244 // The number of infobars currently displayed.
245 int number_of_infobars_;
247 content::NotificationRegistrar registrar_;
249 scoped_ptr<ThemeSyncableService> theme_syncable_service_;
251 base::WeakPtrFactory<ThemeService> weak_ptr_factory_;
253 DISALLOW_COPY_AND_ASSIGN(ThemeService);
256 #endif // CHROME_BROWSER_THEMES_THEME_SERVICE_H_