1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef V8_INSPECTOR_INJECTEDSCRIPT_H_
32#define V8_INSPECTOR_INJECTEDSCRIPT_H_
33
34#include "src/base/macros.h"
35#include "src/inspector/injected-script-native.h"
36#include "src/inspector/inspected-context.h"
37#include "src/inspector/protocol/Forward.h"
38#include "src/inspector/protocol/Runtime.h"
39#include "src/inspector/v8-console.h"
40#include "src/inspector/v8-debugger.h"
41
42#include "include/v8.h"
43
44namespace v8_inspector {
45
46class RemoteObjectId;
47class V8FunctionCall;
48class V8InspectorImpl;
49class V8InspectorSessionImpl;
50
51using protocol::Maybe;
52using protocol::Response;
53
54class InjectedScript final {
55 public:
56  static std::unique_ptr<InjectedScript> create(InspectedContext*);
57  ~InjectedScript();
58
59  InspectedContext* context() const { return m_context; }
60
61  Response getProperties(
62      v8::Local<v8::Object>, const String16& groupName, bool ownProperties,
63      bool accessorPropertiesOnly, bool generatePreview,
64      std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
65          result,
66      Maybe<protocol::Runtime::ExceptionDetails>*);
67  void releaseObject(const String16& objectId);
68
69  Response wrapObject(
70      v8::Local<v8::Value>, const String16& groupName, bool forceValueType,
71      bool generatePreview,
72      std::unique_ptr<protocol::Runtime::RemoteObject>* result) const;
73  Response wrapObjectProperty(v8::Local<v8::Object>, v8::Local<v8::Name> key,
74                              const String16& groupName,
75                              bool forceValueType = false,
76                              bool generatePreview = false) const;
77  Response wrapPropertyInArray(v8::Local<v8::Array>,
78                               v8::Local<v8::String> property,
79                               const String16& groupName,
80                               bool forceValueType = false,
81                               bool generatePreview = false) const;
82  std::unique_ptr<protocol::Runtime::RemoteObject> wrapTable(
83      v8::Local<v8::Value> table, v8::Local<v8::Value> columns) const;
84
85  Response findObject(const RemoteObjectId&, v8::Local<v8::Value>*) const;
86  String16 objectGroupName(const RemoteObjectId&) const;
87  void releaseObjectGroup(const String16&);
88  void setCustomObjectFormatterEnabled(bool);
89  Response resolveCallArgument(protocol::Runtime::CallArgument*,
90                               v8::Local<v8::Value>* result);
91
92  Response createExceptionDetails(
93      const v8::TryCatch&, const String16& groupName, bool generatePreview,
94      Maybe<protocol::Runtime::ExceptionDetails>* result);
95  Response wrapEvaluateResult(
96      v8::MaybeLocal<v8::Value> maybeResultValue, const v8::TryCatch&,
97      const String16& objectGroup, bool returnByValue, bool generatePreview,
98      std::unique_ptr<protocol::Runtime::RemoteObject>* result,
99      Maybe<protocol::Runtime::ExceptionDetails>*);
100  v8::Local<v8::Value> lastEvaluationResult() const;
101
102  class Scope {
103   public:
104    Response initialize();
105    void installCommandLineAPI();
106    void ignoreExceptionsAndMuteConsole();
107    void pretendUserGesture();
108    v8::Local<v8::Context> context() const { return m_context; }
109    InjectedScript* injectedScript() const { return m_injectedScript; }
110    const v8::TryCatch& tryCatch() const { return m_tryCatch; }
111
112   protected:
113    Scope(V8InspectorImpl*, int contextGroupId);
114    virtual ~Scope();
115    virtual Response findInjectedScript(V8InspectorSessionImpl*) = 0;
116
117    V8InspectorImpl* m_inspector;
118    int m_contextGroupId;
119    InjectedScript* m_injectedScript;
120
121   private:
122    void cleanup();
123    v8::debug::ExceptionBreakState setPauseOnExceptionsState(
124        v8::debug::ExceptionBreakState);
125
126    v8::HandleScope m_handleScope;
127    v8::TryCatch m_tryCatch;
128    v8::Local<v8::Context> m_context;
129    std::unique_ptr<V8Console::CommandLineAPIScope> m_commandLineAPIScope;
130    bool m_ignoreExceptionsAndMuteConsole;
131    v8::debug::ExceptionBreakState m_previousPauseOnExceptionsState;
132    bool m_userGesture;
133  };
134
135  class ContextScope : public Scope {
136   public:
137    ContextScope(V8InspectorImpl*, int contextGroupId, int executionContextId);
138    ~ContextScope();
139
140   private:
141    Response findInjectedScript(V8InspectorSessionImpl*) override;
142    int m_executionContextId;
143
144    DISALLOW_COPY_AND_ASSIGN(ContextScope);
145  };
146
147  class ObjectScope : public Scope {
148   public:
149    ObjectScope(V8InspectorImpl*, int contextGroupId,
150                const String16& remoteObjectId);
151    ~ObjectScope();
152    const String16& objectGroupName() const { return m_objectGroupName; }
153    v8::Local<v8::Value> object() const { return m_object; }
154
155   private:
156    Response findInjectedScript(V8InspectorSessionImpl*) override;
157    String16 m_remoteObjectId;
158    String16 m_objectGroupName;
159    v8::Local<v8::Value> m_object;
160
161    DISALLOW_COPY_AND_ASSIGN(ObjectScope);
162  };
163
164  class CallFrameScope : public Scope {
165   public:
166    CallFrameScope(V8InspectorImpl*, int contextGroupId,
167                   const String16& remoteCallFrameId);
168    ~CallFrameScope();
169    size_t frameOrdinal() const { return m_frameOrdinal; }
170
171   private:
172    Response findInjectedScript(V8InspectorSessionImpl*) override;
173    String16 m_remoteCallFrameId;
174    size_t m_frameOrdinal;
175
176    DISALLOW_COPY_AND_ASSIGN(CallFrameScope);
177  };
178
179 private:
180  InjectedScript(InspectedContext*, v8::Local<v8::Object>,
181                 std::unique_ptr<InjectedScriptNative>);
182  v8::Local<v8::Value> v8Value() const;
183  Response wrapValue(v8::Local<v8::Value>, const String16& groupName,
184                     bool forceValueType, bool generatePreview,
185                     v8::Local<v8::Value>* result) const;
186  v8::Local<v8::Object> commandLineAPI();
187
188  InspectedContext* m_context;
189  v8::Global<v8::Value> m_value;
190  v8::Global<v8::Value> m_lastEvaluationResult;
191  std::unique_ptr<InjectedScriptNative> m_native;
192  v8::Global<v8::Object> m_commandLineAPI;
193
194  DISALLOW_COPY_AND_ASSIGN(InjectedScript);
195};
196
197}  // namespace v8_inspector
198
199#endif  // V8_INSPECTOR_INJECTEDSCRIPT_H_
200