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#include "chrome/browser/themes/theme_service_factory.h"
6
7#include "base/logging.h"
8#include "base/prefs/pref_service.h"
9#include "chrome/browser/profiles/incognito_helpers.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/themes/theme_service.h"
12#include "chrome/common/pref_names.h"
13#include "components/keyed_service/content/browser_context_dependency_manager.h"
14#include "components/pref_registry/pref_registry_syncable.h"
15#include "extensions/browser/extension_registry.h"
16
17#if defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS)
18#include "chrome/browser/themes/theme_service_aurax11.h"
19#include "ui/views/linux_ui/linux_ui.h"
20#endif
21
22// static
23ThemeService* ThemeServiceFactory::GetForProfile(Profile* profile) {
24  return static_cast<ThemeService*>(
25      GetInstance()->GetServiceForBrowserContext(profile, true));
26}
27
28// static
29const extensions::Extension* ThemeServiceFactory::GetThemeForProfile(
30    Profile* profile) {
31  std::string id = GetForProfile(profile)->GetThemeID();
32  if (id == ThemeService::kDefaultThemeID)
33    return NULL;
34
35  return extensions::ExtensionRegistry::Get(
36      profile)->enabled_extensions().GetByID(id);
37}
38
39// static
40ThemeServiceFactory* ThemeServiceFactory::GetInstance() {
41  return Singleton<ThemeServiceFactory>::get();
42}
43
44ThemeServiceFactory::ThemeServiceFactory()
45    : BrowserContextKeyedServiceFactory(
46        "ThemeService",
47        BrowserContextDependencyManager::GetInstance()) {}
48
49ThemeServiceFactory::~ThemeServiceFactory() {}
50
51KeyedService* ThemeServiceFactory::BuildServiceInstanceFor(
52    content::BrowserContext* profile) const {
53  ThemeService* provider = NULL;
54#if defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS)
55  provider = new ThemeServiceAuraX11;
56#else
57  provider = new ThemeService;
58#endif
59  provider->Init(static_cast<Profile*>(profile));
60
61  return provider;
62}
63
64void ThemeServiceFactory::RegisterProfilePrefs(
65    user_prefs::PrefRegistrySyncable* registry) {
66#if defined(USE_X11) && !defined(OS_CHROMEOS)
67  bool default_uses_system_theme = false;
68
69#if defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS)
70  const views::LinuxUI* linux_ui = views::LinuxUI::instance();
71  if (linux_ui)
72    default_uses_system_theme = linux_ui->GetDefaultUsesSystemTheme();
73#endif
74
75  registry->RegisterBooleanPref(
76      prefs::kUsesSystemTheme,
77      default_uses_system_theme,
78      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
79#endif
80  registry->RegisterFilePathPref(
81      prefs::kCurrentThemePackFilename,
82      base::FilePath(),
83      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
84  registry->RegisterStringPref(
85      prefs::kCurrentThemeID,
86      ThemeService::kDefaultThemeID,
87      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
88  registry->RegisterDictionaryPref(
89      prefs::kCurrentThemeImages,
90      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
91  registry->RegisterDictionaryPref(
92      prefs::kCurrentThemeColors,
93      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
94  registry->RegisterDictionaryPref(
95      prefs::kCurrentThemeTints,
96      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
97  registry->RegisterDictionaryPref(
98      prefs::kCurrentThemeDisplayProperties,
99      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
100}
101
102content::BrowserContext* ThemeServiceFactory::GetBrowserContextToUse(
103    content::BrowserContext* context) const {
104  return chrome::GetBrowserContextRedirectedInIncognito(context);
105}
106
107bool ThemeServiceFactory::ServiceIsCreatedWithBrowserContext() const {
108  return true;
109}
110