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