InspectorFrontendAPIDispatcher.cpp [plain text]
#include "config.h"
#include "InspectorFrontendAPIDispatcher.h"
#include "Frame.h"
#include "InspectorController.h"
#include "Page.h"
#include "ScriptController.h"
#include "ScriptDisallowedScope.h"
#include "ScriptSourceCode.h"
#include "ScriptState.h"
#include <JavaScriptCore/FrameTracers.h>
#include <wtf/RunLoop.h>
namespace WebCore {
using EvaluationError = InspectorFrontendAPIDispatcher::EvaluationError;
using EvaluationResultHandler = CompletionHandler<Expected<JSC::JSValue, EvaluationError>>;
InspectorFrontendAPIDispatcher::InspectorFrontendAPIDispatcher(Page& frontendPage)
: m_frontendPage(makeWeakPtr(frontendPage))
{
}
InspectorFrontendAPIDispatcher::~InspectorFrontendAPIDispatcher()
{
invalidateQueuedExpressions();
}
void InspectorFrontendAPIDispatcher::reset()
{
m_frontendLoaded = false;
m_suspended = false;
invalidateQueuedExpressions();
}
void InspectorFrontendAPIDispatcher::frontendLoaded()
{
ASSERT(m_frontendPage);
m_frontendLoaded = true;
if (!m_suspended)
evaluateQueuedExpressions();
}
void InspectorFrontendAPIDispatcher::suspend(UnsuspendSoon unsuspendSoon)
{
if (m_suspended)
return;
m_suspended = true;
if (unsuspendSoon == UnsuspendSoon::Yes) {
RunLoop::main().dispatch([protectedThis = makeRef(*this)] {
if (!protectedThis->m_frontendPage)
return;
protectedThis->unsuspend();
});
}
}
void InspectorFrontendAPIDispatcher::unsuspend()
{
if (!m_suspended)
return;
m_suspended = false;
if (m_frontendLoaded)
evaluateQueuedExpressions();
}
JSC::JSGlobalObject* InspectorFrontendAPIDispatcher::frontendGlobalObject()
{
if (!m_frontendPage)
return nullptr;
return m_frontendPage->mainFrame().script().globalObject(mainThreadNormalWorld());
}
static String expressionForEvaluatingCommand(const String& command, Vector<Ref<JSON::Value>>&& arguments)
{
StringBuilder expression;
expression.appendLiteral("InspectorFrontendAPI.dispatch([\"");
expression.append(command);
expression.append('"');
for (auto& argument : arguments) {
expression.appendLiteral(", ");
argument->writeJSON(expression);
}
expression.append(']');
expression.append(')');
return expression.toString();
}
InspectorFrontendAPIDispatcher::EvaluationResult InspectorFrontendAPIDispatcher::dispatchCommandWithResultSync(const String& command, Vector<Ref<JSON::Value>>&& arguments)
{
if (m_suspended)
return makeUnexpected(EvaluationError::ExecutionSuspended);
return evaluateExpression(expressionForEvaluatingCommand(command, WTFMove(arguments)));
}
void InspectorFrontendAPIDispatcher::dispatchCommandWithResultAsync(const String& command, Vector<Ref<JSON::Value>>&& arguments, EvaluationResultHandler&& resultHandler)
{
evaluateOrQueueExpression(expressionForEvaluatingCommand(command, WTFMove(arguments)), WTFMove(resultHandler));
}
void InspectorFrontendAPIDispatcher::dispatchMessageAsync(const String& message)
{
evaluateOrQueueExpression(makeString("InspectorFrontendAPI.dispatchMessageAsync(", message, ")"));
}
void InspectorFrontendAPIDispatcher::evaluateOrQueueExpression(const String& expression, EvaluationResultHandler&& optionalResultHandler)
{
if (!m_frontendPage) {
if (optionalResultHandler)
optionalResultHandler(makeUnexpected(EvaluationError::ContextDestroyed));
return;
}
if (!ScriptDisallowedScope::InMainThread::isScriptAllowed())
suspend(UnsuspendSoon::Yes);
if (!m_frontendLoaded || m_suspended) {
m_queuedEvaluations.append(std::make_pair(expression, WTFMove(optionalResultHandler)));
return;
}
ValueOrException result = evaluateExpression(expression);
if (optionalResultHandler)
optionalResultHandler(result);
}
void InspectorFrontendAPIDispatcher::invalidateQueuedExpressions()
{
auto queuedEvaluations = std::exchange(m_queuedEvaluations, { });
for (auto& pair : queuedEvaluations) {
auto resultHandler = WTFMove(pair.second);
if (resultHandler)
resultHandler(makeUnexpected(EvaluationError::ContextDestroyed));
}
}
void InspectorFrontendAPIDispatcher::evaluateQueuedExpressions()
{
if (!m_frontendPage)
return;
if (m_queuedEvaluations.isEmpty())
return;
auto queuedEvaluations = std::exchange(m_queuedEvaluations, { });
for (auto& pair : queuedEvaluations) {
auto result = evaluateExpression(pair.first);
if (auto resultHandler = WTFMove(pair.second))
resultHandler(result);
}
}
ValueOrException InspectorFrontendAPIDispatcher::evaluateExpression(const String& expression)
{
ASSERT(m_frontendPage);
ASSERT(!m_suspended);
ASSERT(m_queuedEvaluations.isEmpty());
JSC::SuspendExceptionScope scope(&m_frontendPage->inspectorController().vm());
return m_frontendPage->mainFrame().script().evaluateInWorld(ScriptSourceCode(expression), mainThreadNormalWorld());
}
void InspectorFrontendAPIDispatcher::evaluateExpressionForTesting(const String& expression)
{
evaluateOrQueueExpression(expression);
}
}