summaryrefslogtreecommitdiff
path: root/libyelp/web-extension
diff options
context:
space:
mode:
authorMarcos Chavarría Teijeiro <chavarria1991@gmail.com>2014-11-12 10:28:42 +0100
committerDavid King <amigadave@amigadave.com>2015-06-22 13:32:35 +0100
commit901b4fb82e007e9d93deb3f1e13cc36d5b2bf37b (patch)
tree2007392a342967c526c412dfb5b5a97175883de7 /libyelp/web-extension
parent7a2f5fc0ab4709d82de7748080dfe920407b763d (diff)
downloadyelp-901b4fb82e007e9d93deb3f1e13cc36d5b2bf37b.tar.gz
yelp-view: Implement web extension to load resources
I have split the libyelp library into two parts in order to avoid linking the webextension against libyelp. So now we have a libyelpuri with the yelp-uri, yelp-build-uri and their dependecies and the old libyelp with the remaining files. I have modified the yelp-build-uri logic to not have to use yelp-document because it implies to have to add to the new library most of libyelp classes.
Diffstat (limited to 'libyelp/web-extension')
-rw-r--r--libyelp/web-extension/Makefile.am13
-rw-r--r--libyelp/web-extension/yelp-web-extension.c82
2 files changed, 92 insertions, 3 deletions
diff --git a/libyelp/web-extension/Makefile.am b/libyelp/web-extension/Makefile.am
index 0cafe7af..62774d5e 100644
--- a/libyelp/web-extension/Makefile.am
+++ b/libyelp/web-extension/Makefile.am
@@ -3,6 +3,13 @@ webextension_LTLIBRARIES = libyelpwebextension.la
webextensiondir = $(pkglibdir)/web-extensions
libyelpwebextension_la_SOURCES = yelp-web-extension.c
-libyelpwebextension_la_CFLAGS = $(YELP_EXTENSION_CFLAGS)
-libyelpwebextension_la_LIBADD = $(YELP_EXTENSION_LIBS)
-libyelpwebextension_la_LDFLAGS = -module -avoid-version -no-undefined
+
+libyelpwebextension_la_CFLAGS = \
+ $(YELP_EXTENSION_CFLAGS) \
+ -I$(top_srcdir)/libyelp
+
+libyelpwebextension_la_LIBADD = \
+ $(YELP_EXTENSION_LIBS) \
+ $(top_builddir)/libyelp/libyelpcommon.la
+
+libyelpwebextension_la_LDFLAGS = -module -avoid-version -no-undefined \ No newline at end of file
diff --git a/libyelp/web-extension/yelp-web-extension.c b/libyelp/web-extension/yelp-web-extension.c
index 76ef1158..701cfbc2 100644
--- a/libyelp/web-extension/yelp-web-extension.c
+++ b/libyelp/web-extension/yelp-web-extension.c
@@ -20,10 +20,86 @@
#include <webkit2/webkit-web-extension.h>
#include <string.h>
+#include <stdlib.h>
+#include "yelp-uri.h"
+#include "yelp-uri-builder.h"
#define WEBKIT_DOM_USE_UNSTABLE_API
#include <webkitdom/WebKitDOMElementUnstable.h>
+static YelpUri *current_uri;
+
+static gchar *
+get_resource_path (gchar *uri, YelpUri *document_uri)
+{
+ gchar *resource = NULL;
+ gchar *resource_path = 0;
+
+ if (!g_str_has_prefix (uri, "ghelp") &&
+ !g_str_has_prefix (uri, "gnome-help") &&
+ !g_str_has_prefix (uri, "help")) {
+ return NULL;
+ }
+
+ resource = strstr (uri, "/");
+ if (resource) {
+ resource[0] = '\0';
+ resource++;
+ }
+
+ if (resource && resource[0] != '\0')
+ resource_path = yelp_uri_locate_file_uri (document_uri, resource);
+
+ return resource_path;
+}
+
+static gboolean
+web_page_send_request (WebKitWebPage *web_page,
+ WebKitURIRequest *request,
+ WebKitURIResponse *redirected_response,
+ gpointer user_data)
+{
+ const gchar *wk_uri = webkit_uri_request_get_uri (request);
+ gchar *yelp_uri, *current_uri_canonical, *file_path;
+
+ if (!current_uri)
+ return FALSE;
+
+ yelp_uri = build_yelp_uri (wk_uri);
+ current_uri_canonical = yelp_uri_get_canonical_uri (current_uri);
+
+ file_path = get_resource_path (yelp_uri, current_uri);
+
+ if (file_path) {
+ webkit_uri_request_set_uri (request, file_path);
+ g_free (file_path);
+ }
+
+ g_free (yelp_uri);
+ g_free (current_uri_canonical);
+ return FALSE;
+}
+
+static void
+web_page_notify_uri (WebKitWebPage *web_page,
+ GParamSpec *pspec,
+ gpointer data)
+{
+ const gchar *uri = webkit_web_page_get_uri (web_page);
+ gchar *yelp_uri;
+
+ yelp_uri = build_yelp_uri (uri);
+
+ if (current_uri)
+ g_object_unref (current_uri);
+ current_uri = yelp_uri_new (yelp_uri);
+
+ if (!yelp_uri_is_resolved (current_uri))
+ yelp_uri_resolve_sync (current_uri);
+
+ g_free (yelp_uri);
+}
+
static gboolean
web_page_context_menu (WebKitWebPage *web_page,
WebKitContextMenu *context_menu,
@@ -133,6 +209,12 @@ web_page_created_callback (WebKitWebExtension *extension,
g_signal_connect (web_page, "context-menu",
G_CALLBACK (web_page_context_menu),
NULL);
+ g_signal_connect (web_page, "send-request",
+ G_CALLBACK (web_page_send_request),
+ NULL);
+ g_signal_connect (web_page, "notify::uri",
+ G_CALLBACK (web_page_notify_uri),
+ NULL);
}
G_MODULE_EXPORT void