1 | /* |
2 | * Copyright (C) 2012, 2019 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 Lesser General Public |
6 | * License as published by the Free Software Foundation; either |
7 | * version 2,1 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 <gtk/gtk.h> |
23 | #include <webkit2/webkit2.h> |
24 | |
25 | static const char introspectionXML[] = |
26 | "<node>" |
27 | " <interface name='org.webkit.gtk.AccessibilityTest'>" |
28 | " <method name='LoadHTML'>" |
29 | " <arg type='s' name='html' direction='in'/>" |
30 | " <arg type='s' name='baseURI' direction='in'/>" |
31 | " </method>" |
32 | " </interface>" |
33 | "</node>" ; |
34 | |
35 | static void loadChangedCallback(WebKitWebView* webView, WebKitLoadEvent loadEvent, GDBusMethodInvocation* invocation) |
36 | { |
37 | if (loadEvent != WEBKIT_LOAD_FINISHED) |
38 | return; |
39 | |
40 | g_signal_handlers_disconnect_by_func(webView, reinterpret_cast<void*>(loadChangedCallback), invocation); |
41 | g_dbus_method_invocation_return_value(invocation, nullptr); |
42 | } |
43 | |
44 | static const GDBusInterfaceVTable interfaceVirtualTable = { |
45 | // methodCall |
46 | [](GDBusConnection* connection, const char* sender, const char* objectPath, const char* interfaceName, const char* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) { |
47 | if (g_strcmp0(interfaceName, "org.webkit.gtk.AccessibilityTest" )) |
48 | return; |
49 | |
50 | auto* webView = WEBKIT_WEB_VIEW(userData); |
51 | |
52 | if (!g_strcmp0(methodName, "LoadHTML" )) { |
53 | const char* html; |
54 | const char* baseURI; |
55 | g_variant_get(parameters, "(&s&s)" , &html, &baseURI); |
56 | g_signal_connect(webView, "load-changed" , G_CALLBACK(loadChangedCallback), invocation); |
57 | webkit_web_view_load_html(webView, html, baseURI && *baseURI ? baseURI : nullptr); |
58 | } |
59 | }, |
60 | nullptr, |
61 | nullptr, |
62 | { 0, } |
63 | }; |
64 | |
65 | int main(int argc, char** argv) |
66 | { |
67 | // Make sure that the ATK bridge is loaded. |
68 | g_setenv("GTK_MODULES" , "atk-bridge" , TRUE); |
69 | |
70 | gtk_init(&argc, &argv); |
71 | |
72 | GtkWidget* webView = webkit_web_view_new(); |
73 | |
74 | GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
75 | g_signal_connect(window, "delete-event" , G_CALLBACK(gtk_main_quit), nullptr); |
76 | gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(webView)); |
77 | gtk_widget_show_all(window); |
78 | |
79 | g_bus_own_name(G_BUS_TYPE_SESSION, "org.webkit.gtk.AccessibilityTest" , G_BUS_NAME_OWNER_FLAGS_NONE, |
80 | [](GDBusConnection* connection, const char* name, gpointer userData) { |
81 | static GDBusNodeInfo *introspectionData = nullptr; |
82 | if (!introspectionData) |
83 | introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, nullptr); |
84 | |
85 | g_dbus_connection_register_object(connection, "/org/webkit/gtk/AccessibilityTest" , introspectionData->interfaces[0], |
86 | &interfaceVirtualTable, userData, nullptr, nullptr); |
87 | }, nullptr, nullptr, webView, nullptr); |
88 | |
89 | gtk_main(); |
90 | } |
91 | |