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_
|