injected-script.h 9.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright (C) 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

31 32
#ifndef V8_INSPECTOR_INJECTED_SCRIPT_H_
#define V8_INSPECTOR_INJECTED_SCRIPT_H_
33

34
#include <memory>
35
#include <unordered_map>
36
#include <unordered_set>
37

38 39 40
#include "include/v8-exception.h"
#include "include/v8-local-handle.h"
#include "include/v8-persistent-handle.h"
41
#include "src/base/macros.h"
42
#include "src/inspector/inspected-context.h"
43 44
#include "src/inspector/protocol/Forward.h"
#include "src/inspector/protocol/Runtime.h"
45 46
#include "src/inspector/v8-console.h"
#include "src/inspector/v8-debugger.h"
47 48 49 50 51 52

namespace v8_inspector {

class RemoteObjectId;
class V8InspectorImpl;
class V8InspectorSessionImpl;
53
class ValueMirror;
54
enum class WrapMode;
55 56

using protocol::Maybe;
57
using protocol::Response;
58

59 60 61 62 63 64 65
class EvaluateCallback {
 public:
  virtual void sendSuccess(
      std::unique_ptr<protocol::Runtime::RemoteObject> result,
      protocol::Maybe<protocol::Runtime::ExceptionDetails>
          exceptionDetails) = 0;
  virtual void sendFailure(const protocol::DispatchResponse& response) = 0;
66
  virtual ~EvaluateCallback() = default;
67 68
};

69 70
class InjectedScript final {
 public:
71
  InjectedScript(InspectedContext*, int sessionId);
72
  ~InjectedScript();
73 74
  InjectedScript(const InjectedScript&) = delete;
  InjectedScript& operator=(const InjectedScript&) = delete;
75 76 77

