1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/UniquePtrExtensions.h"
9 namespace mozilla::widget
{
11 static void CreateProxyCallback(GObject
*, GAsyncResult
* aResult
,
13 RefPtr
<DBusProxyPromise::Private
> promise
=
14 dont_AddRef(static_cast<DBusProxyPromise::Private
*>(aUserData
));
15 GUniquePtr
<GError
> error
;
16 RefPtr
<GDBusProxy
> proxy
= dont_AddRef(
17 g_dbus_proxy_new_for_bus_finish(aResult
, getter_Transfers(error
)));
19 promise
->Resolve(std::move(proxy
), __func__
);
21 promise
->Reject(std::move(error
), __func__
);
25 RefPtr
<DBusProxyPromise
> CreateDBusProxyForBus(
26 GBusType aBusType
, GDBusProxyFlags aFlags
,
27 GDBusInterfaceInfo
* aInterfaceInfo
, const char* aName
,
28 const char* aObjectPath
, const char* aInterfaceName
,
29 GCancellable
* aCancellable
) {
30 auto promise
= MakeRefPtr
<DBusProxyPromise::Private
>(__func__
);
31 g_dbus_proxy_new_for_bus(aBusType
, aFlags
, aInterfaceInfo
, aName
, aObjectPath
,
32 aInterfaceName
, aCancellable
, CreateProxyCallback
,
33 do_AddRef(promise
).take());
34 return promise
.forget();
37 static void ProxyCallCallback(GObject
* aSourceObject
, GAsyncResult
* aResult
,
39 RefPtr
<DBusCallPromise::Private
> promise
=
40 dont_AddRef(static_cast<DBusCallPromise::Private
*>(aUserData
));
41 GUniquePtr
<GError
> error
;
42 RefPtr
<GVariant
> result
= dont_AddRef(g_dbus_proxy_call_finish(
43 G_DBUS_PROXY(aSourceObject
), aResult
, getter_Transfers(error
)));
45 promise
->Resolve(std::move(result
), __func__
);
47 promise
->Reject(std::move(error
), __func__
);
51 RefPtr
<DBusCallPromise
> DBusProxyCall(GDBusProxy
* aProxy
, const char* aMethod
,
52 GVariant
* aArgs
, GDBusCallFlags aFlags
,
54 GCancellable
* aCancellable
) {
55 auto promise
= MakeRefPtr
<DBusCallPromise::Private
>(__func__
);
56 g_dbus_proxy_call(aProxy
, aMethod
, aArgs
, aFlags
, aTimeout
, aCancellable
,
57 ProxyCallCallback
, do_AddRef(promise
).take());
58 return promise
.forget();
61 static void ProxyCallWithUnixFDListCallback(GObject
* aSourceObject
,
62 GAsyncResult
* aResult
,
64 RefPtr
<DBusCallPromise::Private
> promise
=
65 dont_AddRef(static_cast<DBusCallPromise::Private
*>(aUserData
));
66 GUniquePtr
<GError
> error
;
67 GUnixFDList
** aFDList
= nullptr;
68 RefPtr
<GVariant
> result
=
69 dont_AddRef(g_dbus_proxy_call_with_unix_fd_list_finish(
70 G_DBUS_PROXY(aSourceObject
), aFDList
, aResult
,
71 getter_Transfers(error
)));
73 promise
->Resolve(std::move(result
), __func__
);
75 promise
->Reject(std::move(error
), __func__
);
79 RefPtr
<DBusCallPromise
> DBusProxyCallWithUnixFDList(
80 GDBusProxy
* aProxy
, const char* aMethod
, GVariant
* aArgs
,
81 GDBusCallFlags aFlags
, gint aTimeout
, GUnixFDList
* aFDList
,
82 GCancellable
* aCancellable
) {
83 auto promise
= MakeRefPtr
<DBusCallPromise::Private
>(__func__
);
84 g_dbus_proxy_call_with_unix_fd_list(
85 aProxy
, aMethod
, aArgs
, aFlags
, aTimeout
, aFDList
, aCancellable
,
86 ProxyCallWithUnixFDListCallback
, do_AddRef(promise
).take());
87 return promise
.forget();
90 } // namespace mozilla::widget