[GTK] Fix concatenations of string literals that are not C++11-compliant
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / gtk / tests / WebExtensionTest.cpp
1 /*
2  * Copyright (C) 2012 Igalia S.L.
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <gio/gio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <webkit2/webkit-web-extension.h>
27 #include <wtf/gobject/GOwnPtr.h>
28
29 static const char introspectionXML[] =
30     "<node>"
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'/>"
35     "  </method>"
36     "  <method name='AbortProcess'>"
37     "  </method>"
38     "  <signal name='DocumentLoaded'/>"
39     "  <signal name='URIChanged'>"
40     "   <arg type='s' name='uri' direction='out'/>"
41     "  </signal>"
42     " </interface>"
43     "</node>";
44
45 static void documentLoadedCallback(WebKitWebPage*, gpointer userData)
46 {
47     bool ok = g_dbus_connection_emit_signal(G_DBUS_CONNECTION(userData),
48         0,
49         "/org/webkit/gtk/WebExtensionTest",
50         "org.webkit.gtk.WebExtensionTest",
51         "DocumentLoaded",
52         0,
53         0);
54     g_assert(ok);
55 }
56
57 static void uriChangedCallback(WebKitWebPage* webPage, GParamSpec* pspec, gpointer userData)
58 {
59     bool ok = g_dbus_connection_emit_signal(
60         G_DBUS_CONNECTION(userData),
61         0,
62         "/org/webkit/gtk/WebExtensionTest",
63         "org.webkit.gtk.WebExtensionTest",
64         "URIChanged",
65         g_variant_new("(s)", webkit_web_page_get_uri(webPage)),
66         0);
67     g_assert(ok);
68 }
69
70 static gboolean sendRequestCallback(WebKitWebPage*, WebKitURIRequest* request, WebKitURIResponse*, gpointer)
71 {
72     const char* requestURI = webkit_uri_request_get_uri(request);
73     g_assert(requestURI);
74
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);
81         g_assert(headers);
82         soup_message_headers_append(headers, "DNT", "1");
83     } else if (g_str_has_suffix(requestURI, "/cancel-this.js"))
84         return TRUE;
85
86     return FALSE;
87 }
88
89 static void pageCreatedCallback(WebKitWebExtension*, WebKitWebPage* webPage, gpointer userData)
90 {
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);
94 }
95
96 static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData)
97 {
98     if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest"))
99         return;
100
101     if (!g_strcmp0(methodName, "GetTitle")) {
102         uint64_t pageID;
103         g_variant_get(parameters, "(t)", &pageID);
104
105         WebKitWebExtension* extension = WEBKIT_WEB_EXTENSION(userData);
106         WebKitWebPage* page = webkit_web_extension_get_page(extension, pageID);
107         if (!page) {
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);
111             return;
112         }
113         g_assert_cmpuint(webkit_web_page_get_id(page), ==, pageID);
114
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")) {
119         abort();
120     }
121 }
122
123 static const GDBusInterfaceVTable interfaceVirtualTable = {
124     methodCallCallback, 0, 0
125 };
126
127 static void busAcquiredCallback(GDBusConnection* connection, const char* name, gpointer userData)
128 {
129     static GDBusNodeInfo *introspectionData = 0;
130     if (!introspectionData)
131         introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, 0);
132
133     GOwnPtr<GError> error;
134     unsigned registrationID = g_dbus_connection_register_object(
135         connection,
136         "/org/webkit/gtk/WebExtensionTest",
137         introspectionData->interfaces[0],
138         &interfaceVirtualTable,
139         g_object_ref(userData),
140         static_cast<GDestroyNotify>(g_object_unref),
141         &error.outPtr());
142     if (!registrationID)
143         g_warning("Failed to register object: %s\n", error->message);
144
145     g_signal_connect(WEBKIT_WEB_EXTENSION(userData), "page-created", G_CALLBACK(pageCreatedCallback), connection);
146 }
147
148 extern "C" void webkit_web_extension_initialize(WebKitWebExtension* extension)
149 {
150     g_bus_own_name(
151         G_BUS_TYPE_SESSION,
152         "org.webkit.gtk.WebExtensionTest",
153         G_BUS_NAME_OWNER_FLAGS_NONE,
154         busAcquiredCallback,
155         0, 0,
156         g_object_ref(extension),
157         static_cast<GDestroyNotify>(g_object_unref));
158 }