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 bool m_cleanup; 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 TimeEnd(const v8::debug::ConsoleCallArguments&, 92 const v8::debug::ConsoleContext& consoleContext) override; 93 void TimeStamp(const v8::debug::ConsoleCallArguments&, 94 const v8::debug::ConsoleContext& consoleContext) override; 95 96 template <void (V8Console::*func)(const v8::FunctionCallbackInfo<v8::Value>&)> call(const v8::FunctionCallbackInfo<v8::Value> & info)97 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 98 V8Console* console = 99 static_cast<V8Console*>(info.Data().As<v8::External>()->Value()); 100 (console->*func)(info); 101 } 102 using CommandLineAPIData = std::pair<V8Console*, int>; 103 template <void (V8Console::*func)(const v8::FunctionCallbackInfo<v8::Value>&, 104 int)> call(const v8::FunctionCallbackInfo<v8::Value> & info)105 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 106 CommandLineAPIData* data = static_cast<CommandLineAPIData*>( 107 info.Data().As<v8::ArrayBuffer>()->GetContents().Data()); 108 (data->first->*func)(info, data->second); 109 } 110 template <void (V8Console::*func)(const v8::debug::ConsoleCallArguments&, 111 const v8::debug::ConsoleContext&)> call(const v8::FunctionCallbackInfo<v8::Value> & info)112 static void call(const v8::FunctionCallbackInfo<v8::Value>& info) { 113 CommandLineAPIData* data = static_cast<CommandLineAPIData*>( 114 info.Data().As<v8::ArrayBuffer>()->GetContents().Data()); 115 v8::debug::ConsoleCallArguments args(info); 116 (data->first->*func)(args, v8::debug::ConsoleContext()); 117 } 118 119 // TODO(foolip): There is no spec for the Memory Info API, see blink-dev: 120 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/g5YRCGpC9vs/b4OJz71NmPwJ 121 void memoryGetterCallback(const v8::FunctionCallbackInfo<v8::Value>&); 122 void memorySetterCallback(const v8::FunctionCallbackInfo<v8::Value>&); 123 124 // CommandLineAPI 125 void keysCallback(const v8::FunctionCallbackInfo<v8::Value>&, int sessionId); 126 void valuesCallback(const v8::FunctionCallbackInfo<v8::Value>&, 127 int sessionId); 128 void debugFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 129 int sessionId); 130 void undebugFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 131 int sessionId); 132 void monitorFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 133 int sessionId); 134 void unmonitorFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>&, 135 int sessionId); 136 void lastEvaluationResultCallback(const v8::FunctionCallbackInfo<v8::Value>&, 137 int sessionId); 138 void inspectCallback(const v8::FunctionCallbackInfo<v8::Value>&, 139 int sessionId); 140 void copyCallback(const v8::FunctionCallbackInfo<v8::Value>&, int sessionId); 141 void inspectedObject(const v8::FunctionCallbackInfo<v8::Value>&, 142 int sessionId, unsigned num); inspectedObject0(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)143 void inspectedObject0(const v8::FunctionCallbackInfo<v8::Value>& info, 144 int sessionId) { 145 inspectedObject(info, sessionId, 0); 146 } inspectedObject1(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)147 void inspectedObject1(const v8::FunctionCallbackInfo<v8::Value>& info, 148 int sessionId) { 149 inspectedObject(info, sessionId, 1); 150 } inspectedObject2(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)151 void inspectedObject2(const v8::FunctionCallbackInfo<v8::Value>& info, 152 int sessionId) { 153 inspectedObject(info, sessionId, 2); 154 } inspectedObject3(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)155 void inspectedObject3(const v8::FunctionCallbackInfo<v8::Value>& info, 156 int sessionId) { 157 inspectedObject(info, sessionId, 3); 158 } inspectedObject4(const v8::FunctionCallbackInfo<v8::Value> & info,int sessionId)159 void inspectedObject4(const v8::FunctionCallbackInfo<v8::Value>& info, 160 int sessionId) { 161 inspectedObject(info, sessionId, 4); 162 } 163 void queryObjectsCallback(const v8::FunctionCallbackInfo<v8::Value>& info, 164 int sessionId); 165 166 V8InspectorImpl* m_inspector; 167 }; 168 169 } // namespace v8_inspector 170 171 #endif // V8_INSPECTOR_V8_CONSOLE_H_ 172