Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a custom URL scheme in Webkit GTK?

Let's say I want to use a WebKitWebView in GTK to display some static HTML pages. These pages use a custom URL scheme, let's call it custom://. This scheme represents a local file whose location is not known beforehand, at the time the HTML is generated. What I do is connect to the navigation-requested signal of the webview, and do this:

const gchar *uri = webkit_network_request_get_uri(request);
gchar *scheme = g_uri_parse_scheme(uri); 

if(strcmp(scheme, "custom") == 0) {
    /* DO FILE LOCATING MAGIC HERE */
    webkit_web_view_open(webview, real_location_of_file);
    return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
}
/* etc. */

This seems to work fine, unless the scheme is used in an <img> tag, for example: <img src="custom://myfile.png">, apparently these don't go through the navigation-requested signal.

It seems to me there should be some way to register a handler for the custom URL scheme with Webkit. Is this possible?

like image 411
ptomato Avatar asked Jul 13 '10 15:07

ptomato


2 Answers

I'm more familiar with the Chromium port of WebKit, but I believe that you might need to use webkit_web_resource_get_uri (see webkitwebresource.h) to handle resources such as images.

like image 141
Emerick Rogul Avatar answered Nov 17 '22 17:11

Emerick Rogul


In WebKit GTK 2, there is a more official route for this:

WebKitWebContext *context = webkit_web_context_get_default();
webkit_web_context_register_uri_scheme(context, "custom",
    (WebKitURISchemeRequestCallback)handle_custom,
    NULL, NULL);

/* ... */

static void
handle_custom(WebKitURISchemeRequest *request)
{
    /* DO FILE LOCATING MAGIC HERE */
    GFile *file = g_file_new_for_path(real_location_of_file);
    GFileInputStream *stream = g_file_read(file, NULL, NULL);
    g_object_unref(file);

    webkit_uri_scheme_request_finish(request, stream, -1, NULL);
    g_object_unref(stream);
}
like image 38
ptomato Avatar answered Nov 17 '22 17:11

ptomato