[epiphany] Support view-source: scheme



commit 93ea2cf83391a6464c00de9934b1be5749d82a78
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Thu Jun 16 20:22:20 2022 +0200

    Support view-source: scheme
    
    Minimc Firefox and Chrome and offer view-source: as an option to open source view.
    
    Fixes: https://gitlab.gnome.org/GNOME/epiphany/-/issues/1764
    Part-of: <https://gitlab.gnome.org/GNOME/epiphany/-/merge_requests/1147>

 embed/ephy-embed-shell.c         |  2 +-
 embed/ephy-embed-utils.c         |  5 +----
 embed/ephy-view-source-handler.c | 48 ++++++----------------------------------
 embed/ephy-view-source-handler.h |  2 +-
 embed/ephy-web-view.h            |  1 +
 src/ephy-session.c               |  4 +++-
 src/window-commands.c            | 23 ++-----------------
 7 files changed, 16 insertions(+), 69 deletions(-)
---
diff --git a/embed/ephy-embed-shell.c b/embed/ephy-embed-shell.c
index 4582dc18e..d5f9f029d 100644
--- a/embed/ephy-embed-shell.c
+++ b/embed/ephy-embed-shell.c
@@ -753,7 +753,7 @@ ephy_resource_request_cb (WebKitURISchemeRequest *request)
       g_str_has_prefix (path, "/org/gnome/epiphany/page-templates/") ||
       (g_str_has_prefix (uri, "ephy-pdf:") && g_str_has_prefix (path, "/org/gnome/epiphany/pdfjs/")) ||
       (g_str_has_prefix (uri, "ephy-reader:") && g_str_has_prefix (path, 
"/org/gnome/epiphany/readability/")) ||
-      (g_str_has_prefix (uri, "ephy-source:") && g_str_has_prefix (path, 
"/org/gnome/epiphany/highlightjs/"))) {
+      (g_str_has_prefix (uri, EPHY_VIEW_SOURCE_SCHEME ":") && g_str_has_prefix (path, 
"/org/gnome/epiphany/highlightjs/"))) {
     stream = g_resources_open_stream (path, 0, &error);
     if (stream)
       webkit_uri_scheme_request_finish (request, stream, size, NULL);
diff --git a/embed/ephy-embed-utils.c b/embed/ephy-embed-utils.c
index a21319f31..676165069 100644
--- a/embed/ephy-embed-utils.c
+++ b/embed/ephy-embed-utils.c
@@ -144,7 +144,7 @@ ephy_embed_utils_address_has_web_scheme (const char *address)
                      g_ascii_strncasecmp (address, "about", colonpos) &&
                      g_ascii_strncasecmp (address, "ephy-about", colonpos) &&
                      g_ascii_strncasecmp (address, "ephy-resource", colonpos) &&
-                     g_ascii_strncasecmp (address, "ephy-source", colonpos) &&
+                     g_ascii_strncasecmp (address, EPHY_VIEW_SOURCE_SCHEME, colonpos) &&
                      g_ascii_strncasecmp (address, "ephy-reader", colonpos) &&
                      g_ascii_strncasecmp (address, "ephy-pdf", colonpos) &&
                      g_ascii_strncasecmp (address, "gopher", colonpos) &&
@@ -380,9 +380,6 @@ ephy_embed_utils_is_no_show_address (const char *address)
     if (!strcmp (address, do_not_show_address[i]))
       return TRUE;
 
-  if (g_str_has_prefix (address, EPHY_VIEW_SOURCE_SCHEME))
-    return TRUE;
-
   return FALSE;
 }
 
