From 7d5e5f6c62c3f38acee12dc4114c022441e7d36f Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Thu, 29 Apr 2021 13:16:15 +0000 Subject: [PATCH] Implement V8InspectorSession::initializeCommandLineAPIScope In https://crrev.com/c/2842128, a concern was raised that using `V8InspectorSession::createCommandLineAPI` directly would not cache the command line API definitions and V8 could expose a higher level API for this use case. This CL exposes `InjectedScript::ContextScope` via V8InspectorSession. If this approach is approved, V8InspectorSession::createCommandLineAPI could be removed. Example usage: https://crrev.com/c/2858964 Bug: chromium:1200705 Change-Id: Ib9fcadcb9bbd75c08f0122b0e4ee61e2874c4f8a Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2857640 Commit-Queue: Alex Rudenko Reviewed-by: Yang Guo Cr-Commit-Position: refs/heads/master@{#74289} --- include/v8-inspector.h | 6 ++++++ src/inspector/injected-script.h | 3 ++- src/inspector/v8-inspector-session-impl.cc | 14 ++++++++++++++ src/inspector/v8-inspector-session-impl.h | 3 +++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/v8-inspector.h b/include/v8-inspector.h index 2c30401473..be2c20bbcf 100644 --- a/include/v8-inspector.h +++ b/include/v8-inspector.h @@ -152,6 +152,10 @@ class V8_EXPORT V8InspectorSession { virtual v8::Local get(v8::Local) = 0; virtual ~Inspectable() = default; }; + class V8_EXPORT CommandLineAPIScope { + public: + virtual ~CommandLineAPIScope() = default; + }; virtual void addInspectedObject(std::unique_ptr) = 0; // Dispatching protocol messages. @@ -163,6 +167,8 @@ class V8_EXPORT V8InspectorSession { virtual v8::Local createCommandLineAPI( v8::Local context) = 0; + virtual std::unique_ptr + initializeCommandLineAPIScope(int executionContextId) = 0; // Debugger actions. virtual void schedulePauseOnNextStatement(StringView breakReason, diff --git a/src/inspector/injected-script.h b/src/inspector/injected-script.h index 437fc69455..7f8f41e260 100644 --- a/src/inspector/injected-script.h +++ b/src/inspector/injected-script.h @@ -173,7 +173,8 @@ class InjectedScript final { int m_sessionId; }; - class ContextScope : public Scope { + class ContextScope : public Scope, + public V8InspectorSession::CommandLineAPIScope { public: ContextScope(V8InspectorSessionImpl*, int executionContextId); ~ContextScope() override; diff --git a/src/inspector/v8-inspector-session-impl.cc b/src/inspector/v8-inspector-session-impl.cc index ba3161e602..ae88d26b2f 100644 --- a/src/inspector/v8-inspector-session-impl.cc +++ b/src/inspector/v8-inspector-session-impl.cc @@ -155,6 +155,20 @@ V8InspectorSessionImpl::~V8InspectorSessionImpl() { m_inspector->disconnect(this); } +std::unique_ptr +V8InspectorSessionImpl::initializeCommandLineAPIScope(int executionContextId) { + auto scope = + std::make_unique(this, executionContextId); + auto result = scope->initialize(); + if (!result.IsSuccess()) { + return nullptr; + } + + scope->installCommandLineAPI(); + + return scope; +} + v8::Local V8InspectorSessionImpl::createCommandLineAPI( v8::Local context) { return inspector()->console()->createCommandLineAPI(context, sessionId()); diff --git a/src/inspector/v8-inspector-session-impl.h b/src/inspector/v8-inspector-session-impl.h index af67759b3d..5e3b79cf07 100644 --- a/src/inspector/v8-inspector-session-impl.h +++ b/src/inspector/v8-inspector-session-impl.h @@ -53,6 +53,9 @@ class V8InspectorSessionImpl : public V8InspectorSession, v8::Local createCommandLineAPI( v8::Local context) override; + std::unique_ptr + initializeCommandLineAPIScope(int executionContextId) override; + Response findInjectedScript(int contextId, InjectedScript*&); Response findInjectedScript(RemoteObjectIdBase*, InjectedScript*&); void reset();