c075698bd9bf6e7048c0241cbff8327956272a83
[WebKit-https.git] / Source / JavaScriptCore / inspector / remote / glib / RemoteInspectorGlib.cpp
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 "RemoteInspector.h"
28
29 #if ENABLE(REMOTE_INSPECTOR)
30
31 #include "RemoteAutomationTarget.h"
32 #include "RemoteConnectionToTarget.h"
33 #include "RemoteInspectionTarget.h"
34 #include <gio/gio.h>
35 #include <wtf/NeverDestroyed.h>
36 #include <wtf/RunLoop.h>
37 #include <wtf/glib/GUniquePtr.h>
38
39 #define REMOTE_INSPECTOR_DBUS_INTERFACE "org.webkit.RemoteInspector"
40 #define REMOTE_INSPECTOR_DBUS_OBJECT_PATH "/org/webkit/RemoteInspector"
41 #define INSPECTOR_DBUS_INTERFACE "org.webkit.Inspector"
42 #define INSPECTOR_DBUS_OBJECT_PATH "/org/webkit/Inspector"
43
44 namespace Inspector {
45
46 RemoteInspector& RemoteInspector::singleton()
47 {
48     static NeverDestroyed<RemoteInspector> shared;
49     return shared;
50 }
51
52 RemoteInspector::RemoteInspector()
53 {
54     if (g_getenv("WEBKIT_INSPECTOR_SERVER"))
55         start();
56 }
57
58 void RemoteInspector::start()
59 {
60     std::lock_guard<Lock> lock(m_mutex);
61
62     if (m_enabled)
63         return;
64
65     m_enabled = true;
66     m_cancellable = adoptGRef(g_cancellable_new());
67
68     GUniquePtr<char> inspectorAddress(g_strdup(g_getenv("WEBKIT_INSPECTOR_SERVER")));
69     char* portPtr = g_strrstr(inspectorAddress.get(), ":");
70     ASSERT(portPtr);
71     *portPtr = '\0';
72     portPtr++;
73     unsigned port = g_ascii_strtoull(portPtr, nullptr, 10);
74     GUniquePtr<char> dbusAddress(g_strdup_printf("tcp:host=%s,port=%u", inspectorAddress.get(), port));
75     g_dbus_connection_new_for_address(dbusAddress.get(), G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, nullptr, m_cancellable.get(),
76         [](GObject*, GAsyncResult* result, gpointer userData) {
77             RemoteInspector* inspector = static_cast<RemoteInspector*>(userData);
78             GUniqueOutPtr<GError> error;
79             if (GRefPtr<GDBusConnection> connection = adoptGRef(g_dbus_connection_new_for_address_finish(result, &error.outPtr())))
80                 inspector->setupConnection(WTFMove(connection));
81             else if (!g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED))
82                 g_warning("RemoteInspector failed to connect to inspector server at: %s: %s", g_getenv("WEBKIT_INSPECTOR_SERVER"), error->message);
83     }, this);
84 }
85
86 void RemoteInspector::stopInternal(StopSource)
87 {
88     if (!m_enabled)
89         return;
90
91     m_enabled = false;
92     m_pushScheduled = false;
93     g_cancellable_cancel(m_cancellable.get());
94     m_cancellable = nullptr;
95
96     for (auto targetConnection : m_targetConnectionMap.values())
97         targetConnection->close();
98     m_targetConnectionMap.clear();
99
100     updateHasActiveDebugSession();
101
102     m_automaticInspectionPaused = false;
103     m_dbusConnection = nullptr;
104 }
105
106 static const char introspectionXML[] =
107     "<node>"
108     "  <interface name='" REMOTE_INSPECTOR_DBUS_INTERFACE "'>"
109     "    <method name='GetTargetList'>"
110     "    </method>"
111     "    <method name='Setup'>"
112     "      <arg type='t' name='target' direction='in'/>"
113     "    </method>"
114     "    <method name='SendMessageToTarget'>"
115     "      <arg type='t' name='target' direction='in'/>"
116     "      <arg type='s' name='message' direction='in'/>"
117     "    </method>"
118     "    <method name='FrontendDidClose'>"
119     "      <arg type='t' name='target' direction='in'/>"
120     "    </method>"
121     "  </interface>"
122     "</node>";
123
124 const GDBusInterfaceVTable RemoteInspector::s_interfaceVTable = {
125     // method_call
126     [] (GDBusConnection*, const gchar* /*sender*/, const gchar* /*objectPath*/, const gchar* /*interfaceName*/, const gchar* methodName, GVariant* parameters, GDBusMethodInvocation* invocation, gpointer userData) {
127         auto* inspector = static_cast<RemoteInspector*>(userData);
128         if (!g_strcmp0(methodName, "GetTargetList")) {
129             inspector->receivedGetTargetListMessage();
130             g_dbus_method_invocation_return_value(invocation, nullptr);
131         } else if (!g_strcmp0(methodName, "Setup")) {
132             guint64 targetID;
133             g_variant_get(parameters, "(t)", &targetID);
134             inspector->receivedSetupMessage(targetID);
135             g_dbus_method_invocation_return_value(invocation, nullptr);
136         } else if (!g_strcmp0(methodName, "SendMessageToTarget")) {
137             guint64 targetID;
138             const char* message;
139             g_variant_get(parameters, "(t&s)", &targetID, &message);
140             inspector->receivedDataMessage(targetID, message);
141             g_dbus_method_invocation_return_value(invocation, nullptr);
142         } else if (!g_strcmp0(methodName, "FrontendDidClose")) {
143             guint64 targetID;
144             g_variant_get(parameters, "(t)", &targetID);
145             inspector->receivedCloseMessage(targetID);
146             g_dbus_method_invocation_return_value(invocation, nullptr);
147         }
148     },
149     // get_property
150     nullptr,
151     // set_property
152     nullptr,
153     // padding
154     { 0 }
155 };
156
157 void RemoteInspector::setupConnection(GRefPtr<GDBusConnection>&& connection)
158 {
159     std::lock_guard<Lock> lock(m_mutex);
160
161     ASSERT(connection);
162     ASSERT(!m_dbusConnection);
163     m_dbusConnection = WTFMove(connection);
164
165     static GDBusNodeInfo* introspectionData = nullptr;
166     if (!introspectionData)
167         introspectionData = g_dbus_node_info_new_for_xml(introspectionXML, nullptr);
168
169     g_dbus_connection_register_object(m_dbusConnection.get(), REMOTE_INSPECTOR_DBUS_OBJECT_PATH,
170         introspectionData->interfaces[0], &s_interfaceVTable, this, nullptr, nullptr);
171
172     if (!m_targetMap.isEmpty())
173         pushListingsSoon();
174 }
175
176 static void dbusConnectionCallAsyncReadyCallback(GObject* source, GAsyncResult* result, gpointer)
177 {
178     GUniqueOutPtr<GError> error;
179     GRefPtr<GVariant> resultVariant = adoptGRef(g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, &error.outPtr()));
180     if (!resultVariant && !g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_CANCELLED))
181         g_warning("RemoteInspector failed to send DBus message: %s", error->message);
182 }
183
184 TargetListing RemoteInspector::listingForInspectionTarget(const RemoteInspectionTarget& target) const
185 {
186     if (!target.remoteDebuggingAllowed())
187         return nullptr;
188
189     // FIXME: Support remote debugging of a ServiceWorker.
190     if (target.type() == RemoteInspectionTarget::Type::ServiceWorker)
191         return nullptr;
192
193     ASSERT(target.type() == RemoteInspectionTarget::Type::Web || target.type() == RemoteInspectionTarget::Type::JavaScript);
194     return g_variant_new("(tsssb)", static_cast<guint64>(target.targetIdentifier()),
195         target.type() == RemoteInspectionTarget::Type::Web ? "Web" : "JavaScript",
196         target.name().utf8().data(), target.type() == RemoteInspectionTarget::Type::Web ? target.url().utf8().data() : "null",
197         target.hasLocalDebugger());
198 }
199
200 TargetListing RemoteInspector::listingForAutomationTarget(const RemoteAutomationTarget& target) const
201 {
202     return g_variant_new("(tsssb)", static_cast<guint64>(target.targetIdentifier()),
203         "Automation", target.name().utf8().data(), "null", target.isPaired());
204 }
205
206 void RemoteInspector::pushListingsNow()
207 {
208     if (!m_dbusConnection)
209         return;
210
211     m_pushScheduled = false;
212
213     GVariantBuilder builder;
214     g_variant_builder_init(&builder, G_VARIANT_TYPE("(a(tsssb)b)"));
215     g_variant_builder_open(&builder, G_VARIANT_TYPE("a(tsssb)"));
216     for (auto listing : m_targetListingMap.values())
217         g_variant_builder_add_value(&builder, listing.get());
218     g_variant_builder_close(&builder);
219     g_variant_builder_add(&builder, "b", m_clientCapabilities && m_clientCapabilities->remoteAutomationAllowed);
220     g_dbus_connection_call(m_dbusConnection.get(), nullptr,
221         INSPECTOR_DBUS_OBJECT_PATH, INSPECTOR_DBUS_INTERFACE, "SetTargetList",
222         g_variant_builder_end(&builder), nullptr, G_DBUS_CALL_FLAGS_NO_AUTO_START,
223         -1, m_cancellable.get(), dbusConnectionCallAsyncReadyCallback, nullptr);
224 }
225
226 void RemoteInspector::pushListingsSoon()
227 {
228     if (!m_dbusConnection)
229         return;
230
231     if (m_pushScheduled)
232         return;
233
234     m_pushScheduled = true;
235
236     RunLoop::current().dispatch([this] {
237         std::lock_guard<Lock> lock(m_mutex);
238         if (m_pushScheduled)
239             pushListingsNow();
240     });
241 }
242
243 void RemoteInspector::updateAutomaticInspectionCandidate(RemoteInspectionTarget* target)
244 {
245     std::lock_guard<Lock> lock(m_mutex);
246
247     ASSERT(target);
248     unsigned targetIdentifier = target->targetIdentifier();
249     if (!targetIdentifier)
250         return;
251
252     auto result = m_targetMap.set(targetIdentifier, target);
253     ASSERT_UNUSED(result, !result.isNewEntry);
254
255     // If the target has just allowed remote control, then the listing won't exist yet.
256     // If the target has no identifier remove the old listing.
257     if (auto targetListing = listingForTarget(*target))
258         m_targetListingMap.set(targetIdentifier, targetListing);
259     else
260         m_targetListingMap.remove(targetIdentifier);
261     // FIXME: Implement automatic inspection.
262     pushListingsSoon();
263 }
264
265 void RemoteInspector::sendAutomaticInspectionCandidateMessage()
266 {
267     ASSERT(m_enabled);
268     ASSERT(m_automaticInspectionEnabled);
269     ASSERT(m_automaticInspectionPaused);
270     ASSERT(m_automaticInspectionCandidateTargetIdentifier);
271     ASSERT(m_dbusConnection);
272     // FIXME: Implement automatic inspection.
273 }
274
275 void RemoteInspector::sendMessageToRemote(unsigned targetIdentifier, const String& message)
276 {
277     std::lock_guard<Lock> lock(m_mutex);
278     if (!m_dbusConnection)
279         return;
280
281     g_dbus_connection_call(m_dbusConnection.get(), nullptr,
282         INSPECTOR_DBUS_OBJECT_PATH, INSPECTOR_DBUS_INTERFACE, "SendMessageToFrontend",
283         g_variant_new("(ts)", static_cast<guint64>(targetIdentifier), message.utf8().data()),
284         nullptr, G_DBUS_CALL_FLAGS_NO_AUTO_START,
285         -1, m_cancellable.get(), dbusConnectionCallAsyncReadyCallback, nullptr);
286 }
287
288 void RemoteInspector::receivedGetTargetListMessage()
289 {
290     std::lock_guard<Lock> lock(m_mutex);
291     pushListingsNow();
292 }
293
294 void RemoteInspector::receivedSetupMessage(unsigned targetIdentifier)
295 {
296     setup(targetIdentifier);
297 }
298
299 void RemoteInspector::receivedDataMessage(unsigned targetIdentifier, const char* message)
300 {
301     RefPtr<RemoteConnectionToTarget> connectionToTarget;
302     {
303         std::lock_guard<Lock> lock(m_mutex);
304         connectionToTarget = m_targetConnectionMap.get(targetIdentifier);
305         if (!connectionToTarget)
306             return;
307     }
308     connectionToTarget->sendMessageToTarget(String::fromUTF8(message));
309 }
310
311 void RemoteInspector::receivedCloseMessage(unsigned targetIdentifier)
312 {
313     RefPtr<RemoteConnectionToTarget> connectionToTarget;
314     {
315         std::lock_guard<Lock> lock(m_mutex);
316         RemoteControllableTarget* target = m_targetMap.get(targetIdentifier);
317         if (!target)
318             return;
319
320         connectionToTarget = m_targetConnectionMap.take(targetIdentifier);
321         updateHasActiveDebugSession();
322     }
323
324     if (connectionToTarget)
325         connectionToTarget->close();
326 }
327
328 void RemoteInspector::setup(unsigned targetIdentifier)
329 {
330     RemoteControllableTarget* target;
331     {
332         std::lock_guard<Lock> lock(m_mutex);
333         target = m_targetMap.get(targetIdentifier);
334         if (!target)
335             return;
336     }
337
338     auto connectionToTarget = adoptRef(*new RemoteConnectionToTarget(*target));
339     ASSERT(is<RemoteInspectionTarget>(target) || is<RemoteAutomationTarget>(target));
340     if (!connectionToTarget->setup()) {
341         connectionToTarget->close();
342         return;
343     }
344
345     std::lock_guard<Lock> lock(m_mutex);
346     m_targetConnectionMap.set(targetIdentifier, WTFMove(connectionToTarget));
347
348     updateHasActiveDebugSession();
349 }
350
351 void RemoteInspector::sendMessageToTarget(unsigned targetIdentifier, const char* message)
352 {
353     if (auto connectionToTarget = m_targetConnectionMap.get(targetIdentifier))
354         connectionToTarget->sendMessageToTarget(String::fromUTF8(message));
355 }
356
357 void RemoteInspector::requestAutomationSession(const char* sessionID, const Client::SessionCapabilities& capabilities)
358 {
359     if (!m_client)
360         return;
361
362     if (!m_clientCapabilities || !m_clientCapabilities->remoteAutomationAllowed)
363         return;
364
365     if (!sessionID || !sessionID[0])
366         return;
367
368     m_client->requestAutomationSession(String::fromUTF8(sessionID), capabilities);
369     updateClientCapabilities();
370 }
371
372 } // namespace Inspector
373
374 #endif // ENABLE(REMOTE_INSPECTOR)