Commit 1527c487 authored by Alex Rudenko's avatar Alex Rudenko Committed by Commit Bot

Expose V8CommandLineAPIScope and V8InspectorSession::createCommandLineAPI

This CL extracts CommandLineAPIScope from V8Console and exposes it
as V8CommandLineAPIScope. Also, it exposes V8InspectorSession::createCommandLineAPI.
These changes will be used by InspectorPageAgent to install command
line APIs when evaluating scripts added using CDP's command
Page.addScriptToEvaluateOnNewDocument.

Chromium CL: https://crrev.com/c/2835786

Doc: https://docs.google.com/document/d/1zGG7-NZMb-aOfFfHf1u4VsP4C-lZettopCvYDC6pkBw/
Bug: chromium:1200705
Change-Id: I39b27f957cfb6d682ea84e385eaf25d09d261b58
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2835712Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Commit-Queue: Alex Rudenko <alexrudenko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74096}
parent 91e19288
...@@ -35,6 +35,28 @@ class Domain; ...@@ -35,6 +35,28 @@ class Domain;
} }
} // namespace protocol } // namespace protocol
class V8_EXPORT V8CommandLineAPIScope {
public:
V8CommandLineAPIScope(v8::Local<v8::Context>,
v8::Local<v8::Object> commandLineAPI,
v8::Local<v8::Object> global);
~V8CommandLineAPIScope();
V8CommandLineAPIScope(const V8CommandLineAPIScope&) = delete;
V8CommandLineAPIScope& operator=(const V8CommandLineAPIScope&) = delete;
private:
static void accessorGetterCallback(
v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value>&);
static void accessorSetterCallback(v8::Local<v8::Name>, v8::Local<v8::Value>,
const v8::PropertyCallbackInfo<void>&);
v8::Local<v8::Context> m_context;
v8::Local<v8::Object> m_commandLineAPI;
v8::Local<v8::Object> m_global;
v8::Local<v8::Set> m_installedMethods;
v8::Local<v8::ArrayBuffer> m_thisReference;
};
class V8_EXPORT StringView { class V8_EXPORT StringView {
public: public:
StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {} StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
...@@ -139,6 +161,9 @@ class V8_EXPORT V8InspectorSession { ...@@ -139,6 +161,9 @@ class V8_EXPORT V8InspectorSession {
virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>> virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
supportedDomains() = 0; supportedDomains() = 0;
virtual v8::Local<v8::Object> createCommandLineAPI(
v8::Local<v8::Context> context) = 0;
// Debugger actions. // Debugger actions.
virtual void schedulePauseOnNextStatement(StringView breakReason, virtual void schedulePauseOnNextStatement(StringView breakReason,
StringView breakDetails) = 0; StringView breakDetails) = 0;
......
...@@ -891,7 +891,7 @@ Response InjectedScript::Scope::initialize() { ...@@ -891,7 +891,7 @@ Response InjectedScript::Scope::initialize() {
void InjectedScript::Scope::installCommandLineAPI() { void InjectedScript::Scope::installCommandLineAPI() {
DCHECK(m_injectedScript && !m_context.IsEmpty() && DCHECK(m_injectedScript && !m_context.IsEmpty() &&
!m_commandLineAPIScope.get()); !m_commandLineAPIScope.get());
m_commandLineAPIScope.reset(new V8Console::CommandLineAPIScope( m_commandLineAPIScope.reset(new V8CommandLineAPIScope(
m_context, m_injectedScript->commandLineAPI(), m_context->Global())); m_context, m_injectedScript->commandLineAPI(), m_context->Global()));
} }
......
...@@ -164,7 +164,7 @@ class InjectedScript final { ...@@ -164,7 +164,7 @@ class InjectedScript final {
v8::HandleScope m_handleScope; v8::HandleScope m_handleScope;
v8::TryCatch m_tryCatch; v8::TryCatch m_tryCatch;
v8::Local<v8::Context> m_context; v8::Local<v8::Context> m_context;
std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope; std::unique_ptr<V8CommandLineAPIScope> m_commandLineAPIScope;
bool m_ignoreExceptionsAndMuteConsole; bool m_ignoreExceptionsAndMuteConsole;
v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState; v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
bool m_userGesture; bool m_userGesture;
......
...@@ -783,9 +783,9 @@ static bool isCommandLineAPIGetter(const String16& name) { ...@@ -783,9 +783,9 @@ static bool isCommandLineAPIGetter(const String16& name) {
((name[1] >= '0' && name[1] <= '4') || name[1] == '_'); ((name[1] >= '0' && name[1] <= '4') || name[1] == '_');
} }
void V8Console::CommandLineAPIScope::accessorGetterCallback( void V8CommandLineAPIScope::accessorGetterCallback(
v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) { v8::Local<v8::Name> name, const v8::PropertyCallbackInfo<v8::Value>& info) {
CommandLineAPIScope* scope = *static_cast<CommandLineAPIScope**>( V8CommandLineAPIScope* scope = *static_cast<V8CommandLineAPIScope**>(
info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data()); info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data());
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
if (scope == nullptr) { if (scope == nullptr) {
...@@ -810,10 +810,10 @@ void V8Console::CommandLineAPIScope::accessorGetterCallback( ...@@ -810,10 +810,10 @@ void V8Console::CommandLineAPIScope::accessorGetterCallback(
} }
} }
void V8Console::CommandLineAPIScope::accessorSetterCallback( void V8CommandLineAPIScope::accessorSetterCallback(
v8::Local<v8::Name> name, v8::Local<v8::Value> value, v8::Local<v8::Name> name, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) { const v8::PropertyCallbackInfo<void>& info) {
CommandLineAPIScope* scope = *static_cast<CommandLineAPIScope**>( V8CommandLineAPIScope* scope = *static_cast<V8CommandLineAPIScope**>(
info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data()); info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data());
if (scope == nullptr) return; if (scope == nullptr) return;
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
...@@ -823,7 +823,7 @@ void V8Console::CommandLineAPIScope::accessorSetterCallback( ...@@ -823,7 +823,7 @@ void V8Console::CommandLineAPIScope::accessorSetterCallback(
USE(scope->m_installedMethods->Delete(context, name).FromMaybe(false)); USE(scope->m_installedMethods->Delete(context, name).FromMaybe(false));
} }
V8Console::CommandLineAPIScope::CommandLineAPIScope( V8CommandLineAPIScope::V8CommandLineAPIScope(
v8::Local<v8::Context> context, v8::Local<v8::Object> commandLineAPI, v8::Local<v8::Context> context, v8::Local<v8::Object> commandLineAPI,
v8::Local<v8::Object> global) v8::Local<v8::Object> global)
: m_context(context), : m_context(context),
...@@ -834,9 +834,9 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope( ...@@ -834,9 +834,9 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope(
v8::MicrotasksScope::kDoNotRunMicrotasks); v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Array> names; v8::Local<v8::Array> names;
if (!m_commandLineAPI->GetOwnPropertyNames(context).ToLocal(&names)) return; if (!m_commandLineAPI->GetOwnPropertyNames(context).ToLocal(&names)) return;
m_thisReference = m_thisReference = v8::ArrayBuffer::New(context->GetIsolate(),
v8::ArrayBuffer::New(context->GetIsolate(), sizeof(CommandLineAPIScope*)); sizeof(V8CommandLineAPIScope*));
*static_cast<CommandLineAPIScope**>( *static_cast<V8CommandLineAPIScope**>(
m_thisReference->GetBackingStore()->Data()) = this; m_thisReference->GetBackingStore()->Data()) = this;
for (uint32_t i = 0; i < names->Length(); ++i) { for (uint32_t i = 0; i < names->Length(); ++i) {
v8::Local<v8::Value> name; v8::Local<v8::Value> name;
...@@ -846,8 +846,8 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope( ...@@ -846,8 +846,8 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope(
continue; continue;
if (!m_global if (!m_global
->SetAccessor(context, name.As<v8::Name>(), ->SetAccessor(context, name.As<v8::Name>(),
CommandLineAPIScope::accessorGetterCallback, V8CommandLineAPIScope::accessorGetterCallback,
CommandLineAPIScope::accessorSetterCallback, V8CommandLineAPIScope::accessorSetterCallback,
m_thisReference, v8::DEFAULT, v8::DontEnum, m_thisReference, v8::DEFAULT, v8::DontEnum,
v8::SideEffectType::kHasNoSideEffect) v8::SideEffectType::kHasNoSideEffect)
.FromMaybe(false)) { .FromMaybe(false)) {
...@@ -859,10 +859,10 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope( ...@@ -859,10 +859,10 @@ V8Console::CommandLineAPIScope::CommandLineAPIScope(
} }
} }
V8Console::CommandLineAPIScope::~CommandLineAPIScope() { V8CommandLineAPIScope::~V8CommandLineAPIScope() {
v8::MicrotasksScope microtasksScope(m_context->GetIsolate(), v8::MicrotasksScope microtasksScope(m_context->GetIsolate(),
v8::MicrotasksScope::kDoNotRunMicrotasks); v8::MicrotasksScope::kDoNotRunMicrotasks);
*static_cast<CommandLineAPIScope**>( *static_cast<V8CommandLineAPIScope**>(
m_thisReference->GetBackingStore()->Data()) = nullptr; m_thisReference->GetBackingStore()->Data()) = nullptr;
v8::Local<v8::Array> names = m_installedMethods->AsArray(); v8::Local<v8::Array> names = m_installedMethods->AsArray();
for (uint32_t i = 0; i < names->Length(); ++i) { for (uint32_t i = 0; i < names->Length(); ++i) {
......
...@@ -24,29 +24,6 @@ class V8Console : public v8::debug::ConsoleDelegate { ...@@ -24,29 +24,6 @@ class V8Console : public v8::debug::ConsoleDelegate {
void installMemoryGetter(v8::Local<v8::Context> context, void installMemoryGetter(v8::Local<v8::Context> context,
v8::Local<v8::Object> console); v8::Local<v8::Object> console);
class V8_NODISCARD CommandLineAPIScope {
public:
CommandLineAPIScope(v8::Local<v8::Context>,
v8::Local<v8::Object> commandLineAPI,
v8::Local<v8::Object> global);
~CommandLineAPIScope();
CommandLineAPIScope(const CommandLineAPIScope&) = delete;
CommandLineAPIScope& operator=(const CommandLineAPIScope&) = delete;
private:
static void accessorGetterCallback(
v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value>&);
static void accessorSetterCallback(v8::Local<v8::Name>,
v8::Local<v8::Value>,
const v8::PropertyCallbackInfo<void>&);
v8::Local<v8::Context> m_context;
v8::Local<v8::Object> m_commandLineAPI;
v8::Local<v8::Object> m_global;
v8::Local<v8::Set> m_installedMethods;
v8::Local<v8::ArrayBuffer> m_thisReference;
};
explicit V8Console(V8InspectorImpl* inspector); explicit V8Console(V8InspectorImpl* inspector);
private: private:
......
...@@ -155,6 +155,11 @@ V8InspectorSessionImpl::~V8InspectorSessionImpl() { ...@@ -155,6 +155,11 @@ V8InspectorSessionImpl::~V8InspectorSessionImpl() {
m_inspector->disconnect(this); m_inspector->disconnect(this);
} }
v8::Local<v8::Object> V8InspectorSessionImpl::createCommandLineAPI(
v8::Local<v8::Context> context) {
return inspector()->console()->createCommandLineAPI(context, sessionId());
}
protocol::DictionaryValue* V8InspectorSessionImpl::agentState( protocol::DictionaryValue* V8InspectorSessionImpl::agentState(
const String16& name) { const String16& name) {
protocol::DictionaryValue* state = m_state->getObject(name); protocol::DictionaryValue* state = m_state->getObject(name);
......
...@@ -50,6 +50,9 @@ class V8InspectorSessionImpl : public V8InspectorSession, ...@@ -50,6 +50,9 @@ class V8InspectorSessionImpl : public V8InspectorSession,
int contextGroupId() const { return m_contextGroupId; } int contextGroupId() const { return m_contextGroupId; }
int sessionId() const { return m_sessionId; } int sessionId() const { return m_sessionId; }
v8::Local<v8::Object> createCommandLineAPI(
v8::Local<v8::Context> context) override;
Response findInjectedScript(int contextId, InjectedScript*&); Response findInjectedScript(int contextId, InjectedScript*&);
Response findInjectedScript(RemoteObjectIdBase*, InjectedScript*&); Response findInjectedScript(RemoteObjectIdBase*, InjectedScript*&);
void reset(); void reset();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment