File: dark_mode_manager_linux.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (106 lines) | stat: -rw-r--r-- 3,849 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_
#define CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_

#include <string>
#include <vector>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/native_theme_observer.h"

namespace dbus {
class Bus;
class ErrorResponse;
class MessageReader;
class ObjectProxy;
class Response;
class Signal;
}  // namespace dbus

namespace ui {

class LinuxUiTheme;
class DarkModeManagerLinuxTest;

// Observes the system color scheme preference using
// org.freedesktop.portal.Settings. Falls back to the toolkit preference if
// org.freedesktop.portal.Settings is unavailable.  Propagates the dark mode
// preference to the web theme.
class DarkModeManagerLinux : public NativeThemeObserver {
 public:
  DarkModeManagerLinux();
  DarkModeManagerLinux(
      scoped_refptr<dbus::Bus> bus,
      LinuxUiTheme* default_linux_ui_theme,
      const std::vector<raw_ptr<LinuxUiTheme, VectorExperimental>>*
          linux_ui_themes,
      std::vector<raw_ptr<NativeTheme, VectorExperimental>> native_themes);
  DarkModeManagerLinux(const DarkModeManagerLinux&) = delete;
  DarkModeManagerLinux& operator=(const DarkModeManagerLinux&) = delete;
  ~DarkModeManagerLinux() override;

  bool prefer_dark_theme() const { return prefer_dark_theme_; }

 private:
  friend class DarkModeManagerLinuxTest;
  FRIEND_TEST_ALL_PREFIXES(DarkModeManagerLinuxTest, UseNativeThemeSetting);
  FRIEND_TEST_ALL_PREFIXES(DarkModeManagerLinuxTest, UsePortalSetting);
  FRIEND_TEST_ALL_PREFIXES(DarkModeManagerLinuxTest, UsePortalAccentColor);

  constexpr static char kFreedesktopSettingsService[] =
      "org.freedesktop.portal.Desktop";
  constexpr static char kFreedesktopSettingsObjectPath[] =
      "/org/freedesktop/portal/desktop";
  constexpr static char kFreedesktopSettingsInterface[] =
      "org.freedesktop.portal.Settings";
  constexpr static char kSettingChangedSignal[] = "SettingChanged";
  constexpr static char kReadMethod[] = "Read";
  constexpr static char kSettingsNamespace[] = "org.freedesktop.appearance";
  constexpr static char kColorSchemeKey[] = "color-scheme";
  constexpr static char kAccentColorKey[] = "accent-color";
  constexpr static int kFreedesktopColorSchemeDark = 1;

  // ui::NativeThemeObserver:
  void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;

  // D-Bus async handlers
  void OnSignalConnected(const std::string& interface_name,
                         const std::string& signal_name,
                         bool connected);
  void OnPortalSettingChanged(dbus::Signal* signal);
  void OnReadColorSchemeResponse(dbus::Response* response);
  void OnReadAccentColorResponse(dbus::Response* response);
  void OnReadError(dbus::ErrorResponse* error);

  // Sets `prefer_dark_theme_` and propagates to the web theme.
  void SetColorScheme(bool prefer_dark_theme, bool from_toolkit_theme);

  void SetAccentColor(dbus::MessageReader* reader);

  raw_ptr<const std::vector<raw_ptr<LinuxUiTheme, VectorExperimental>>>
      linux_ui_themes_;
  std::vector<raw_ptr<NativeTheme, VectorExperimental>> native_themes_;

  scoped_refptr<dbus::Bus> bus_;
  raw_ptr<dbus::ObjectProxy> settings_proxy_;

  bool prefer_dark_theme_ = false;
  bool ignore_toolkit_theme_changes_ = false;

  base::ScopedObservation<NativeTheme, NativeThemeObserver>
      native_theme_observer_{this};

  base::WeakPtrFactory<DarkModeManagerLinux> weak_ptr_factory_{this};
};

}  // namespace ui

#endif  // CHROME_BROWSER_UI_VIEWS_DARK_MODE_MANAGER_LINUX_H_