[epiphany/pgriffis/web-extension/api-cleanups: 12/20] WebExtensions: notifications: Rewrite using async handlers
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/api-cleanups: 12/20] WebExtensions: notifications: Rewrite using async handlers
- Date: Wed, 6 Jul 2022 01:22:16 +0000 (UTC)
commit f00a544c392d38f9c8e983b1bc365e8fc3b1f2a5
Author: Patrick Griffis <pgriffis igalia com>
Date: Sun Jul 3 14:45:26 2022 -0500
WebExtensions: notifications: Rewrite using async handlers
src/webextension/api/notifications.c | 74 +++++++++++++++---------------------
1 file changed, 31 insertions(+), 43 deletions(-)
---
diff --git a/src/webextension/api/notifications.c b/src/webextension/api/notifications.c
index 79135dc10..a132a5283 100644
--- a/src/webextension/api/notifications.c
+++ b/src/webextension/api/notifications.c
@@ -34,11 +34,11 @@ create_extension_notification_id (EphyWebExtension *web_extension,
return g_strconcat (ephy_web_extension_get_guid (web_extension), ".", id, NULL);
}
-static char *
-notifications_handler_create (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
- GError **error)
+static void
+notifications_handler_create (EphyWebExtensionSender *sender,
+ char *name,
+ JSCValue *args,
+ GTask *task)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
g_autoptr (JSCValue) button_array = NULL;
@@ -58,23 +58,23 @@ notifications_handler_create (EphyWebExtensionSender *sender,
value = jsc_value_object_get_property_at_index (args, 1);
} else {
if (strcmp (name, "update") == 0) {
- g_set_error_literal (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.update(): id not given");
- return NULL;
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.update(): id not given");
+ return;
}
id = g_dbus_generate_guid ();
}
if (!jsc_value_is_object (value)) {
- g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "notifications.%s():
notificationOptions not given", name);
- return NULL;
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.%s(): notificationOptions not given", name);
+ return;
}
title = api_utils_get_string_property (value, "title", NULL);
message = api_utils_get_string_property (value, "message", NULL);
if (!title || !message) {
- g_set_error (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "notifications.%s():
title and message are required", name);
- return NULL;
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.%s(): title and message are required", name);
+ return;
}
notification = g_notification_new (title);
@@ -94,22 +94,22 @@ notifications_handler_create (EphyWebExtensionSender *sender,
namespaced_id = create_extension_notification_id (sender->extension, id);
g_application_send_notification (G_APPLICATION (ephy_shell_get_default ()), namespaced_id, notification);
- return g_strdup_printf ("\"%s\"", id);
+ g_task_return_pointer (task, g_strdup_printf ("\"%s\"", id), g_free);
}
-static char *
-notifications_handler_clear (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
- GError **error)
+static void
+notifications_handler_clear (EphyWebExtensionSender *sender,
+ char *name,
+ JSCValue *args,
+ GTask *task)
{
g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
g_autofree char *id = NULL;
g_autofree char *namespaced_id = NULL;
if (!jsc_value_is_string (value)) {
- g_set_error_literal (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.clear(): id not given");
- return NULL;
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"notifications.clear(): id not given");
+ return;
}
id = jsc_value_to_string (value);
@@ -118,20 +118,20 @@ notifications_handler_clear (EphyWebExtensionSender *sender,
g_application_withdraw_notification (G_APPLICATION (ephy_shell_get_default ()), namespaced_id);
/* We don't actually know if a notification was withdrawn but lets assume it was. */
- return g_strdup ("true");
+ g_task_return_pointer (task, g_strdup ("true"), g_free);
}
-static char *
-notifications_handler_get_all (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
- GError **error)
+static void
+notifications_handler_get_all (EphyWebExtensionSender *sender,
+ char *name,
+ JSCValue *args,
+ GTask *task)
{
/* GNotification does not provide information on the state of notifications. */
- return g_strdup ("[]");
+ g_task_return_pointer (task, g_strdup ("[]"), g_free);
}
-static EphyWebExtensionSyncApiHandler notifications_handlers[] = {
+static EphyWebExtensionAsyncApiHandler notifications_handlers[] = {
{"create", notifications_handler_create},
{"clear", notifications_handler_clear},
{"getAll", notifications_handler_get_all},
@@ -144,32 +144,20 @@ ephy_web_extension_api_notifications_handler (EphyWebExtensionSender *sender,
JSCValue *args,
GTask *task)
{
- g_autoptr (GError) error = NULL;
-
if (!ephy_web_extension_has_permission (sender->extension, "notifications")) {
g_warning ("Extension %s tried to use notifications without permission.", ephy_web_extension_get_name
(sender->extension));
- error = g_error_new_literal (WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_PERMISSION_DENIED, "Permission
Denied");
- g_task_return_error (task, g_steal_pointer (&error));
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_PERMISSION_DENIED, "Permission
Denied");
return;
}
for (guint idx = 0; idx < G_N_ELEMENTS (notifications_handlers); idx++) {
- EphyWebExtensionSyncApiHandler handler = notifications_handlers[idx];
- char *ret;
+ EphyWebExtensionAsyncApiHandler handler = notifications_handlers[idx];
if (g_strcmp0 (handler.name, name) == 0) {
- ret = handler.execute (sender, name, args, &error);
-
- if (error)
- g_task_return_error (task, g_steal_pointer (&error));
- else
- g_task_return_pointer (task, ret, g_free);
-
+ handler.execute (sender, name, args, task);
return;
}
}
- g_warning ("%s(): '%s' not implemented by Epiphany!", __FUNCTION__, name);
- error = g_error_new_literal (WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_NOT_IMPLEMENTED, "Not Implemented");
- g_task_return_error (task, g_steal_pointer (&error));
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_NOT_IMPLEMENTED, "Not
Implemented");
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]