File: request.cc

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 (211 lines) | stat: -rw-r--r-- 7,605 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// 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.

#include "components/dbus/xdg/request.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/nix/xdg_util.h"
#include "base/unguessable_token.h"
#include "components/dbus/properties/types.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"

namespace dbus_xdg {

namespace {

constexpr char kPortalServiceName[] = "org.freedesktop.portal.Desktop";
constexpr char kRequestInterface[] = "org.freedesktop.portal.Request";
constexpr char kSignalResponse[] = "Response";

// These constants are defined in
// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html#org-freedesktop-portal-request-response
enum class ResponseCode : uint32_t {
  kSuccess = 0,
  kUserCancelled = 1,
  kOther = 2,
};

}  // namespace

Request::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)
    : bus_(bus),
      callback_(std::move(callback)),
      portal_service_name_(test_portal_service_name.empty()
                               ? kPortalServiceName
                               : test_portal_service_name) {
  CHECK(bus_);
  CHECK(callback_);

  auto handle_token = base::UnguessableToken::Create().ToString();
  request_object_path_ =
      dbus::ObjectPath(base::nix::XdgDesktopPortalRequestPath(
          bus->GetConnectionName(), handle_token));
  auto* request_proxy =
      bus->GetObjectProxy(portal_service_name_, request_object_path_);

  // Connect to the "Response" signal before making the method call to avoid a
  // race condition.
  request_proxy->ConnectToSignal(
      kRequestInterface, kSignalResponse,
      base::BindRepeating(&Request::OnResponseSignal,
                          weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&Request::OnSignalConnected,
                     weak_ptr_factory_.GetWeakPtr()));

  dbus::MethodCall method_call(interface_name, method_name);
  dbus::MessageWriter writer(&method_call);
  arguments.Write(&writer);

  options.PutAs("handle_token", DbusString(handle_token));
  options.Write(&writer);
  object_proxy->CallMethodWithErrorResponse(
      &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
      base::BindOnce(&Request::OnMethodResponse,
                     weak_ptr_factory_.GetWeakPtr()));
}

Request::~Request() {
  if (!bus_) {
    return;
  }

  auto* request_proxy =
      bus_->GetObjectProxy(portal_service_name_, request_object_path_);
  dbus::MethodCall method_call(kRequestInterface, "Close");
  request_proxy->CallMethod(
      &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
      base::BindOnce(
          [](scoped_refptr<dbus::Bus> bus, dbus::ObjectPath request_object_path,
             std::string portal_service_name, dbus::Response*) {
            bus->RemoveObjectProxy(portal_service_name, request_object_path,
                                   base::DoNothing());
          },
          std::move(bus_), std::move(request_object_path_),
          std::move(portal_service_name_)));
}

void Request::OnMethodResponse(dbus::Response* response,
                               dbus::ErrorResponse* error_response) {
  if (!response) {
    VLOG(1) << "Method call failed.";
    Finish(base::unexpected(ResponseError::kMethodCallFailed));
    return;
  }

  // Requests always have a single object path in their response.
  dbus::MessageReader reader(response);
  dbus::ObjectPath new_object_path;
  if (!reader.PopObjectPath(&new_object_path)) {
    LOG(ERROR) << "Failed to read object path from response.";
    Finish(base::unexpected(ResponseError::kInvalidMethodResponse));
    return;
  }
  if (reader.HasMoreData()) {
    LOG(ERROR) << "Unexpected extra data in response.";
    Finish(base::unexpected(ResponseError::kInvalidMethodResponse));
    return;
  }

  // If there's no error reading the response, the response signal handler will
  // call Finish(), so Finish() should not be called past this point.
  if (new_object_path == request_object_path_) {
    return;
  }

  // Prior to version 0.9 of xdg-desktop-portal, a race condition was possible
  // where the signal would be emitted before the client connected to it. This
  // was fixed by adding the "handle_token" key to the options dictionary. To
  // maintain compatibility with older versions of xdg-desktop-portal, the
  // spec recommends clients check the returned object path matches their
  // expectation, and reconnect the signal handler to the returned object path
  // if not. In the wild, nearly all xdg-desktop-portal implementations are
  // version 0.9 or later.
  bus_->RemoveObjectProxy(portal_service_name_, request_object_path_,
                          base::DoNothing());
  request_object_path_ = new_object_path;
  auto* new_request_proxy =
      bus_->GetObjectProxy(portal_service_name_, request_object_path_);
  new_request_proxy->ConnectToSignal(
      kRequestInterface, kSignalResponse,
      base::BindRepeating(&Request::OnResponseSignal,
                          weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(&Request::OnSignalConnected,
                     weak_ptr_factory_.GetWeakPtr()));
}

void Request::OnResponseSignal(dbus::Signal* signal) {
  dbus::MessageReader reader(signal);

  uint32_t response_code = 0;
  if (!reader.PopUint32(&response_code)) {
    LOG(ERROR) << "Failed to read response code from signal.";
    Finish(base::unexpected(ResponseError::kInvalidSignalResponse));
    return;
  }

  switch (static_cast<ResponseCode>(response_code)) {
    case ResponseCode::kSuccess:
      break;
    case ResponseCode::kUserCancelled:
      LOG(ERROR) << "Request cancelled by user.";
      Finish(base::unexpected(ResponseError::kRequestCancelledByUser));
      return;
    case ResponseCode::kOther:
      LOG(ERROR) << "Request ended (non-user cancelled).";
      Finish(base::unexpected(ResponseError::kRequestCancelledOther));
      return;
    default:
      LOG(ERROR) << "Invalid request response code: " << response_code;
      Finish(base::unexpected(ResponseError::kInvalidResponseCode));
      return;
  }

  DbusDictionary results;
  if (!results.Read(&reader)) {
    LOG(ERROR) << "Failed to read results dictionary.";
    Finish(base::unexpected(ResponseError::kInvalidSignalResponse));
    return;
  }

  Finish(base::ok(std::move(results)));
}

void Request::OnSignalConnected(const std::string& interface_name,
                                const std::string& signal_name,
                                bool success) {
  if (!success) {
    LOG(ERROR) << "Failed to connect to signal: " << interface_name << "."
               << signal_name;
    Finish(base::unexpected(ResponseError::kSignalConnectionFailed));
  }
}

void Request::Finish(base::expected<DbusDictionary, ResponseError>&& result) {
  if (!callback_) {
    return;
  }

  bus_->RemoveObjectProxy(portal_service_name_, request_object_path_,
                          base::DoNothing());
  bus_.reset();
  weak_ptr_factory_.InvalidateWeakPtrs();
  std::move(callback_).Run(std::move(result));
  // `this` may be destroyed by the callback.
}

}  // namespace dbus_xdg