2
* Copyright (C) 2017 Igalia S.L.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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.
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.
27
#include "RemoteInspectorClient.h"
29
#if ENABLE(REMOTE_INSPECTOR)
31
#include "APIDebuggableInfo.h"
32
#include "RemoteWebInspectorProxy.h"
33
#include <JavaScriptCore/RemoteInspectorUtils.h>
34
#include <WebCore/InspectorDebuggableType.h>
36
#include <wtf/NeverDestroyed.h>
37
#include <wtf/glib/GUniquePtr.h>
38
#include <wtf/text/Base64.h>
42
class RemoteInspectorProxy final : public RemoteWebInspectorProxyClient {
43
WTF_MAKE_FAST_ALLOCATED();
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)
51
m_proxy->setClient(this);
54
~RemoteInspectorProxy()
56
m_proxy->setClient(nullptr);
57
m_proxy->invalidate();
60
void load(Inspector::DebuggableType debuggableType)
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());
73
void setTargetName(const CString& name)
76
m_proxy->updateWindowTitle(name);
80
void sendMessageToFrontend(const String& message)
82
m_proxy->sendMessageToFrontend(message);
85
void sendMessageToBackend(const String& message) override
87
m_inspectorClient.sendMessageToBackend(m_connectionID, m_targetID, message);
90
void closeFromFrontend() override
92
m_inspectorClient.closeFromFrontend(m_connectionID, m_targetID);
96
Ref<RemoteWebInspectorProxy> m_proxy;
97
RemoteInspectorClient& m_inspectorClient;
98
uint64_t m_connectionID;
102
const SocketConnection::MessageHandlers& RemoteInspectorClient::messageHandlers()
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();
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()));
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);
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 });
137
client.setTargetList(connectionID, WTFMove(targetList));
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;
145
g_variant_get(parameters, "(tt&s)", &connectionID, &targetID, &message);
146
client.sendMessageToFrontend(connectionID, targetID, message);
150
return messageHandlers;
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()))
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))
165
auto* client = static_cast<RemoteInspectorClient*>(userData);
167
client->setupConnection(SocketConnection::create(WTFMove(connection), messageHandlers(), client));
169
WTFLogAlways("RemoteInspectorClient failed to connect to inspector server: %s", error->message);
170
client->m_observer.connectionClosed(*client);
175
RemoteInspectorClient::~RemoteInspectorClient()
177
g_cancellable_cancel(m_cancellable.get());
180
void RemoteInspectorClient::setupConnection(Ref<SocketConnection>&& connection)
182
m_socketConnection = WTFMove(connection);
183
m_socketConnection->sendMessage("SetupInspectorClient", g_variant_new("(@ay)", g_variant_new_bytestring(Inspector::backendCommandsHash().data())));
186
void RemoteInspectorClient::setBackendCommands(const char* backendCommands)
188
if (!backendCommands || !backendCommands[0]) {
189
m_backendCommandsURL = String();
193
Vector<char> base64Data;
194
base64Encode(backendCommands, strlen(backendCommands), base64Data);
195
m_backendCommandsURL = "data:text/javascript;base64," + base64Data;
198
void RemoteInspectorClient::connectionDidClose()
201
m_inspectorProxyMap.clear();
202
m_socketConnection = nullptr;
203
m_observer.connectionClosed(*this);
206
static Inspector::DebuggableType debuggableType(const String& targetType)
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();
217
void RemoteInspectorClient::inspect(uint64_t connectionID, uint64_t targetID, const String& targetType)
219
auto addResult = m_inspectorProxyMap.ensure(std::make_pair(connectionID, targetID), [this, connectionID, targetID] {
220
return makeUnique<RemoteInspectorProxy>(*this, connectionID, targetID);
222
if (!addResult.isNewEntry) {
223
addResult.iterator->value->show();
227
m_socketConnection->sendMessage("Setup", g_variant_new("(tt)", connectionID, targetID));
228
addResult.iterator->value->load(debuggableType(targetType));
231
void RemoteInspectorClient::sendMessageToBackend(uint64_t connectionID, uint64_t targetID, const String& message)
233
m_socketConnection->sendMessage("SendMessageToBackend", g_variant_new("(tts)", connectionID, targetID, message.utf8().data()));
236
void RemoteInspectorClient::closeFromFrontend(uint64_t connectionID, uint64_t targetID)
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));
243
void RemoteInspectorClient::setTargetList(uint64_t connectionID, Vector<Target>&& targetList)
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)
251
for (const auto& target : targetList) {
252
if (target.id == connectionTargetPair.second) {
253
m_inspectorProxyMap.get(connectionTargetPair)->setTargetName(target.name);
259
targetsToRemove.append(connectionTargetPair.second);
261
for (auto targetID : targetsToRemove)
262
m_inspectorProxyMap.remove(std::make_pair(connectionID, targetID));
264
m_targets.set(connectionID, WTFMove(targetList));
265
m_observer.targetListChanged(*this);
268
void RemoteInspectorClient::sendMessageToFrontend(uint64_t connectionID, uint64_t targetID, const char* message)
270
auto proxy = m_inspectorProxyMap.get(std::make_pair(connectionID, targetID));
274
proxy->sendMessageToFrontend(String::fromUTF8(message));
277
} // namespace WebKit
279
#endif // ENABLE(REMOTE_INSPECTOR)