[GTK] Expose user script messages to GObject DOM bindings
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKit2Gtk / 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 <JavaScriptCore/JSContextRef.h>
23 #include <JavaScriptCore/JSRetainPtr.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <webkit2/webkit-web-extension.h>
28 #include <wtf/Deque.h>
29 #include <wtf/OwnPtr.h>
30 #include <wtf/PassOwnPtr.h>
31 #include <wtf/ProcessID.h>
32 #include <wtf/gobject/GRefPtr.h>
33 #include <wtf/gobject/GUniquePtr.h>
34 #include <wtf/text/CString.h>
35
36 #define WEBKIT_DOM_USE_UNSTABLE_API
37 #include <webkitdom/WebKitDOMWebKitNamespace.h>
38 #include <webkitdom/WebKitDOMUserMessageHandlersNamespace.h>
39 #include <webkitdom/WebKitDOMUserMessageHandler.h>
40
41 static const char introspectionXML[] =
42     "<node>"
43     " <interface name='org.webkit.gtk.WebExtensionTest'>"
44     "  <method name='GetTitle'>"
45     "   <arg type='t' name='pageID' direction='in'/>"
46     "   <arg type='s' name='title' direction='out'/>"
47     "  </method>"
48     "  <method name='AbortProcess'>"
49     "  </method>"
50     "  <method name='RunJavaScriptInIsolatedWorld'>"
51     "   <arg type='t' name='pageID' direction='in'/>"
52     "   <arg type='s' name='script' direction='in'/>"
53     "  </method>"
54     "  <method name='GetInitializationUserData'>"
55     "   <arg type='s' name='userData' direction='out'/>"
56     "  </method>"
57     "  <method name='GetProcessIdentifier'>"
58     "   <arg type='u' name='identifier' direction='out'/>"
59     "  </method>"
60     "  <signal name='DocumentLoaded'/>"
61     "  <signal name='URIChanged'>"
62     "   <arg type='s' name='uri' direction='out'/>"
63     "  </signal>"
64     " </interface>"
65     "</node>";
66
67 static GRefPtr<GVariant> initializationUserData;
68
69
70 typedef enum {
71     DocumentLoadedSignal,
72     URIChangedSignal,
73 } DelayedSignalType;
74
75 struct DelayedSignal {
76     DelayedSignal(DelayedSignalType type)
77         : type(type)
78     {
79     }
80
81     DelayedSignal(DelayedSignalType type, const char* uri)
82         : type(type)
83         , uri(uri)
84     {
85     }
86
87     DelayedSignalType type;
88     CString uri;
89 };
90
91 Deque<OwnPtr<DelayedSignal>> delayedSignalsQueue;
92
93 static void emitDocumentLoaded(GDBusConnection* connection)
94 {
95     bool ok = g_dbus_connection_emit_signal(
96         connection,
97         0,
98         "/org/webkit/gtk/WebExtensionTest",
99         "org.webkit.gtk.WebExtensionTest",
100         "DocumentLoaded",
101         0,
102         0);
103     g_assert(ok);
104 }
105
106 static void documentLoadedCallback(WebKitWebPage* webPage, WebKitWebExtension* extension)
107 {
108     // FIXME: Too much code just to send a message, we need convenient custom API for this.
109     WebKitDOMDocument* document = webkit_web_page_get_dom_document(webPage);
110     WebKitDOMDOMWindow* window = webkit_dom_document_get_default_view(document);
111     if (WebKitDOMWebKitNamespace* webkit = webkit_dom_dom_window_get_webkit_namespace(window)) {
112         WebKitDOMUserMessageHandlersNamespace* messageHandlers = webkit_dom_webkit_namespace_get_message_handlers(webkit);
113         if (WebKitDOMUserMessageHandler* handler = webkit_dom_user_message_handlers_namespace_get_handler(messageHandlers, "dom"))
114             webkit_dom_user_message_handler_post_message(handler, "DocumentLoaded");
115     }
116
117
118     gpointer data = g_object_get_data(G_OBJECT(extension), "dbus-connection");
119     if (data)
120         emitDocumentLoaded(G_DBUS_CONNECTION(data));
121     else
122         delayedSignalsQueue.append(adoptPtr(new DelayedSignal(DocumentLoadedSignal)));
123 }
124
125 static void emitURIChanged(GDBusConnection* connection, const char* uri)
126 {
127     bool ok = g_dbus_connection_emit_signal(
128         connection,
129         0,
130         "/org/webkit/gtk/WebExtensionTest",
131         "org.webkit.gtk.WebExtensionTest",
132         "URIChanged",
133         g_variant_new("(s)", uri),
134         0);
135     g_assert(ok);
136 }
137
138 static void uriChangedCallback(WebKitWebPage* webPage, GParamSpec* pspec, WebKitWebExtension* extension)
139 {
140     gpointer data = g_object_get_data(G_OBJECT(extension), "dbus-connection");
141     if (data)
142         emitURIChanged(G_DBUS_CONNECTION(data), webkit_web_page_get_uri(webPage));
143     else
144         delayedSignalsQueue.append(adoptPtr(new DelayedSignal(URIChangedSignal, webkit_web_page_get_uri(webPage))));
145 }
146
147 static gboolean sendRequestCallback(WebKitWebPage*, WebKitURIRequest* request, WebKitURIResponse* redirectResponse, gpointer)
148 {
149     const char* requestURI = webkit_uri_request_get_uri(request);
150     g_assert(requestURI);
151
152     if (const char* suffix = g_strrstr(requestURI, "/remove-this/javascript.js")) {
153         GUniquePtr<char> prefix(g_strndup(requestURI, strlen(requestURI) - strlen(suffix)));
154         GUniquePtr<char> newURI(g_strdup_printf("%s/javascript.js", prefix.get()));
155         webkit_uri_request_set_uri(request, newURI.get());
156     } else if (const char* suffix = g_strrstr(requestURI, "/remove-this/javascript-after-redirection.js")) {
157         // Redirected from /redirected.js, redirectResponse should be nullptr.
158         g_assert(WEBKIT_IS_URI_RESPONSE(redirectResponse));
159         g_assert(g_str_has_suffix(webkit_uri_response_get_uri(redirectResponse), "/redirected.js"));
160
161         GUniquePtr<char> prefix(g_strndup(requestURI, strlen(requestURI) - strlen(suffix)));
162         GUniquePtr<char> newURI(g_strdup_printf("%s/javascript-after-redirection.js", prefix.get()));
163         webkit_uri_request_set_uri(request, newURI.get());
164     } else if (g_str_has_suffix(requestURI, "/redirected.js")) {
165         // Original request, redirectResponse should be nullptr.
166         g_assert(!redirectResponse);
167     } else if (g_str_has_suffix(requestURI, "/add-do-not-track-header")) {
168         SoupMessageHeaders* headers = webkit_uri_request_get_http_headers(request);
169         g_assert(headers);
170         soup_message_headers_append(headers, "DNT", "1");
171     } else if (g_str_has_suffix(requestURI, "/cancel-this.js"))
172         return TRUE;
173
174     return FALSE;
175 }
176
177 static GVariant* serializeContextMenu(WebKitContextMenu* menu)
178 {
179     GVariantBuilder builder;
180     g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
181     GList* items = webkit_context_menu_get_items(menu);
182     for (GList* it = items; it; it = g_list_next(it))
183         g_variant_builder_add(&builder, "u", webkit_context_menu_item_get_stock_action(WEBKIT_CONTEXT_MENU_ITEM(it->data)));
184     return g_variant_builder_end(&builder);
185 }
186
187 static GVariant* serializeNode(WebKitDOMNode* node)
188 {
189     GVariantBuilder builder;
190     g_variant_builder_init(&builder, G_VARIANT_TYPE_ARRAY);
191     g_variant_builder_add(&builder, "{sv}", "Name", g_variant_new_take_string(webkit_dom_node_get_node_name(node)));
192     g_variant_builder_add(&builder, "{sv}", "Type", g_variant_new_uint32(webkit_dom_node_get_node_type(node)));
193     g_variant_builder_add(&builder, "{sv}", "Contents", g_variant_new_take_string(webkit_dom_node_get_text_content(node)));
194     WebKitDOMNode* parent = webkit_dom_node_get_parent_node(node);
195     g_variant_builder_add(&builder, "{sv}", "Parent", parent ? g_variant_new_take_string(webkit_dom_node_get_node_name(parent)) : g_variant_new_string("ROOT"));
196     return g_variant_builder_end(&builder);
197 }
198
199 static gboolean contextMenuCallback(WebKitWebPage* page, WebKitContextMenu* menu, WebKitWebHitTestResult* hitTestResult, gpointer)
200 {
201     const char* pageURI = webkit_web_page_get_uri(page);
202     if (!g_strcmp0(pageURI, "ContextMenuTestDefault")) {
203         webkit_context_menu_set_user_data(menu, serializeContextMenu(menu));
204         return FALSE;
205     }
206
207     if (!g_strcmp0(pageURI, "ContextMenuTestCustom")) {
208         // Remove Back and Forward, and add Inspector action.
209         webkit_context_menu_remove(menu, webkit_context_menu_first(menu));
210         webkit_context_menu_remove(menu, webkit_context_menu_first(menu));
211         webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
212         webkit_context_menu_append(menu, webkit_context_menu_item_new_from_stock_action(WEBKIT_CONTEXT_MENU_ACTION_INSPECT_ELEMENT));
213         webkit_context_menu_set_user_data(menu, serializeContextMenu(menu));
214         return TRUE;
215     }
216
217     if (!g_strcmp0(pageURI, "ContextMenuTestClear")) {
218         webkit_context_menu_remove_all(menu);
219         return TRUE;
220     }
221
222     if (!g_strcmp0(pageURI, "ContextMenuTestNode")) {
223         WebKitDOMNode* node = webkit_web_hit_test_result_get_node(hitTestResult);
224         g_assert(WEBKIT_DOM_IS_NODE(node));
225         webkit_context_menu_set_user_data(menu, serializeNode(node));
226         return TRUE;
227     }
228
229     return FALSE;
230 }
231
232 static void pageCreatedCallback(WebKitWebExtension* extension, WebKitWebPage* webPage, gpointer)
233 {
234     g_signal_connect(webPage, "document-loaded", G_CALLBACK(documentLoadedCallback), extension);
235     g_signal_connect(webPage, "notify::uri", G_CALLBACK(uriChangedCallback), extension);
236     g_signal_connect(webPage, "send-request", G_CALLBACK(sendRequestCallback), nullptr);
237     g_signal_connect(webPage, "context-menu", G_CALLBACK(contextMenuCallback), nullptr);
238 }
239
240 static JSValueRef echoCallback(JSContextRef jsContext, JSObjectRef, JSObjectRef, size_t argumentCount, const JSValueRef arguments[], JSValueRef*)
241 {
242     if (argumentCount <= 0)
243         return JSValueMakeUndefined(jsContext);
244
245     JSRetainPtr<JSStringRef> string(Adopt, JSValueToStringCopy(jsContext, arguments[0], 0));
246     return JSValueMakeString(jsContext, string.get());
247 }
248
249 static void windowObjectCleared(WebKitScriptWorld* world, WebKitWebPage* page, WebKitFrame* frame, gpointer)
250 {
251     JSGlobalContextRef jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world);
252     g_assert(jsContext);
253     JSObjectRef globalObject = JSContextGetGlobalObject(jsContext);
254     g_assert(globalObject);
255
256     JSRetainPtr<JSStringRef> functionName(Adopt, JSStringCreateWithUTF8CString("echo"));
257     JSObjectRef function = JSObjectMakeFunctionWithCallback(jsContext, functionName.get(), echoCallback);
258     JSObjectSetProperty(jsContext, globalObject, functionName.get(), function, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
259 }
260
261 static WebKitWebPage* getWebPage(WebKitWebExtension* extension, uint64_t pageID, GDBusMethodInvocation* invocation)
262 {
263     WebKitWebPage* page = webkit_web_extension_get_page(extension, pageID);
264     if (!page) {
265         g_dbus_method_invocation_return_error(
266             invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
267             "Invalid page ID: %" G_GUINT64_FORMAT, pageID);
268         return 0;
269     }
270
271     g_assert_cmpuint(webkit_web_page_get_id(page), ==, pageID);
272     return page;
273 }
274
275 static void methodCallCallback(GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData)
276 {
277     if (g_strcmp0(interfaceName, "org.webkit.gtk.WebExtensionTest"))
278         return;
279
280     if (!g_strcmp0(methodName, "GetTitle")) {
281         uint64_t pageID;
282         g_variant_get(parameters, "(t)", &pageID);
283         WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation);
284         if (!page)
285             return;
286
287         WebKitDOMDocument* document = webkit_web_page_get_dom_document(page);
288         GUniquePtr<char> title(webkit_dom_document_get_title(document));
289         g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)", title.get()));
290     } else if (!g_strcmp0(methodName, "RunJavaScriptInIsolatedWorld")) {
291         uint64_t pageID;
292         const char* script;
293         g_variant_get(parameters, "(t&s)", &pageID, &script);
294         WebKitWebPage* page = getWebPage(WEBKIT_WEB_EXTENSION(userData), pageID, invocation);
295         if (!page)
296             return;
297
298         GRefPtr<WebKitScriptWorld> world = adoptGRef(webkit_script_world_new());
299         g_assert(webkit_script_world_get_default() != world.get());
300         WebKitFrame* frame = webkit_web_page_get_main_frame(page);
301         JSGlobalContextRef jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world.get());
302         JSRetainPtr<JSStringRef> jsScript(Adopt, JSStringCreateWithUTF8CString(script));
303         JSEvaluateScript(jsContext, jsScript.get(), 0, 0, 0, 0);
304         g_dbus_method_invocation_return_value(invocation, 0);
305     } else if (!g_strcmp0(methodName, "AbortProcess")) {
306         abort();
307     } else if (!g_strcmp0(methodName, "GetInitializationUserData")) {
308         g_assert(initializationUserData);
309         g_assert(g_variant_is_of_type(initializationUserData.get(), G_VARIANT_TYPE_STRING));
310         g_dbus_method_invocation_return_value(invocation, g_variant_new("(s)",
311             g_variant_get_string(initializationUserData.get(), nullptr)));
312     } else if (!g_strcmp0(methodName, "GetProcessIdentifier")) {
313         g_dbus_method_invocation_return_value(invocation,
314             g_variant_new("(u)", static_cast<guint32>(getCurrentProcessID())));
315     }
316 }
317
318 static const GDBusInterfaceVTable interfaceVirtualTable = {
319     methodCallCallback, 0, 0, { 0, }
320 };
321
322 static void busAcquiredCallback(GDBusConnection* connection, const char* name, gpointer userData)
323 {
324     static GDBusNodeInfo* introspectionData = 0;
325     if (!introspectionData)
326         introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, 0);
327
328     GUniqueOutPtr<GError> error;
329     unsigned registrationID = g_dbus_connection_register_object(
330         connection,
331         "/org/webkit/gtk/WebExtensionTest",
332         introspectionData->interfaces[0],
333         &interfaceVirtualTable,
334         g_object_ref(userData),
335         static_cast<GDestroyNotify>(g_object_unref),
336         &error.outPtr());
337     if (!registrationID)
338         g_warning("Failed to register object: %s\n", error->message);
339
340     g_object_set_data(G_OBJECT(userData), "dbus-connection", connection);
341     while (delayedSignalsQueue.size()) {
342         OwnPtr<DelayedSignal> delayedSignal = delayedSignalsQueue.takeFirst();
343         switch (delayedSignal->type) {
344         case DocumentLoadedSignal:
345             emitDocumentLoaded(connection);
346             break;
347         case URIChangedSignal:
348             emitURIChanged(connection, delayedSignal->uri.data());
349             break;
350         }
351     }
352 }
353
354 static GUniquePtr<char> makeBusName(GVariant* userData)
355 {
356     // When the web extension is used by TestMultiprocess, an uint32
357     // identifier is passed as user data. It uniquely identifies
358     // the web process, and the UI side expects it added as suffix to
359     // the bus name.
360     if (userData && g_variant_is_of_type(userData, G_VARIANT_TYPE_UINT32))
361         return GUniquePtr<char>(g_strdup_printf("org.webkit.gtk.WebExtensionTest%u", g_variant_get_uint32(userData)));
362
363     return GUniquePtr<char>(g_strdup("org.webkit.gtk.WebExtensionTest"));
364 }
365
366 extern "C" void webkit_web_extension_initialize_with_user_data(WebKitWebExtension* extension, GVariant* userData)
367 {
368     initializationUserData = userData;
369
370     g_signal_connect(extension, "page-created", G_CALLBACK(pageCreatedCallback), extension);
371     g_signal_connect(webkit_script_world_get_default(), "window-object-cleared", G_CALLBACK(windowObjectCleared), 0);
372
373     GUniquePtr<char> busName(makeBusName(userData));
374     g_bus_own_name(
375         G_BUS_TYPE_SESSION,
376         busName.get(),
377         G_BUS_NAME_OWNER_FLAGS_NONE,
378         busAcquiredCallback,
379         0, 0,
380         g_object_ref(extension),
381         static_cast<GDestroyNotify>(g_object_unref));
382 }