~mmach/netext73/webkit2gtk

« back to all changes in this revision

Viewing changes to Source/WebKit/UIProcess/Inspector/glib/RemoteInspectorClient.cpp

  • Committer: mmach
  • Date: 2023-06-16 17:21:37 UTC
  • Revision ID: netbit73@gmail.com-20230616172137-2rqx6yr96ga9g3kp
1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2017 Igalia S.L.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 
14
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 
17
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
18
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
19
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
20
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
21
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
22
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
23
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "RemoteInspectorClient.h"
 
28
 
 
29
#if ENABLE(REMOTE_INSPECTOR)
 
30
 
 
31
#include "APIDebuggableInfo.h"
 
32
#include "RemoteWebInspectorProxy.h"
 
33
#include <JavaScriptCore/RemoteInspectorUtils.h>
 
34
#include <WebCore/InspectorDebuggableType.h>
 
35
#include <gio/gio.h>
 
36
#include <wtf/NeverDestroyed.h>
 
37
#include <wtf/glib/GUniquePtr.h>
 
38
#include <wtf/text/Base64.h>
 
39
 
 
40
namespace WebKit {
 
41
 
 
42
class RemoteInspectorProxy final : public RemoteWebInspectorProxyClient {
 
43
    WTF_MAKE_FAST_ALLOCATED();
 
44
public:
 
45
    RemoteInspectorProxy(RemoteInspectorClient& inspectorClient, uint64_t connectionID, uint64_t targetID)
 
46
        : m_proxy(RemoteWebInspectorProxy::create())
 
47
        , m_inspectorClient(inspectorClient)
 
48
        , m_connectionID(connectionID)
 
49
        , m_targetID(targetID)
 
50
    {
 
51
        m_proxy->setClient(this);
 
52
    }
 
53
 
 
54
    ~RemoteInspectorProxy()
 
55
    {
 
56
        m_proxy->setClient(nullptr);
 
57
        m_proxy->invalidate();
 
58
    }
 
59
 
 
60
    void load(Inspector::DebuggableType debuggableType)
 
61
    {
 
62
        // FIXME <https://webkit.org/b/205536>: this should infer more useful data about the debug target.
 
63
        Ref<API::DebuggableInfo> debuggableInfo = API::DebuggableInfo::create(DebuggableInfoData::empty());
 
64
        debuggableInfo->setDebuggableType(debuggableType);
 
65
        m_proxy->load(WTFMove(debuggableInfo), m_inspectorClient.backendCommandsURL());
 
66
    }
 
67
 
 
68
    void show()
 
69
    {
 
70
        m_proxy->show();
 
71
    }
 
72
 
 
73
    void setTargetName(const CString& name)
 
74
    {
 
75
#if PLATFORM(GTK)
 
76
        m_proxy->updateWindowTitle(name);
 
77
#endif
 
78
    }
 
79
 
 
80
    void sendMessageToFrontend(const String& message)
 
81
    {
 
82
        m_proxy->sendMessageToFrontend(message);
 
83
    }
 
84
 
 
85
    void sendMessageToBackend(const String& message) override
 
86
    {
 
87
        m_inspectorClient.sendMessageToBackend(m_connectionID, m_targetID, message);
 
88
    }
 
89
 
 
90
    void closeFromFrontend() override
 
91
    {
 
92
        m_inspectorClient.closeFromFrontend(m_connectionID, m_targetID);
 
93
    }
 
94
 
 
95
private:
 
96
    Ref<RemoteWebInspectorProxy> m_proxy;
 
97
    RemoteInspectorClient& m_inspectorClient;
 
98
    uint64_t m_connectionID;
 
99
    uint64_t m_targetID;
 
100
};
 
101
 
 
102
const SocketConnection::MessageHandlers& RemoteInspectorClient::messageHandlers()
 
103
{
 
104
    static NeverDestroyed<const SocketConnection::MessageHandlers> messageHandlers = SocketConnection::MessageHandlers({
 
105
    { "DidClose", std::pair<CString, SocketConnection::MessageCallback> { { },
 
106
        [](SocketConnection&, GVariant*, gpointer userData) {
 
107
            auto& client = *static_cast<RemoteInspectorClient*>(userData);
 
108
            client.connectionDidClose();
 
109
        }}
 
110
    },
 
111
    { "DidSetupInspectorClient", std::pair<CString, SocketConnection::MessageCallback> { "(ay)",
 
112
        [](SocketConnection&, GVariant* parameters, gpointer userData) {
 
113
            auto& client = *static_cast<RemoteInspectorClient*>(userData);
 
114
            GRefPtr<GVariant> backendCommandsVariant;
 
115
            g_variant_get(parameters, "(@ay)", &backendCommandsVariant.outPtr());
 
116
            client.setBackendCommands(g_variant_get_bytestring(backendCommandsVariant.get()));
 
117
        }}
 
118
    },
 
119
    { "SetTargetList", std::pair<CString, SocketConnection::MessageCallback> { "(ta(tsssb))",
 
120
        [](SocketConnection&, GVariant* parameters, gpointer userData) {
 
121
            auto& client = *static_cast<RemoteInspectorClient*>(userData);
 
122
            guint64 connectionID;
 
123
            GUniqueOutPtr<GVariantIter> iter;
 
124
            g_variant_get(parameters, "(ta(tsssb))", &connectionID, &iter.outPtr());
 
125
            size_t targetCount = g_variant_iter_n_children(iter.get());
 
126
            Vector<RemoteInspectorClient::Target> targetList;
 
127
            targetList.reserveInitialCapacity(targetCount);
 
128
            guint64 targetID;
 
129
            const char* type;
 
130
            const char* name;
 
131
            const char* url;
 
132
            gboolean hasLocalDebugger;
 
133
            while (g_variant_iter_loop(iter.get(), "(t&s&s&sb)", &targetID, &type, &name, &url, &hasLocalDebugger)) {
 
134
                if (!g_strcmp0(type, "JavaScript") || !g_strcmp0(type, "ServiceWorker") || !g_strcmp0(type, "WebPage"))
 
135
                    targetList.uncheckedAppend({ targetID, type, name, url });
 
136
            }
 
137
            client.setTargetList(connectionID, WTFMove(targetList));
 
138
        }}
 
139
    },
 
140
    { "SendMessageToFrontend", std::pair<CString, SocketConnection::MessageCallback> { "(tts)",
 
141
        [](SocketConnection&, GVariant* parameters, gpointer userData) {
 
142
            auto& client = *static_cast<RemoteInspectorClient*>(userData);
 
143
            guint64 connectionID, targetID;
 
144
            const char* message;
 
145
            g_variant_get(parameters, "(tt&s)", &connectionID, &targetID, &message);
 
146
            client.sendMessageToFrontend(connectionID, targetID, message);
 
147
        }}
 
148
    }
 
149
    });
 
150
    return messageHandlers;
 
151
}
 
152
 
 
153
RemoteInspectorClient::RemoteInspectorClient(const char* address, unsigned port, RemoteInspectorObserver& observer)
 
154
    : m_hostAndPort(String::fromUTF8(address) + ':' + String::number(port))
 
155
    , m_observer(observer)
 
156
    , m_cancellable(adoptGRef(g_cancellable_new()))
 
157
{
 
158
    GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new());
 
159
    g_socket_client_connect_to_host_async(socketClient.get(), m_hostAndPort.utf8().data(), 0, m_cancellable.get(),
 
160
        [](GObject* object, GAsyncResult* result, gpointer userData) {
 
161
            GUniqueOutPtr<GError> error;
 
162
            GRefPtr<GSocketConnection> connection = adoptGRef(g_socket_client_connect_to_host_finish(G_SOCKET_CLIENT(object), result, &error.outPtr()));
 
163
            if (g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED))
 
164
                return;
 
165
            auto* client = static_cast<RemoteInspectorClient*>(userData);
 
166
            if (connection)
 
167
                client->setupConnection(SocketConnection::create(WTFMove(connection), messageHandlers(), client));
 
168
            else {
 
169
                WTFLogAlways("RemoteInspectorClient failed to connect to inspector server: %s", error->message);
 
170
                client->m_observer.connectionClosed(*client);
 
171
            }
 
172
    }, this);
 
