File: gnome_display_config_dbus_client.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 (103 lines) | stat: -rw-r--r-- 4,230 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
// 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 REMOTING_HOST_LINUX_GNOME_DISPLAY_CONFIG_DBUS_CLIENT_H_
#define REMOTING_HOST_LINUX_GNOME_DISPLAY_CONFIG_DBUS_CLIENT_H_

#include <gio/gio.h>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "remoting/host/linux/gnome_display_config.h"
#include "remoting/host/linux/scoped_glib.h"
#include "ui/base/glib/scoped_gobject.h"

namespace remoting {

// This class provides a wrapper for the GNOME D-Bus API for querying the
// current display (monitor) configuration, and for applying a modified config.
// This wrapper never blocks the caller thread - the caller provides a
// OnceCallback to receive the current config. The caller can modify the
// returned GnomeDisplayConfig structure, and then ask GNOME to apply the
// changes. Note that applying a display config will take effect immediately,
// but will also cause GNOME to display a warning which allows the user to keep
// or revert the changes.
//
// Instances of this class only support a single callback at a time - later
// requests will cancel earlier ones. Changes to GNOME's display config
// should be infrequent, and if multiple operations are ongoing, only the
// latest operation should take effect.
class GnomeDisplayConfigDBusClient {
 public:
  using Callback = base::OnceCallback<void(GnomeDisplayConfig)>;

  GnomeDisplayConfigDBusClient();
  GnomeDisplayConfigDBusClient(const GnomeDisplayConfigDBusClient&) = delete;
  GnomeDisplayConfigDBusClient& operator=(const GnomeDisplayConfigDBusClient&) =
      delete;
  ~GnomeDisplayConfigDBusClient();

  // Initializes the object by requesting a D-Bus connection. This should be
  // called on the same thread as all other public methods, including the dtor.
  // The private static methods get called by GLib and may execute on a
  // different thread.
  void Init();

  // Request the latest config from GNOME. On success, the config will be
  // provided to the callback.
  void GetMonitorsConfig(Callback callback);

  // Requests GNOME to apply a new config. If successful, the change will
  // take immediate effect, but the user may see a popup window and they may
  // choose to revert back to the previous settings.
  void ApplyMonitorsConfig(GnomeDisplayConfig config);

  // Fakes a GetCurrentState() response from GNOME. This allows unittests to
  // exercise this code without relying on GNOME or DBus services.
  void FakeDisplayConfigForTest(ScopedGVariant config);

 private:
  static void OnDBusGetReply(GObject* source_object,
                             GAsyncResult* result,
                             gpointer user_data);
  static void OnDisplayConfigCurrentStateReply(GObject* source_object,
                                               GAsyncResult* result,
                                               gpointer user_data);
  static void OnApplyMonitorsConfigReply(GObject* source_object,
                                         GAsyncResult* result,
                                         gpointer user_data);

  // Starts an async call to the DBus GetCurrentState() method.
  void CallDBusGetCurrentState();

  // Called by OnDBusGetReply().
  void OnDBusGet(ScopedGObject<GDBusConnection> dbus_connection);

  // Called by OnDisplayConfigCurrentStateReply().
  void OnDisplayConfigCurrentState(ScopedGVariant config);

  // Called by OnDisplayConfigCurrentStateReply() on error.
  void OnDisplayConfigCurrentStateError();

  base::WeakPtr<GnomeDisplayConfigDBusClient> weak_ptr_;

  scoped_refptr<base::SequencedTaskRunner> caller_task_runner_;

  ScopedGObject<GCancellable> cancellable_
      GUARDED_BY_CONTEXT(sequence_checker_);
  ScopedGObject<GDBusConnection> dbus_connection_
      GUARDED_BY_CONTEXT(sequence_checker_);

  Callback pending_callback_ GUARDED_BY_CONTEXT(sequence_checker_);

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<GnomeDisplayConfigDBusClient> weak_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_HOST_LINUX_GNOME_DISPLAY_CONFIG_DBUS_CLIENT_H_