  InspectedContext* context() const { return m_context; }

78 79
  Response getProperties(
      v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
80 81
      bool accessorPropertiesOnly, bool nonIndexedPropertiesOnly,
      WrapMode wrapMode,
82 83 84
      std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
          result,
      Maybe<protocol::Runtime::ExceptionDetails>*);
85

86
  Response getInternalAndPrivateProperties(
87 88 89
      v8::Local<v8::Value>, const String16& groupName,
      std::unique_ptr<
          protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
90 91 92 93
          internalProperties,
      std::unique_ptr<
          protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
          privateProperties);
94

95 96
  void releaseObject(const String16& objectId);

97 98 99 100 101 102 103 104
  Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
                      WrapMode wrapMode,
                      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
  Response wrapObject(v8::Local<v8::Value>, const String16& groupName,
                      WrapMode wrapMode,
                      v8::MaybeLocal<v8::Value> customPreviewConfig,
                      int maxCustomPreviewDepth,
                      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
105 106 107 108
  Response wrapObjectMirror(
      const ValueMirror& mirror, const String16& groupName, WrapMode wrapMode,
      v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
109
  std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
110
      v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> columns);
111

112 113
  void addPromiseCallback(V8InspectorSessionImpl* session,
                          v8::MaybeLocal<v8::Value> value,
114
                          const String16& objectGroup, WrapMode wrapMode,
115
                          bool replMode,
116 117
                          std::unique_ptr<EvaluateCallback> callback);

118
  Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
119 120 121
  String16 objectGroupName(const RemoteObjectId&) const;
  void releaseObjectGroup(const String16&);
  void setCustomObjectFormatterEnabled(bool);
122 123 124 125
  Response resolveCallArgument(protocol::Runtime::CallArgument*,
                               v8::Local<v8::Value>* result);

  Response createExceptionDetails(
126
      const v8::TryCatch&, const String16& groupName,
127
      Maybe<protocol::Runtime::ExceptionDetails>* result);
128 129 130 131 132
  Response createExceptionDetails(
      v8::Local<v8::Message> message, v8::Local<v8::Value> exception,
      const String16& groupName,
      Maybe<protocol::Runtime::ExceptionDetails>* result);

133 134
  Response wrapEvaluateResult(
      v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
135
      const String16& objectGroup, WrapMode wrapMode,
136 137 138
      std::unique_ptr<protocol::Runtime::RemoteObject>* result,
      Maybe<protocol::Runtime::ExceptionDetails>*);
  v8::Local<v8::Value> lastEvaluationResult() const;
139
  void setLastEvaluationResult(v8::Local<v8::Value> result);
140 141 142

  class Scope {
   public:
143 144
    Response initialize();
    void installCommandLineAPI();
145 146
    void ignoreExceptionsAndMuteConsole();
    void pretendUserGesture();
147
    void allowCodeGenerationFromStrings();
148 149 150
    v8::Local<v8::Context> context() const { return m_context; }
    InjectedScript* injectedScript() const { return m_injectedScript; }
    const v8::TryCatch& tryCatch() const { return m_tryCatch; }
151
    V8InspectorImpl* inspector() const { return m_inspector; }
152 153

   protected:
154
    explicit Scope(V8InspectorSessionImpl*);
155
    virtual ~Scope();
156
    virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
157 158 159 160 161 162

    V8InspectorImpl* m_inspector;
    InjectedScript* m_injectedScript;

   private:
    void cleanup();
163 164
    v8::debug::ExceptionBreakState setPauseOnExceptionsState(
        v8::debug::ExceptionBreakState);
165 166 167 168

    v8::HandleScope m_handleScope;
    v8::TryCatch m_tryCatch;
    v8::Local<v8::Context> m_context;
169
    std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
170
    bool m_ignoreExceptionsAndMuteConsole;
171
    v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
172
    bool m_userGesture;
173
    bool m_allowEval;
174 175
    int m_contextGroupId;
    int m_sessionId;
176 177
  };

178 179
  class ContextScope : public Scope,
                       public V8InspectorSession::CommandLineAPIScope {
180
   public:
181
    ContextScope(V8InspectorSessionImpl*, int executionContextId);
182
    ~ContextScope() override;
183 184
    ContextScope(const ContextScope&) = delete;
    ContextScope& operator=(const ContextScope&) = delete;
185 186

   private:
187
    Response findInjectedScript(V8InspectorSessionImpl*) override;
188 189 190 191 192
    int m_executionContextId;
  };

  class ObjectScope : public Scope {
   public:
193
    ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
194
    ~ObjectScope() override;
195 196
    ObjectScope(const ObjectScope&) = delete;
    ObjectScope& operator=(const ObjectScope&) = delete;
197 198 199 200
    const String16& objectGroupName() const { return m_objectGroupName; }
    v8::Local<v8::Value> object() const { return m_object; }

   private:
201
    Response findInjectedScript(V8InspectorSessionImpl*) override;
202 203 204 205 206 207 208
    String16 m_remoteObjectId;
    String16 m_objectGroupName;
    v8::Local<v8::Value> m_object;
  };

  class CallFrameScope : public Scope {
   public:
209
    CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
210
    ~CallFrameScope() override;
211 212
    CallFrameScope(const CallFrameScope&) = delete;
    CallFrameScope& operator=(const CallFrameScope&) = delete;
213 214 215
    size_t frameOrdinal() const { return m_frameOrdinal; }

   private:
216
    Response findInjectedScript(V8InspectorSessionImpl*) override;
217 218 219
    String16 m_remoteCallFrameId;
    size_t m_frameOrdinal;
  };
220
  String16 bindObject(v8::Local<v8::Value>, const String16& groupName);
221 222 223

 private:
  v8::Local<v8::Object> commandLineAPI();
224
  void unbindObject(int id);
225

226 227 228 229
  static Response bindRemoteObjectIfNeeded(
      int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
      const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);

230 231 232 233
  class ProtocolPromiseHandler;
  void discardEvaluateCallbacks();
  std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
      EvaluateCallback* callback);
234 235 236 237
  Response addExceptionToDetails(
      v8::Local<v8::Value> exception,
      protocol::Runtime::ExceptionDetails* exceptionDetails,
      const String16& objectGroup);
238

239
  InspectedContext* m_context;
240
  int m_sessionId;
241 242
  v8::Global<v8::Value> m_lastEvaluationResult;
  v8::Global<v8::Object> m_commandLineAPI;
243 244 245 246
  int m_lastBoundObjectId = 1;
  std::unordered_map<int, v8::Global<v8::Value>> m_idToWrappedObject;
  std::unordered_map<int, String16> m_idToObjectGroupName;
  std::unordered_map<String16, std::vector<int>> m_nameToObjectGroup;
247
  std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
248
  bool m_customPreviewEnabled = false;
249 250 251 252
};

}  // namespace v8_inspector

253
#endif  // V8_INSPECTOR_INJECTED_SCRIPT_H_