[epiphany/pgriffis/web-extension/api-cleanups] WebExtensions: pageAction: Port to json-glib
- From: Patrick Griffis <pgriffis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany/pgriffis/web-extension/api-cleanups] WebExtensions: pageAction: Port to json-glib
- Date: Mon, 4 Jul 2022 02:23:05 +0000 (UTC)
commit 777edb332a2c3bce3559dc259ec2bc77b1621138
Author: Patrick Griffis <pgriffis igalia com>
Date: Sun Jul 3 21:21:27 2022 -0500
WebExtensions: pageAction: Port to json-glib
src/webextension/api/pageaction.c | 126 ++++++++++++++++++++-----------------
src/webextension/ephy-json-utils.c | 35 +++++++++++
src/webextension/ephy-json-utils.h | 5 ++
3 files changed, 108 insertions(+), 58 deletions(-)
---
diff --git a/src/webextension/api/pageaction.c b/src/webextension/api/pageaction.c
index e48ba26d1..4ac7468a0 100644
--- a/src/webextension/api/pageaction.c
+++ b/src/webextension/api/pageaction.c
@@ -20,6 +20,7 @@
#include "config.h"
+#include "ephy-json-utils.h"
#include "ephy-shell.h"
#include "ephy-web-extension.h"
#include "ephy-window.h"
@@ -27,147 +28,150 @@
#include "pageaction.h"
static GtkWidget *
-pageaction_get_action (EphyWebExtension *extension,
- JSCValue *value)
+get_action_for_tab_id (EphyWebExtension *extension,
+ gint64 tab_id)
{
- EphyWebView *web_view = NULL;
- EphyShell *shell = ephy_shell_get_default ();
EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
- g_autoptr (JSCValue) tab_id = NULL;
- gint32 nr;
-
- if (!jsc_value_is_object (value))
- return NULL;
+ EphyShell *shell = ephy_shell_get_default ();
+ EphyWebView *web_view;
- tab_id = jsc_value_object_get_property (value, "tabId");
- if (!jsc_value_is_number (tab_id))
+ if (tab_id <= 0)
return NULL;
- nr = jsc_value_to_int32 (tab_id);
- web_view = ephy_shell_get_web_view (shell, nr);
- if (!web_view) {
- LOG ("%s(): Invalid tabId '%d', abort\n", __FUNCTION__, nr);
+ web_view = ephy_shell_get_web_view (shell, tab_id);
+ if (!web_view)
return NULL;
- }
return ephy_web_extension_manager_get_page_action (manager, extension, web_view);
}
static void
pageaction_handler_seticon (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
+ const char *method_name,
+ JsonArray *args,
GTask *task)
{
+ JsonObject *details = ephy_json_array_get_object (args, 0);
+ gint64 tab_id;
+ const char *path;
GtkWidget *action;
- g_autofree char *path_str = NULL;
- g_autoptr (JSCValue) path_value = NULL;
g_autoptr (GdkPixbuf) pixbuf = NULL;
- g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
- action = pageaction_get_action (sender->extension, value);
+ if (!details) {
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.setIcon(): Missing details object");
+ return;
+ }
+
+ tab_id = ephy_json_object_get_int (details, "tabId");
+ action = get_action_for_tab_id (sender->extension, tab_id);
if (!action) {
- g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Invalid
Arguments");
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.setIcon(): Failed to find action by tabId");
return;
}
- path_value = jsc_value_object_get_property (value, "path");
- if (jsc_value_is_string (path_value)) {
- path_str = jsc_value_to_string (path_value);
- pixbuf = ephy_web_extension_load_pixbuf (sender->extension, path_str, -1);
- } else {
+ if (ephy_json_object_get_object (details, "path")) {
+ /* FIXME: Support object here. */
g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.setIcon(): Currently only single path strings are supported.");
return;
}
+ /* FIXME: path == "" should reset to default icon and path == NULL should be set to the mainfest's
page_action icon. */
+ path = ephy_json_object_get_string (details, "path");
+ if (path)
+ pixbuf = ephy_web_extension_load_pixbuf (sender->extension, path, -1);
+
gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (action))), pixbuf);
g_task_return_pointer (task, NULL, NULL);
}
static void
pageaction_handler_settitle (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
+ const char *method_name,
+ JsonArray *args,
GTask *task)
{
+ JsonObject *details = ephy_json_array_get_object (args, 0);
+ gint64 tab_id;
+ const char *title;
GtkWidget *action;
- g_autoptr (JSCValue) title = NULL;
- g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
- action = pageaction_get_action (sender->extension, value);
- if (!action) {
- g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Invalid
Arguments");
+ if (!details) {
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.setTitle(): Missing details object");
return;
}
- title = jsc_value_object_get_property (value, "title");
- gtk_widget_set_tooltip_text (action, jsc_value_to_string (title));
+ tab_id = ephy_json_object_get_int (details, "tabId");
+ action = get_action_for_tab_id (sender->extension, tab_id);
+ if (!action) {
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.setTitle(): Failed to find action by tabId");
+ return;
+ }
+ /* FIXME: NULL title should set it to the default value in the manifest. */
+ title = ephy_json_object_get_string (details, "title");
+ gtk_widget_set_tooltip_text (action, title);
g_task_return_pointer (task, NULL, NULL);
}
static void
pageaction_handler_gettitle (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
+ const char *method_name,
+ JsonArray *args,
GTask *task)
{
- g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
+ gint64 tab_id = ephy_json_array_get_int (args, 0);
GtkWidget *action;
g_autofree char *title = NULL;
- action = pageaction_get_action (sender->extension, value);
+ action = get_action_for_tab_id (sender->extension, tab_id);
if (!action) {
- g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Invalid
Arguments");
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.getTitle(): Failed to find action by tabId");
return;
}
title = gtk_widget_get_tooltip_text (action);
-
g_task_return_pointer (task, g_strdup_printf ("\"%s\"", title ? title : ""), g_free);
}
static void
pageaction_handler_show (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
+ const char *method_name,
+ JsonArray *args,
GTask *task)
{
- g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
+ gint64 tab_id = ephy_json_array_get_int (args, 0);
GtkWidget *action;
- action = pageaction_get_action (sender->extension, value);
+ action = get_action_for_tab_id (sender->extension, tab_id);
if (!action) {
- g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Invalid
Arguments");
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.show(): Failed to find action by tabId");
return;
}
gtk_widget_set_visible (action, TRUE);
-
g_task_return_pointer (task, NULL, NULL);
}
static void
pageaction_handler_hide (EphyWebExtensionSender *sender,
- char *name,
- JSCValue *args,
+ const char *method_name,
+ JsonArray *args,
GTask *task)
{
- g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
+ gint64 tab_id = ephy_json_array_get_int (args, 0);
GtkWidget *action;
- action = pageaction_get_action (sender->extension, value);
+ action = get_action_for_tab_id (sender->extension, tab_id);
if (!action) {
- g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Invalid
Arguments");
+ g_task_return_new_error (task, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT,
"pageAction.hide(): Failed to find action by tabId");
return;
}
gtk_widget_set_visible (action, FALSE);
-
g_task_return_pointer (task, NULL, NULL);
}
-static EphyWebExtensionAsyncApiHandler pageaction_handlers[] = {
+static EphyWebExtensionApiHandler pageaction_handlers[] = {
{"setIcon", pageaction_handler_seticon},
{"setTitle", pageaction_handler_settitle},
{"getTitle", pageaction_handler_gettitle},
@@ -181,11 +185,17 @@ ephy_web_extension_api_pageaction_handler (EphyWebExtensionSender *sender,
JSCValue *args,
GTask *task)
{
+ /* Temporary conversion as we migrate to json-glib here. */
+ g_autofree char *json = jsc_value_to_json (args, 0);
+ g_autoptr (JsonNode) json_node = json_from_string (json, NULL);
+ JsonArray *json_args = json_node_get_array (json_node);
+ json_array_seal (json_args);
+
for (guint idx = 0; idx < G_N_ELEMENTS (pageaction_handlers); idx++) {
- EphyWebExtensionAsyncApiHandler handler = pageaction_handlers[idx];
+ EphyWebExtensionApiHandler handler = pageaction_handlers[idx];
if (g_strcmp0 (handler.name, name) == 0) {
- handler.execute (sender, name, args, task);
+ handler.execute (sender, name, json_args, task);
return;
}
}
diff --git a/src/webextension/ephy-json-utils.c b/src/webextension/ephy-json-utils.c
index a3f3fa36a..cc49eb38e 100644
--- a/src/webextension/ephy-json-utils.c
+++ b/src/webextension/ephy-json-utils.c
@@ -215,6 +215,26 @@ ephy_json_node_to_string (JsonNode *node)
return json_node_get_string (node);
}
+/**
+ * ephy_json_node_get_int:
+ * @node: (nullable): A #JsonNode or %NULL
+ *
+ * This safely turns a #JsonNode into an int.
+ *
+ * Returns: (transfer none): A int or -1 if not a number.
+ */
+gint64
+ephy_json_node_get_int (JsonNode *node)
+{
+ if (!node || !JSON_NODE_HOLDS_VALUE (node))
+ return -1;
+
+ if (json_node_get_value_type (node) != G_TYPE_INT64)
+ return -1;
+
+ return json_node_get_int (node);
+}
+
/**
* ephy_json_array_get_string_with_default:
* @array: A #JsonArray
@@ -265,3 +285,18 @@ ephy_json_array_get_object (JsonArray *array,
{
return ephy_json_node_get_object (json_array_get_element (array, index));
}
+
+/**
+ * ephy_json_array_get_int:
+ * @array: A #JsonArray
+ *
+ * This safely gets an int from an array.
+ *
+ * Returns: (transfer none): An int or -1 if not a number.
+ */
+gint64
+ephy_json_array_get_int (JsonArray *array,
+ guint index)
+{
+ return ephy_json_node_get_int (json_array_get_element (array, index));
+}
diff --git a/src/webextension/ephy-json-utils.h b/src/webextension/ephy-json-utils.h
index 86e78eb5b..e950dcaeb 100644
--- a/src/webextension/ephy-json-utils.h
+++ b/src/webextension/ephy-json-utils.h
@@ -50,6 +50,8 @@ JsonObject *ephy_json_node_get_object (JsonNode *node);
const char *ephy_json_node_to_string (JsonNode *node);
+gint64 ephy_json_node_get_int (JsonNode *node);
+
const char *ephy_json_array_get_string (JsonArray *array,
guint index);
@@ -60,3 +62,6 @@ const char *ephy_json_array_get_string_with_default
JsonObject *ephy_json_array_get_object (JsonArray *array,
guint index);
+
+gint64 ephy_json_array_get_int (JsonArray *array,
+ guint index);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]