11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/inspector/v8-inspector-session-impl.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "../../third_party/inspector_protocol/crdtp/cbor.h"
81cb0ef41Sopenharmony_ci#include "../../third_party/inspector_protocol/crdtp/dispatch.h"
91cb0ef41Sopenharmony_ci#include "../../third_party/inspector_protocol/crdtp/json.h"
101cb0ef41Sopenharmony_ci#include "src/base/logging.h"
111cb0ef41Sopenharmony_ci#include "src/base/macros.h"
121cb0ef41Sopenharmony_ci#include "src/inspector/injected-script.h"
131cb0ef41Sopenharmony_ci#include "src/inspector/inspected-context.h"
141cb0ef41Sopenharmony_ci#include "src/inspector/protocol/Protocol.h"
151cb0ef41Sopenharmony_ci#include "src/inspector/remote-object-id.h"
161cb0ef41Sopenharmony_ci#include "src/inspector/search-util.h"
171cb0ef41Sopenharmony_ci#include "src/inspector/string-util.h"
181cb0ef41Sopenharmony_ci#include "src/inspector/v8-console-agent-impl.h"
191cb0ef41Sopenharmony_ci#include "src/inspector/v8-debugger-agent-impl.h"
201cb0ef41Sopenharmony_ci#include "src/inspector/v8-debugger.h"
211cb0ef41Sopenharmony_ci#include "src/inspector/v8-heap-profiler-agent-impl.h"
221cb0ef41Sopenharmony_ci#include "src/inspector/v8-inspector-impl.h"
231cb0ef41Sopenharmony_ci#include "src/inspector/v8-profiler-agent-impl.h"
241cb0ef41Sopenharmony_ci#include "src/inspector/v8-runtime-agent-impl.h"
251cb0ef41Sopenharmony_ci#include "src/inspector/v8-schema-agent-impl.h"
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_cinamespace v8_inspector {
281cb0ef41Sopenharmony_cinamespace {
291cb0ef41Sopenharmony_ciusing v8_crdtp::span;
301cb0ef41Sopenharmony_ciusing v8_crdtp::SpanFrom;
311cb0ef41Sopenharmony_ciusing v8_crdtp::Status;
321cb0ef41Sopenharmony_ciusing v8_crdtp::cbor::CheckCBORMessage;
331cb0ef41Sopenharmony_ciusing v8_crdtp::json::ConvertCBORToJSON;
341cb0ef41Sopenharmony_ciusing v8_crdtp::json::ConvertJSONToCBOR;
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_cibool IsCBORMessage(StringView msg) {
371cb0ef41Sopenharmony_ci  return msg.is8Bit() && msg.length() >= 2 && msg.characters8()[0] == 0xd8 &&
381cb0ef41Sopenharmony_ci         msg.characters8()[1] == 0x5a;
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ciStatus ConvertToCBOR(StringView state, std::vector<uint8_t>* cbor) {
421cb0ef41Sopenharmony_ci  return state.is8Bit()
431cb0ef41Sopenharmony_ci             ? ConvertJSONToCBOR(
441cb0ef41Sopenharmony_ci                   span<uint8_t>(state.characters8(), state.length()), cbor)
451cb0ef41Sopenharmony_ci             : ConvertJSONToCBOR(
461cb0ef41Sopenharmony_ci                   span<uint16_t>(state.characters16(), state.length()), cbor);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cistd::unique_ptr<protocol::DictionaryValue> ParseState(StringView state) {
501cb0ef41Sopenharmony_ci  std::vector<uint8_t> converted;
511cb0ef41Sopenharmony_ci  span<uint8_t> cbor;
521cb0ef41Sopenharmony_ci  if (IsCBORMessage(state))
531cb0ef41Sopenharmony_ci    cbor = span<uint8_t>(state.characters8(), state.length());
541cb0ef41Sopenharmony_ci  else if (ConvertToCBOR(state, &converted).ok())
551cb0ef41Sopenharmony_ci    cbor = SpanFrom(converted);
561cb0ef41Sopenharmony_ci  if (!cbor.empty()) {
571cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::Value> value =
581cb0ef41Sopenharmony_ci        protocol::Value::parseBinary(cbor.data(), cbor.size());
591cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::DictionaryValue> dictionaryValue =
601cb0ef41Sopenharmony_ci        protocol::DictionaryValue::cast(std::move(value));
611cb0ef41Sopenharmony_ci    if (dictionaryValue) return dictionaryValue;
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci  return protocol::DictionaryValue::create();
641cb0ef41Sopenharmony_ci}
651cb0ef41Sopenharmony_ci}  // namespace
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// static
681cb0ef41Sopenharmony_cibool V8InspectorSession::canDispatchMethod(StringView method) {
691cb0ef41Sopenharmony_ci  return stringViewStartsWith(method,
701cb0ef41Sopenharmony_ci                              protocol::Runtime::Metainfo::commandPrefix) ||
711cb0ef41Sopenharmony_ci         stringViewStartsWith(method,
721cb0ef41Sopenharmony_ci                              protocol::Debugger::Metainfo::commandPrefix) ||
731cb0ef41Sopenharmony_ci         stringViewStartsWith(method,
741cb0ef41Sopenharmony_ci                              protocol::Profiler::Metainfo::commandPrefix) ||
751cb0ef41Sopenharmony_ci         stringViewStartsWith(
761cb0ef41Sopenharmony_ci             method, protocol::HeapProfiler::Metainfo::commandPrefix) ||
771cb0ef41Sopenharmony_ci         stringViewStartsWith(method,
781cb0ef41Sopenharmony_ci                              protocol::Console::Metainfo::commandPrefix) ||
791cb0ef41Sopenharmony_ci         stringViewStartsWith(method,
801cb0ef41Sopenharmony_ci                              protocol::Schema::Metainfo::commandPrefix);
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci// static
841cb0ef41Sopenharmony_ciint V8ContextInfo::executionContextId(v8::Local<v8::Context> context) {
851cb0ef41Sopenharmony_ci  return InspectedContext::contextId(context);
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_cistd::unique_ptr<V8InspectorSessionImpl> V8InspectorSessionImpl::create(
891cb0ef41Sopenharmony_ci    V8InspectorImpl* inspector, int contextGroupId, int sessionId,
901cb0ef41Sopenharmony_ci    V8Inspector::Channel* channel, StringView state) {
911cb0ef41Sopenharmony_ci  return std::unique_ptr<V8InspectorSessionImpl>(new V8InspectorSessionImpl(
921cb0ef41Sopenharmony_ci      inspector, contextGroupId, sessionId, channel, state));
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciV8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector,
961cb0ef41Sopenharmony_ci                                               int contextGroupId,
971cb0ef41Sopenharmony_ci                                               int sessionId,
981cb0ef41Sopenharmony_ci                                               V8Inspector::Channel* channel,
991cb0ef41Sopenharmony_ci                                               StringView savedState)
1001cb0ef41Sopenharmony_ci    : m_contextGroupId(contextGroupId),
1011cb0ef41Sopenharmony_ci      m_sessionId(sessionId),
1021cb0ef41Sopenharmony_ci      m_inspector(inspector),
1031cb0ef41Sopenharmony_ci      m_channel(channel),
1041cb0ef41Sopenharmony_ci      m_customObjectFormatterEnabled(false),
1051cb0ef41Sopenharmony_ci      m_dispatcher(this),
1061cb0ef41Sopenharmony_ci      m_state(ParseState(savedState)),
1071cb0ef41Sopenharmony_ci      m_runtimeAgent(nullptr),
1081cb0ef41Sopenharmony_ci      m_debuggerAgent(nullptr),
1091cb0ef41Sopenharmony_ci      m_heapProfilerAgent(nullptr),
1101cb0ef41Sopenharmony_ci      m_profilerAgent(nullptr),
1111cb0ef41Sopenharmony_ci      m_consoleAgent(nullptr),
1121cb0ef41Sopenharmony_ci      m_schemaAgent(nullptr) {
1131cb0ef41Sopenharmony_ci  m_state->getBoolean("use_binary_protocol", &use_binary_protocol_);
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  m_runtimeAgent.reset(new V8RuntimeAgentImpl(
1161cb0ef41Sopenharmony_ci      this, this, agentState(protocol::Runtime::Metainfo::domainName)));
1171cb0ef41Sopenharmony_ci  protocol::Runtime::Dispatcher::wire(&m_dispatcher, m_runtimeAgent.get());
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  m_debuggerAgent.reset(new V8DebuggerAgentImpl(
1201cb0ef41Sopenharmony_ci      this, this, agentState(protocol::Debugger::Metainfo::domainName)));
1211cb0ef41Sopenharmony_ci  protocol::Debugger::Dispatcher::wire(&m_dispatcher, m_debuggerAgent.get());
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  m_profilerAgent.reset(new V8ProfilerAgentImpl(
1241cb0ef41Sopenharmony_ci      this, this, agentState(protocol::Profiler::Metainfo::domainName)));
1251cb0ef41Sopenharmony_ci  protocol::Profiler::Dispatcher::wire(&m_dispatcher, m_profilerAgent.get());
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  m_heapProfilerAgent.reset(new V8HeapProfilerAgentImpl(
1281cb0ef41Sopenharmony_ci      this, this, agentState(protocol::HeapProfiler::Metainfo::domainName)));
1291cb0ef41Sopenharmony_ci  protocol::HeapProfiler::Dispatcher::wire(&m_dispatcher,
1301cb0ef41Sopenharmony_ci                                           m_heapProfilerAgent.get());
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  m_consoleAgent.reset(new V8ConsoleAgentImpl(
1331cb0ef41Sopenharmony_ci      this, this, agentState(protocol::Console::Metainfo::domainName)));
1341cb0ef41Sopenharmony_ci  protocol::Console::Dispatcher::wire(&m_dispatcher, m_consoleAgent.get());
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  m_schemaAgent.reset(new V8SchemaAgentImpl(
1371cb0ef41Sopenharmony_ci      this, this, agentState(protocol::Schema::Metainfo::domainName)));
1381cb0ef41Sopenharmony_ci  protocol::Schema::Dispatcher::wire(&m_dispatcher, m_schemaAgent.get());
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  if (savedState.length()) {
1411cb0ef41Sopenharmony_ci    m_runtimeAgent->restore();
1421cb0ef41Sopenharmony_ci    m_debuggerAgent->restore();
1431cb0ef41Sopenharmony_ci    m_heapProfilerAgent->restore();
1441cb0ef41Sopenharmony_ci    m_profilerAgent->restore();
1451cb0ef41Sopenharmony_ci    m_consoleAgent->restore();
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci}
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciV8InspectorSessionImpl::~V8InspectorSessionImpl() {
1501cb0ef41Sopenharmony_ci  v8::Isolate::Scope scope(m_inspector->isolate());
1511cb0ef41Sopenharmony_ci  discardInjectedScripts();
1521cb0ef41Sopenharmony_ci  m_consoleAgent->disable();
1531cb0ef41Sopenharmony_ci  m_profilerAgent->disable();
1541cb0ef41Sopenharmony_ci  m_heapProfilerAgent->disable();
1551cb0ef41Sopenharmony_ci  m_debuggerAgent->disable();
1561cb0ef41Sopenharmony_ci  m_runtimeAgent->disable();
1571cb0ef41Sopenharmony_ci  m_inspector->disconnect(this);
1581cb0ef41Sopenharmony_ci}
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_cistd::unique_ptr<V8InspectorSession::CommandLineAPIScope>
1611cb0ef41Sopenharmony_ciV8InspectorSessionImpl::initializeCommandLineAPIScope(int executionContextId) {
1621cb0ef41Sopenharmony_ci  auto scope =
1631cb0ef41Sopenharmony_ci      std::make_unique<InjectedScript::ContextScope>(this, executionContextId);
1641cb0ef41Sopenharmony_ci  auto result = scope->initialize();
1651cb0ef41Sopenharmony_ci  if (!result.IsSuccess()) {
1661cb0ef41Sopenharmony_ci    return nullptr;
1671cb0ef41Sopenharmony_ci  }
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  scope->installCommandLineAPI();
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  return scope;
1721cb0ef41Sopenharmony_ci}
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ciprotocol::DictionaryValue* V8InspectorSessionImpl::agentState(
1751cb0ef41Sopenharmony_ci    const String16& name) {
1761cb0ef41Sopenharmony_ci  protocol::DictionaryValue* state = m_state->getObject(name);
1771cb0ef41Sopenharmony_ci  if (!state) {
1781cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::DictionaryValue> newState =
1791cb0ef41Sopenharmony_ci        protocol::DictionaryValue::create();
1801cb0ef41Sopenharmony_ci    state = newState.get();
1811cb0ef41Sopenharmony_ci    m_state->setObject(name, std::move(newState));
1821cb0ef41Sopenharmony_ci  }
1831cb0ef41Sopenharmony_ci  return state;
1841cb0ef41Sopenharmony_ci}
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_cistd::unique_ptr<StringBuffer> V8InspectorSessionImpl::serializeForFrontend(
1871cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::Serializable> message) {
1881cb0ef41Sopenharmony_ci  std::vector<uint8_t> cbor = message->Serialize();
1891cb0ef41Sopenharmony_ci  DCHECK(CheckCBORMessage(SpanFrom(cbor)).ok());
1901cb0ef41Sopenharmony_ci  if (use_binary_protocol_) return StringBufferFrom(std::move(cbor));
1911cb0ef41Sopenharmony_ci  std::vector<uint8_t> json;
1921cb0ef41Sopenharmony_ci  Status status = ConvertCBORToJSON(SpanFrom(cbor), &json);
1931cb0ef41Sopenharmony_ci  DCHECK(status.ok());
1941cb0ef41Sopenharmony_ci  USE(status);
1951cb0ef41Sopenharmony_ci  // TODO(johannes): It should be OK to make a StringBuffer from |json|
1961cb0ef41Sopenharmony_ci  // directly, since it's 7 Bit US-ASCII with anything else escaped.
1971cb0ef41Sopenharmony_ci  // However it appears that the Node.js tests (or perhaps even production)
1981cb0ef41Sopenharmony_ci  // assume that the StringBuffer is 16 Bit. It probably accesses
1991cb0ef41Sopenharmony_ci  // characters16() somehwere without checking is8Bit. Until it's fixed
2001cb0ef41Sopenharmony_ci  // we take a detour via String16 which makes the StringBuffer 16 bit.
2011cb0ef41Sopenharmony_ci  String16 string16(reinterpret_cast<const char*>(json.data()), json.size());
2021cb0ef41Sopenharmony_ci  return StringBufferFrom(std::move(string16));
2031cb0ef41Sopenharmony_ci}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::SendProtocolResponse(
2061cb0ef41Sopenharmony_ci    int callId, std::unique_ptr<protocol::Serializable> message) {
2071cb0ef41Sopenharmony_ci  m_channel->sendResponse(callId, serializeForFrontend(std::move(message)));
2081cb0ef41Sopenharmony_ci}
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::SendProtocolNotification(
2111cb0ef41Sopenharmony_ci    std::unique_ptr<protocol::Serializable> message) {
2121cb0ef41Sopenharmony_ci  m_channel->sendNotification(serializeForFrontend(std::move(message)));
2131cb0ef41Sopenharmony_ci}
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::FallThrough(int callId,
2161cb0ef41Sopenharmony_ci                                         const v8_crdtp::span<uint8_t> method,
2171cb0ef41Sopenharmony_ci                                         v8_crdtp::span<uint8_t> message) {
2181cb0ef41Sopenharmony_ci  // There's no other layer to handle the command.
2191cb0ef41Sopenharmony_ci  UNREACHABLE();
2201cb0ef41Sopenharmony_ci}
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::FlushProtocolNotifications() {
2231cb0ef41Sopenharmony_ci  m_channel->flushProtocolNotifications();
2241cb0ef41Sopenharmony_ci}
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::reset() {
2271cb0ef41Sopenharmony_ci  m_debuggerAgent->reset();
2281cb0ef41Sopenharmony_ci  m_runtimeAgent->reset();
2291cb0ef41Sopenharmony_ci  discardInjectedScripts();
2301cb0ef41Sopenharmony_ci}
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::discardInjectedScripts() {
2331cb0ef41Sopenharmony_ci  m_inspectedObjects.clear();
2341cb0ef41Sopenharmony_ci  int sessionId = m_sessionId;
2351cb0ef41Sopenharmony_ci  m_inspector->forEachContext(m_contextGroupId,
2361cb0ef41Sopenharmony_ci                              [&sessionId](InspectedContext* context) {
2371cb0ef41Sopenharmony_ci                                context->discardInjectedScript(sessionId);
2381cb0ef41Sopenharmony_ci                              });
2391cb0ef41Sopenharmony_ci}
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ciResponse V8InspectorSessionImpl::findInjectedScript(
2421cb0ef41Sopenharmony_ci    int contextId, InjectedScript*& injectedScript) {
2431cb0ef41Sopenharmony_ci  injectedScript = nullptr;
2441cb0ef41Sopenharmony_ci  InspectedContext* context =
2451cb0ef41Sopenharmony_ci      m_inspector->getContext(m_contextGroupId, contextId);
2461cb0ef41Sopenharmony_ci  if (!context)
2471cb0ef41Sopenharmony_ci    return Response::ServerError("Cannot find context with specified id");
2481cb0ef41Sopenharmony_ci  injectedScript = context->getInjectedScript(m_sessionId);
2491cb0ef41Sopenharmony_ci  if (!injectedScript) {
2501cb0ef41Sopenharmony_ci    injectedScript = context->createInjectedScript(m_sessionId);
2511cb0ef41Sopenharmony_ci    if (m_customObjectFormatterEnabled)
2521cb0ef41Sopenharmony_ci      injectedScript->setCustomObjectFormatterEnabled(true);
2531cb0ef41Sopenharmony_ci  }
2541cb0ef41Sopenharmony_ci  return Response::Success();
2551cb0ef41Sopenharmony_ci}
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_ciResponse V8InspectorSessionImpl::findInjectedScript(
2581cb0ef41Sopenharmony_ci    RemoteObjectIdBase* objectId, InjectedScript*& injectedScript) {
2591cb0ef41Sopenharmony_ci  if (objectId->isolateId() != m_inspector->isolateId())
2601cb0ef41Sopenharmony_ci    return Response::ServerError("Cannot find context with specified id");
2611cb0ef41Sopenharmony_ci  return findInjectedScript(objectId->contextId(), injectedScript);
2621cb0ef41Sopenharmony_ci}
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::releaseObjectGroup(StringView objectGroup) {
2651cb0ef41Sopenharmony_ci  releaseObjectGroup(toString16(objectGroup));
2661cb0ef41Sopenharmony_ci}
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::releaseObjectGroup(const String16& objectGroup) {
2691cb0ef41Sopenharmony_ci  int sessionId = m_sessionId;
2701cb0ef41Sopenharmony_ci  m_inspector->forEachContext(
2711cb0ef41Sopenharmony_ci      m_contextGroupId, [&objectGroup, &sessionId](InspectedContext* context) {
2721cb0ef41Sopenharmony_ci        InjectedScript* injectedScript = context->getInjectedScript(sessionId);
2731cb0ef41Sopenharmony_ci        if (injectedScript) injectedScript->releaseObjectGroup(objectGroup);
2741cb0ef41Sopenharmony_ci      });
2751cb0ef41Sopenharmony_ci}
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_cibool V8InspectorSessionImpl::unwrapObject(
2781cb0ef41Sopenharmony_ci    std::unique_ptr<StringBuffer>* error, StringView objectId,
2791cb0ef41Sopenharmony_ci    v8::Local<v8::Value>* object, v8::Local<v8::Context>* context,
2801cb0ef41Sopenharmony_ci    std::unique_ptr<StringBuffer>* objectGroup) {
2811cb0ef41Sopenharmony_ci  String16 objectGroupString;
2821cb0ef41Sopenharmony_ci  Response response = unwrapObject(toString16(objectId), object, context,
2831cb0ef41Sopenharmony_ci                                   objectGroup ? &objectGroupString : nullptr);
2841cb0ef41Sopenharmony_ci  if (response.IsError()) {
2851cb0ef41Sopenharmony_ci    if (error) {
2861cb0ef41Sopenharmony_ci      const std::string& msg = response.Message();
2871cb0ef41Sopenharmony_ci      *error = StringBufferFrom(String16::fromUTF8(msg.data(), msg.size()));
2881cb0ef41Sopenharmony_ci    }
2891cb0ef41Sopenharmony_ci    return false;
2901cb0ef41Sopenharmony_ci  }
2911cb0ef41Sopenharmony_ci  if (objectGroup)
2921cb0ef41Sopenharmony_ci    *objectGroup = StringBufferFrom(std::move(objectGroupString));
2931cb0ef41Sopenharmony_ci  return true;
2941cb0ef41Sopenharmony_ci}
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ciResponse V8InspectorSessionImpl::unwrapObject(const String16& objectId,
2971cb0ef41Sopenharmony_ci                                              v8::Local<v8::Value>* object,
2981cb0ef41Sopenharmony_ci                                              v8::Local<v8::Context>* context,
2991cb0ef41Sopenharmony_ci                                              String16* objectGroup) {
3001cb0ef41Sopenharmony_ci  std::unique_ptr<RemoteObjectId> remoteId;
3011cb0ef41Sopenharmony_ci  Response response = RemoteObjectId::parse(objectId, &remoteId);
3021cb0ef41Sopenharmony_ci  if (!response.IsSuccess()) return response;
3031cb0ef41Sopenharmony_ci  InjectedScript* injectedScript = nullptr;
3041cb0ef41Sopenharmony_ci  response = findInjectedScript(remoteId.get(), injectedScript);
3051cb0ef41Sopenharmony_ci  if (!response.IsSuccess()) return response;
3061cb0ef41Sopenharmony_ci  response = injectedScript->findObject(*remoteId, object);
3071cb0ef41Sopenharmony_ci  if (!response.IsSuccess()) return response;
3081cb0ef41Sopenharmony_ci  *context = injectedScript->context()->context();
3091cb0ef41Sopenharmony_ci  if (objectGroup) *objectGroup = injectedScript->objectGroupName(*remoteId);
3101cb0ef41Sopenharmony_ci  return Response::Success();
3111cb0ef41Sopenharmony_ci}
3121cb0ef41Sopenharmony_ci
3131cb0ef41Sopenharmony_cistd::unique_ptr<protocol::Runtime::API::RemoteObject>
3141cb0ef41Sopenharmony_ciV8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context,
3151cb0ef41Sopenharmony_ci                                   v8::Local<v8::Value> value,
3161cb0ef41Sopenharmony_ci                                   StringView groupName, bool generatePreview) {
3171cb0ef41Sopenharmony_ci  return wrapObject(context, value, toString16(groupName), generatePreview);
3181cb0ef41Sopenharmony_ci}
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_cistd::unique_ptr<protocol::Runtime::RemoteObject>
3211cb0ef41Sopenharmony_ciV8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context,
3221cb0ef41Sopenharmony_ci                                   v8::Local<v8::Value> value,
3231cb0ef41Sopenharmony_ci                                   const String16& groupName,
3241cb0ef41Sopenharmony_ci                                   bool generatePreview) {
3251cb0ef41Sopenharmony_ci  InjectedScript* injectedScript = nullptr;
3261cb0ef41Sopenharmony_ci  findInjectedScript(InspectedContext::contextId(context), injectedScript);
3271cb0ef41Sopenharmony_ci  if (!injectedScript) return nullptr;
3281cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::RemoteObject> result;
3291cb0ef41Sopenharmony_ci  injectedScript->wrapObject(
3301cb0ef41Sopenharmony_ci      value, groupName,
3311cb0ef41Sopenharmony_ci      generatePreview ? WrapMode::kWithPreview : WrapMode::kNoPreview, &result);
3321cb0ef41Sopenharmony_ci  return result;
3331cb0ef41Sopenharmony_ci}
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_cistd::unique_ptr<protocol::Runtime::RemoteObject>
3361cb0ef41Sopenharmony_ciV8InspectorSessionImpl::wrapTable(v8::Local<v8::Context> context,
3371cb0ef41Sopenharmony_ci                                  v8::Local<v8::Object> table,
3381cb0ef41Sopenharmony_ci                                  v8::MaybeLocal<v8::Array> columns) {
3391cb0ef41Sopenharmony_ci  InjectedScript* injectedScript = nullptr;
3401cb0ef41Sopenharmony_ci  findInjectedScript(InspectedContext::contextId(context), injectedScript);
3411cb0ef41Sopenharmony_ci  if (!injectedScript) return nullptr;
3421cb0ef41Sopenharmony_ci  return injectedScript->wrapTable(table, columns);
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::setCustomObjectFormatterEnabled(bool enabled) {
3461cb0ef41Sopenharmony_ci  m_customObjectFormatterEnabled = enabled;
3471cb0ef41Sopenharmony_ci  int sessionId = m_sessionId;
3481cb0ef41Sopenharmony_ci  m_inspector->forEachContext(
3491cb0ef41Sopenharmony_ci      m_contextGroupId, [&enabled, &sessionId](InspectedContext* context) {
3501cb0ef41Sopenharmony_ci        InjectedScript* injectedScript = context->getInjectedScript(sessionId);
3511cb0ef41Sopenharmony_ci        if (injectedScript)
3521cb0ef41Sopenharmony_ci          injectedScript->setCustomObjectFormatterEnabled(enabled);
3531cb0ef41Sopenharmony_ci      });
3541cb0ef41Sopenharmony_ci}
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::reportAllContexts(V8RuntimeAgentImpl* agent) {
3571cb0ef41Sopenharmony_ci  m_inspector->forEachContext(m_contextGroupId,
3581cb0ef41Sopenharmony_ci                              [&agent](InspectedContext* context) {
3591cb0ef41Sopenharmony_ci                                agent->reportExecutionContextCreated(context);
3601cb0ef41Sopenharmony_ci                              });
3611cb0ef41Sopenharmony_ci}
3621cb0ef41Sopenharmony_ci
3631cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::dispatchProtocolMessage(StringView message) {
3641cb0ef41Sopenharmony_ci  using v8_crdtp::span;
3651cb0ef41Sopenharmony_ci  using v8_crdtp::SpanFrom;
3661cb0ef41Sopenharmony_ci  span<uint8_t> cbor;
3671cb0ef41Sopenharmony_ci  std::vector<uint8_t> converted_cbor;
3681cb0ef41Sopenharmony_ci  if (IsCBORMessage(message)) {
3691cb0ef41Sopenharmony_ci    use_binary_protocol_ = true;
3701cb0ef41Sopenharmony_ci    m_state->setBoolean("use_binary_protocol", true);
3711cb0ef41Sopenharmony_ci    cbor = span<uint8_t>(message.characters8(), message.length());
3721cb0ef41Sopenharmony_ci  } else {
3731cb0ef41Sopenharmony_ci    // We're ignoring the return value of the conversion function
3741cb0ef41Sopenharmony_ci    // intentionally. It means the |parsed_message| below will be nullptr.
3751cb0ef41Sopenharmony_ci    auto status = ConvertToCBOR(message, &converted_cbor);
3761cb0ef41Sopenharmony_ci    if (!status.ok()) {
3771cb0ef41Sopenharmony_ci      m_channel->sendNotification(
3781cb0ef41Sopenharmony_ci          serializeForFrontend(v8_crdtp::CreateErrorNotification(
3791cb0ef41Sopenharmony_ci              v8_crdtp::DispatchResponse::ParseError(status.ToASCIIString()))));
3801cb0ef41Sopenharmony_ci      return;
3811cb0ef41Sopenharmony_ci    }
3821cb0ef41Sopenharmony_ci    cbor = SpanFrom(converted_cbor);
3831cb0ef41Sopenharmony_ci  }
3841cb0ef41Sopenharmony_ci  v8_crdtp::Dispatchable dispatchable(cbor);
3851cb0ef41Sopenharmony_ci  if (!dispatchable.ok()) {
3861cb0ef41Sopenharmony_ci    if (dispatchable.HasCallId()) {
3871cb0ef41Sopenharmony_ci      m_channel->sendNotification(serializeForFrontend(
3881cb0ef41Sopenharmony_ci          v8_crdtp::CreateErrorNotification(dispatchable.DispatchError())));
3891cb0ef41Sopenharmony_ci    } else {
3901cb0ef41Sopenharmony_ci      m_channel->sendResponse(
3911cb0ef41Sopenharmony_ci          dispatchable.CallId(),
3921cb0ef41Sopenharmony_ci          serializeForFrontend(v8_crdtp::CreateErrorResponse(
3931cb0ef41Sopenharmony_ci              dispatchable.CallId(), dispatchable.DispatchError())));
3941cb0ef41Sopenharmony_ci    }
3951cb0ef41Sopenharmony_ci    return;
3961cb0ef41Sopenharmony_ci  }
3971cb0ef41Sopenharmony_ci  m_dispatcher.Dispatch(dispatchable).Run();
3981cb0ef41Sopenharmony_ci}
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_cistd::vector<uint8_t> V8InspectorSessionImpl::state() {
4011cb0ef41Sopenharmony_ci  return m_state->Serialize();
4021cb0ef41Sopenharmony_ci}
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_cistd::vector<std::unique_ptr<protocol::Schema::API::Domain>>
4051cb0ef41Sopenharmony_ciV8InspectorSessionImpl::supportedDomains() {
4061cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<protocol::Schema::Domain>> domains =
4071cb0ef41Sopenharmony_ci      supportedDomainsImpl();
4081cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<protocol::Schema::API::Domain>> result;
4091cb0ef41Sopenharmony_ci  for (size_t i = 0; i < domains.size(); ++i)
4101cb0ef41Sopenharmony_ci    result.push_back(std::move(domains[i]));
4111cb0ef41Sopenharmony_ci  return result;
4121cb0ef41Sopenharmony_ci}
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_cistd::vector<std::unique_ptr<protocol::Schema::Domain>>
4151cb0ef41Sopenharmony_ciV8InspectorSessionImpl::supportedDomainsImpl() {
4161cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<protocol::Schema::Domain>> result;
4171cb0ef41Sopenharmony_ci  result.push_back(protocol::Schema::Domain::create()
4181cb0ef41Sopenharmony_ci                       .setName(protocol::Runtime::Metainfo::domainName)
4191cb0ef41Sopenharmony_ci                       .setVersion(protocol::Runtime::Metainfo::version)
4201cb0ef41Sopenharmony_ci                       .build());
4211cb0ef41Sopenharmony_ci  result.push_back(protocol::Schema::Domain::create()
4221cb0ef41Sopenharmony_ci                       .setName(protocol::Debugger::Metainfo::domainName)
4231cb0ef41Sopenharmony_ci                       .setVersion(protocol::Debugger::Metainfo::version)
4241cb0ef41Sopenharmony_ci                       .build());
4251cb0ef41Sopenharmony_ci  result.push_back(protocol::Schema::Domain::create()
4261cb0ef41Sopenharmony_ci                       .setName(protocol::Profiler::Metainfo::domainName)
4271cb0ef41Sopenharmony_ci                       .setVersion(protocol::Profiler::Metainfo::version)
4281cb0ef41Sopenharmony_ci                       .build());
4291cb0ef41Sopenharmony_ci  result.push_back(protocol::Schema::Domain::create()
4301cb0ef41Sopenharmony_ci                       .setName(protocol::HeapProfiler::Metainfo::domainName)
4311cb0ef41Sopenharmony_ci                       .setVersion(protocol::HeapProfiler::Metainfo::version)
4321cb0ef41Sopenharmony_ci                       .build());
4331cb0ef41Sopenharmony_ci  result.push_back(protocol::Schema::Domain::create()
4341cb0ef41Sopenharmony_ci                       .setName(protocol::Schema::Metainfo::domainName)
4351cb0ef41Sopenharmony_ci                       .setVersion(protocol::Schema::Metainfo::version)
4361cb0ef41Sopenharmony_ci                       .build());
4371cb0ef41Sopenharmony_ci  return result;
4381cb0ef41Sopenharmony_ci}
4391cb0ef41Sopenharmony_ci
4401cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::addInspectedObject(
4411cb0ef41Sopenharmony_ci    std::unique_ptr<V8InspectorSession::Inspectable> inspectable) {
4421cb0ef41Sopenharmony_ci  m_inspectedObjects.insert(m_inspectedObjects.begin(), std::move(inspectable));
4431cb0ef41Sopenharmony_ci  if (m_inspectedObjects.size() > kInspectedObjectBufferSize)
4441cb0ef41Sopenharmony_ci    m_inspectedObjects.resize(kInspectedObjectBufferSize);
4451cb0ef41Sopenharmony_ci}
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ciV8InspectorSession::Inspectable* V8InspectorSessionImpl::inspectedObject(
4481cb0ef41Sopenharmony_ci    unsigned num) {
4491cb0ef41Sopenharmony_ci  if (num >= m_inspectedObjects.size()) return nullptr;
4501cb0ef41Sopenharmony_ci  return m_inspectedObjects[num].get();
4511cb0ef41Sopenharmony_ci}
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::schedulePauseOnNextStatement(
4541cb0ef41Sopenharmony_ci    StringView breakReason, StringView breakDetails) {
4551cb0ef41Sopenharmony_ci  std::vector<uint8_t> cbor;
4561cb0ef41Sopenharmony_ci  ConvertToCBOR(breakDetails, &cbor);
4571cb0ef41Sopenharmony_ci  m_debuggerAgent->schedulePauseOnNextStatement(
4581cb0ef41Sopenharmony_ci      toString16(breakReason),
4591cb0ef41Sopenharmony_ci      protocol::DictionaryValue::cast(
4601cb0ef41Sopenharmony_ci          protocol::Value::parseBinary(cbor.data(), cbor.size())));
4611cb0ef41Sopenharmony_ci}
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::cancelPauseOnNextStatement() {
4641cb0ef41Sopenharmony_ci  m_debuggerAgent->cancelPauseOnNextStatement();
4651cb0ef41Sopenharmony_ci}
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::breakProgram(StringView breakReason,
4681cb0ef41Sopenharmony_ci                                          StringView breakDetails) {
4691cb0ef41Sopenharmony_ci  std::vector<uint8_t> cbor;
4701cb0ef41Sopenharmony_ci  ConvertToCBOR(breakDetails, &cbor);
4711cb0ef41Sopenharmony_ci  m_debuggerAgent->breakProgram(
4721cb0ef41Sopenharmony_ci      toString16(breakReason),
4731cb0ef41Sopenharmony_ci      protocol::DictionaryValue::cast(
4741cb0ef41Sopenharmony_ci          protocol::Value::parseBinary(cbor.data(), cbor.size())));
4751cb0ef41Sopenharmony_ci}
4761cb0ef41Sopenharmony_ci
4771cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::setSkipAllPauses(bool skip) {
4781cb0ef41Sopenharmony_ci  m_debuggerAgent->setSkipAllPauses(skip);
4791cb0ef41Sopenharmony_ci}
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::resume(bool terminateOnResume) {
4821cb0ef41Sopenharmony_ci  m_debuggerAgent->resume(terminateOnResume);
4831cb0ef41Sopenharmony_ci}
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::stepOver() { m_debuggerAgent->stepOver({}); }
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_cistd::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
4881cb0ef41Sopenharmony_ciV8InspectorSessionImpl::searchInTextByLines(StringView text, StringView query,
4891cb0ef41Sopenharmony_ci                                            bool caseSensitive, bool isRegex) {
4901cb0ef41Sopenharmony_ci  // TODO(dgozman): search may operate on StringView and avoid copying |text|.
4911cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> matches =
4921cb0ef41Sopenharmony_ci      searchInTextByLinesImpl(this, toString16(text), toString16(query),
4931cb0ef41Sopenharmony_ci                              caseSensitive, isRegex);
4941cb0ef41Sopenharmony_ci  std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>> result;
4951cb0ef41Sopenharmony_ci  for (size_t i = 0; i < matches.size(); ++i)
4961cb0ef41Sopenharmony_ci    result.push_back(std::move(matches[i]));
4971cb0ef41Sopenharmony_ci  return result;
4981cb0ef41Sopenharmony_ci}
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_civoid V8InspectorSessionImpl::triggerPreciseCoverageDeltaUpdate(
5011cb0ef41Sopenharmony_ci    StringView occasion) {
5021cb0ef41Sopenharmony_ci  m_profilerAgent->triggerPreciseCoverageDeltaUpdate(toString16(occasion));
5031cb0ef41Sopenharmony_ci}
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci}  // namespace v8_inspector
506