[epiphany/pgriffis/web-extension-gtask: 8/15] WebExtensions: Update all API handlers to take a list of args




commit ad0f4146b3d2198ef5e0d273fc16f2c71647b818
Author: Patrick Griffis <pgriffis igalia com>
Date:   Wed May 25 15:21:59 2022 -0500

    WebExtensions: Update all API handlers to take a list of args
    
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1119>

 src/webextension/api/notifications.c |  8 +++--
 src/webextension/api/pageaction.c    | 41 ++++++++++++++---------
 src/webextension/api/runtime.c       |  3 +-
 src/webextension/api/storage.c       | 33 +++++++++---------
 src/webextension/api/tabs.c          | 65 ++++++++++++++++++------------------
 5 files changed, 84 insertions(+), 66 deletions(-)
---
diff --git a/src/webextension/api/notifications.c b/src/webextension/api/notifications.c
index d63c119ef..8e33bd783 100644
--- a/src/webextension/api/notifications.c
+++ b/src/webextension/api/notifications.c
@@ -30,16 +30,20 @@ notifications_handler_create (EphyWebExtension *self,
                               char             *name,
                               JSCValue         *args)
 {
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   g_autofree char *title_str = NULL;
   g_autofree char *message_str = NULL;
   g_autoptr (JSCValue) title = NULL;
   g_autoptr (JSCValue) message = NULL;
   EphyNotification *notify;
 
-  title = jsc_value_object_get_property (args, "title");
+  if (!jsc_value_is_object (value))
+    return NULL;
+
+  title = jsc_value_object_get_property (value, "title");
   title_str = jsc_value_to_string (title);
 
-  message = jsc_value_object_get_property (args, "message");
+  message = jsc_value_object_get_property (value, "message");
   message_str = jsc_value_to_string (message);
 
   notify = ephy_notification_new (g_strdup (title_str), g_strdup (message_str));
diff --git a/src/webextension/api/pageaction.c b/src/webextension/api/pageaction.c
index 1f2d53ff4..70b6df697 100644
--- a/src/webextension/api/pageaction.c
+++ b/src/webextension/api/pageaction.c
@@ -28,7 +28,7 @@
 
 static GtkWidget *
 pageaction_get_action (EphyWebExtension *self,
-                       JSCValue         *args)
+                       JSCValue         *value)
 {
   EphyWebView *web_view = NULL;
   EphyShell *shell = ephy_shell_get_default ();
@@ -36,14 +36,18 @@ pageaction_get_action (EphyWebExtension *self,
   g_autoptr (JSCValue) tab_id = NULL;
   gint32 nr;
 
-  if (jsc_value_object_has_property (args, "tabId")) {
-    tab_id = jsc_value_object_get_property (args, "tabId");
-    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);
-      return NULL;
-    }
+  if (!jsc_value_is_object (value))
+    return NULL;
+
+  tab_id = jsc_value_object_get_property (value, "tabId");
+  if (!jsc_value_is_number (tab_id))
+    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);
+    return NULL;
   }
 
   return ephy_web_extension_manager_get_page_action (manager, self, web_view);
