InspectorAuditAgent.cpp [plain text]
#include "config.h"
#include "InspectorAuditAgent.h"
#include "Debugger.h"
#include "InjectedScript.h"
#include "JSLock.h"
#include "ObjectConstructor.h"
#include <wtf/RefPtr.h>
#include <wtf/Vector.h>
#include <wtf/text/StringBuilder.h>
#include <wtf/text/WTFString.h>
namespace Inspector {
using namespace JSC;
InspectorAuditAgent::InspectorAuditAgent(AgentContext& context)
: InspectorAgentBase("Audit"_s)
, m_backendDispatcher(AuditBackendDispatcher::create(context.backendDispatcher, this))
, m_injectedScriptManager(context.injectedScriptManager)
, m_debugger(context.environment.debugger())
{
}
InspectorAuditAgent::~InspectorAuditAgent() = default;
void InspectorAuditAgent::didCreateFrontendAndBackend(FrontendRouter*, BackendDispatcher*)
{
}
void InspectorAuditAgent::willDestroyFrontendAndBackend(DisconnectReason)
{
}
Protocol::ErrorStringOr<void> InspectorAuditAgent::setup(Optional<Protocol::Runtime::ExecutionContextId>&& executionContextId)
{
Protocol::ErrorString errorString;
if (hasActiveAudit())
return makeUnexpected("Must call teardown before calling setup again"_s);
InjectedScript injectedScript = injectedScriptForEval(errorString, WTFMove(executionContextId));
if (injectedScript.hasNoValue())
return makeUnexpected(errorString);
JSC::JSGlobalObject* globalObject = injectedScript.globalObject();
if (!globalObject)
return makeUnexpected("Missing execution state of injected script for given executionContextId"_s);
VM& vm = globalObject->vm();
JSC::JSLockHolder lock(globalObject);
m_injectedWebInspectorAuditValue.set(vm, constructEmptyObject(globalObject));
if (!m_injectedWebInspectorAuditValue)
return makeUnexpected("Unable to construct injected WebInspectorAudit object."_s);
populateAuditObject(globalObject, m_injectedWebInspectorAuditValue);
return { };
}
Protocol::ErrorStringOr<std::tuple<Ref<Protocol::Runtime::RemoteObject>, Optional<bool> >> InspectorAuditAgent::run(const String& test, Optional<Protocol::Runtime::ExecutionContextId>&& executionContextId)
{
Protocol::ErrorString errorString;
InjectedScript injectedScript = injectedScriptForEval(errorString, WTFMove(executionContextId));
if (injectedScript.hasNoValue())
return makeUnexpected(errorString);
StringBuilder functionString;
functionString.appendLiteral("(function(WebInspectorAudit) { \"use strict\"; return eval(`(");
functionString.append(test.isolatedCopy().replace('`', "\\`"));
functionString.appendLiteral(")`)(WebInspectorAudit); })");
InjectedScript::ExecuteOptions options;
options.objectGroup = "audit"_s;
if (m_injectedWebInspectorAuditValue)
options.args = { m_injectedWebInspectorAuditValue.get() };
RefPtr<Protocol::Runtime::RemoteObject> result;
Optional<bool> wasThrown;
Optional<int> savedResultIndex;
JSC::Debugger::TemporarilyDisableExceptionBreakpoints temporarilyDisableExceptionBreakpoints(m_debugger);
temporarilyDisableExceptionBreakpoints.replace();
muteConsole();
injectedScript.execute(errorString, functionString.toString(), WTFMove(options), result, wasThrown, savedResultIndex);
unmuteConsole();
if (!result)
return makeUnexpected(errorString);
return { { result.releaseNonNull(), WTFMove(wasThrown) } };
}
Protocol::ErrorStringOr<void> InspectorAuditAgent::teardown()
{
if (!hasActiveAudit())
return makeUnexpected("Must call setup before calling teardown"_s);
m_injectedWebInspectorAuditValue.clear();
return { };
}
bool InspectorAuditAgent::hasActiveAudit() const
{
return !!m_injectedWebInspectorAuditValue;
}
void InspectorAuditAgent::populateAuditObject(JSC::JSGlobalObject* globalObject, JSC::Strong<JSC::JSObject>& auditObject)
{
ASSERT(globalObject);
if (!globalObject)
return;
JSC::VM& vm = globalObject->vm();
JSC::JSLockHolder lock(vm);
auditObject->putDirect(vm, JSC::Identifier::fromString(vm, "Version"), JSC::JSValue(Protocol::Audit::VERSION));
}
}