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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AsyncDBus.h"
#include "gio/gio.h"
#include "mozilla/XREAppData.h"
#include "nsAppShell.h"
namespace mozilla::widget {
static void CreateProxyCallback(GObject*, GAsyncResult* aResult,
gpointer aUserData) {
RefPtr<DBusProxyPromise::Private> promise =
dont_AddRef(static_cast<DBusProxyPromise::Private*>(aUserData));
GUniquePtr<GError> error;
RefPtr<GDBusProxy> proxy = dont_AddRef(
g_dbus_proxy_new_for_bus_finish(aResult, getter_Transfers(error)));
if (proxy) {
promise->Resolve(std::move(proxy), __func__);
} else {
promise->Reject(std::move(error), __func__);
}
nsAppShell::DBusConnectionCheck();
}
RefPtr<DBusProxyPromise> CreateDBusProxyForBus(
GBusType aBusType, GDBusProxyFlags aFlags,
GDBusInterfaceInfo* aInterfaceInfo, const char* aName,
const char* aObjectPath, const char* aInterfaceName,
GCancellable* aCancellable) {
nsAppShell::DBusConnectionCheck();
auto promise = MakeRefPtr<DBusProxyPromise::Private>(__func__);
g_dbus_proxy_new_for_bus(aBusType, aFlags, aInterfaceInfo, aName, aObjectPath,
aInterfaceName, aCancellable, CreateProxyCallback,
do_AddRef(promise).take());
return promise.forget();
}
static void ProxyCallCallback(GObject* aSourceObject, GAsyncResult* aResult,
gpointer aUserData) {
RefPtr<DBusCallPromise::Private> promise =
dont_AddRef(static_cast<DBusCallPromise::Private*>(aUserData));
GUniquePtr<GError> error;
RefPtr<GVariant> result = dont_AddRef(g_dbus_proxy_call_finish(
G_DBUS_PROXY(aSourceObject), aResult, getter_Transfers(error)));
if (result) {
promise->Resolve(std::move(result), __func__);
} else {
promise->Reject(std::move(error), __func__);
}
nsAppShell::DBusConnectionCheck();
}
RefPtr<DBusCallPromise> DBusProxyCall(GDBusProxy* aProxy, const char* aMethod,
GVariant* aArgs, GDBusCallFlags aFlags,
gint aTimeout,
GCancellable* aCancellable) {
auto promise = MakeRefPtr<DBusCallPromise::Private>(__func__);
nsAppShell::DBusConnectionCheck();
g_dbus_proxy_call(aProxy, aMethod, aArgs, aFlags, aTimeout, aCancellable,
ProxyCallCallback, do_AddRef(promise).take());
return promise.forget();
}
static void ProxyCallWithUnixFDListCallback(GObject* aSourceObject,
GAsyncResult* aResult,
gpointer aUserData) {
RefPtr<DBusCallPromise::Private> promise =
dont_AddRef(static_cast<DBusCallPromise::Private*>(aUserData));
GUniquePtr<GError> error;
GUnixFDList** aFDList = nullptr;
RefPtr<GVariant> result =
dont_AddRef(g_dbus_proxy_call_with_unix_fd_list_finish(
G_DBUS_PROXY(aSourceObject), aFDList, aResult,
getter_Transfers(error)));
if (result) {
promise->Resolve(std::move(result), __func__);
} else {
promise->Reject(std::move(error), __func__);
}
nsAppShell::DBusConnectionCheck();
}
RefPtr<DBusCallPromise> DBusProxyCallWithUnixFDList(
GDBusProxy* aProxy, const char* aMethod, GVariant* aArgs,
GDBusCallFlags aFlags, gint aTimeout, GUnixFDList* aFDList,
GCancellable* aCancellable) {
auto promise = MakeRefPtr<DBusCallPromise::Private>(__func__);
nsAppShell::DBusConnectionCheck();
g_dbus_proxy_call_with_unix_fd_list(
aProxy, aMethod, aArgs, aFlags, aTimeout, aFDList, aCancellable,
ProxyCallWithUnixFDListCallback, do_AddRef(promise).take());
return promise.forget();
}
// Tokens should be unique and not guessable. To avoid clashes with calls
// made from unrelated libraries, it is a good idea to use a per-library
// prefix combined with a random number.
// Here, we build the token by concatenating MOZ_APP_NAME (e.g. "firefox"),
// with some unique name from the caller, plus a serial and random number.
void MakePortalRequestToken(const nsCString& aType, nsACString& aToken) {
static Atomic<unsigned, MemoryOrdering::Relaxed> sTokenSerial;
aToken.Truncate();
aToken.AppendPrintf(MOZ_APP_NAME "_%s_%u_%u", aType.get(), sTokenSerial++,
g_random_int());
XREAppData::SanitizeNameForDBus(aToken);
}
// The portal request paths are well known:
//
// Since version 0.9 of xdg-desktop-portal, the handle will be of the form
// /org/freedesktop/portal/desktop/request/SENDER/TOKEN
// where SENDER is the callers unique name, with the initial ':' removed and
// all '.' replaced by '_', and TOKEN is a unique token that the caller
// provided with the handle_token key in the options vardict.
//
// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Request.html#org-freedesktop-portal-request
void GetPortalRequestPath(GDBusProxy* aProxy, const nsCString& aRequestToken,
nsACString& aOutPath) {
aOutPath.Truncate();
GDBusConnection* connection = g_dbus_proxy_get_connection(aProxy);
nsAutoCString senderName(g_dbus_connection_get_unique_name(connection));
senderName.ReplaceChar('.', '_');
aOutPath.AppendPrintf("/org/freedesktop/portal/desktop/request/%s/%s",
senderName.get() + 1, aRequestToken.get());
}
struct PortalResponseData {
explicit PortalResponseData(PortalResponseListener aCallback)
: mCallback(std::move(aCallback)) {
MOZ_COUNT_CTOR(PortalResponseData);
}
MOZ_COUNTED_DTOR(PortalResponseData);
PortalResponseListener mCallback;
guint mSubscriptionId = 0;
static void Release(gpointer aData) {
delete static_cast<PortalResponseData*>(aData);
}
static void Fired(GDBusConnection* connection, const gchar* sender_name,
const gchar* object_path, const gchar* interface_name,
const gchar* signal_name, GVariant* parameters,
gpointer user_data) {
nsAppShell::DBusConnectionCheck();
auto* data = static_cast<PortalResponseData*>(user_data);
auto callback = std::move(data->mCallback);
g_dbus_connection_signal_unsubscribe(connection, data->mSubscriptionId);
data = nullptr; // Dangling now!
callback(parameters);
}
};
guint OnDBusPortalResponse(GDBusProxy* aProxy, const nsCString& aRequestToken,
PortalResponseListener aCallback) {
nsAppShell::DBusConnectionCheck();
auto boxedData = MakeUnique<PortalResponseData>(std::move(aCallback));
nsAutoCString requestPath;
GetPortalRequestPath(aProxy, aRequestToken, requestPath);
auto* data = boxedData.get();
guint subscriptionId = g_dbus_connection_signal_subscribe(
g_dbus_proxy_get_connection(aProxy), "org.freedesktop.portal.Desktop",
"org.freedesktop.portal.Request", "Response", requestPath.get(), nullptr,
G_DBUS_SIGNAL_FLAGS_NONE, PortalResponseData::Fired, boxedData.release(),
PortalResponseData::Release);
data->mSubscriptionId = subscriptionId;
return subscriptionId;
}
} // namespace mozilla::widget
|