RemoteConnectionToTarget.cpp [plain text]
#include "config.h"
#include "RemoteConnectionToTarget.h"
#if ENABLE(REMOTE_INSPECTOR)
#include "RemoteAutomationTarget.h"
#include "RemoteInspectionTarget.h"
#include "RemoteInspector.h"
#include <wtf/RunLoop.h>
namespace Inspector {
RemoteConnectionToTarget::RemoteConnectionToTarget(RemoteControllableTarget& target)
: m_target(&target)
{
}
RemoteConnectionToTarget::~RemoteConnectionToTarget()
{
}
bool RemoteConnectionToTarget::setup(bool isAutomaticInspection, bool automaticallyPause)
{
LockHolder lock(m_targetMutex);
if (!m_target)
return false;
auto targetIdentifier = this->targetIdentifier().valueOr(0);
if (!m_target || !m_target->remoteControlAllowed()) {
RemoteInspector::singleton().setupFailed(targetIdentifier);
m_target = nullptr;
} else if (is<RemoteInspectionTarget>(m_target)) {
auto target = downcast<RemoteInspectionTarget>(m_target);
target->connect(*this, isAutomaticInspection, automaticallyPause);
m_connected = true;
RemoteInspector::singleton().updateTargetListing(targetIdentifier);
} else if (is<RemoteAutomationTarget>(m_target)) {
auto target = downcast<RemoteAutomationTarget>(m_target);
target->connect(*this);
m_connected = true;
RemoteInspector::singleton().updateTargetListing(targetIdentifier);
}
return true;
}
void RemoteConnectionToTarget::sendMessageToTarget(const String& message)
{
RemoteControllableTarget* target = nullptr;
{
LockHolder lock(m_targetMutex);
if (!m_target)
return;
target = m_target;
}
target->dispatchMessageFromRemote(message);
}
void RemoteConnectionToTarget::close()
{
RunLoop::current().dispatch([this, protectThis = makeRef(*this)] {
LockHolder lock(m_targetMutex);
if (!m_target)
return;
auto targetIdentifier = m_target->targetIdentifier();
if (m_connected)
m_target->disconnect(*this);
m_target = nullptr;
RemoteInspector::singleton().updateTargetListing(targetIdentifier);
});
}
void RemoteConnectionToTarget::targetClosed()
{
LockHolder lock(m_targetMutex);
m_target = nullptr;
}
Optional<TargetID> RemoteConnectionToTarget::targetIdentifier() const
{
return m_target ? Optional<TargetID>(m_target->targetIdentifier()) : WTF::nullopt;
}
void RemoteConnectionToTarget::sendMessageToFrontend(const String& message)
{
if (!m_target)
return;
RemoteInspector::singleton().sendMessageToRemote(m_target->targetIdentifier(), message);
}
}
#endif // ENABLE(REMOTE_INSPECTOR)