-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinspector_client.cpp
More file actions
173 lines (153 loc) · 5.53 KB
/
Copy pathinspector_client.cpp
File metadata and controls
173 lines (153 loc) · 5.53 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <boost/beast/websocket/stream.hpp>
#include <boost/asio/buffer.hpp>
#include <qurl.h>
#include <qcoreapplication.h>
#include "framework.h"
#include "inspector_client.h"
namespace shelllet {
const int kContextGroupId = 1;
class DispatchMessagesTask : public v8::Task {
public:
explicit DispatchMessagesTask(inspect::InspectorClient* agent, const std::string& message)
: agent_(agent)
, message_(message) {
}
void Run() override {
if (agent_ != nullptr) {
agent_->dispatchProtocolMessage(message_);
}
}
private:
inspect::InspectorClient* agent_;
std::string message_;
};
extern v8::Global<v8::Context> global_context;
}
shelllet::inspect::InspectorClient::InspectorClient(QObject* receiver, const v8::Local<v8::Context>& context, v8::Platform* platform)
: isolate_(context->GetIsolate())
, platform_(platform)
, receiver_(receiver)
{
std::string name = "{}";
inspector_ = v8_inspector::V8Inspector::create(isolate_, this);
inspector_->contextCreated(v8_inspector::V8ContextInfo(
context, kContextGroupId, v8_inspector::StringView(reinterpret_cast<const std::uint8_t*>(name.c_str()), name.length())));
channel_.reset(new InspectorChannel(inspector_.get(), this));
}
void shelllet::inspect::InspectorClient::sendMessageToFrontend(const v8_inspector::StringView& string)
{
int length = static_cast<int>(string.length());
v8::Local<v8::String> message = (string.is8Bit()
? v8::String::NewFromOneByte(
isolate_,
reinterpret_cast<const uint8_t*>(string.characters8())/*,
v8::NewStringType::kNormal, length*/)
: v8::String::NewFromTwoByte(
isolate_,
reinterpret_cast<const uint16_t*>(string.characters16())/*,
v8::NewStringType::kNormal, length*/))
.ToLocalChecked();
std::string resp = ToString({ isolate_, message });
QCoreApplication::postEvent(receiver_, new MessageOutEvent(resp));
}
void shelllet::inspect::InspectorClient::runMessageLoopOnPause(int contextGroupId)
{
is_paused_ = true;
while (is_paused_ && waitForFrontendMessageWhilePaused()) {
pumpMessageLoop();
}
is_paused_ = false;
}
void shelllet::inspect::InspectorClient::quitMessageLoopOnPause()
{
is_paused_ = false;
}
void shelllet::inspect::InspectorClient::pauseOnNextJavascriptStatement(const std::string& reason)
{
if (channel_)
channel_->pauseOnNextJavascriptStatement(reason);
}
double shelllet::inspect::InspectorClient::currentTimeMS()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}
void shelllet::inspect::InspectorClient::runIfWaitingForDebugger(int contextGroupId)
{
pauseOnNextJavascriptStatement("Break at bootstrap");
QCoreApplication::postEvent(receiver_, new ContinueRunEvent);
}
//
//std::unique_ptr<v8_inspector::StringBuffer> shelllet::inspect::InspectorClient::resourceNameToUrl(const v8_inspector::StringView& resourceName)
//{
// v8::Locker locker(isolate_);
// v8::HandleScope handle_scope(isolate_);
// int length = static_cast<int>(resourceName.length());
// //DCHECK_LT(length, v8::String::kMaxLength);
// v8::Local<v8::String> message =
// (resourceName.is8Bit()
// ? v8::String::NewFromOneByte(
// isolate_,
// reinterpret_cast<const uint8_t*>(resourceName.characters8()),
// v8::NewStringType::kNormal, length)
// : v8::String::NewFromTwoByte(
// isolate_,
// reinterpret_cast<const uint16_t*>(resourceName.characters16()),
// v8::NewStringType::kNormal, length))
// .ToLocalChecked();
// std::filesystem::path file = shelllet::ToString({ isolate_, message });
// //file = "E:\\vision\\debug.js";
// if (!std::filesystem::is_regular_file(file))
// return nullptr;
//
// std::string url = QUrl::fromLocalFile(QString::fromUtf8(file.string().c_str())).url().toStdString();
// return v8_inspector::StringBuffer::create({ reinterpret_cast<const uint8_t*> (url.c_str()), url.size() });
//}
void shelllet::inspect::InspectorClient::postInspectorMessage(const std::string& message)
{
//v8::Locker locker(isolate_);
//v8::Isolate::Scope isolate_scope(isolate_);
//v8::HandleScope handle_scope(isolate_);
platform_->GetForegroundTaskRunner(isolate_)->PostTask(std::make_unique<DispatchMessagesTask>(this, message));
if (is_paused_)
is_ready_ = true;
else {
QCoreApplication::postEvent(receiver_, new PumpMessageLoopEvent);
}
}
void shelllet::inspect::InspectorClient::consoleAPIMessage(int contextGroupId, v8::Isolate::MessageErrorLevel level, const v8_inspector::StringView& message, const v8_inspector::StringView& url, unsigned lineNumber, unsigned columnNumber, v8_inspector::V8StackTrace*)
{
throw std::logic_error("The method or operation is not implemented.");
}
v8::Local<v8::Context> shelllet::inspect::InspectorClient::ensureDefaultContextInGroup(int groupId)
{
return global_context.Get(isolate_);
}
void shelllet::inspect::InspectorClient::dispatchProtocolMessage(const std::string& message)
{
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
channel_->dispatchProtocolMessage(message);
}
bool shelllet::inspect::InspectorClient::waitForFrontendMessageWhilePaused()
{
while (!is_ready_) {
QCoreApplication::processEvents();
}
is_ready_ = !is_ready_;
return true;
}
void shelllet::inspect::InspectorClient::disconnectFrontend()
{
is_ready_ = true;
quitMessageLoopOnPause();
}
void shelllet::inspect::InspectorClient::pumpMessageLoop()
{
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
while (v8::platform::PumpMessageLoop(platform_, isolate_)) {
QCoreApplication::processEvents();
}
}