File: dark_mode_manager_linux.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (185 lines) | stat: -rw-r--r-- 6,511 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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.

#include "chrome/browser/ui/views/dark_mode_manager_linux.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "components/dbus/thread_linux/dbus_thread_linux.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "ui/linux/linux_ui.h"
#include "ui/linux/linux_ui_factory.h"
#include "ui/native_theme/native_theme.h"

namespace ui {

namespace {

scoped_refptr<dbus::Bus> CreateBus() {
  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SESSION;
  options.connection_type = dbus::Bus::PRIVATE;
  options.dbus_task_runner = dbus_thread_linux::GetTaskRunner();
  return base::MakeRefCounted<dbus::Bus>(options);
}

}  // namespace

DarkModeManagerLinux::DarkModeManagerLinux()
    : DarkModeManagerLinux(CreateBus(),
                           ui::GetDefaultLinuxUiTheme(),
                           &ui::GetLinuxUiThemes(),
                           std::vector<ui::NativeTheme*>{
                               ui::NativeTheme::GetInstanceForNativeUi(),
                               ui::NativeTheme::GetInstanceForWeb()}) {}

DarkModeManagerLinux::DarkModeManagerLinux(
    scoped_refptr<dbus::Bus> bus,
    LinuxUiTheme* default_linux_ui_theme,
    const std::vector<LinuxUiTheme*>* linux_ui_themes,
    std::vector<NativeTheme*> native_themes)
    : linux_ui_themes_(linux_ui_themes),
      native_themes_(native_themes),
      bus_(bus),
      settings_proxy_(bus_->GetObjectProxy(
          kFreedesktopSettingsService,
          dbus::ObjectPath(kFreedesktopSettingsObjectPath))) {
  // Subscribe to changes in the color scheme preference.
  settings_proxy_->ConnectToSignal(
      kFreedesktopSettingsInterface, kSettingChangedSignal,
      base::BindRepeating(&DarkModeManagerLinux::OnPortalSettingChanged,
                          weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&DarkModeManagerLinux::OnSignalConnected,
                     weak_ptr_factory_.GetWeakPtr()));

  // Read initial color scheme preference.
  dbus::MethodCall method_call(kFreedesktopSettingsInterface, kReadMethod);
  dbus::MessageWriter writer(&method_call);
  writer.AppendString(kSettingsNamespace);
  writer.AppendString(kColorSchemeKey);
  settings_proxy_->CallMethodWithErrorCallback(
      &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
      base::BindOnce(&DarkModeManagerLinux::OnReadColorSchemeResponse,
                     weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&DarkModeManagerLinux::OnReadColorSchemeError,
                     weak_ptr_factory_.GetWeakPtr()));

  // Read the toolkit preference while asynchronously fetching the
  // portal preference.
  if (default_linux_ui_theme) {
    auto* native_theme = default_linux_ui_theme->GetNativeTheme();
    native_theme_observer_.Observe(native_theme);
    SetColorScheme(native_theme->ShouldUseDarkColors(), true);
  }
}

DarkModeManagerLinux::~DarkModeManagerLinux() {
  settings_proxy_ = nullptr;
  dbus::Bus* const bus_ptr = bus_.get();
  // `task_runner` may be nullptr in testing.
  if (auto* task_runner = bus_ptr->GetDBusTaskRunner()) {
    task_runner->PostTask(
        FROM_HERE,
        base::BindOnce(&dbus::Bus::ShutdownAndBlock, std::move(bus_)));
  }
}

void DarkModeManagerLinux::OnNativeThemeUpdated(
    ui::NativeTheme* observed_theme) {
  SetColorScheme(observed_theme->ShouldUseDarkColors(), true);
}

void DarkModeManagerLinux::OnSignalConnected(const std::string& interface_name,
                                             const std::string& signal_name,
                                             bool connected) {
  // Nothing to do.  Continue using the toolkit setting if !connected.
}

void DarkModeManagerLinux::OnPortalSettingChanged(dbus::Signal* signal) {
  dbus::MessageReader reader(signal);

  std::string namespace_changed;
  std::string key_changed;
  dbus::MessageReader variant_reader(nullptr);
  if (!reader.PopString(&namespace_changed) ||
      !reader.PopString(&key_changed) || !reader.PopVariant(&variant_reader)) {
    LOG(ERROR) << "Received malformed Setting Changed signal from "
                  "org.freedesktop.portal.Settings";
    return;
  }

  if (namespace_changed != kSettingsNamespace ||
      key_changed != kColorSchemeKey) {
    return;
  }

  uint32_t new_color_scheme;
  if (!variant_reader.PopUint32(&new_color_scheme)) {
    LOG(ERROR)
        << "Failed to read color-scheme value from SettingChanged signal";
    return;
  }

  SetColorScheme(new_color_scheme == kFreedesktopColorSchemeDark, false);
}

void DarkModeManagerLinux::OnReadColorSchemeResponse(dbus::Response* response) {
  if (!response) {
    // Continue using the toolkit setting.
    return;
  }

  dbus::MessageReader reader(response);
  dbus::MessageReader variant_reader(nullptr);
  if (!reader.PopVariant(&variant_reader)) {
    LOG(ERROR) << "Failed to read variant from Read method response";
    return;
  }

  uint32_t new_color_scheme;
  if (!variant_reader.PopVariantOfUint32(&new_color_scheme)) {
    LOG(ERROR) << "Failed to read color-scheme value from Read "
                  "method response";
    return;
  }

  // Once we read the org.freedesktop.appearance color-scheme setting
  // successfully, it should always take precedence over the toolkit color
  // scheme.
  ignore_toolkit_theme_changes_ = true;

  SetColorScheme(new_color_scheme == kFreedesktopColorSchemeDark, false);
}

void DarkModeManagerLinux::OnReadColorSchemeError(dbus::ErrorResponse* error) {
  // Ignore errors.  It's expected that the settings portal may not exist.
}

void DarkModeManagerLinux::SetColorScheme(bool prefer_dark_theme,
                                          bool from_toolkit_theme) {
  if (from_toolkit_theme && ignore_toolkit_theme_changes_) {
    return;
  }
  if (!from_toolkit_theme) {
    for (auto* linux_ui_theme : *linux_ui_themes_) {
      linux_ui_theme->SetDarkTheme(prefer_dark_theme);
    }
  }
  if (prefer_dark_theme_ == prefer_dark_theme) {
    return;
  }
  prefer_dark_theme_ = prefer_dark_theme;

  for (NativeTheme* theme : native_themes_) {
    theme->set_use_dark_colors(prefer_dark_theme_);
    theme->set_preferred_color_scheme(
        prefer_dark_theme_ ? NativeTheme::PreferredColorScheme::kDark
                           : NativeTheme::PreferredColorScheme::kLight);
    theme->NotifyOnNativeThemeUpdated();
  }
}

}  // namespace ui