[epiphany/pgriffis/web-extension/tabs: 2/5] WebExtensions: Implement tabs.remove()




commit e6e8cd0104951211ae0cc4e51c72c48cd3429d98
Author: Patrick Griffis <pgriffis igalia com>
Date:   Thu Jun 2 17:11:27 2022 -0500

    WebExtensions: Implement tabs.remove()

 .../resources/js/webextensions.js                  |  1 +
 src/webextension/api/tabs.c                        | 56 ++++++++++++++++++++++
 2 files changed, 57 insertions(+)
---
diff --git a/embed/web-process-extension/resources/js/webextensions.js 
b/embed/web-process-extension/resources/js/webextensions.js
index c59c4bcae..8694656f6 100644
--- a/embed/web-process-extension/resources/js/webextensions.js
+++ b/embed/web-process-extension/resources/js/webextensions.js
@@ -23,6 +23,7 @@ window.browser.tabs = {
     query: function (...args) { return ephy_message ('tabs.query', args); },
     get: function (...args) { return ephy_message ('tabs.get', args); },
     insertCSS: function (...args) { return ephy_message ('tabs.insertCSS', args); },
+    remove: function (...args) { return ephy_message ('tabs.remove', args); },
     removeCSS: function (...args) { return ephy_message ('tabs.removeCSS', args); },
     sendMessage: function (...args) { return ephy_message ('tabs.sendMessage', args); },
     onActivated: new EphyEventListener (),
diff --git a/src/webextension/api/tabs.c b/src/webextension/api/tabs.c
index 7e812c9d4..8df12f66a 100644
--- a/src/webextension/api/tabs.c
+++ b/src/webextension/api/tabs.c
@@ -613,10 +613,66 @@ tabs_handler_create (EphyWebExtension  *self,
   return json_to_string (root, FALSE);
 }
 
+static void
+close_tab_id (EphyShell *shell,
+              int        tab_id)
+{
+  EphyWindow *parent;
+  EphyTabView *tab_view;
+  GtkWidget *web_view;
+
+  web_view = GTK_WIDGET (get_web_view_for_tab_id (shell, tab_id, &parent));
+  if (!web_view)
+    return;
+
+  tab_view = ephy_window_get_tab_view (parent);
+  ephy_tab_view_close (tab_view, gtk_widget_get_parent (gtk_widget_get_parent (web_view)));
+}
+
+static char *
+tabs_handler_remove (EphyWebExtension  *self,
+                     char              *name,
+                     JSCValue          *args,
+                     gint64             extension_page_id,
+                     GError           **error)
+{
+  EphyShell *shell = ephy_shell_get_default ();
+  g_autoptr (JSCValue) tab_ids = NULL;
+
+  /* FIXME: Epiphany should use webkit_web_view_try_close() and this should wait until after that. */
+
+  tab_ids = jsc_value_object_get_property_at_index (args, 0);
+
+  if (jsc_value_is_number (tab_ids)) {
+    close_tab_id (shell, jsc_value_to_int32 (tab_ids));
+    return NULL;
+  }
+
+  if (jsc_value_is_array (tab_ids)) {
+    g_autoptr (JSCValue) value = jsc_value_object_get_property_at_index (tab_ids, 0);
+    for (guint i = 1; !jsc_value_is_undefined (value); i++) {
+      if (jsc_value_is_number (value))
+        close_tab_id (shell, jsc_value_to_int32 (value));
+
+      g_object_unref (value);
+      value = jsc_value_object_get_property_at_index (tab_ids, i);
+    }
+
+    return NULL;
+  }
+
+  g_set_error_literal (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.remove(): 
First argument is not a number or array.");
+  return NULL;
+}
+  g_set_error_literal (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "tabs.close(): 
First argument is not a number or array.");
+  return NULL;
+}
+
 static EphyWebExtensionSyncApiHandler tabs_handlers[] = {
   {"create", tabs_handler_create},
   {"query", tabs_handler_query},
   {"insertCSS", tabs_handler_insert_css},
+  {"remove", tabs_handler_remove},
   {"removeCSS", tabs_handler_remove_css},
   {"get", tabs_handler_get},
   {"executeScript", tabs_handler_execute_script},


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