mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-31 15:32:51 +00:00
We must reply to requests received from the client in the order they are received. The wrench in this requirement is handling requests that must be performed asynchronously, such as fetching the serialized DOM tree from the WebContent process. We currently handle this with a "block token". Async request handlers hold a token that blocks any subsequent responses from being sent. When that token is removed (i.e. the async request now has a response to be sent), the async response is then sent followed by the blocked responses in-order. This strategy had a limitation that we could not handle an actor trying to take 2 block tokens, meaning only one async request could be handled at a time. This has been fine so far, but an upcoming feature (style sheet sources) will break this limitation. The client will request N sources at a time, which would try to take N block tokens. The new strategy is to assign all requests an ID, and store a list of request IDs that are awaiting a response. When the server wants to send a reply, we match the ID of the replied-to message to this list of IDs. If it is not the first in this list, then we are blocked waiting for an earlier reply, and just store the response. When the earlier request(s) receive their response, we can then send out all blocked replies (up to the next request that has not yet received a response).
110 lines
3.4 KiB
C++
110 lines
3.4 KiB
C++
/*
|
|
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <AK/Debug.h>
|
|
#include <AK/JsonObject.h>
|
|
#include <LibDevTools/Actors/HighlighterActor.h>
|
|
#include <LibDevTools/Actors/InspectorActor.h>
|
|
#include <LibDevTools/Actors/PageStyleActor.h>
|
|
#include <LibDevTools/Actors/TabActor.h>
|
|
#include <LibDevTools/Actors/WalkerActor.h>
|
|
#include <LibDevTools/DevToolsDelegate.h>
|
|
#include <LibDevTools/DevToolsServer.h>
|
|
|
|
namespace DevTools {
|
|
|
|
NonnullRefPtr<InspectorActor> InspectorActor::create(DevToolsServer& devtools, String name, WeakPtr<TabActor> tab)
|
|
{
|
|
return adopt_ref(*new InspectorActor(devtools, move(name), move(tab)));
|
|
}
|
|
|
|
InspectorActor::InspectorActor(DevToolsServer& devtools, String name, WeakPtr<TabActor> tab)
|
|
: Actor(devtools, move(name))
|
|
, m_tab(move(tab))
|
|
{
|
|
}
|
|
|
|
InspectorActor::~InspectorActor() = default;
|
|
|
|
void InspectorActor::handle_message(Message const& message)
|
|
{
|
|
JsonObject response;
|
|
|
|
if (message.type == "getPageStyle"sv) {
|
|
if (!m_page_style)
|
|
m_page_style = devtools().register_actor<PageStyleActor>(*this);
|
|
|
|
response.set("pageStyle"sv, m_page_style->serialize_style());
|
|
send_response(message, move(response));
|
|
return;
|
|
}
|
|
|
|
if (message.type == "getHighlighterByType"sv) {
|
|
auto type_name = get_required_parameter<String>(message, "typeName"sv);
|
|
if (!type_name.has_value())
|
|
return;
|
|
|
|
auto highlighter = m_highlighters.ensure(*type_name, [&]() -> NonnullRefPtr<HighlighterActor> {
|
|
return devtools().register_actor<HighlighterActor>(*this);
|
|
});
|
|
|
|
response.set("highlighter"sv, highlighter->serialize_highlighter());
|
|
send_response(message, move(response));
|
|
return;
|
|
}
|
|
|
|
if (message.type == "getWalker"sv) {
|
|
if (auto tab = m_tab.strong_ref()) {
|
|
devtools().delegate().inspect_tab(tab->description(),
|
|
async_handler<InspectorActor>(message, [](auto& self, auto dom_tree, auto& response) {
|
|
if (!WalkerActor::is_suitable_for_dom_inspection(dom_tree)) {
|
|
dbgln_if(DEVTOOLS_DEBUG, "Did not receive a suitable DOM tree: {}", dom_tree);
|
|
return;
|
|
}
|
|
|
|
self.received_dom_tree(response, move(dom_tree.as_object()));
|
|
}));
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (message.type == "supportsHighlighters"sv) {
|
|
response.set("value"sv, true);
|
|
send_response(message, move(response));
|
|
return;
|
|
}
|
|
|
|
send_unrecognized_packet_type_error(message);
|
|
}
|
|
|
|
void InspectorActor::received_dom_tree(JsonObject& response, JsonObject dom_tree)
|
|
{
|
|
auto& walker_actor = devtools().register_actor<WalkerActor>(m_tab, move(dom_tree));
|
|
m_walker = walker_actor;
|
|
|
|
JsonObject walker;
|
|
walker.set("actor"sv, walker_actor.name());
|
|
walker.set("root"sv, walker_actor.serialize_root());
|
|
|
|
response.set("walker"sv, move(walker));
|
|
}
|
|
|
|
RefPtr<TabActor> InspectorActor::tab_for(WeakPtr<InspectorActor> const& weak_inspector)
|
|
{
|
|
if (auto inspector = weak_inspector.strong_ref())
|
|
return inspector->m_tab.strong_ref();
|
|
return {};
|
|
}
|
|
|
|
RefPtr<WalkerActor> InspectorActor::walker_for(WeakPtr<InspectorActor> const& weak_inspector)
|
|
{
|
|
if (auto inspector = weak_inspector.strong_ref())
|
|
return inspector->m_walker.strong_ref();
|
|
return {};
|
|
}
|
|
|
|
}
|