@@ -57,12 +61,13 @@ pageaction_handler_seticon (EphyWebExtension *self,
   GtkWidget *action;
   g_autoptr (JSCValue) path = NULL;
   g_autoptr (GdkPixbuf) pixbuf = NULL;
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
 
-  action = pageaction_get_action (self, args);
+  action = pageaction_get_action (self, value);
   if (!action)
     return NULL;
 
-  path = jsc_value_object_get_property (args, "path");
+  path = jsc_value_object_get_property (value, "path");
   pixbuf = ephy_web_extension_load_pixbuf (self, jsc_value_to_string (path));
 
   gtk_image_set_from_pixbuf (GTK_IMAGE (gtk_bin_get_child (GTK_BIN (action))), pixbuf);
@@ -77,12 +82,13 @@ pageaction_handler_settitle (EphyWebExtension *self,
 {
   GtkWidget *action;
   g_autoptr (JSCValue) title = NULL;
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
 
-  action = pageaction_get_action (self, args);
+  action = pageaction_get_action (self, value);
   if (!action)
     return NULL;
 
-  title = jsc_value_object_get_property (args, "title");
+  title = jsc_value_object_get_property (value, "title");
   gtk_widget_set_tooltip_text (action, jsc_value_to_string (title));
 
   return NULL;
@@ -93,10 +99,11 @@ pageaction_handler_gettitle (EphyWebExtension *self,
                              char             *name,
                              JSCValue         *args)
 {
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   GtkWidget *action;
   g_autofree char *title = NULL;
 
-  action = pageaction_get_action (self, args);
+  action = pageaction_get_action (self, value);
   if (!action)
     return NULL;
 
@@ -110,9 +117,10 @@ pageaction_handler_show (EphyWebExtension *self,
                          char             *name,
                          JSCValue         *args)
 {
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   GtkWidget *action;
 
-  action = pageaction_get_action (self, args);
+  action = pageaction_get_action (self, value);
   if (!action)
     return NULL;
 
@@ -126,9 +134,10 @@ pageaction_handler_hide (EphyWebExtension *self,
                          char             *name,
                          JSCValue         *args)
 {
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   GtkWidget *action;
 
-  action = pageaction_get_action (self, args);
+  action = pageaction_get_action (self, value);
   if (!action)
     return NULL;
 
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 303edbade..4f9b1be1e 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -53,9 +53,10 @@ runtime_handler_send_message (EphyWebExtension *self,
   EphyShell *shell = ephy_shell_get_default ();
   EphyWebExtensionManager *manager = ephy_shell_get_web_extension_manager (shell);
   WebKitWebView *view = WEBKIT_WEB_VIEW (ephy_web_extension_manager_get_background_web_view (manager, self));
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   g_autofree char *script = NULL;
 
-  script = g_strdup_printf ("window.browser.runtime.onMessage._emit(%s);", jsc_value_to_json (args, 2));
+  script = g_strdup_printf ("window.browser.runtime.onMessage._emit(%s);", jsc_value_to_json (value, 0));
   webkit_web_view_run_javascript (view, script, NULL, NULL, NULL);
 
   return NULL;
diff --git a/src/webextension/api/storage.c b/src/webextension/api/storage.c
index ff31c8764..9f048b1df 100644
--- a/src/webextension/api/storage.c
+++ b/src/webextension/api/storage.c
@@ -65,14 +65,15 @@ storage_handler_local_set (EphyWebExtension *self,
   JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
   JsonObject *local_storage_obj = json_node_get_object (local_storage);
   g_auto (GStrv) keys = NULL;
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
 
-  if (!jsc_value_is_object (args))
+  if (!jsc_value_is_object (value))
     return NULL;
 
-  keys = jsc_value_object_enumerate_properties (args);
+  keys = jsc_value_object_enumerate_properties (value);
 
   for (guint i = 0; keys[i]; i++)
-    json_object_set_member (local_storage_obj, keys[i], node_from_value_property (args, keys[i]));
+    json_object_set_member (local_storage_obj, keys[i], node_from_value_property (value, keys[i]));
 
   /* FIXME: Implement storage.onChanged */
   /* FIXME: Async IO */
@@ -91,15 +92,16 @@ storage_handler_local_get (EphyWebExtension *self,
   g_autoptr (JsonBuilder) builder = NULL;
   g_autoptr (JsonNode) root = NULL;
   g_auto (GStrv) keys = NULL;
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
 
-  if (jsc_value_is_null (args))
+  if (jsc_value_is_null (value))
     return json_to_string (local_storage, FALSE);
 
   builder = json_builder_new ();
   json_builder_begin_object (builder);
 
-  if (jsc_value_is_string (args)) {
-    g_autofree char *key = jsc_value_to_string (args);
+  if (jsc_value_is_string (value)) {
+    g_autofree char *key = jsc_value_to_string (value);
     JsonNode *member = json_object_get_member (local_storage_obj, key);
     if (member) {
       json_builder_set_member_name (builder, key);
@@ -108,8 +110,8 @@ storage_handler_local_get (EphyWebExtension *self,
     goto end_get;
   }
 
-  if (jsc_value_is_array (args)) {
-    keys = strv_from_value (args);
+  if (jsc_value_is_array (value)) {
+    keys = strv_from_value (value);
     for (guint i = 0; keys[i]; i++) {
       const char *key = keys[i];
       JsonNode *member = json_object_get_member (local_storage_obj, key);
@@ -121,14 +123,14 @@ storage_handler_local_get (EphyWebExtension *self,
     goto end_get;
   }
 
-  if (jsc_value_is_object (args)) {
-    keys = jsc_value_object_enumerate_properties (args);
+  if (jsc_value_is_object (value)) {
+    keys = jsc_value_object_enumerate_properties (value);
     for (guint i = 0; keys[i]; i++) {
       const char *key = keys[i];
       JsonNode *member = json_object_get_member (local_storage_obj, key);
       json_builder_set_member_name (builder, key);
       if (!member)
-        member = node_from_value_property (args, key);
+        member = node_from_value_property (value, key);
       json_builder_add_value (builder, member);
     }
     goto end_get;
@@ -147,15 +149,16 @@ storage_handler_local_remove (EphyWebExtension *self,
 {
   JsonNode *local_storage = ephy_web_extension_get_local_storage (self);
   JsonObject *local_storage_obj = json_node_get_object (local_storage);
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
 
-  if (jsc_value_is_string (args)) {
-    g_autofree char *key = jsc_value_to_string (args);
+  if (jsc_value_is_string (value)) {
+    g_autofree char *key = jsc_value_to_string (value);
     json_object_remove_member (local_storage_obj, key);
     goto end_remove;
   }
 
-  if (jsc_value_is_array (args)) {
-    g_auto (GStrv) keys = strv_from_value (args);
+  if (jsc_value_is_array (value)) {
+    g_auto (GStrv) keys = strv_from_value (value);
     for (guint i = 0; keys[i]; i++) {
       json_object_remove_member (local_storage_obj, keys[i]);
     }
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 47e0fb1c9..66d264319 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -154,6 +154,7 @@ tabs_handler_query (EphyWebExtension *self,
   g_autoptr (JsonBuilder) builder = json_builder_new ();
   g_autoptr (JsonNode) root = NULL;
   EphyShell *shell = ephy_shell_get_default ();
+  g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (args, 0);
   GList *windows;
   EphyWindow *active_window;
   TabQuery current_window;
@@ -161,10 +162,13 @@ tabs_handler_query (EphyWebExtension *self,
   gint32 window_id;
   gint32 tab_index;
 
-  active = get_tab_query_property (args, "active");
-  current_window = get_tab_query_property (args, "currentWindow");
-  window_id = get_number_property (args, "windowId");
-  tab_index = get_number_property (args, "index");
+  if (!jsc_value_is_object (value))
+    return NULL;
+
+  active = get_tab_query_property (value, "active");
+  current_window = get_tab_query_property (value, "currentWindow");
+  window_id = get_number_property (value, "windowId");
+  tab_index = get_number_property (value, "index");
 
   if (window_id == WINDOW_ID_CURRENT) {
     current_window = TRUE;
@@ -233,16 +237,16 @@ tabs_handler_insert_css (EphyWebExtension *self,
 
   /* This takes an optional first argument so it's either:
    * [tabId:int, details:obj], or [details:obj] */
-  if (!jsc_value_is_array (args))
-    return NULL;
-
-  obj = jsc_value_object_get_property_at_index (args, 0);
-  if (!jsc_value_is_object (obj)) {
-    g_object_unref (obj);
-    tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  if (jsc_value_is_number (tab_id_value)) {
     obj = jsc_value_object_get_property_at_index (args, 1);
+  } else {
+    obj = g_steal_pointer (&tab_id_value);
   }
 
+  if (!jsc_value_is_object (obj))
+    return NULL;
+
   if (!tab_id_value)
     target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
   else
@@ -280,16 +284,16 @@ tabs_handler_remove_css (EphyWebExtension *self,
 
   /* This takes an optional first argument so it's either:
    * [tabId:int, details:obj], or [details:obj] */
-  if (!jsc_value_is_array (args))
-    return NULL;
-
-  obj = jsc_value_object_get_property_at_index (args, 0);
-  if (!jsc_value_is_object (obj)) {
-    g_object_unref (obj);
-    tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  if (jsc_value_is_number (tab_id_value)) {
     obj = jsc_value_object_get_property_at_index (args, 1);
+  } else {
+    obj = g_steal_pointer (&tab_id_value);
   }
 
+  if (!jsc_value_is_object (obj))
+    return NULL;
+
   if (!tab_id_value)
     target_web_view = WEBKIT_WEB_VIEW (ephy_shell_get_active_web_view (shell));
   else
@@ -323,8 +327,8 @@ tabs_handler_get (EphyWebExtension *self,
   EphyWebView *target_web_view;
   EphyWindow *parent_window;
 
-  /* FIXME: These should raise error on failure. */
-  if (!jsc_value_is_number (args))
+  tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  if (!jsc_value_is_number (tab_id_value))
     return NULL;
 
   target_web_view = EPHY_WEB_VIEW (get_web_view_for_tab_id (shell, jsc_value_to_int32 (args), 
&parent_window));
@@ -353,16 +357,16 @@ tabs_handler_execute_script (EphyWebExtension *self,
 
   /* This takes an optional first argument so it's either:
    * [tabId:int, details:obj], or [details:obj] */
-  if (!jsc_value_is_array (args))
-    return NULL;
-
-  obj = jsc_value_object_get_property_at_index (args, 0);
-  if (!jsc_value_is_object (obj)) {
-    g_object_unref (obj);
-    tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  tab_id_value = jsc_value_object_get_property_at_index (args, 0);
+  if (jsc_value_is_number (tab_id_value)) {
     obj = jsc_value_object_get_property_at_index (args, 1);
+  } else {
+    obj = g_steal_pointer (&tab_id_value);
   }
 
+  if (!jsc_value_is_object (obj))
+    return NULL;
+
   file_value = jsc_value_object_get_property (obj, "file");
   code_value = jsc_value_object_get_property (obj, "code");
 
@@ -408,15 +412,12 @@ tabs_handler_send_message (EphyWebExtension *self,
   EphyShell *shell = ephy_shell_get_default ();
   WebKitWebView *target_web_view;
 
-  if (!jsc_value_is_array (args))
-    return NULL;
-
   tab_id_value = jsc_value_object_get_property_at_index (args, 0);
-  if (!tab_id_value)
+  if (!jsc_value_is_number (tab_id_value))
     return NULL;
 
   message_value = jsc_value_object_get_property_at_index (args, 1);
-  if (!message_value)
+  if (jsc_value_is_undefined (message_value))
     return NULL;
 
   serialized_message = jsc_value_to_json (message_value, 0);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]