1/*
2 * Copyright (C) 2011 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_V8_RUNTIME_AGENT_IMPL_H_
32#define V8_INSPECTOR_V8_RUNTIME_AGENT_IMPL_H_
33
34#include <memory>
35#include <unordered_map>
36
37#include "include/v8-persistent-handle.h"
38#include "src/base/macros.h"
39#include "src/inspector/protocol/Forward.h"
40#include "src/inspector/protocol/Runtime.h"
41
42namespace v8 {
43class Script;
44}  // namespace v8
45
46namespace v8_inspector {
47
48class InjectedScript;
49class InspectedContext;
50class RemoteObjectIdBase;
51class V8ConsoleMessage;
52class V8InspectorImpl;
53class V8InspectorSessionImpl;
54
55using protocol::Response;
56using protocol::Maybe;
57
58class V8RuntimeAgentImpl : public protocol::Runtime::Backend {
59 public:
60  V8RuntimeAgentImpl(V8InspectorSessionImpl*, protocol::FrontendChannel*,
61                     protocol::DictionaryValue* state);
62  ~V8RuntimeAgentImpl() override;
63  V8RuntimeAgentImpl(const V8RuntimeAgentImpl&) = delete;
64  V8RuntimeAgentImpl& operator=(const V8RuntimeAgentImpl&) = delete;
65  void restore();
66
67  // Part of the protocol.
68  Response enable() override;
69  Response disable() override;
70  void evaluate(const String16& expression, Maybe<String16> objectGroup,
71                Maybe<bool> includeCommandLineAPI, Maybe<bool> silent,
72                Maybe<int> executionContextId, Maybe<bool> returnByValue,
73                Maybe<bool> generatePreview, Maybe<bool> userGesture,
74                Maybe<bool> awaitPromise, Maybe<bool> throwOnSideEffect,
75                Maybe<double> timeout, Maybe<bool> disableBreaks,
76                Maybe<bool> replMode, Maybe<bool> allowUnsafeEvalBlockedByCSP,
77                Maybe<String16> uniqueContextId,
78                Maybe<bool> generateWebDriverValue,
79                std::unique_ptr<EvaluateCallback>) override;
80  void awaitPromise(const String16& promiseObjectId, Maybe<bool> returnByValue,
81                    Maybe<bool> generatePreview,
82                    std::unique_ptr<AwaitPromiseCallback>) override;
83  void callFunctionOn(
84      const String16& expression, Maybe<String16> objectId,
85      Maybe<protocol::Array<protocol::Runtime::CallArgument>> optionalArguments,
86      Maybe<bool> silent, Maybe<bool> returnByValue,
87      Maybe<bool> generatePreview, Maybe<bool> userGesture,
88      Maybe<bool> awaitPromise, Maybe<int> executionContextId,
89      Maybe<String16> objectGroup, Maybe<bool> throwOnSideEffect,
90      Maybe<bool> generateWebDriverValue,
91      std::unique_ptr<CallFunctionOnCallback>) override;
92  Response releaseObject(const String16& objectId) override;
93  Response getProperties(
94      const String16& objectId, Maybe<bool> ownProperties,
95      Maybe<bool> accessorPropertiesOnly, Maybe<bool> generatePreview,
96      Maybe<bool> nonIndexedPropertiesOnly,
97      std::unique_ptr<protocol::Array<protocol::Runtime::PropertyDescriptor>>*
98          result,
99      Maybe<protocol::Array<protocol::Runtime::InternalPropertyDescriptor>>*
100          internalProperties,
101      Maybe<protocol::Array<protocol::Runtime::PrivatePropertyDescriptor>>*
102          privateProperties,
103      Maybe<protocol::Runtime::ExceptionDetails>*) override;
104  Response releaseObjectGroup(const String16& objectGroup) override;
105  Response runIfWaitingForDebugger() override;
106  Response setCustomObjectFormatterEnabled(bool) override;
107  Response setMaxCallStackSizeToCapture(int) override;
108  Response discardConsoleEntries() override;
109  Response compileScript(const String16& expression, const String16& sourceURL,
110                         bool persistScript, Maybe<int> executionContextId,
111                         Maybe<String16>*,
112                         Maybe<protocol::Runtime::ExceptionDetails>*) override;
113  void runScript(const String16&, Maybe<int> executionContextId,
114                 Maybe<String16> objectGroup, Maybe<bool> silent,
115                 Maybe<bool> includeCommandLineAPI, Maybe<bool> returnByValue,
116                 Maybe<bool> generatePreview, Maybe<bool> awaitPromise,
117                 std::unique_ptr<RunScriptCallback>) override;
118  Response queryObjects(
119      const String16& prototypeObjectId, Maybe<String16> objectGroup,
120      std::unique_ptr<protocol::Runtime::RemoteObject>* objects) override;
121  Response globalLexicalScopeNames(
122      Maybe<int> executionContextId,
123      std::unique_ptr<protocol::Array<String16>>* outNames) override;
124  Response getIsolateId(String16* outIsolateId) override;
125  Response getHeapUsage(double* out_usedSize, double* out_totalSize) override;
126  void terminateExecution(
127      std::unique_ptr<TerminateExecutionCallback> callback) override;
128
129  Response addBinding(const String16& name, Maybe<int> executionContextId,
130                      Maybe<String16> executionContextName) override;
131  Response removeBinding(const String16& name) override;
132  void addBindings(InspectedContext* context);
133  Response getExceptionDetails(const String16& errorObjectId,
134                               Maybe<protocol::Runtime::ExceptionDetails>*
135                                   out_exceptionDetails) override;
136
137  void reset();
138  void reportExecutionContextCreated(InspectedContext*);
139  void reportExecutionContextDestroyed(InspectedContext*);
140  void inspect(std::unique_ptr<protocol::Runtime::RemoteObject> objectToInspect,
141               std::unique_ptr<protocol::DictionaryValue> hints,
142               int executionContextId);
143  void messageAdded(V8ConsoleMessage*);
144  bool enabled() const { return m_enabled; }
145
146 private:
147  bool reportMessage(V8ConsoleMessage*, bool generatePreview);
148
149  static void bindingCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
150  void bindingCalled(const String16& name, const String16& payload,
151                     int executionContextId);
152  void addBinding(InspectedContext* context, const String16& name);
153
154  V8InspectorSessionImpl* m_session;
155  protocol::DictionaryValue* m_state;
156  protocol::Runtime::Frontend m_frontend;
157  V8InspectorImpl* m_inspector;
158  bool m_enabled;
159  std::unordered_map<String16, std::unique_ptr<v8::Global<v8::Script>>>
160      m_compiledScripts;
161  // Binding name -> executionContextIds mapping.
162  std::unordered_map<String16, std::unordered_set<int>> m_activeBindings;
163};
164
165}  // namespace v8_inspector
166
167#endif  // V8_INSPECTOR_V8_RUNTIME_AGENT_IMPL_H_
168