173
}
 
174
 
 
175
RemoteInspectorClient::~RemoteInspectorClient()
 
176
{
 
177
    g_cancellable_cancel(m_cancellable.get());
 
178
}
 
179
 
 
180
void RemoteInspectorClient::setupConnection(Ref<SocketConnection>&& connection)
 
181
{
 
182
    m_socketConnection = WTFMove(connection);
 
183
    m_socketConnection->sendMessage("SetupInspectorClient", g_variant_new("(@ay)", g_variant_new_bytestring(Inspector::backendCommandsHash().data())));
 
184
}
 
185
 
 
186
void RemoteInspectorClient::setBackendCommands(const char* backendCommands)
 
187
{
 
188
    if (!backendCommands || !backendCommands[0]) {
 
189
        m_backendCommandsURL = String();
 
190
        return;
 
191
    }
 
192
 
 
193
    Vector<char> base64Data;
 
194
    base64Encode(backendCommands, strlen(backendCommands), base64Data);
 
195
    m_backendCommandsURL = "data:text/javascript;base64," + base64Data;
 
196
}
 
197
 
 
198
void RemoteInspectorClient::connectionDidClose()
 
199
{
 
200
    m_targets.clear();
 
201
    m_inspectorProxyMap.clear();
 
202
    m_socketConnection = nullptr;
 
203
    m_observer.connectionClosed(*this);
 
204
}
 
