CommandLineAPIHost.cpp [plain text]
#include "config.h"
#include "CommandLineAPIHost.h"
#include "Database.h"
#include "Document.h"
#include "InspectorDOMAgent.h"
#include "InspectorDOMStorageAgent.h"
#include "InspectorDatabaseAgent.h"
#include "JSCommandLineAPIHost.h"
#include "JSDOMGlobalObject.h"
#include "JSEventListener.h"
#include "Pasteboard.h"
#include "Storage.h"
#include <JavaScriptCore/InspectorAgent.h>
#include <JavaScriptCore/InspectorConsoleAgent.h>
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSLock.h>
#include <JavaScriptCore/ScriptValue.h>
#include <wtf/JSONValues.h>
#include <wtf/RefPtr.h>
#include <wtf/StdLibExtras.h>
namespace WebCore {
using namespace JSC;
using namespace Inspector;
Ref<CommandLineAPIHost> CommandLineAPIHost::create()
{
return adoptRef(*new CommandLineAPIHost);
}
CommandLineAPIHost::CommandLineAPIHost()
: m_inspectedObject(std::make_unique<InspectableObject>())
{
}
CommandLineAPIHost::~CommandLineAPIHost() = default;
void CommandLineAPIHost::disconnect()
{
m_inspectorAgent = nullptr;
m_consoleAgent = nullptr;
m_domAgent = nullptr;
m_domStorageAgent = nullptr;
m_databaseAgent = nullptr;
}
void CommandLineAPIHost::inspect(JSC::ExecState& state, JSC::JSValue valueToInspect, JSC::JSValue hintsValue)
{
if (!m_inspectorAgent)
return;
RefPtr<JSON::Object> hintsObject;
if (!Inspector::toInspectorValue(state, hintsValue)->asObject(hintsObject))
return;
auto remoteObject = BindingTraits<Inspector::Protocol::Runtime::RemoteObject>::runtimeCast(Inspector::toInspectorValue(state, valueToInspect));
m_inspectorAgent->inspect(WTFMove(remoteObject), WTFMove(hintsObject));
}
static Vector<CommandLineAPIHost::ListenerEntry> listenerEntriesFromListenerInfo(ExecState& state, Document& document, const EventListenerInfo& listenerInfo)
{
VM& vm = state.vm();
Vector<CommandLineAPIHost::ListenerEntry> entries;
for (auto& eventListener : listenerInfo.eventListenerVector) {
if (!is<JSEventListener>(eventListener->callback())) {
ASSERT_NOT_REACHED();
continue;
}
auto& jsListener = downcast<JSEventListener>(eventListener->callback());
if (&jsListener.isolatedWorld() != ¤tWorld(state))
continue;
auto function = jsListener.jsFunction(document);
if (!function)
continue;
entries.append({ JSC::Strong<JSC::JSObject>(vm, function), eventListener->useCapture(), eventListener->isPassive(), eventListener->isOnce() });
}
return entries;
}
auto CommandLineAPIHost::getEventListeners(JSC::ExecState& state, Node* node) -> EventListenersRecord
{
if (!m_domAgent)
return { };
if (!node)
return { };
Vector<EventListenerInfo> listenerInfoArray;
m_domAgent->getEventListeners(node, listenerInfoArray, false);
EventListenersRecord result;
for (auto& listenerInfo : listenerInfoArray) {
auto entries = listenerEntriesFromListenerInfo(state, node->document(), listenerInfo);
if (entries.isEmpty())
continue;
result.append({ listenerInfo.eventType, WTFMove(entries) });
}
return result;
}
void CommandLineAPIHost::clearConsoleMessages()
{
if (m_consoleAgent) {
ErrorString unused;
m_consoleAgent->clearMessages(unused);
}
}
void CommandLineAPIHost::copyText(const String& text)
{
Pasteboard::createForCopyAndPaste()->writePlainText(text, Pasteboard::CannotSmartReplace);
}
JSC::JSValue CommandLineAPIHost::InspectableObject::get(JSC::ExecState&)
{
return { };
}
void CommandLineAPIHost::addInspectedObject(std::unique_ptr<CommandLineAPIHost::InspectableObject> object)
{
m_inspectedObject = WTFMove(object);
}
JSC::JSValue CommandLineAPIHost::inspectedObject(JSC::ExecState& state)
{
if (!m_inspectedObject)
return jsUndefined();
JSC::JSLockHolder lock(&state);
auto scriptValue = m_inspectedObject->get(state);
return scriptValue ? scriptValue : jsUndefined();
}
String CommandLineAPIHost::databaseId(Database& database)
{
if (m_databaseAgent)
return m_databaseAgent->databaseId(database);
return { };
}
String CommandLineAPIHost::storageId(Storage& storage)
{
if (m_domStorageAgent)
return m_domStorageAgent->storageId(storage);
return { };
}
JSValue CommandLineAPIHost::wrapper(ExecState* exec, JSDOMGlobalObject* globalObject)
{
JSValue value = m_wrappers.getWrapper(globalObject);
if (value)
return value;
JSObject* prototype = JSCommandLineAPIHost::createPrototype(exec->vm(), *globalObject);
Structure* structure = JSCommandLineAPIHost::createStructure(exec->vm(), globalObject, prototype);
JSCommandLineAPIHost* commandLineAPIHost = JSCommandLineAPIHost::create(structure, globalObject, makeRef(*this));
m_wrappers.addWrapper(globalObject, commandLineAPIHost);
return commandLineAPIHost;
}
void CommandLineAPIHost::clearAllWrappers()
{
m_wrappers.clearAllWrappers();
m_inspectedObject = std::make_unique<InspectableObject>();
}
}