1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* Copyright (C) 2012 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <webkit2/webkit-web-extension.h>
#include <wtf/gobject/GOwnPtr.h>
static const char introspectionXML[] =
"<node>"
" <interface name='org.webkit.gtk.WebExtensionTest'>"
" <method name='GetTitle'>"
" <arg type='t' name='pageID' direction='in'/>"
" <arg type='s' name='title' direction='out'/>"
" </method>"
" <method name='AbortProcess'>"
" </method>"
" <signal name='DocumentLoaded'/>"
" <signal name='URIChanged'>"
" <arg type='s' name='uri' direction='out'/>"
" </signal>"
" </interface>"
"</node>";
static void documentLoadedCallback(WebKitWebPage*, gpointer userData)
{
bool ok = g_dbus_connection_emit_signal(G_DBUS_CONNECTION(userData),
0,
"/org/webkit/gtk/WebExtensionTest",
"org.webkit.gtk.WebExtensionTest",
"DocumentLoaded",
0,
0);
g_assert(ok);
}
static void uriChangedCallback(WebKitWebPage* webPage, GParamSpec* pspec, gpointer userData)
{
bool ok = g_dbus_connection_emit_signal(
G_DBUS_CONNECTION(userData),
0,
"/org/webkit/gtk/WebExtensionTest",
"org.webkit.gtk.WebExtensionTest",
"URIChanged",
g_variant_new("(s)", webkit_web_page_get_uri(webPage)),
0);
g_assert(ok);
}
static gboolean sendRequestCallback(WebKitWebPage*, WebKitURIRequest* request, WebKitURIResponse*, gpointer)
{
const char* requestURI = webkit_uri_request_get_uri(request);
g_assert(requestURI);
if (const char* suffix = g_strrstr(requestURI, "/remove-this/javascript.js")) {
GOwnPtr<char> prefix(g_strndup(requestURI, strlen(requestURI) - strlen(suffix)));
GOwnPtr<char> newURI(g_strdup_printf("%s/javascript.js", prefix.get()));
webkit_uri_request_set_uri(request, newURI.get());
} else if (g_str_has_suffix(requestURI, "/add-do-not-track-header")) {
SoupMessageHeaders* headers = webkit_uri_request_get_http_headers(request);
g_assert(headers);
soup_message_headers_append(headers, "DNT", "1");
} else if (g_str_has_suffix(requestURI, "/cancel-this.js"))
return TRUE;
return FALSE;
}
static void pageCreatedCallback(WebKitWebExtension*, WebKitWebPage* webPage, gpointer userData)
{
g_signal_connect(webPage, "document-loaded", G_CALLBACK(documentLoadedCallback), userData);
g_signal_connect(webPage, "notify::uri", G_CALLBACK(uriChangedCallback), userData);
g_signal_connect(webPage, "send-request", G_CALLBACK(sendRequestCallback), 0);
}
static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData)
{
if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest"))
return;
if (!g_strcmp0(methodName, "GetTitle")) {
uint64_t pageID;
g_variant_get(parameters, "(t)", &pageID);
WebKitWebExtension* extension = WEBKIT_WEB_EXTENSION(userData);
WebKitWebPage* page = webkit_web_extension_get_page(extension, pageID);
if (!page) {
g_dbus_method_invocation_return_error(
invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"Invalid page ID: %" G_GUINT64_FORMAT, pageID);
return;
}
g_assert_cmpuint(webkit_web_page_get_id(page), ==, pageID);
WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
GOwnPtr<char> title(webkit_dom_document_get_title(document));
g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", title.get()));
} else if (!g_strcmp0(methodName, "AbortProcess")) {
abort();
}
}
static const GDBusInterfaceVTable interfaceVirtualTable = {
methodCallCallback, 0, 0, { 0, }
};
static void busAcquiredCallback(GDBusConnection* connection, const char* name, gpointer userData)
{
static GDBusNodeInfo *introspectionData = 0;
if (!introspectionData)
introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, 0);
GOwnPtr<GError> error;
unsigned registrationID = g_dbus_connection_register_object(
connection,
"/org/webkit/gtk/WebExtensionTest",
introspectionData->interfaces[0],
&interfaceVirtualTable,
g_object_ref(userData),
static_cast<GDestroyNotify>(g_object_unref),
&error.outPtr());
if (!registrationID)
g_warning("Failed to register object: %s\n", error->message);
g_signal_connect(WEBKIT_WEB_EXTENSION(userData), "page-created", G_CALLBACK(pageCreatedCallback), connection);
}
extern "C" void webkit_web_extension_initialize(WebKitWebExtension* extension)
{
g_bus_own_name(
G_BUS_TYPE_SESSION,
"org.webkit.gtk.WebExtensionTest",
G_BUS_NAME_OWNER_FLAGS_NONE,
busAcquiredCallback,
0, 0,
g_object_ref(extension),
static_cast<GDestroyNotify>(g_object_unref));
}
|