Skip to content

Commit

Permalink
Added SchemeSupported to the OpenURI portal
Browse files Browse the repository at this point in the history
  • Loading branch information
xhorak committed Oct 10, 2024
1 parent 9481527 commit a3ad5c1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
14 changes: 14 additions & 0 deletions data/org.freedesktop.portal.OpenURI.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,20 @@
<arg type="o" name="handle" direction="out"/>
</method>

<!--
SchemeSupported:
@scheme: The URI protocol scheme to check
@supported: True if the URI scheme is supported
Checks if OpenURI supports the protocol scheme @scheme.
The SchemeSupported method was introduced in version 5 of the OpenURI portal API.
-->
<method name="SchemeSupported">
<arg type="s" name="scheme" direction="in"/>
<arg type="b" name="supported" direction="out"/>
</method>

<property name="version" type="u" access="read"/>
</interface>
</node>
25 changes: 24 additions & 1 deletion src/open-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,28 @@ handle_open_in_thread_func (GTask *task,
g_object_ref (request));
}

static gboolean
handle_scheme_supported (XdpDbusOpenURI *object,
GDBusMethodInvocation *invocation,
const gchar *arg_scheme)
{
if (arg_scheme == NULL || *arg_scheme == '\0')
{
g_dbus_method_invocation_return_error (invocation,
XDG_DESKTOP_PORTAL_ERROR,
XDG_DESKTOP_PORTAL_ERROR_INVALID_ARGUMENT,
"Scheme not specified");
return G_DBUS_METHOD_INVOCATION_HANDLED;
}

g_autoptr(GAppInfo) app_info = g_app_info_get_default_for_uri_scheme (arg_scheme);

g_debug ("Handler for scheme: %s%s found.", arg_scheme, app_info ? "" : " not");
g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", app_info != NULL));

return G_DBUS_METHOD_INVOCATION_HANDLED;
}

static gboolean
handle_open_uri (XdpDbusOpenURI *object,
GDBusMethodInvocation *invocation,
Expand Down Expand Up @@ -1094,12 +1116,13 @@ open_uri_iface_init (XdpDbusOpenURIIface *iface)
iface->handle_open_uri = handle_open_uri;
iface->handle_open_file = handle_open_file;
iface->handle_open_directory = handle_open_directory;
iface->handle_scheme_supported = handle_scheme_supported;
}

static void
open_uri_init (OpenURI *openuri)
{
xdp_dbus_open_uri_set_version (XDP_DBUS_OPEN_URI (openuri), 4);
xdp_dbus_open_uri_set_version (XDP_DBUS_OPEN_URI (openuri), 5);
}

static void
Expand Down
58 changes: 58 additions & 0 deletions tests/openuri.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,61 @@ test_open_directory (void)
while (!got_info)
g_main_context_iteration (NULL, TRUE);
}

void
test_scheme_supported(void) {
g_autoptr(GError) error = NULL;

g_autoptr(GDBusProxy) proxy = g_dbus_proxy_new_for_bus_sync (
G_BUS_TYPE_SESSION,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.freedesktop.portal.Desktop",
"/org/freedesktop/portal/desktop",
"org.freedesktop.portal.OpenURI",
NULL,
&error);

g_assert_no_error (error);

GVariant *result;
gboolean supported;

// Existing scheme
result = g_dbus_proxy_call_sync (proxy,
"SchemeSupported",
g_variant_new("(s)", "https"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_variant_get (result, "(b)", &supported);
g_assert_true (supported);
g_variant_unref (result);

// Non existing scheme
result = g_dbus_proxy_call_sync (proxy,
"SchemeSupported",
g_variant_new("(s)", "bogusnonexistanthandler"),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error (error);
g_variant_get (result, "(b)", &supported);
g_assert_false (supported);
g_variant_unref (result);

// Missing scheme name
g_dbus_proxy_call_sync (proxy,
"SchemeSupported",
g_variant_new("(s)", ""),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_error (error,
XDG_DESKTOP_PORTAL_ERROR,
XDG_DESKTOP_PORTAL_ERROR_INVALID_ARGUMENT);
}
1 change: 1 addition & 0 deletions tests/openuri.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ void test_open_uri_close (void);
void test_open_uri_cancel (void);
void test_open_uri_lockdown (void);
void test_open_directory (void);
void test_scheme_supported (void);
3 changes: 2 additions & 1 deletion tests/test-portals.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ DEFINE_TEST_EXISTS(inhibit, INHIBIT, 3)
DEFINE_TEST_EXISTS(location, LOCATION, 1)
DEFINE_TEST_EXISTS(network_monitor, NETWORK_MONITOR, 3)
DEFINE_TEST_EXISTS(notification, NOTIFICATION, 1)
DEFINE_TEST_EXISTS(open_uri, OPEN_URI, 4)
DEFINE_TEST_EXISTS(open_uri, OPEN_URI, 5)
DEFINE_TEST_EXISTS(print, PRINT, 3)
DEFINE_TEST_EXISTS(proxy_resolver, PROXY_RESOLVER, 1)
DEFINE_TEST_EXISTS(screenshot, SCREENSHOT, 2)
Expand Down Expand Up @@ -585,6 +585,7 @@ main (int argc, char **argv)
g_test_add_func ("/portal/openuri/cancel", test_open_uri_cancel);
g_test_add_func ("/portal/openuri/lockdown", test_open_uri_lockdown);
g_test_add_func ("/portal/openuri/directory", test_open_directory);
g_test_add_func ("/portal/openuri/scheme-supported", test_scheme_supported);

g_test_add_func ("/portal/wallpaper/basic", test_wallpaper_basic);
g_test_add_func ("/portal/wallpaper/delay", test_wallpaper_delay);
Expand Down

0 comments on commit a3ad5c1

Please sign in to comment.