summaryrefslogtreecommitdiff
path: root/Tools/TestWebKitAPI/gtk/WebKit2Gtk/WebKitTestBus.cpp
blob: 88456ce4f4174ee6a0122c72c916ec9328ec1def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2012 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitTestBus.h"

WebKitTestBus::WebKitTestBus()
    : m_bus(adoptGRef(g_test_dbus_new(G_TEST_DBUS_NONE)))
{
}

WebKitTestBus::~WebKitTestBus()
{
    g_test_dbus_down(m_bus.get());
}

bool WebKitTestBus::run()
{
    CString display = g_getenv("DISPLAY");
    g_test_dbus_up(m_bus.get());
    m_address = g_test_dbus_get_bus_address(m_bus.get());
    g_setenv("DISPLAY", display.data(), FALSE);
    return !m_address.isNull();
}

GDBusConnection* WebKitTestBus::getOrCreateConnection()
{
    if (m_connection)
        return m_connection.get();

    g_assert(!m_address.isNull());
    m_connection = adoptGRef(g_dbus_connection_new_for_address_sync(m_address.data(),
        static_cast<GDBusConnectionFlags>(G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
        nullptr, nullptr, nullptr));
    g_assert(m_connection.get());
    return m_connection.get();
}

static void onNameAppeared(GDBusConnection*, const char*, const char*, gpointer userData)
{
    g_main_loop_quit(static_cast<GMainLoop*>(userData));
}

GDBusProxy* WebKitTestBus::createProxy(const char* serviceName, const char* objectPath, const char* interfaceName, GMainLoop* mainLoop)
{
    unsigned watcherID = g_bus_watch_name_on_connection(getOrCreateConnection(), serviceName, G_BUS_NAME_WATCHER_FLAGS_NONE, onNameAppeared, nullptr, mainLoop, nullptr);
    g_main_loop_run(mainLoop);
    g_bus_unwatch_name(watcherID);

    GDBusProxy* proxy = g_dbus_proxy_new_sync(
        connection(),
        G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
        nullptr, // GDBusInterfaceInfo
        serviceName,
        objectPath,
        interfaceName,
        nullptr, // GCancellable
        nullptr);
    g_assert(proxy);
    return proxy;
}