1// Copyright 2013 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_aurax11.h"
6
7#include "base/bind.h"
8#include "base/prefs/pref_service.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/themes/custom_theme_supplier.h"
11#include "chrome/common/pref_names.h"
12#include "ui/gfx/image/image.h"
13#include "ui/native_theme/native_theme_aura.h"
14#include "ui/views/linux_ui/linux_ui.h"
15
16namespace {
17
18class SystemThemeX11 : public CustomThemeSupplier {
19 public:
20  explicit SystemThemeX11(PrefService* pref_service);
21
22  // Overridden from CustomThemeSupplier:
23  virtual void StartUsingTheme() OVERRIDE;
24  virtual void StopUsingTheme() OVERRIDE;
25  virtual bool GetColor(int id, SkColor* color) const OVERRIDE;
26  virtual gfx::Image GetImageNamed(int id) OVERRIDE;
27  virtual bool HasCustomImage(int id) const OVERRIDE;
28
29 private:
30  virtual ~SystemThemeX11();
31
32  // These pointers are not owned by us.
33  views::LinuxUI* const linux_ui_;
34  PrefService* const pref_service_;
35
36  DISALLOW_COPY_AND_ASSIGN(SystemThemeX11);
37};
38
39SystemThemeX11::SystemThemeX11(PrefService* pref_service)
40    : CustomThemeSupplier(NATIVE_X11),
41      linux_ui_(views::LinuxUI::instance()),
42      pref_service_(pref_service) {}
43
44void SystemThemeX11::StartUsingTheme() {
45  pref_service_->SetBoolean(prefs::kUsesSystemTheme, true);
46  // Have the former theme notify its observers of change.
47  ui::NativeThemeAura::instance()->NotifyObservers();
48}
49
50void SystemThemeX11::StopUsingTheme() {
51  pref_service_->SetBoolean(prefs::kUsesSystemTheme, false);
52  // Have the former theme notify its observers of change.
53  if (linux_ui_)
54    linux_ui_->GetNativeTheme(NULL)->NotifyObservers();
55}
56
57bool SystemThemeX11::GetColor(int id, SkColor* color) const {
58  return linux_ui_ && linux_ui_->GetColor(id, color);
59}
60
61gfx::Image SystemThemeX11::GetImageNamed(int id) {
62  return linux_ui_ ? linux_ui_->GetThemeImageNamed(id) : gfx::Image();
63}
64
65bool SystemThemeX11::HasCustomImage(int id) const {
66  return linux_ui_ && linux_ui_->HasCustomImage(id);
67}
68
69SystemThemeX11::~SystemThemeX11() {}
70
71}  // namespace
72
73ThemeServiceAuraX11::ThemeServiceAuraX11() {}
74
75ThemeServiceAuraX11::~ThemeServiceAuraX11() {}
76
77bool ThemeServiceAuraX11::ShouldInitWithSystemTheme() const {
78  return profile()->GetPrefs()->GetBoolean(prefs::kUsesSystemTheme);
79}
80
81void ThemeServiceAuraX11::UseSystemTheme() {
82  SetCustomDefaultTheme(new SystemThemeX11(profile()->GetPrefs()));
83}
84
85bool ThemeServiceAuraX11::IsSystemThemeDistinctFromDefaultTheme() const {
86  return true;
87}
88
89bool ThemeServiceAuraX11::UsingDefaultTheme() const {
90  return ThemeService::UsingDefaultTheme() && !UsingSystemTheme();
91}
92
93bool ThemeServiceAuraX11::UsingSystemTheme() const {
94  const CustomThemeSupplier* theme_supplier = get_theme_supplier();
95  return theme_supplier &&
96         theme_supplier->get_theme_type() == CustomThemeSupplier::NATIVE_X11;
97}
98