2025-02-15 07:35:58 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Debug.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
#include <LibDevTools/Connection.h>
|
|
|
|
|
|
|
|
namespace DevTools {
|
|
|
|
|
|
|
|
NonnullRefPtr<Connection> Connection::create(NonnullOwnPtr<Core::BufferedTCPSocket> socket)
|
|
|
|
{
|
|
|
|
return adopt_ref(*new Connection(move(socket)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::Connection(NonnullOwnPtr<Core::BufferedTCPSocket> socket)
|
|
|
|
: m_socket(move(socket))
|
|
|
|
{
|
|
|
|
m_socket->on_ready_to_read = [this]() {
|
|
|
|
if (auto result = on_ready_to_read(); result.is_error()) {
|
|
|
|
if (on_connection_closed)
|
|
|
|
on_connection_closed();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection() = default;
|
|
|
|
|
|
|
|
// https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#packets
|
|
|
|
void Connection::send_message(JsonValue const& message)
|
|
|
|
{
|
2025-02-17 15:08:17 -05:00
|
|
|
auto serialized = message.serialized();
|
2025-02-15 07:35:58 -05:00
|
|
|
|
|
|
|
if constexpr (DEVTOOLS_DEBUG) {
|
|
|
|
if (message.is_object() && message.as_object().get("error"sv).has_value())
|
|
|
|
dbgln("\x1b[1;31m<<\x1b[0m {}", serialized);
|
|
|
|
else
|
|
|
|
dbgln("\x1b[1;32m<<\x1b[0m {}", serialized);
|
|
|
|
}
|
|
|
|
|
2025-02-17 15:08:17 -05:00
|
|
|
if (m_socket->write_formatted("{}:{}", serialized.byte_count(), serialized).is_error()) {
|
2025-02-15 07:35:58 -05:00
|
|
|
if (on_connection_closed)
|
|
|
|
on_connection_closed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#packets
|
|
|
|
ErrorOr<JsonValue> Connection::read_message()
|
|
|
|
{
|
|
|
|
ByteBuffer length_buffer;
|
|
|
|
|
|
|
|
// FIXME: `read_until(':')` would be nicer here, but that seems to return immediately without receiving any data.
|
|
|
|
while (true) {
|
|
|
|
auto byte = TRY(m_socket->read_value<u8>());
|
|
|
|
if (byte == ':') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length_buffer.append(byte);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto length = StringView { length_buffer }.to_number<size_t>();
|
|
|
|
if (!length.has_value())
|
|
|
|
return Error::from_string_literal("Could not read message length from DevTools client");
|
|
|
|
|
|
|
|
ByteBuffer message_buffer;
|
|
|
|
message_buffer.resize(*length);
|
|
|
|
|
|
|
|
TRY(m_socket->read_until_filled(message_buffer));
|
|
|
|
|
|
|
|
auto message = TRY(JsonValue::from_string(message_buffer));
|
|
|
|
dbgln_if(DEVTOOLS_DEBUG, "\x1b[1;33m>>\x1b[0m {}", message);
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> Connection::on_ready_to_read()
|
|
|
|
{
|
|
|
|
// https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#the-request-reply-pattern
|
|
|
|
// Note that it is correct for a client to send several requests to a request/reply actor without waiting for a
|
|
|
|
// reply to each request before sending the next; requests can be pipelined.
|
|
|
|
while (TRY(m_socket->can_read_without_blocking())) {
|
|
|
|
auto message = TRY(read_message());
|
|
|
|
if (!message.is_object())
|
|
|
|
continue;
|
|
|
|
|
2025-03-12 08:01:23 -04:00
|
|
|
Core::deferred_invoke([this, message = move(message)]() mutable {
|
2025-02-15 07:35:58 -05:00
|
|
|
if (on_message_received)
|
2025-03-12 08:01:23 -04:00
|
|
|
on_message_received(move(message.as_object()));
|
2025-02-15 07:35:58 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|