InspectorTargetAgent.cpp [plain text]
#include "config.h"
#include "InspectorTargetAgent.h"
#include "InspectorTarget.h"
namespace Inspector {
InspectorTargetAgent::InspectorTargetAgent(FrontendRouter& frontendRouter, BackendDispatcher& backendDispatcher)
: InspectorAgentBase("Target"_s)
, m_frontendDispatcher(std::make_unique<TargetFrontendDispatcher>(frontendRouter))
, m_backendDispatcher(TargetBackendDispatcher::create(backendDispatcher, this))
{
}
void InspectorTargetAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
{
m_isConnected = true;
connectToTargets();
}
void InspectorTargetAgent::willDestroyFrontendAndBackend(DisconnectReason)
{
disconnectFromTargets();
m_isConnected = false;
}
void InspectorTargetAgent::exists(ErrorString&)
{
}
void InspectorTargetAgent::sendMessageToTarget(ErrorString& errorString, const String& targetId, const String& message)
{
InspectorTarget* target = m_targets.get(targetId);
if (!target) {
errorString = "Target not found."_s;
return;
}
target->sendMessageToTargetBackend(message);
}
void InspectorTargetAgent::sendMessageFromTargetToFrontend(const String& targetId, const String& message)
{
ASSERT_WITH_MESSAGE(m_targets.get(targetId), "Sending a message from an untracked target to the frontend.");
m_frontendDispatcher->dispatchMessageFromTarget(targetId, message);
}
static Protocol::Target::TargetInfo::Type targetTypeToProtocolType(InspectorTargetType type)
{
switch (type) {
case InspectorTargetType::JavaScriptContext:
return Protocol::Target::TargetInfo::Type::JavaScript;
case InspectorTargetType::Page:
return Protocol::Target::TargetInfo::Type::Page;
case InspectorTargetType::DedicatedWorker:
return Protocol::Target::TargetInfo::Type::Worker;
case InspectorTargetType::ServiceWorker:
return Protocol::Target::TargetInfo::Type::ServiceWorker;
}
ASSERT_NOT_REACHED();
return Protocol::Target::TargetInfo::Type::JavaScript;
}
static Ref<Protocol::Target::TargetInfo> buildTargetInfoObject(const InspectorTarget& target)
{
return Protocol::Target::TargetInfo::create()
.setTargetId(target.identifier())
.setType(targetTypeToProtocolType(target.type()))
.release();
}
void InspectorTargetAgent::targetCreated(InspectorTarget& target)
{
auto addResult = m_targets.set(target.identifier(), &target);
ASSERT_UNUSED(addResult, addResult.isNewEntry);
if (!m_isConnected)
return;
target.connect(frontendChannel());
m_frontendDispatcher->targetCreated(buildTargetInfoObject(target));
}
void InspectorTargetAgent::targetDestroyed(InspectorTarget& target)
{
m_targets.remove(target.identifier());
if (!m_isConnected)
return;
target.disconnect(frontendChannel());
m_frontendDispatcher->targetDestroyed(target.identifier());
}
void InspectorTargetAgent::connectToTargets()
{
auto& channel = frontendChannel();
for (InspectorTarget* target : m_targets.values()) {
target->connect(channel);
m_frontendDispatcher->targetCreated(buildTargetInfoObject(*target));
}
}
void InspectorTargetAgent::disconnectFromTargets()
{
auto& channel = frontendChannel();
for (InspectorTarget* target : m_targets.values())
target->disconnect(channel);
}
}