diff --git a/embed/ephy-view-source-handler.c b/embed/ephy-view-source-handler.c
index 10e4ddc44..fb716e8b8 100644
--- a/embed/ephy-view-source-handler.c
+++ b/embed/ephy-view-source-handler.c
@@ -194,12 +194,9 @@ ephy_view_source_request_begin_get_source_from_uri (EphyViewSourceRequest *reque
 
 static gint
 embed_is_displaying_matching_uri (EphyEmbed *embed,
-                                  GUri      *uri)
+                                  char      *uri)
 {
   EphyWebView *web_view;
-  g_autoptr (GUri) view_uri = NULL;
-  g_autofree char *modified_uri = NULL;
-  g_autofree char *uri_string = NULL;
 
   if (ephy_embed_has_load_pending (embed))
     return -1;
@@ -208,18 +205,11 @@ embed_is_displaying_matching_uri (EphyEmbed *embed,
   if (ephy_web_view_is_loading (web_view))
     return -1;
 
-  view_uri = g_uri_parse (ephy_web_view_get_address (web_view),
-                          G_URI_FLAGS_NONE, NULL);
-  if (!view_uri)
-    return -1;
-
-  modified_uri = g_uri_to_string_partial (view_uri, G_URI_HIDE_FRAGMENT);
-  uri_string = g_uri_to_string (uri);
-  return strcmp (modified_uri, uri_string);
+  return g_strcmp0 (ephy_web_view_get_address (web_view), uri);
 }
 
 static WebKitWebView *
-get_web_view_matching_uri (GUri *uri)
+get_web_view_matching_uri (const char *uri)
 {
   EphyEmbedShell *shell;
   GtkWindow *window;
@@ -248,8 +238,7 @@ out:
 static void
 ephy_view_source_request_start (EphyViewSourceRequest *request)
 {
-  g_autoptr (GUri) uri = NULL;
-  g_autoptr (GUri) converted_uri = NULL;
+  const char *converted_uri;
   const char *original_uri;
   WebKitWebView *web_view;
 
@@ -257,38 +246,15 @@ ephy_view_source_request_start (EphyViewSourceRequest *request)
     g_list_prepend (request->source_handler->outstanding_requests, request);
 
   original_uri = webkit_uri_scheme_request_get_uri (request->scheme_request);
-  uri = g_uri_parse (original_uri, G_URI_FLAGS_ENCODED | G_URI_FLAGS_SCHEME_NORMALIZE, NULL);
-
-  if (!uri || !g_uri_get_fragment (uri)) {
-    /* Can't assert because user could theoretically input something weird */
-    GError *error = g_error_new (WEBKIT_NETWORK_ERROR,
-                                 WEBKIT_NETWORK_ERROR_FAILED,
-                                 _("%s is not a valid URI"),
-                                 original_uri);
-    finish_uri_scheme_request (request, NULL, error);
-    g_error_free (error);
-    return;
-  }
 
-  /* Convert e.g. ephy-source://gnome.org#https to https://gnome.org */
-  converted_uri = g_uri_build (g_uri_get_flags (uri),
-                               g_uri_get_fragment (uri),
-                               g_uri_get_userinfo (uri),
-                               g_uri_get_host (uri),
-                               g_uri_get_port (uri),
-                               g_uri_get_path (uri),
-                               g_uri_get_query (uri),
-                               NULL);
-  g_assert (converted_uri);
+  /* Convert e.g. view-source:https://gnome.org to https://gnome.org */
+  converted_uri = original_uri + strlen (EPHY_VIEW_SOURCE_SCHEME) + 1;
 
   web_view = get_web_view_matching_uri (converted_uri);
   if (web_view)
     ephy_view_source_request_begin_get_source_from_web_view (request, WEBKIT_WEB_VIEW (web_view));
   else {
-    g_autofree char *modified_uri = NULL;
-
-    modified_uri = g_uri_to_string (converted_uri);
-    ephy_view_source_request_begin_get_source_from_uri (request, modified_uri);
+    ephy_view_source_request_begin_get_source_from_uri (request, converted_uri);
   }
 }
 
diff --git a/embed/ephy-view-source-handler.h b/embed/ephy-view-source-handler.h
index 268b0cce6..1957f869d 100644
--- a/embed/ephy-view-source-handler.h
+++ b/embed/ephy-view-source-handler.h
@@ -28,7 +28,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (EphyViewSourceHandler, ephy_view_source_handler, EPHY, VIEW_SOURCE_HANDLER, GObject)
 
-#define EPHY_VIEW_SOURCE_SCHEME "ephy-source"
+#define EPHY_VIEW_SOURCE_SCHEME "view-source"
 
 EphyViewSourceHandler *ephy_view_source_handler_new            (void);
 
diff --git a/embed/ephy-web-view.h b/embed/ephy-web-view.h
index 5e45cb861..62176b70a 100644
--- a/embed/ephy-web-view.h
+++ b/embed/ephy-web-view.h
@@ -44,6 +44,7 @@ G_DECLARE_FINAL_TYPE (EphyWebView, ephy_web_view, EPHY, WEB_VIEW, WebKitWebView)
                                         "^inspector://.*$|" \
                                         "^webkit://.*$|" \
                                         "^ephy-resource://.*$|" \
+                                        "^view-source:.*$|" \
                                         "^ephy-reader:.*$" \
                                         ")"
 
diff --git a/src/ephy-session.c b/src/ephy-session.c
index 764ed1a19..f10617e2d 100644
--- a/src/ephy-session.c
+++ b/src/ephy-session.c
@@ -38,6 +38,7 @@
 #include "ephy-shell.h"
 #include "ephy-string.h"
 #include "ephy-tab-view.h"
+#include "ephy-view-source-handler.h"
 #include "ephy-window.h"
 
 #include <glib/gi18n.h>
@@ -894,7 +895,8 @@ session_seems_reasonable (GList *windows)
             strcmp (g_uri_get_scheme (uri), "data") == 0 ||
             strcmp (g_uri_get_scheme (uri), "file") == 0 ||
             strcmp (g_uri_get_scheme (uri), "ephy-reader") == 0 ||
-            strcmp (g_uri_get_scheme (uri), "ephy-pdf") == 0)
+            strcmp (g_uri_get_scheme (uri), "ephy-pdf") == 0 ||
+            strcmp (g_uri_get_scheme (uri), EPHY_VIEW_SOURCE_SCHEME) == 0)
           sane = TRUE;
       }
 
