2 * Copyright (C) 2012 Igalia S.L.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include <webkit2/webkit-web-extension.h>
27 #include <wtf/gobject/GOwnPtr.h>
29 static const char introspectionXML[] =
31 " <interface name='org.webkit.gtk.WebExtensionTest'>"
32 " <method name='GetTitle'>"
33 " <arg type='t' name='pageID' direction='in'/>"
34 " <arg type='s' name='title' direction='out'/>"
36 " <method name='AbortProcess'>"
38 " <signal name='DocumentLoaded'/>"
39 " <signal name='URIChanged'>"
40 " <arg type='s' name='uri' direction='out'/>"
45 static void documentLoadedCallback(WebKitWebPage*, gpointer userData)
47 bool ok = g_dbus_connection_emit_signal(G_DBUS_CONNECTION(userData),
49 "/org/webkit/gtk/WebExtensionTest",
50 "org.webkit.gtk.WebExtensionTest",
57 static void uriChangedCallback(WebKitWebPage* webPage, GParamSpec* pspec, gpointer userData)
59 bool ok = g_dbus_connection_emit_signal(
60 G_DBUS_CONNECTION(userData),
62 "/org/webkit/gtk/WebExtensionTest",
63 "org.webkit.gtk.WebExtensionTest",
65 g_variant_new("(s)", webkit_web_page_get_uri(webPage)),
70 static gboolean sendRequestCallback(WebKitWebPage*, WebKitURIRequest* request, WebKitURIResponse*, gpointer)
72 const char* requestURI = webkit_uri_request_get_uri(request);
75 if (const char* suffix = g_strrstr(requestURI, "/remove-this/javascript.js")) {
76 GOwnPtr<char> prefix(g_strndup(requestURI, strlen(requestURI) - strlen(suffix)));
77 GOwnPtr<char> newURI(g_strdup_printf("%s/javascript.js", prefix.get()));
78 webkit_uri_request_set_uri(request, newURI.get());
79 } else if (g_str_has_suffix(requestURI, "/add-do-not-track-header")) {
80 SoupMessageHeaders* headers = webkit_uri_request_get_http_headers(request);
82 soup_message_headers_append(headers, "DNT", "1");
83 } else if (g_str_has_suffix(requestURI, "/cancel-this.js"))
89 static void pageCreatedCallback(WebKitWebExtension*, WebKitWebPage* webPage, gpointer userData)
91 g_signal_connect(webPage, "document-loaded", G_CALLBACK(documentLoadedCallback), userData);
92 g_signal_connect(webPage, "notify::uri", G_CALLBACK(uriChangedCallback), userData);
93 g_signal_connect(webPage, "send-request", G_CALLBACK(sendRequestCallback), 0);
96 static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData)
98 if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest"))
101 if (!g_strcmp0(methodName, "GetTitle")) {
103 g_variant_get(parameters, "(t)", &pageID);
105 WebKitWebExtension* extension = WEBKIT_WEB_EXTENSION(userData);
106 WebKitWebPage* page = webkit_web_extension_get_page(extension, pageID);
108 g_dbus_method_invocation_return_error(
109 invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
110 "Invalid page ID: %"G_GUINT64_FORMAT, pageID);
113 g_assert_cmpuint(webkit_web_page_get_id(page), ==, pageID);
115 WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
116 GOwnPtr<char> title(webkit_dom_document_get_title(document));
117 g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", title.get()));
118 } else if (!g_strcmp0(methodName, "AbortProcess")) {
123 static const GDBusInterfaceVTable interfaceVirtualTable = {
124 methodCallCallback, 0, 0
127 static void busAcquiredCallback(GDBusConnection* connection, const char* name, gpointer userData)
129 static GDBusNodeInfo *introspectionData = 0;
130 if (!introspectionData)
131 introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, 0);
133 GOwnPtr<GError> error;
134 unsigned registrationID = g_dbus_connection_register_object(
136 "/org/webkit/gtk/WebExtensionTest",
137 introspectionData->interfaces[0],
138 &interfaceVirtualTable,
139 g_object_ref(userData),
140 static_cast<GDestroyNotify>(g_object_unref),
143 g_warning("Failed to register object: %s\n", error->message);
145 g_signal_connect(WEBKIT_WEB_EXTENSION(userData), "page-created", G_CALLBACK(pageCreatedCallback), connection);
148 extern "C" void webkit_web_extension_initialize(WebKitWebExtension* extension)
152 "org.webkit.gtk.WebExtensionTest",
153 G_BUS_NAME_OWNER_FLAGS_NONE,
156 g_object_ref(extension),
157 static_cast<GDestroyNotify>(g_object_unref));