blob: b573d2e6df87c0b42e69be3294e64a90e0076793 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)90dce4d2013-05-29 14:40:03 +010017#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
20#include "ui/base/theme_provider.h"
21
Ben Murdoch558790d2013-07-30 15:19:42 +010022class CustomThemeSupplier;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000023class BrowserThemePack;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024class ThemeSyncableService;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025class Profile;
26
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000027namespace base {
28class FilePath;
29}
30
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031namespace color_utils {
32struct HSL;
33}
34
35namespace extensions {
36class Extension;
37}
38
39namespace gfx {
40class Image;
41}
42
Ben Murdoch558790d2013-07-30 15:19:42 +010043namespace theme_service_internal {
44class ThemeServiceTest;
45}
46
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047namespace ui {
48class ResourceBundle;
49}
50
51#ifdef __OBJC__
52@class NSString;
53// Sent whenever the browser theme changes. Object => NSValue wrapping the
54// ThemeService that changed.
55extern "C" NSString* const kBrowserThemeDidChangeNotification;
56#endif // __OBJC__
57
58class ThemeService : public base::NonThreadSafe,
59 public content::NotificationObserver,
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010060 public BrowserContextKeyedService,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000061 public ui::ThemeProvider {
62 public:
63 // Public constants used in ThemeService and its subclasses:
Torne (Richard Coles)58218062012-11-14 11:43:16 +000064 static const char* kDefaultThemeID;
65
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066 ThemeService();
67 virtual ~ThemeService();
68
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069 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 Murdochbb1529c2013-08-08 10:24:53 +010087 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)58218062012-11-14 11:43:16 +000091 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)2a99a7e2013-03-28 15:31:22 +0000100 // 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)58218062012-11-14 11:43:16 +0000105 // Set the current theme to the theme defined in |extension|.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000106 // |extension| must already be added to this profile's
107 // ExtensionService.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000108 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 Murdochbb1529c2013-08-08 10:24:53 +0100137 // Remove preference values for themes that are no longer in use.
138 void RemoveUnusedThemes();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000139
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)2a99a7e2013-03-28 15:31:22 +0000145 typedef std::map<base::FilePath, int> ImagesDiskCache;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000146
147 protected:
Ben Murdoch558790d2013-07-30 15:19:42 +0100148 // 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)58218062012-11-14 11:43:16 +0000155 // 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 Murdochbb1529c2013-08-08 10:24:53 +0100173 // from ClearAllThemeData().
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000174 virtual void FreePlatformCaches();
175
Ben Murdocheb525c52013-07-10 11:40:50 +0100176 Profile* profile() const { return profile_; }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000177
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000178 void set_ready() { ready_ = true; }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000179
Ben Murdoch558790d2013-07-30 15:19:42 +0100180 const CustomThemeSupplier* get_theme_supplier() const {
181 return theme_supplier_.get();
182 }
183
Ben Murdochbb1529c2013-08-08 10:24:53 +0100184 // 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)58218062012-11-14 11:43:16 +0000189 private:
Ben Murdoch558790d2013-07-30 15:19:42 +0100190 friend class theme_service_internal::ThemeServiceTest;
191
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +0100192 // 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 Murdochbb1529c2013-08-08 10:24:53 +0100196 // 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)58218062012-11-14 11:43:16 +0000200 // Saves the filename of the cached theme pack.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000201 void SavePackName(const base::FilePath& pack_path);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000202
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 Murdocheb525c52013-07-10 11:40:50 +0100210 // Returns true if the profile belongs to a managed user.
211 bool IsManagedUser() const;
212
Ben Murdoch558790d2013-07-30 15:19:42 +0100213 // 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)58218062012-11-14 11:43:16 +0000217#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 Murdochbb1529c2013-08-08 10:24:53 +0100226 // |nsimage_cache_| retains the images it has cached.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000227 typedef std::map<int, NSImage*> NSImageMap;
228 mutable NSImageMap nsimage_cache_;
229
Ben Murdochbb1529c2013-08-08 10:24:53 +0100230 // |nscolor_cache_| retains the colors it has cached.
231 typedef std::map<int, NSColor*> NSColorMap;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000232 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 Murdoch558790d2013-07-30 15:19:42 +0100241 scoped_refptr<CustomThemeSupplier> theme_supplier_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000242
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)58218062012-11-14 11:43:16 +0000250 DISALLOW_COPY_AND_ASSIGN(ThemeService);
251};
252
253#endif // CHROME_BROWSER_THEMES_THEME_SERVICE_H_