205
 
 
206
static Inspector::DebuggableType debuggableType(const String& targetType)
 
207
{
 
208
    if (targetType == "JavaScript")
 
209
        return Inspector::DebuggableType::JavaScript;
 
210
    if (targetType == "ServiceWorker")
 
211
        return Inspector::DebuggableType::ServiceWorker;
 
212
    if (targetType == "WebPage")
 
213
        return Inspector::DebuggableType::WebPage;
 
214
    RELEASE_ASSERT_NOT_REACHED();
 
215
}
 
216
 
 
217
void RemoteInspectorClient::inspect(uint64_t connectionID, uint64_t targetID, const String& targetType)
 
218
{
 
219
    auto addResult = m_inspectorProxyMap.ensure(std::make_pair(connectionID, targetID), [this, connectionID, targetID] {
 
220
        return makeUnique<RemoteInspectorProxy>(*this, connectionID, targetID);
 
221
    });
 
222
    if (!addResult.isNewEntry) {
 
223
        addResult.iterator->value->show();
 
224
        return;
 
225
    }
 
226
 
 
227
    m_socketConnection->sendMessage("Setup", g_variant_new("(tt)", connectionID, targetID));
 
228
    addResult.iterator->value->load(debuggableType(targetType));
 
229
}
 
230
 
 
231
void RemoteInspectorClient::sendMessageToBackend(uint64_t connectionID, uint64_t targetID, const String& message)
 
232
{
 
233
    m_socketConnection->sendMessage("SendMessageToBackend", g_variant_new("(tts)", connectionID, targetID, message.utf8().data()));
 
234
}
 
235
 
 
236
void RemoteInspectorClient::closeFromFrontend(uint64_t connectionID, uint64_t targetID)
 
237
{
 
238
    ASSERT(m_inspectorProxyMap.contains(std::make_pair(connectionID, targetID)));
 
239
    m_socketConnection->sendMessage("FrontendDidClose", g_variant_new("(tt)", connectionID, targetID));
 
240
    m_inspectorProxyMap.remove(std::make_pair(connectionID, targetID));
 
241
}
 
242
 
 
243
void RemoteInspectorClient::setTargetList(uint64_t connectionID, Vector<Target>&& targetList)
 
244
{
 
245
    // Find closed targets to remove them.
 
246
    Vector<uint64_t, 4> targetsToRemove;
 
247
    for (auto& connectionTargetPair : m_inspectorProxyMap.keys()) {
 
248
        if (connectionTargetPair.first != connectionID)
 
249
            continue;
 
250
        bool found = false;
 
251
        for (const auto& target : targetList) {
 
252
            if (target.id == connectionTargetPair.second) {
 
253
                m_inspectorProxyMap.get(connectionTargetPair)->setTargetName(target.name);
 
254
                found = true;
 
255
                break;
 
256
            }
 
257
        }
 
258
        if (!found)
 
259
            targetsToRemove.append(connectionTargetPair.second);
 
260
    }
 
261
    for (auto targetID : targetsToRemove)
 
262
        m_inspectorProxyMap.remove(std::make_pair(connectionID, targetID));
 
263
 
 
264
    m_targets.set(connectionID, WTFMove(targetList));
 
265
    m_observer.targetListChanged(*this);
 
266
}
 
267
 
 
268
void RemoteInspectorClient::sendMessageToFrontend(uint64_t connectionID, uint64_t targetID, const char* message)
 
269
{
 
270
    auto proxy = m_inspectorProxyMap.get(std::make_pair(connectionID, targetID));
 
271
    ASSERT(proxy);
 
272
    if (!proxy)
 
273
        return;
 
274
    proxy->sendMessageToFrontend(String::fromUTF8(message));
 
275
}
 
276
 
 
277
} // namespace WebKit
 
278
 
 
279
#endif // ENABLE(REMOTE_INSPECTOR)