1 // Copyright 2016 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_INSPECTOR_V8_CONSOLE_H_ 6 #define V8_INSPECTOR_V8_CONSOLE_H_ 7 8 #include "src/base/macros.h" 9 10 #include "include/v8.h" 11 #include "src/debug/interface-types.h" 12 13 namespace v8_inspector { 14 15 class InspectedContext; 16 class V8InspectorImpl; 17 18 // Console API 19 // https://console.spec.whatwg.org/#console-namespace 20 class V8Console : public v8::debug::ConsoleDelegate { 21 public: 22 v8::Local<v8::Object> createCommandLineAPI(v8::Local<v8::Context> context, 23 int sessionId); 24 void installMemoryGetter(v8::Local<v8::Context> context, 25 v8::Local<v8::Object> console); 26 27 class CommandLineAPIScope { 28 public: 29 CommandLineAPIScope(v8::Local<v8::Context>, 30 v8::Local<v8::Object> commandLineAPI, 31 v8::Local<v8::Object> global); 32 ~CommandLineAPIScope(); 33 34 private: 35 static void accessorGetterCallback( 36 v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value>&); 37 static void accessorSetterCallback(v8::Local<v8::Name>, 38 v8::Local<v8::Value>, 39 const v8::PropertyCallbackInfo<void>&); 40 41 v8::Local<v8::Context> m_context; 42 v8::Local<v8::Object> m_commandLineAPI; 43 v8::Local<v8::Object> m_global; 44 v8::Local<v8::Set> m_installedMethods; 45 v8::Local<v8::ArrayBuffer> m_thisReference; 46 47 DISALLOW_COPY_AND_ASSIGN(CommandLineAPIScope); 48 }; 49 50 explicit V8Console(V8InspectorImpl* inspector); 51 52 private: 53 void Debug(const v8::debug::ConsoleCallArguments&, 54 const v8::debug::ConsoleContext& consoleContext) override; 55 void Error(const v8::debug::ConsoleCallArguments&, 56 const v8::debug::ConsoleContext& consoleContext) override; 57 void Info(const v8::debug::ConsoleCallArguments&, 58 const v8::debug::ConsoleContext& consoleContext) override; 59 void Log(const v8::debug::ConsoleCallArguments&, 60 const v8::debug::ConsoleContext& consoleContext) override; 61 void Warn(const v8::debug::ConsoleCallArguments&, 62 const v8::debug::ConsoleContext& consoleContext) override; 63 void Dir(const v8::debug::ConsoleCallArguments&, 64 const v8::debug::ConsoleContext& consoleContext) override; 65 void DirXml(const v8::debug::ConsoleCallArguments&, 66 const v8::debug::ConsoleContext& consoleContext) override; 67 void Table(const v8::debug::ConsoleCallArguments&, 68 const v8::debug::ConsoleContext& consoleContext) override; 69 void Trace(const v8::debug::ConsoleCallArguments&, 70 const v8::debug::ConsoleContext& consoleContext) override; 71 void Group(const v8::debug::ConsoleCallArguments&, 72 const v8::debug::ConsoleContext& consoleContext) override; 73 void GroupCollapsed(const v8::debug::ConsoleCallArguments&, 74 const v8::debug::ConsoleContext& consoleContext) override; 75 void GroupEnd(const v8::debug::ConsoleCallArguments&, 76 const v8::debug::ConsoleContext& consoleContext) override; 77 void Clear(const v8::debug::ConsoleCallArguments&, 78 const v8::debug::ConsoleContext& consoleContext) override; 79 void Count(const v8::debug::ConsoleCallArguments&, 80 const v8::debug::ConsoleContext& consoleContext) override; 81 void CountReset(const v8::debug::ConsoleCallArguments&, 82 const v8::debug::ConsoleContext& consoleContext) override; 83 void Assert(const v8::debug::ConsoleCallArguments&, 84 const v8::debug::ConsoleContext& consoleContext) override; 85 void Profile(const v8::debug::ConsoleCallArguments&, 86 const v8::debug::ConsoleContext& consoleContext) override; 87 void ProfileEnd(const v8::debug::ConsoleCallArguments&, 88 const v8::debug::ConsoleContext& consoleContext) override; 89 void Time(const v8::debug::ConsoleCallArguments&, 90 const v8::debug::ConsoleContext& consoleContext) override; 91 void TimeLog(const v8::debug::ConsoleCallArguments&, 92 const v8::debug::ConsoleContext& consoleContext) override; 93 void TimeEnd(const v8::debug::ConsoleCallArguments&, 94 const v8::debug::ConsoleContext& consoleContext) override; 95 void TimeStamp(const v8::debug::ConsoleCallArguments&, 96 const v8::debug::ConsoleContext& consoleContext) override; 97 98 template <void (V8Console::*func)(const v8::FunctionCallbackInfo<v8::Value>&)> call(const v8::FunctionCallbackInfo<v8::Value> & info)99 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 100 V8Console* console = 101 static_cast<V8Console*>(info.Data().As<v8::External>()->Value()); 102 (console->*func)(info); 103 } 104 using CommandLineAPIData = std::pair<V8Console*, int>; 105 template <void (V8Console::*func)(const v8::FunctionCallbackInfo<v8::Value>&, 106 int)> call(const v8::FunctionCallbackInfo<v8::Value> & info)107 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 108 CommandLineAPIData* data = static_cast<CommandLineAPIData*>( 109 info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data()); 110 (data->first->*func)(info, data->second); 111 } 112 template <void (V8Console::*func)(const v8::debug::ConsoleCallArguments&, 113 const v8::debug::ConsoleContext&)> call(const v8::FunctionCallbackInfo<v8::Value> & info)114 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 115 CommandLineAPIData* data = static_cast<CommandLineAPIData*>( 116 info.Data().As<v8::ArrayBuffer>()->GetBackingStore()->Data()); 117 v8::debug::ConsoleCallArguments args(info); 118 (data->first->*func)(args, v8::debug::ConsoleContext()); 119 } 120 121 // TODO(foolip): There is no spec for the Memory Info API, see blink-dev: 122 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/g5YRCGpC9vs/b4OJz71NmPwJ 123 void memoryGetterCallback(const v8::FunctionCallbackInfo<v8::Value>&); 124 void memorySetterCallback(const v8::FunctionCallbackInfo<v8::Value>&); 125 126 // CommandLineAPI 127 void keysCallback(const v8::FunctionCallbackInfo<v8::Value>&, int sessionId); 128 void valuesCallback(const v8::FunctionCallbackInfo<v8::Value>&, 129 int sessionId); 130 void debugFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 131 int sessionId); 132 void undebugFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 133 int sessionId); 134 void monitorFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 135 int sessionId); 136 void unmonitorFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 137 int sessionId); 138 void lastEvaluationResultCallback(const v8::FunctionCallbackInfo<v8::Value>&, 139 int sessionId); 140 void inspectCallback(const v8::FunctionCallbackInfo<v8::Value>&, 141 int sessionId); 142 void copyCallback(const v8::FunctionCallbackInfo<v8::Value>&, int sessionId); 143 void inspectedObject(const v8::FunctionCallbackInfo<v8::Value>&, 144 int sessionId, unsigned num); inspectedObject0(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)145 void inspectedObject0(const v8::FunctionCallbackInfo<v8::Value>& info, 146 int sessionId) { 147 inspectedObject(info, sessionId, 0); 148 } inspectedObject1(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)149 void inspectedObject1(const v8::FunctionCallbackInfo<v8::Value>& info, 150 int sessionId) { 151 inspectedObject(info, sessionId, 1); 152 } inspectedObject2(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)153 void inspectedObject2(const v8::FunctionCallbackInfo<v8::Value>& info, 154 int sessionId) { 155 inspectedObject(info, sessionId, 2); 156 } inspectedObject3(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)157 void inspectedObject3(const v8::FunctionCallbackInfo<v8::Value>& info, 158 int sessionId) { 159 inspectedObject(info, sessionId, 3); 160 } inspectedObject4(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)161 void inspectedObject4(const v8::FunctionCallbackInfo<v8::Value>& info, 162 int sessionId) { 163 inspectedObject(info, sessionId, 4); 164 } 165 void queryObjectsCallback(const v8::FunctionCallbackInfo<v8::Value>& info, 166 int sessionId); 167 168 V8InspectorImpl* m_inspector; 169 }; 170 171 } // namespace v8_inspector 172 173 #endif // V8_INSPECTOR_V8_CONSOLE_H_ 174