summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/inspector/thread_debugger.h
blob: 7adb02fcbea856a8101d964339f25ca463f304e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_

#include <memory>
#include "base/macros.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/user_gesture_indicator.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "v8/include/v8-inspector.h"
#include "v8/include/v8.h"

namespace blink {

class ExecutionContext;
class SourceLocation;

class CORE_EXPORT ThreadDebugger : public v8_inspector::V8InspectorClient,
                                   public V8PerIsolateData::Data {
 public:
  explicit ThreadDebugger(v8::Isolate*);
  ~ThreadDebugger() override;

  static ThreadDebugger* From(v8::Isolate*);
  virtual bool IsWorker() = 0;
  v8_inspector::V8Inspector* GetV8Inspector() const {
    return v8_inspector_.get();
  }

  static void IdleStarted(v8::Isolate*);
  static void IdleFinished(v8::Isolate*);

  void AsyncTaskScheduled(const StringView& task_name,
                          void* task,
                          bool recurring);
  void AsyncTaskCanceled(void* task);
  void AllAsyncTasksCanceled();
  void AsyncTaskStarted(void* task);
  void AsyncTaskFinished(void* task);
  unsigned PromiseRejected(v8::Local<v8::Context>,
                           const String& error_message,
                           v8::Local<v8::Value> exception,
                           std::unique_ptr<SourceLocation>);
  void PromiseRejectionRevoked(v8::Local<v8::Context>,
                               unsigned promise_rejection_id);

  v8_inspector::V8StackTraceId StoreCurrentStackTrace(
      const StringView& description);
  void ExternalAsyncTaskStarted(const v8_inspector::V8StackTraceId& parent);
  void ExternalAsyncTaskFinished(const v8_inspector::V8StackTraceId& parent);

 protected:
  virtual int ContextGroupId(ExecutionContext*) = 0;
  virtual void ReportConsoleMessage(ExecutionContext*,
                                    mojom::ConsoleMessageSource,
                                    mojom::ConsoleMessageLevel,
                                    const String& message,
                                    SourceLocation*) = 0;
  void installAdditionalCommandLineAPI(v8::Local<v8::Context>,
                                       v8::Local<v8::Object>) override;
  void CreateFunctionProperty(v8::Local<v8::Context>,
                              v8::Local<v8::Object>,
                              const char* name,
                              v8::FunctionCallback,
                              const char* description,
                              v8::SideEffectType side_effect_type);
  static v8::Maybe<bool> CreateDataPropertyInArray(v8::Local<v8::Context>,
                                                   v8::Local<v8::Array>,
                                                   int index,
                                                   v8::Local<v8::Value>);
  static mojom::ConsoleMessageLevel V8MessageLevelToMessageLevel(
      v8::Isolate::MessageErrorLevel);

  v8::Isolate* isolate_;

 private:
  // V8InspectorClient implementation.
  void beginUserGesture() override;
  void endUserGesture() override;
  std::unique_ptr<v8_inspector::StringBuffer> valueSubtype(
      v8::Local<v8::Value>) override;
  bool formatAccessorsAsProperties(v8::Local<v8::Value>) override;
  double currentTimeMS() override;
  bool isInspectableHeapObject(v8::Local<v8::Object>) override;
  void consoleTime(const v8_inspector::StringView& title) override;
  void consoleTimeEnd(const v8_inspector::StringView& title) override;
  void consoleTimeStamp(const v8_inspector::StringView& title) override;
  void startRepeatingTimer(double,
                           v8_inspector::V8InspectorClient::TimerCallback,
                           void* data) override;
  void cancelTimer(void* data) override;

  void OnTimer(TimerBase*);

  static void SetMonitorEventsCallback(
      const v8::FunctionCallbackInfo<v8::Value>&,
      bool enabled);
  static void MonitorEventsCallback(const v8::FunctionCallbackInfo<v8::Value>&);
  static void UnmonitorEventsCallback(
      const v8::FunctionCallbackInfo<v8::Value>&);

  static void GetEventListenersCallback(
      const v8::FunctionCallbackInfo<v8::Value>&);

  std::unique_ptr<v8_inspector::V8Inspector> v8_inspector_;
  Vector<std::unique_ptr<TaskRunnerTimer<ThreadDebugger>>> timers_;
  Vector<v8_inspector::V8InspectorClient::TimerCallback> timer_callbacks_;
  Vector<void*> timer_data_;
  std::unique_ptr<UserGestureIndicator> user_gesture_indicator_;
  DISALLOW_COPY_AND_ASSIGN(ThreadDebugger);
};

}  // namespace blink

namespace WTF {

template <>
struct CrossThreadCopier<v8_inspector::V8StackTraceId> {
  typedef v8_inspector::V8StackTraceId Type;
  static Type Copy(const Type& id) { return id; }
};

}  // namespace WTF

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_INSPECTOR_THREAD_DEBUGGER_H_