File: request.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (80 lines) | stat: -rw-r--r-- 2,767 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DBUS_XDG_REQUEST_H_
#define COMPONENTS_DBUS_XDG_REQUEST_H_

#include <string>

#include "base/component_export.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "components/dbus/properties/types.h"

namespace dbus {
class Bus;
class ObjectProxy;
}  // namespace dbus

namespace dbus_xdg {

// Error codes returned by the callback.
enum class COMPONENT_EXPORT(COMPONENTS_DBUS) ResponseError {
  kSignalConnectionFailed,
  kMethodCallFailed,
  kInvalidMethodResponse,
  kInvalidSignalResponse,
  kRequestCancelledByUser,
  kRequestCancelledOther,
  kInvalidResponseCode,
};

using ResponseCallback = base::OnceCallback<void(
    /*results=*/base::expected<DbusDictionary, ResponseError>)>;

class COMPONENT_EXPORT(COMPONENTS_DBUS) Request {
 public:
  // Makes a DBus XDG request and runs `callback` with the results. `arguments`
  // is of type DbusParameters, or can be any DbusType if there's exactly one
  // argument. The `options` dictionary contains any options except for
  // handle_token which will be set and managed internally.
  // `test_portal_service_name` may be provided to override in tests.
  // https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html
  Request(scoped_refptr<dbus::Bus> bus,
          dbus::ObjectProxy* object_proxy,
          const std::string& interface_name,
          const std::string& method_name,
          const DbusType& arguments,
          DbusDictionary&& options,
          ResponseCallback callback,
          const std::string& test_portal_service_name = std::string());

  Request(Request&& other) noexcept = delete;
  Request& operator=(Request&& other) noexcept = delete;

  // If the request has not finished, the destructor will call the "Close"
  // method on the request object, and the callback will not be run.
  ~Request();

 private:
  void OnMethodResponse(dbus::Response* response,
                        dbus::ErrorResponse* error_response);
  void OnResponseSignal(dbus::Signal* signal);
  void OnSignalConnected(const std::string& interface_name,
                         const std::string& signal_name,
                         bool success);
  // `this` may be destructed after Finish() runs.
  void Finish(base::expected<DbusDictionary, ResponseError>&& result);

  scoped_refptr<dbus::Bus> bus_;
  dbus::ObjectPath request_object_path_;
  ResponseCallback callback_;
  std::string portal_service_name_;
  base::WeakPtrFactory<Request> weak_ptr_factory_{this};
};

}  // namespace dbus_xdg

#endif  // COMPONENTS_DBUS_XDG_REQUEST_H_