diff --git a/src/window-commands.c b/src/window-commands.c
index 8a7e52b91..01eb6249b 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -2457,9 +2457,7 @@ window_cmd_page_source (GSimpleAction *action,
   EphyWindow *window = user_data;
   EphyEmbed *embed;
   EphyEmbed *new_embed;
-  g_autoptr (GUri) uri = NULL;
-  g_autoptr (GUri) converted_uri = NULL;
-  char *source_uri;
+  g_autofree char *source_uri = NULL;
   const char *address;
 
   embed = ephy_embed_container_get_active_child
@@ -2472,22 +2470,7 @@ window_cmd_page_source (GSimpleAction *action,
   if (strstr (address, EPHY_VIEW_SOURCE_SCHEME) == address)
     return;
 
-  uri = g_uri_parse (address, G_URI_FLAGS_ENCODED | G_URI_FLAGS_SCHEME_NORMALIZE, NULL);
-  if (!uri) {
-    g_critical ("Failed to construct GUri for %s", address);
-    return;
-  }
-
-  /* Convert e.g. https://gnome.org to ephy-source://gnome.org#https */
-  converted_uri = g_uri_build (g_uri_get_flags (uri),
-                               EPHY_VIEW_SOURCE_SCHEME,
-                               g_uri_get_userinfo (uri),
-                               g_uri_get_host (uri),
-                               g_uri_get_port (uri),
-                               g_uri_get_path (uri),
-                               g_uri_get_query (uri),
-                               g_uri_get_scheme (uri));
-  source_uri = g_uri_to_string (converted_uri);
+  source_uri = g_strdup_printf ("%s:%s", EPHY_VIEW_SOURCE_SCHEME, address);
 
   new_embed = ephy_shell_new_tab
                 (ephy_shell_get_default (),
@@ -2497,8 +2480,6 @@ window_cmd_page_source (GSimpleAction *action,
 
   webkit_web_view_load_uri (EPHY_GET_WEBKIT_WEB_VIEW_FROM_EMBED (new_embed), source_uri);
   gtk_widget_grab_focus (GTK_WIDGET (new_embed));
-
-  g_free (source_uri);
 }
 
 void


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