injected-script.h 8.1 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 31 32 33
/*
 * 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.
 */

#ifndef V8_INSPECTOR_INJECTEDSCRIPT_H_
#define V8_INSPECTOR_INJECTEDSCRIPT_H_

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

37
#include "src/base/macros.h"
38
#include "src/inspector/inspected-context.h"
39 40
#include "src/inspector/protocol/Forward.h"
#include "src/inspector/protocol/Runtime.h"
41 42
#include "src/inspector/v8-console.h"
#include "src/inspector/v8-debugger.h"
43

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

namespace v8_inspector {

class RemoteObjectId;
class V8FunctionCall;
class V8InspectorImpl;
class V8InspectorSessionImpl;

using protocol::Maybe;
54
using protocol::Response;
55

56 57 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;
  virtual ~EvaluateCallback() {}
};

66 67
class InjectedScript final {
 public:
68 69
  static std::unique_ptr<InjectedScript> create(InspectedContext*,
                                                int sessionId);
70
  ~InjectedScript();
71 72
  static InjectedScript* fromInjectedScriptHost(v8::Isolate* isolate,
                                                v8::Local<v8::Object>);
73 74 75

  InspectedContext* context() const { return m_context; }

76 77 78
  Response getProperties(
      v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
      bool accessorPropertiesOnly, bool generatePreview,
79 80 81 82 83
      std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
          result,
      Maybe<protocol::Runtime::ExceptionDetails>*);
  void releaseObject(const String16& objectId);

84 85 86 87
  Response wrapObject(
      v8::Local<v8::Value>, const String16& groupName, bool forceValueType,
      bool generatePreview,
      std::unique_ptr<protocol::Runtime::RemoteObject>* result) const;
88 89 90
  std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
      v8::Local<v8::Value> table, v8::Local<v8::Value> columns) const;

91 92 93 94 95 96
  void addPromiseCallback(V8InspectorSessionImpl* session,
                          v8::MaybeLocal<v8::Value> value,
                          const String16& objectGroup, bool returnByValue,
                          bool generatePreview,
                          std::unique_ptr<EvaluateCallback> callback);

97
  Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
98 99 100
  String16 objectGroupName(const RemoteObjectId&) const;
  void releaseObjectGroup(const String16&);
  void setCustomObjectFormatterEnabled(bool);
101 102 103 104 105 106 107 108 109
  Response resolveCallArgument(protocol::Runtime::CallArgument*,
                               v8::Local<v8::Value>* result);

  Response createExceptionDetails(
      const v8::TryCatch&, const String16& groupName, bool generatePreview,
      Maybe<protocol::Runtime::ExceptionDetails>* result);
  Response wrapEvaluateResult(
      v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
      const String16& objectGroup, bool returnByValue, bool generatePreview,
110 111 112
      std::unique_ptr<protocol::Runtime::RemoteObject>* result,
      Maybe<protocol::Runtime::ExceptionDetails>*);
  v8::Local<v8::Value> lastEvaluationResult() const;
113
  void setLastEvaluationResult(v8::Local<v8::Value> result);
114

115 116
  int bindObject(v8::Local<v8::Value>, const String16& groupName);

117 118
  class Scope {
   public:
119 120
    Response initialize();
    void installCommandLineAPI();
121 122 123 124 125 126 127
    void ignoreExceptionsAndMuteConsole();
    void pretendUserGesture();
    v8::Local<v8::Context> context() const { return m_context; }
    InjectedScript* injectedScript() const { return m_injectedScript; }
    const v8::TryCatch& tryCatch() const { return m_tryCatch; }

   protected:
128
    explicit Scope(V8InspectorSessionImpl*);
129
    virtual ~Scope();
130
    virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
131 132 133 134 135 136

    V8InspectorImpl* m_inspector;
    InjectedScript* m_injectedScript;

   private:
    void cleanup();
137 138
    v8::debug::ExceptionBreakState setPauseOnExceptionsState(
        v8::debug::ExceptionBreakState);
139 140 141 142 143 144

    v8::HandleScope m_handleScope;
    v8::TryCatch m_tryCatch;
    v8::Local<v8::Context> m_context;
    std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
    bool m_ignoreExceptionsAndMuteConsole;
145
    v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
146
    bool m_userGesture;
147 148
    int m_contextGroupId;
    int m_sessionId;
149 150 151 152
  };

  class ContextScope : public Scope {
   public:
153
    ContextScope(V8InspectorSessionImpl*, int executionContextId);
154 155 156
    ~ContextScope();

   private:
157
    Response findInjectedScript(V8InspectorSessionImpl*) override;
158
    int m_executionContextId;
159 160

    DISALLOW_COPY_AND_ASSIGN(ContextScope);
161 162 163 164
  };

  class ObjectScope : public Scope {
   public:
165
    ObjectScope(V8InspectorSessionImpl*, const String16& remoteObjectId);
166 167 168 169 170
    ~ObjectScope();
    const String16& objectGroupName() const { return m_objectGroupName; }
    v8::Local<v8::Value> object() const { return m_object; }

   private:
171
    Response findInjectedScript(V8InspectorSessionImpl*) override;
172 173 174
    String16 m_remoteObjectId;
    String16 m_objectGroupName;
    v8::Local<v8::Value> m_object;
175 176

    DISALLOW_COPY_AND_ASSIGN(ObjectScope);
177 178 179 180
  };

  class CallFrameScope : public Scope {
   public:
181
    CallFrameScope(V8InspectorSessionImpl*, const String16& remoteCallFrameId);
182 183 184 185
    ~CallFrameScope();
    size_t frameOrdinal() const { return m_frameOrdinal; }

   private:
186
    Response findInjectedScript(V8InspectorSessionImpl*) override;
187 188
    String16 m_remoteCallFrameId;
    size_t m_frameOrdinal;
189 190

    DISALLOW_COPY_AND_ASSIGN(CallFrameScope);
191 192 193
  };

 private:
194
  InjectedScript(InspectedContext*, v8::Local<v8::Object>, int sessionId);
195
  v8::Local<v8::Value> v8Value() const;
196 197 198
  Response wrapValue(v8::Local<v8::Value>, const String16& groupName,
                     bool forceValueType, bool generatePreview,
                     v8::Local<v8::Value>* result) const;
199
  v8::Local<v8::Object> commandLineAPI();
200
  void unbindObject(int id);
201

202 203 204 205 206
  class ProtocolPromiseHandler;
  void discardEvaluateCallbacks();
  std::unique_ptr<EvaluateCallback> takeEvaluateCallback(
      EvaluateCallback* callback);

207 208
  InspectedContext* m_context;
  v8::Global<v8::Value> m_value;
209
  int m_sessionId;
210 211
  v8::Global<v8::Value> m_lastEvaluationResult;
  v8::Global<v8::Object> m_commandLineAPI;
212 213 214 215
  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;
216
  std::unordered_set<EvaluateCallback*> m_evaluateCallbacks;
217 218

  DISALLOW_COPY_AND_ASSIGN(InjectedScript);
219 220 221 222 223
};

}  // namespace v8_inspector

#endif  // V8_INSPECTOR_INJECTEDSCRIPT_H_