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);
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <LibDevTools/Actor.h>
namespace DevTools {
class NodeActor final : public Actor {
public:
static constexpr auto base_name = "node"sv;
static NonnullRefPtr<NodeActor> create(DevToolsServer&, String name, WeakPtr<WalkerActor>);
virtual ~NodeActor() override;
virtual void handle_message(StringView type, JsonObject const&) override;
private:
NodeActor(DevToolsServer&, String name, WeakPtr<WalkerActor>);
WeakPtr<WalkerActor> m_walker;
};
}

View file

@ -6,8 +6,10 @@
#include <AK/JsonArray.h>
#include <AK/StringUtils.h>
#include <LibDevTools/Actors/NodeActor.h>
#include <LibDevTools/Actors/TabActor.h>
#include <LibDevTools/Actors/WalkerActor.h>
#include <LibDevTools/DevToolsServer.h>
#include <LibWeb/DOM/NodeType.h>
namespace DevTools {
@ -276,11 +278,12 @@ Optional<JsonObject const&> WalkerActor::find_node_by_selector(JsonObject const&
void WalkerActor::populate_dom_tree_cache(JsonObject& node, JsonObject const* parent)
{
auto& node_actor = devtools().register_actor<NodeActor>(*this);
m_dom_node_to_parent_map.set(&node, parent);
auto actor = MUST(String::formatted("{}-node{}", name(), m_dom_node_count++));
m_actor_to_dom_node_map.set(actor, &node);
node.set("actor"sv, actor);
m_actor_to_dom_node_map.set(node_actor.name(), &node);
node.set("actor"sv, node_actor.name());
auto children = node.get_array("children"sv);
if (!children.has_value())

View file

@ -48,7 +48,6 @@ private:
HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
HashMap<String, JsonObject const*> m_actor_to_dom_node_map;
size_t m_dom_node_count { 0 };
};
}

View file

@ -5,6 +5,7 @@ set(SOURCES
Actors/FrameActor.cpp
Actors/HighlighterActor.cpp
Actors/InspectorActor.cpp
Actors/NodeActor.cpp
Actors/PageStyleActor.cpp
Actors/PreferenceActor.cpp
Actors/ProcessActor.cpp

View file

@ -32,16 +32,16 @@ public:
ActorType& register_actor(Args&&... args)
{
String name;
auto id = m_actor_count++;
if constexpr (IsSame<ActorType, RootActor>) {
name = String::from_utf8_without_validation(ActorType::base_name.bytes());
} else {
name = MUST(String::formatted("server{}-{}{}", m_server_id, ActorType::base_name, m_actor_count));
name = MUST(String::formatted("server{}-{}{}", m_server_id, ActorType::base_name, id));
}
auto actor = ActorType::create(*this, name, forward<Args>(args)...);
m_actor_registry.set(name, actor);
++m_actor_count;
return actor;
}

View file

@ -17,6 +17,7 @@ class DevToolsServer;
class FrameActor;
class HighlighterActor;
class InspectorActor;
class NodeActor;
class PageStyleActor;
class PreferenceActor;
class ProcessActor;