1 // Copyright 2016 The Chromium 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 THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_ 7 8 #include <memory> 9 #include "base/macros.h" 10 #include "third_party/blink/public/mojom/devtools/console_message.mojom-blink-forward.h" 11 #include "third_party/blink/renderer/core/core_export.h" 12 #include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h" 13 #include "third_party/blink/renderer/platform/timer.h" 14 #include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h" 15 #include "third_party/blink/renderer/platform/wtf/forward.h" 16 #include "third_party/blink/renderer/platform/wtf/vector.h" 17 #include "v8/include/v8-inspector.h" 18 #include "v8/include/v8.h" 19 20 namespace blink { 21 22 class ExecutionContext; 23 class SourceLocation; 24 25 class CORE_EXPORT ThreadDebugger : public v8_inspector::V8InspectorClient, 26 public V8PerIsolateData::Data { 27 public: 28 explicit ThreadDebugger(v8::Isolate*); 29 ~ThreadDebugger() override; 30 31 static ThreadDebugger* From(v8::Isolate*); 32 virtual bool IsWorker() = 0; GetV8Inspector()33 v8_inspector::V8Inspector* GetV8Inspector() const { 34 return v8_inspector_.get(); 35 } 36 37 static void IdleStarted(v8::Isolate*); 38 static void IdleFinished(v8::Isolate*); 39 40 void AsyncTaskScheduled(const StringView& task_name, 41 void* task, 42 bool recurring); 43 void AsyncTaskCanceled(void* task); 44 void AllAsyncTasksCanceled(); 45 void AsyncTaskStarted(void* task); 46 void AsyncTaskFinished(void* task); 47 unsigned PromiseRejected(v8::Local<v8::Context>, 48 const String& error_message, 49 v8::Local<v8::Value> exception, 50 std::unique_ptr<SourceLocation>); 51 void PromiseRejectionRevoked(v8::Local<v8::Context>, 52 unsigned promise_rejection_id); 53 54 v8_inspector::V8StackTraceId StoreCurrentStackTrace( 55 const StringView& description); 56 void ExternalAsyncTaskStarted(const v8_inspector::V8StackTraceId& parent); 57 void ExternalAsyncTaskFinished(const v8_inspector::V8StackTraceId& parent); 58 59 protected: 60 virtual int ContextGroupId(ExecutionContext*) = 0; 61 virtual void ReportConsoleMessage(ExecutionContext*, 62 mojom::ConsoleMessageSource, 63 mojom::ConsoleMessageLevel, 64 const String& message, 65 SourceLocation*) = 0; 66 void installAdditionalCommandLineAPI(v8::Local<v8::Context>, 67 v8::Local<v8::Object>) override; 68 void CreateFunctionProperty(v8::Local<v8::Context>, 69 v8::Local<v8::Object>, 70 const char* name, 71 v8::FunctionCallback, 72 const char* description, 73 v8::SideEffectType side_effect_type); 74 static v8::Maybe<bool> CreateDataPropertyInArray(v8::Local<v8::Context>, 75 v8::Local<v8::Array>, 76 int index, 77 v8::Local<v8::Value>); 78 static mojom::ConsoleMessageLevel V8MessageLevelToMessageLevel( 79 v8::Isolate::MessageErrorLevel); 80 81 v8::Isolate* isolate_; 82 83 private: 84 // V8InspectorClient implementation. 85 void beginUserGesture() override; 86 void endUserGesture() override; 87 std::unique_ptr<v8_inspector::StringBuffer> valueSubtype( 88 v8::Local<v8::Value>) override; 89 bool formatAccessorsAsProperties(v8::Local<v8::Value>) override; 90 double currentTimeMS() override; 91 bool isInspectableHeapObject(v8::Local<v8::Object>) override; 92 void consoleTime(const v8_inspector::StringView& title) override; 93 void consoleTimeEnd(const v8_inspector::StringView& title) override; 94 void consoleTimeStamp(const v8_inspector::StringView& title) override; 95 void startRepeatingTimer(double, 96 v8_inspector::V8InspectorClient::TimerCallback, 97 void* data) override; 98 void cancelTimer(void* data) override; 99 100 void OnTimer(TimerBase*); 101 102 static void SetMonitorEventsCallback( 103 const v8::FunctionCallbackInfo<v8::Value>&, 104 bool enabled); 105 static void MonitorEventsCallback(const v8::FunctionCallbackInfo<v8::Value>&); 106 static void UnmonitorEventsCallback( 107 const v8::FunctionCallbackInfo<v8::Value>&); 108 109 static void GetEventListenersCallback( 110 const v8::FunctionCallbackInfo<v8::Value>&); 111 112 std::unique_ptr<v8_inspector::V8Inspector> v8_inspector_; 113 Vector<std::unique_ptr<TaskRunnerTimer<ThreadDebugger>>> timers_; 114 Vector<v8_inspector::V8InspectorClient::TimerCallback> timer_callbacks_; 115 Vector<void*> timer_data_; 116 DISALLOW_COPY_AND_ASSIGN(ThreadDebugger); 117 }; 118 119 } // namespace blink 120 121 namespace WTF { 122 123 template <> 124 struct CrossThreadCopier<v8_inspector::V8StackTraceId> { 125 typedef v8_inspector::V8StackTraceId Type; 126 static Type Copy(const Type& id) { return id; } 127 }; 128 129 } // namespace WTF 130 131 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_ 132