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 | #include "WebKitTestBus.h" |
22 | |
23 | WebKitTestBus::WebKitTestBus() |
24 | : m_bus(adoptGRef(g_test_dbus_new(G_TEST_DBUS_NONE))) |
25 | { |
26 | } |
27 | |
28 | WebKitTestBus::~WebKitTestBus() |
29 | { |
30 | g_test_dbus_down(m_bus.get()); |
31 | } |
32 | |
33 | bool WebKitTestBus::run() |
34 | { |
35 | CString display = g_getenv("DISPLAY" ); |
36 | CString runtimeDir = g_getenv("XDG_RUNTIME_DIR" ); |
37 | g_test_dbus_up(m_bus.get()); |
38 | m_address = g_test_dbus_get_bus_address(m_bus.get()); |
39 | if (!display.isNull()) |
40 | g_setenv("DISPLAY" , display.data(), FALSE); |
41 | if (!runtimeDir.isNull()) |
42 | g_setenv("XDG_RUNTIME_DIR" , runtimeDir.data(), FALSE); |
43 | return !m_address.isNull(); |
44 | } |
45 | |
46 | GDBusConnection* WebKitTestBus::getOrCreateConnection() |
47 | { |
48 | if (m_connection) |
49 | return m_connection.get(); |
50 | |
51 | g_assert_false(m_address.isNull()); |
52 | m_connection = adoptGRef(g_dbus_connection_new_for_address_sync(m_address.data(), |
53 | static_cast<GDBusConnectionFlags>(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION), |
54 | nullptr, nullptr, nullptr)); |
55 | g_assert_nonnull(m_connection.get()); |
56 | return m_connection.get(); |
57 | } |
58 | |
59 | static void onNameAppeared(GDBusConnection*, const char*, const char*, gpointer userData) |
60 | { |
61 | g_main_loop_quit(static_cast<GMainLoop*>(userData)); |
62 | } |
63 | |
64 | GDBusProxy* WebKitTestBus::createProxy(const char* serviceName, const char* objectPath, const char* interfaceName, GMainLoop* mainLoop) |
65 | { |
66 | unsigned watcherID = g_bus_watch_name_on_connection(getOrCreateConnection(), serviceName, G_BUS_NAME_WATCHER_FLAGS_NONE, onNameAppeared, nullptr, mainLoop, nullptr); |
67 | g_main_loop_run(mainLoop); |
68 | g_bus_unwatch_name(watcherID); |
69 | |
70 | GDBusProxy* proxy = g_dbus_proxy_new_sync( |
71 | connection(), |
72 | G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START, |
73 | nullptr, // GDBusInterfaceInfo |
74 | serviceName, |
75 | objectPath, |
76 | interfaceName, |
77 | nullptr, // GCancellable |
78 | nullptr); |
79 | g_assert_nonnull(proxy); |
80 | return proxy; |
81 | } |
82 | |