11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright (C) 2012 Google Inc. All rights reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
51cb0ef41Sopenharmony_ci * modification, are permitted provided that the following conditions are
61cb0ef41Sopenharmony_ci * met:
71cb0ef41Sopenharmony_ci *
81cb0ef41Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
91cb0ef41Sopenharmony_ci * notice, this list of conditions and the following disclaimer.
101cb0ef41Sopenharmony_ci *     * Redistributions in binary form must reproduce the above
111cb0ef41Sopenharmony_ci * copyright notice, this list of conditions and the following disclaimer
121cb0ef41Sopenharmony_ci * in the documentation and/or other materials provided with the
131cb0ef41Sopenharmony_ci * distribution.
141cb0ef41Sopenharmony_ci *     * Neither the name of Google Inc. nor the names of its
151cb0ef41Sopenharmony_ci * contributors may be used to endorse or promote products derived from
161cb0ef41Sopenharmony_ci * this software without specific prior written permission.
171cb0ef41Sopenharmony_ci *
181cb0ef41Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
191cb0ef41Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
201cb0ef41Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
211cb0ef41Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
221cb0ef41Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
231cb0ef41Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
241cb0ef41Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
251cb0ef41Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
261cb0ef41Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
271cb0ef41Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
281cb0ef41Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
291cb0ef41Sopenharmony_ci */
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci#ifndef V8_INSPECTOR_INJECTED_SCRIPT_H_
321cb0ef41Sopenharmony_ci#define V8_INSPECTOR_INJECTED_SCRIPT_H_
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#include <memory>
351cb0ef41Sopenharmony_ci#include <unordered_map>
361cb0ef41Sopenharmony_ci#include <unordered_set>
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci#include "include/v8-exception.h"
391cb0ef41Sopenharmony_ci#include "include/v8-local-handle.h"
401cb0ef41Sopenharmony_ci#include "include/v8-persistent-handle.h"
411cb0ef41Sopenharmony_ci#include "src/base/macros.h"
421cb0ef41Sopenharmony_ci#include "src/inspector/inspected-context.h"
431cb0ef41Sopenharmony_ci#include "src/inspector/protocol/Forward.h"
441cb0ef41Sopenharmony_ci#include "src/inspector/protocol/Runtime.h"
451cb0ef41Sopenharmony_ci#include "src/inspector/v8-console.h"
461cb0ef41Sopenharmony_ci#include "src/inspector/v8-debugger.h"
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cinamespace v8_inspector {
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciclass RemoteObjectId;
511cb0ef41Sopenharmony_ciclass V8InspectorImpl;
521cb0ef41Sopenharmony_ciclass V8InspectorSessionImpl;
531cb0ef41Sopenharmony_ciclass ValueMirror;
541cb0ef41Sopenharmony_cienum class WrapMode;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciusing protocol::Maybe;
571cb0ef41Sopenharmony_ciusing protocol::Response;
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciclass EvaluateCallback {
601cb0ef41Sopenharmony_ci public:
611cb0ef41Sopenharmony_ci  virtual void sendSuccess(
621cb0ef41Sopenharmony_ci      std::unique_ptr<protocol::Runtime::RemoteObject> result,
631cb0ef41Sopenharmony_ci      protocol::Maybe<protocol::Runtime::ExceptionDetails>
641cb0ef41Sopenharmony_ci          exceptionDetails) = 0;
651cb0ef41Sopenharmony_ci  virtual void sendFailure(const protocol::DispatchResponse& response) = 0;
661cb0ef41Sopenharmony_ci  virtual ~EvaluateCallback() = default;
671cb0ef41Sopenharmony_ci};
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ciclass InjectedScript final {
701cb0ef41Sopenharmony_ci public:
711cb0ef41Sopenharmony_ci  InjectedScript(InspectedContext*, int sessionId);
721cb0ef41Sopenharmony_ci  ~InjectedScript();
731cb0ef41Sopenharmony_ci  InjectedScript(const InjectedScript&) = delete;
741cb0ef41Sopenharmony_ci  InjectedScript& operator=(const InjectedScript&) = delete;
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  InspectedContext* context() const { return m_context; }
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  Response getProperties(
791cb0ef41Sopenharmony_ci      v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
801cb0ef41Sopenharmony_ci      bool accessorPropertiesOnly, bool nonIndexedPropertiesOnly,
811cb0ef41Sopenharmony_ci      WrapMode wrapMode,
821cb0ef41Sopenharmony_ci      std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
831cb0ef41Sopenharmony_ci          result,
841cb0ef41Sopenharmony_ci      Maybe<protocol::Runtime::ExceptionDetails>*);
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  Response getInternalAndPrivateProperties(
871cb0ef41Sopenharmony_ci      v8::Local<v8::Value>, const String16& groupName,
881cb0ef41Sopenharmony_ci      bool accessorPropertiesOnly,
891cb0ef41Sopenharmony_ci      std::unique_ptr<
901cb0ef41Sopenharmony_ci          protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
911cb0ef41Sopenharmony_ci          internalProperties,
921cb0ef41Sopenharmony_ci      std::unique_ptr<
931cb0ef41Sopenharmony_ci          protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
941cb0ef41Sopenharmony_ci          privateProperties);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  void releaseObject(const String16& objectId);
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
991cb0ef41Sopenharmony_ci                      WrapMode wrapMode,
1001cb0ef41Sopenharmony_ci                      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
1011cb0ef41Sopenharmony_ci  Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
1021cb0ef41Sopenharmony_ci                      WrapMode wrapMode,
1031cb0ef41Sopenharmony_ci                      v8::MaybeLocal<v8::Value> customPreviewConfig,
1041cb0ef41Sopenharmony_ci                      int maxCustomPreviewDepth,
1051cb0ef41Sopenharmony_ci                      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
1061cb0ef41Sopenharmony_ci  Response wrapObjectMirror(
1071cb0ef41Sopenharmony_ci      const ValueMirror& mirror, const String16& groupName, WrapMode wrapMode,
1081cb0ef41Sopenharmony_ci      v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
1091cb0ef41Sopenharmony_ci      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
1101cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
1111cb0ef41Sopenharmony_ci      v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> columns);
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  void addPromiseCallback(V8InspectorSessionImpl* session,
1141cb0ef41Sopenharmony_ci                          v8::MaybeLocal<v8::Value> value,
1151cb0ef41Sopenharmony_ci                          const String16& objectGroup, WrapMode wrapMode,
1161cb0ef41Sopenharmony_ci                          bool replMode,
1171cb0ef41Sopenharmony_ci                          std::unique_ptr<EvaluateCallback> callback);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
1201cb0ef41Sopenharmony_ci  String16 objectGroupName(const RemoteObjectId&) const;
1211cb0ef41Sopenharmony_ci  void releaseObjectGroup(const String16&);
1221cb0ef41Sopenharmony_ci  void setCustomObjectFormatterEnabled(bool);
1231cb0ef41Sopenharmony_ci  Response resolveCallArgument(protocol::Runtime::CallArgument*,
1241cb0ef41Sopenharmony_ci                               v8::Local<v8::Value>* result);
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  Response createExceptionDetails(
1271cb0ef41Sopenharmony_ci      const v8::TryCatch&, const String16& groupName,
1281cb0ef41Sopenharmony_ci      Maybe<protocol::Runtime::ExceptionDetails>* result);
1291cb0ef41Sopenharmony_ci  Response createExceptionDetails(
1301cb0ef41Sopenharmony_ci      v8::Local<v8::Message> message, v8::Local<v8::Value> exception,
1311cb0ef41Sopenharmony_ci      const String16& groupName,
1321cb0ef41Sopenharmony_ci      Maybe<protocol::Runtime::ExceptionDetails>* result);
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  Response wrapEvaluateResult(
1351cb0ef41Sopenharmony_ci      v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
1361cb0ef41Sopenharmony_ci      const String16& objectGroup, WrapMode wrapMode,
1371cb0ef41Sopenharmony_ci      std::unique_ptr<protocol::Runtime::RemoteObject>* result,
1381cb0ef41Sopenharmony_ci      Maybe<protocol::Runtime::ExceptionDetails>*);
1391cb0ef41Sopenharmony_ci  v8::Local<v8::Value> lastEvaluationResult() const;
1401cb0ef41Sopenharmony_ci  void setLastEvaluationResult(v8::Local<v8::Value> result);
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  class Scope {
1431cb0ef41Sopenharmony_ci   public:
1441cb0ef41Sopenharmony_ci    Response initialize();
1451cb0ef41Sopenharmony_ci    void installCommandLineAPI();
1461cb0ef41Sopenharmony_ci    void ignoreExceptionsAndMuteConsole();
1471cb0ef41Sopenharmony_ci    void pretendUserGesture();
1481cb0ef41Sopenharmony_ci    void allowCodeGenerationFromStrings();
1491cb0ef41Sopenharmony_ci    v8::Local<v8::Context> context() const { return m_context; }
1501cb0ef41Sopenharmony_ci    InjectedScript* injectedScript() const { return m_injectedScript; }
1511cb0ef41Sopenharmony_ci    const v8::TryCatch& tryCatch() const { return m_tryCatch; }
1521cb0ef41Sopenharmony_ci    V8InspectorImpl* inspector() const { return m_inspector; }
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci   protected:
1551cb0ef41Sopenharmony_ci    explicit Scope(V8InspectorSessionImpl*);
1561cb0ef41Sopenharmony_ci    virtual ~Scope();
1571cb0ef41Sopenharmony_ci    virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci    V8InspectorImpl* m_inspector;
1601cb0ef41Sopenharmony_ci    InjectedScript* m_injectedScript;
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci   private:
1631cb0ef41Sopenharmony_ci    void cleanup();
1641cb0ef41Sopenharmony_ci    v8::debug::ExceptionBreakState setPauseOnExceptionsState(
1651cb0ef41Sopenharmony_ci        v8::debug::ExceptionBreakState);
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci    v8::HandleScope m_handleScope;
1681cb0ef41Sopenharmony_ci    v8::TryCatch m_tryCatch;
1691cb0ef41Sopenharmony_ci    v8::Local<v8::Context> m_context;
1701cb0ef41Sopenharmony_ci    std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
1711cb0ef41Sopenharmony_ci    bool m_ignoreExceptionsAndMuteConsole;
1721cb0ef41Sopenharmony_ci    v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
1731cb0ef41Sopenharmony_ci    bool m_userGesture;
1741cb0ef41Sopenharmony_ci    bool m_allowEval;
1751cb0ef41Sopenharmony_ci    int m_contextGroupId;
1761cb0ef41Sopenharmony_ci    int m_sessionId;
1771cb0ef41Sopenharmony_ci  };
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci  class ContextScope : public Scope,
1801cb0ef41Sopenharmony_ci                       public V8InspectorSession::CommandLineAPIScope {
1811cb0ef41Sopenharmony_ci   public:
1821cb0ef41Sopenharmony_ci    ContextScope(V8InspectorSessionImpl*, int executionContextId);
1831cb0ef41Sopenharmony_ci    ~ContextScope() override;
1841cb0ef41Sopenharmony_ci    ContextScope(const ContextScope&) = delete;
1851cb0ef41Sopenharmony_ci    ContextScope& operator=(const ContextScope&) = delete;
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci   private:
1881cb0ef41Sopenharmony_ci    Response findInjectedScript(V8InspectorSessionImpl*) override;
1891cb0ef41Sopenharmony_ci    int m_executionContextId;
1901cb0ef41Sopenharmony_ci  };
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  class ObjectScope : public Scope {
1931cb0ef41Sopenharmony_ci   public:
1941cb0ef41Sopenharmony_ci    ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
1951cb0ef41Sopenharmony_ci    ~ObjectScope() override;
1961cb0ef41Sopenharmony_ci    ObjectScope(const ObjectScope&) = delete;
1971cb0ef41Sopenharmony_ci    ObjectScope& operator=(const ObjectScope&) = delete;
1981cb0ef41Sopenharmony_ci    const String16& objectGroupName() const { return m_objectGroupName; }
1991cb0ef41Sopenharmony_ci    v8::Local<v8::Value> object() const { return m_object; }
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci   private:
2021cb0ef41Sopenharmony_ci    Response findInjectedScript(V8InspectorSessionImpl*) override;
2031cb0ef41Sopenharmony_ci    String16 m_remoteObjectId;
2041cb0ef41Sopenharmony_ci    String16 m_objectGroupName;
2051cb0ef41Sopenharmony_ci    v8::Local<v8::Value> m_object;
2061cb0ef41Sopenharmony_ci  };
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci  class CallFrameScope : public Scope {
2091cb0ef41Sopenharmony_ci   public:
2101cb0ef41Sopenharmony_ci    CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
2111cb0ef41Sopenharmony_ci    ~CallFrameScope() override;
2121cb0ef41Sopenharmony_ci    CallFrameScope(const CallFrameScope&) = delete;
2131cb0ef41Sopenharmony_ci    CallFrameScope& operator=(const CallFrameScope&) = delete;
2141cb0ef41Sopenharmony_ci    size_t frameOrdinal() const { return m_frameOrdinal; }
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci   private:
2171cb0ef41Sopenharmony_ci    Response findInjectedScript(V8InspectorSessionImpl*) override;
2181cb0ef41Sopenharmony_ci    String16 m_remoteCallFrameId;
2191cb0ef41Sopenharmony_ci    size_t m_frameOrdinal;
2201cb0ef41Sopenharmony_ci  };
2211cb0ef41Sopenharmony_ci  String16 bindObject(v8::Local<v8::Value>, const String16& groupName);
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci private:
2241cb0ef41Sopenharmony_ci  v8::Local<v8::Object> commandLineAPI();
2251cb0ef41Sopenharmony_ci  void unbindObject(int id);
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  static Response bindRemoteObjectIfNeeded(
2281cb0ef41Sopenharmony_ci      int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
2291cb0ef41Sopenharmony_ci      const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  class ProtocolPromiseHandler;
2321cb0ef41Sopenharmony_ci  void discardEvaluateCallbacks();
2331cb0ef41Sopenharmony_ci  std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
2341cb0ef41Sopenharmony_ci      EvaluateCallback* callback);
2351cb0ef41Sopenharmony_ci  Response addExceptionToDetails(
2361cb0ef41Sopenharmony_ci      v8::Local<v8::Value> exception,
2371cb0ef41Sopenharmony_ci      protocol::Runtime::ExceptionDetails* exceptionDetails,
2381cb0ef41Sopenharmony_ci      const String16& objectGroup);
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci  InspectedContext* m_context;
2411cb0ef41Sopenharmony_ci  int m_sessionId;
2421cb0ef41Sopenharmony_ci  v8::Global<v8::Value> m_lastEvaluationResult;
2431cb0ef41Sopenharmony_ci  v8::Global<v8::Object> m_commandLineAPI;
2441cb0ef41Sopenharmony_ci  int m_lastBoundObjectId = 1;
2451cb0ef41Sopenharmony_ci  std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
2461cb0ef41Sopenharmony_ci  std::unordered_map<int, String16> m_idToObjectGroupName;
2471cb0ef41Sopenharmony_ci  std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
2481cb0ef41Sopenharmony_ci  std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
2491cb0ef41Sopenharmony_ci  bool m_customPreviewEnabled = false;
2501cb0ef41Sopenharmony_ci};
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci}  // namespace v8_inspector
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci#endif  // V8_INSPECTOR_INJECTED_SCRIPT_H_
255