[epiphany/pgriffis/web-extension/background-page: 1/2] WebExtensions: Fix runtime.openOptionsPage()




commit b819e5e718821841bb2d7696ae099ecdb06d672e
Author: Patrick Griffis <pgriffis igalia com>
Date:   Wed Jun 8 15:46:05 2022 -0500

    WebExtensions: Fix runtime.openOptionsPage()
    
    This is not ideal but currently we don't support loading extension pages
    in normal EphyWebViews.
    
    For now we can just open a new Window for options
    with a web view that has the extension API.

 po/POTFILES.in                                |  1 +
 src/webextension/api/runtime.c                | 38 ++++++++++++++++++---------
 src/webextension/ephy-web-extension-manager.c |  8 +++---
 src/webextension/ephy-web-extension-manager.h |  2 ++
 src/webextension/ephy-web-extension.c         |  4 +--
 src/webextension/ephy-web-extension.h         |  2 +-
 6 files changed, 35 insertions(+), 20 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index d5f25d115..f4f4e811a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -93,4 +93,5 @@ src/resources/gtk/web-extensions-dialog.ui
 src/search-provider/ephy-search-provider.c
 src/synced-tabs-dialog.c
 src/webapp-provider/ephy-webapp-provider.c
+src/webextension/api/runtime.c
 src/window-commands.c
diff --git a/src/webextension/api/runtime.c b/src/webextension/api/runtime.c
index 0419a7097..fee77464f 100644
--- a/src/webextension/api/runtime.c
+++ b/src/webextension/api/runtime.c
@@ -106,22 +106,34 @@ runtime_handler_open_options_page (EphyWebExtension  *self,
                                    WebKitWebView     *web_view,
                                    GError           **error)
 {
-  const char *data = ephy_web_extension_get_option_ui_page (self);
+  const char *options_ui = ephy_web_extension_get_option_ui_page (self);
+  EphyShell *shell = ephy_shell_get_default ();
+  g_autofree char *title = NULL;
+  g_autofree char *options_uri = NULL;
+  GtkWidget *new_web_view;
+  GtkWindow *new_window;
+
+  if (!options_ui) {
+    g_set_error_literal (error, WEB_EXTENSION_ERROR, WEB_EXTENSION_ERROR_INVALID_ARGUMENT, "Extension does 
not have an options page");
+    return NULL;
+  }
 
-  if (data) {
-    EphyEmbed *embed;
-    EphyShell *shell = ephy_shell_get_default ();
-    WebKitWebView *web_view;
-    GtkWindow *window = gtk_application_get_active_window (GTK_APPLICATION (shell));
+  title = g_strdup_printf (_("Options for %s"), ephy_web_extension_get_name (self));
+  options_uri = g_strdup_printf ("ephy-webextension://%s/%s", ephy_web_extension_get_guid (self), 
options_ui);
 
-    embed = ephy_shell_new_tab (shell,
-                                EPHY_WINDOW (window),
-                                NULL,
-                                EPHY_NEW_TAB_JUMP);
+  new_window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
+  gtk_window_set_transient_for (new_window, gtk_application_get_active_window (GTK_APPLICATION (shell)));
+  gtk_window_set_destroy_with_parent (new_window, TRUE);
+  gtk_window_set_title (new_window, title);
+  gtk_window_set_position (new_window, GTK_WIN_POS_CENTER_ON_PARENT);
 
-    web_view = EPHY_GET_WEBKIT_WEB_VIEW_FROM_EMBED (embed);
-    webkit_web_view_load_html (web_view, data, NULL);
-  }
+  new_web_view = ephy_web_extensions_manager_create_web_extensions_webview (self);
+  gtk_container_add (GTK_CONTAINER (new_window), new_web_view);
+
+  webkit_web_view_load_uri (WEBKIT_WEB_VIEW (new_web_view), options_uri);
+
+  gtk_widget_show (new_web_view);
+  gtk_window_present (new_window);
 
   return NULL;
 }
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index 523e3b7b8..214df4302 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -850,8 +850,8 @@ decide_policy_cb (WebKitWebView            *web_view,
   return TRUE;
 }
 
-static GtkWidget *
-create_web_extensions_webview (EphyWebExtension *web_extension)
+GtkWidget *
+ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension *web_extension)
 {
   EphyWebExtensionManager *manager = ephy_web_extension_manager_get_default ();
   g_autoptr (WebKitUserContentManager) ucm = NULL;
@@ -950,7 +950,7 @@ create_browser_popup (EphyWebExtension *web_extension)
   g_autofree char *popup_uri = NULL;
   const char *popup;
 
-  web_view = create_web_extensions_webview (web_extension);
+  web_view = ephy_web_extensions_manager_create_web_extensions_webview (web_extension);
   gtk_widget_hide (web_view); /* Shown in on_popup_load_changed. */
   ephy_web_extension_manager_register_popup_view (manager, web_extension, web_view);
 
@@ -1151,7 +1151,7 @@ run_background_script (EphyWebExtensionManager *self,
   page = ephy_web_extension_background_web_view_get_page (web_extension);
 
   /* Create new background web_view */
-  background = create_web_extensions_webview (web_extension);
+  background = ephy_web_extensions_manager_create_web_extensions_webview (web_extension);
   ephy_web_extension_manager_set_background_web_view (self, web_extension, WEBKIT_WEB_VIEW (background));
 
   if (page) {
diff --git a/src/webextension/ephy-web-extension-manager.h b/src/webextension/ephy-web-extension-manager.h
index 3e639c8af..e9fd62412 100644
--- a/src/webextension/ephy-web-extension-manager.h
+++ b/src/webextension/ephy-web-extension-manager.h
@@ -97,4 +97,6 @@ void                     ephy_web_extension_manager_emit_in_tab_with_reply
                                                                                      const char              
*sender_json,
                                                                                      GTask                   
*reply_task);
 
+GtkWidget                *ephy_web_extensions_manager_create_web_extensions_webview (EphyWebExtension        
*web_extension);
+
 G_END_DECLS
diff --git a/src/webextension/ephy-web-extension.c b/src/webextension/ephy-web-extension.c
index 2a71f79d8..885e3f976 100644
--- a/src/webextension/ephy-web-extension.c
+++ b/src/webextension/ephy-web-extension.c
@@ -1241,13 +1241,13 @@ ephy_web_extension_custom_css_style (EphyWebExtension *self,
   return css->style;
 }
 
-char *
+const char *
 ephy_web_extension_get_option_ui_page (EphyWebExtension *self)
 {
   if (!self->options_ui)
     return NULL;
 
-  return ephy_web_extension_get_resource_as_string (self, self->options_ui->page);
+  return self->options_ui->page;
 }
 
 const char *
diff --git a/src/webextension/ephy-web-extension.h b/src/webextension/ephy-web-extension.h
index 53d20de56..78cd1e557 100644
--- a/src/webextension/ephy-web-extension.h
+++ b/src/webextension/ephy-web-extension.h
@@ -142,7 +142,7 @@ GList                 *ephy_web_extension_get_custom_css_list             (EphyW
 WebKitUserStyleSheet  *ephy_web_extension_custom_css_style                (EphyWebExtension *self,
                                                                            gpointer          custom_css);
 
-char                  *ephy_web_extension_get_option_ui_page              (EphyWebExtension *self);
+const char            *ephy_web_extension_get_option_ui_page              (EphyWebExtension *self);
 
 const char            *ephy_web_extension_get_guid                        (EphyWebExtension *self);
 


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