injected-script.h 9.18 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
#include "src/base/macros.h"
39
#include "src/inspector/inspected-context.h"
40 41
#include "src/inspector/protocol/Forward.h"
#include "src/inspector/protocol/Runtime.h"
42 43
#include "src/inspector/v8-console.h"
#include "src/inspector/v8-debugger.h"
44

45
#include "include/v8.h"
46 47 48 49 50 51

namespace v8_inspector {

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

using protocol::Maybe;
56
using protocol::Response;
57

58 59 60 61 62 63 64
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;
65
  virtual ~EvaluateCallback() = default;
66 67
};

68 69
class InjectedScript final {
 public:
70
  InjectedScript(InspectedContext*, int sessionId);
71 72 73 74
  ~InjectedScript();

  InspectedContext* context() const { return m_context; }

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

82
  Response getInternalAndPrivateProperties(
83 84 85
      v8::Local<v8::Value>, const String16& groupName,
      std::unique_ptr<
          protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
86 87 88 89
          internalProperties,
      std::unique_ptr<
          protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
          privateProperties);
90

91 92
  void releaseObject(const String16& objectId);

93 94 95 96 97 98 99 100
  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);
101 102 103 104
  Response wrapObjectMirror(
      const ValueMirror& mirror, const String16& groupName, WrapMode wrapMode,
      v8::MaybeLocal<v8::Value> customPreviewConfig, int maxCustomPreviewDepth,
      std::unique_ptr<protocol::Runtime::RemoteObject>* result);
105
  std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
106
      v8::Local<v8::Object> table, v8::MaybeLocal<v8::Array> columns);
107

108 109
  void addPromiseCallback(V8InspectorSessionImpl* session,
                          v8::MaybeLocal<v8::Value> value,
110
                          const String16& objectGroup, WrapMode wrapMode,
111
                          bool replMode,
112 113
                          std::unique_ptr<EvaluateCallback> callback);

114
  Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
115 116 117
  String16 objectGroupName(const RemoteObjectId&) const;
  void releaseObjectGroup(const String16&);
  void setCustomObjectFormatterEnabled(bool);
118 119 120 121
  Response resolveCallArgument(protocol::Runtime::CallArgument*,
                               v8::Local<v8::Value>* result);

  Response createExceptionDetails(
122
      const v8::TryCatch&, const String16& groupName,
123
      Maybe<protocol::Runtime::ExceptionDetails>* result);
124 125 126 127 128
  Response createExceptionDetails(
      v8::Local<v8::Message> message, v8::Local<v8::Value> exception,
      const String16& groupName,
      Maybe<protocol::Runtime::ExceptionDetails>* result);

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

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

   protected:
150
    explicit Scope(V8InspectorSessionImpl*);
151
    virtual ~Scope();
152
    virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
153 154 155 156 157 158

    V8InspectorImpl* m_inspector;
    InjectedScript* m_injectedScript;

   private:
    void cleanup();
159 160
    v8::debug::ExceptionBreakState setPauseOnExceptionsState(
        v8::debug::ExceptionBreakState);
161 162 163 164 165 166

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

  class ContextScope : public Scope {
   public:
176
    ContextScope(V8InspectorSessionImpl*, int executionContextId);
177
    ~ContextScope() override;
178 179

   private:
180
    Response findInjectedScript(V8InspectorSessionImpl*) override;
181
    int m_executionContextId;
182 183

    DISALLOW_COPY_AND_ASSIGN(ContextScope);
184 185 186 187
  };

  class ObjectScope : public Scope {
   public:
188
    ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
189
    ~ObjectScope() override;
190 191 192 193
    const String16& objectGroupName() const { return m_objectGroupName; }
    v8::Local<v8::Value> object() const { return m_object; }

   private:
194
    Response findInjectedScript(V8InspectorSessionImpl*) override;
195 196 197
    String16 m_remoteObjectId;
    String16 m_objectGroupName;
    v8::Local<v8::Value> m_object;
198 199

    DISALLOW_COPY_AND_ASSIGN(ObjectScope);
200 201 202 203
  };

  class CallFrameScope : public Scope {
   public:
204
    CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
205
    ~CallFrameScope() override;
206 207 208
    size_t frameOrdinal() const { return m_frameOrdinal; }

   private:
209
    Response findInjectedScript(V8InspectorSessionImpl*) override;
210 211
    String16 m_remoteCallFrameId;
    size_t m_frameOrdinal;
212 213

    DISALLOW_COPY_AND_ASSIGN(CallFrameScope);
214
  };
215
  String16 bindObject(v8::Local<v8::Value>, const String16& groupName);
216 217 218

 private:
  v8::Local<v8::Object> commandLineAPI();
219
  void unbindObject(int id);
220

221 222 223 224
  static Response bindRemoteObjectIfNeeded(
      int sessionId, v8::Local<v8::Context> context, v8::Local<v8::Value>,
      const String16& groupName, protocol::Runtime::RemoteObject* remoteObject);

225 226 227 228
  class ProtocolPromiseHandler;
  void discardEvaluateCallbacks();
  std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
      EvaluateCallback* callback);
229 230 231 232
  Response addExceptionToDetails(
      v8::Local<v8::Value> exception,
      protocol::Runtime::ExceptionDetails* exceptionDetails,
      const String16& objectGroup);
233

234
  InspectedContext* m_context;
235
  int m_sessionId;
236 237
  v8::Global<v8::Value> m_lastEvaluationResult;
  v8::Global<v8::Object> m_commandLineAPI;
238 239 240 241
  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;
242
  std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
243
  bool m_customPreviewEnabled = false;
244 245

  DISALLOW_COPY_AND_ASSIGN(InjectedScript);
246 247 248 249
};

}  // namespace v8_inspector

250
#endif  // V8_INSPECTOR_INJECTED_SCRIPT_H_