LibDevTools: Implement a real actor for DOM nodes

The DevTools client will now send requests to the node actor, rather
than just sending messages to other actors on the node's behalf.

This exposed a slight issue in the way we assign actor IDs. Node actors
are created in the walker actor constructor, which executes before the
actor ID is incremented. So we must be sure to increment the actor ID
before invoking any actor constructors. Otherwise, the walker actor and
the first node actor have the same numeric ID.
This commit is contained in:
Timothy Flynn 2025-02-21 15:19:35 -05:00 committed by Tim Flynn
commit c56bf8ac93
Notes: github-actions[bot] 2025-02-24 17:06:54 +00:00
7 changed files with 83 additions and 6 deletions

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/JsonObject.h>
#include <LibDevTools/Actors/NodeActor.h>
#include <LibDevTools/Actors/WalkerActor.h>
namespace DevTools {
NonnullRefPtr<NodeActor> NodeActor::create(DevToolsServer& devtools, String name, WeakPtr<WalkerActor> walker)
{
return adopt_ref(*new NodeActor(devtools, move(name), move(walker)));
}
NodeActor::NodeActor(DevToolsServer& devtools, String name, WeakPtr<WalkerActor> walker)
: Actor(devtools, move(name))
, m_walker(move(walker))
{
}
NodeActor::~NodeActor() = default;
void NodeActor::handle_message(StringView type, JsonObject const&)
{
JsonObject response;
response.set("from"sv, name());
if (type == "getUniqueSelector"sv) {
if (auto walker = m_walker.strong_ref()) {
if (auto const& dom_node = walker->dom_node(name()); dom_node.has_value())
response.set("value"sv, dom_node->node.get_string("name"sv)->to_ascii_lowercase());
}
send_message(move(response));
return;
}
send_unrecognized_packet_type_error(type);
}
}