-
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathJsV8InspectorClient.cpp
More file actions
266 lines (211 loc) · 10.6 KB
/
JsV8InspectorClient.cpp
File metadata and controls
266 lines (211 loc) · 10.6 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "JsV8InspectorClient.h"
#include <sstream>
#include <assert.h>
#include <include/libplatform/libplatform.h>
#include <v8_inspector/src/inspector/v8-log-agent-impl.h>
#include "Runtime.h"
#include "NativeScriptException.h"
#include "ArgConverter.h"
#include "DOMDomainCallbackHandlers.h"
#include "NetworkDomainCallbackHandlers.h"
using namespace std;
using namespace tns;
using namespace v8;
using namespace v8_inspector;
JsV8InspectorClient::JsV8InspectorClient(v8::Isolate* isolate)
: isolate_(isolate),
inspector_(nullptr),
session_(nullptr),
connection(nullptr),
context_(),
running_nested_loop_(false),
isConnected(false) {
JEnv env;
inspectorClass = env.FindClass("com/tns/AndroidJsV8Inspector");
assert(inspectorClass != nullptr);
sendMethod = env.GetStaticMethodID(inspectorClass, "send", "(Ljava/lang/Object;Ljava/lang/String;)V");
assert(sendMethod != nullptr);
sendToDevToolsConsoleMethod = env.GetStaticMethodID(inspectorClass, "sendToDevToolsConsole", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V");
assert(sendToDevToolsConsoleMethod != nullptr);
getInspectorMessageMethod = env.GetStaticMethodID(inspectorClass, "getInspectorMessage", "(Ljava/lang/Object;)Ljava/lang/String;");
assert(getInspectorMessageMethod != nullptr);
}
void JsV8InspectorClient::connect(jobject connection) {
JEnv env;
this->connection = env.NewGlobalRef(connection);
this->isConnected = true;
}
void JsV8InspectorClient::scheduleBreak() {
Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handleScope(isolate_);
this->session_->schedulePauseOnNextStatement(v8_inspector::StringView(), v8_inspector::StringView());
}
void JsV8InspectorClient::createInspectorSession(v8::Isolate* isolate, const v8::Local<v8::Context>& context) {
session_ = inspector_->connect(0, this, v8_inspector::StringView());
}
void JsV8InspectorClient::disconnect() {
if (this->connection == nullptr) {
return;
}
Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handleScope(isolate_);
session_->resume();
session_.reset();
JEnv env;
env.DeleteGlobalRef(this->connection);
this->connection = nullptr;
this->isConnected = false;
this->createInspectorSession(isolate_, JsV8InspectorClient::PersistentToLocal(isolate_, context_));
}
void JsV8InspectorClient::dispatchMessage(const std::string& message) {
Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handleScope(isolate_);
Context::Scope context_scope(isolate_->GetCurrentContext());
this->doDispatchMessage(isolate_, message);
}
void JsV8InspectorClient::runMessageLoopOnPause(int context_group_id) {
if (running_nested_loop_) {
return;
}
JEnv env;
terminated_ = false;
running_nested_loop_ = true;
while (!terminated_) {
JniLocalRef msg(env.CallStaticObjectMethod(inspectorClass, getInspectorMessageMethod, this->connection));
if (!msg.IsNull()) {
auto inspectorMessage = ArgConverter::jstringToString(msg);
this->doDispatchMessage(this->isolate_, inspectorMessage);
}
while (v8::platform::PumpMessageLoop(Runtime::platform, isolate_)) {
}
}
terminated_ = false;
running_nested_loop_ = false;
}
void JsV8InspectorClient::quitMessageLoopOnPause() {
terminated_ = true;
}
v8::Local<v8::Context> JsV8InspectorClient::ensureDefaultContextInGroup(int contextGroupId) {
v8::Local<v8::Context> context = PersistentToLocal(isolate_, context_);
return context;
}
void JsV8InspectorClient::doDispatchMessage(v8::Isolate* isolate, const std::string& message) {
if (session_ == nullptr) {
return;
}
const String16 msg(message.c_str());
v8_inspector::StringView message_view(reinterpret_cast<const uint16_t*>(msg.characters16()), msg.length());
session_->dispatchProtocolMessage(message_view);
}
void JsV8InspectorClient::sendProtocolResponse(int callId, const v8_inspector::StringView& message) {
sendProtocolNotification(message);
}
static v8_inspector::String16 ToString16(const v8_inspector::StringView& string) {
if (string.is8Bit()) {
return v8_inspector::String16(reinterpret_cast<const char*>(string.characters8()), string.length());
}
return v8_inspector::String16(reinterpret_cast<const uint16_t*>(string.characters16()), string.length());
}
void JsV8InspectorClient::sendProtocolNotification(const v8_inspector::StringView& message) {
if (inspectorClass == nullptr || this->connection == nullptr) {
return;
}
v8_inspector::String16 msg = ToString16(message);
JEnv env;
JniLocalRef str(env.NewStringUTF(msg.utf8().c_str()));
env.CallStaticVoidMethod(inspectorClass, sendMethod, this->connection, (jstring) str);
}
void JsV8InspectorClient::flushProtocolNotifications() {
}
template<class TypeName>
inline v8::Local<TypeName> StrongPersistentToLocal(const v8::Persistent<TypeName>& persistent) {
return *reinterpret_cast<v8::Local<TypeName> *>(const_cast<v8::Persistent<TypeName> *>(&persistent));
}
template<class TypeName>
inline v8::Local<TypeName> WeakPersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent) {
return v8::Local<TypeName>::New(isolate, persistent);
}
template<class TypeName>
inline v8::Local<TypeName> JsV8InspectorClient::PersistentToLocal(v8::Isolate* isolate, const v8::Persistent<TypeName>& persistent) {
if (persistent.IsWeak()) {
return WeakPersistentToLocal(isolate, persistent);
} else {
return StrongPersistentToLocal(persistent);
}
}
void JsV8InspectorClient::init() {
if (inspector_ != nullptr) {
return;
}
v8::HandleScope handle_scope(isolate_);
v8::Local<Context> context = isolate_->GetCurrentContext();
inspector_ = V8Inspector::create(isolate_, this);
inspector_->contextCreated(v8_inspector::V8ContextInfo(context, 0, v8_inspector::StringView()));
v8::Persistent<v8::Context> persistentContext(context->GetIsolate(), JsV8InspectorClient::PersistentToLocal(isolate_, context_));
context_.Reset(isolate_, persistentContext);
this->createInspectorSession(isolate_, context);
}
JsV8InspectorClient* JsV8InspectorClient::GetInstance() {
if (instance == nullptr) {
instance = new JsV8InspectorClient(Runtime::GetRuntime(0)->GetIsolate());
}
return instance;
}
void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> exception) {
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
// v8::Local<v8::Context> context = isolate->GetEnteredContext();
// if (context.IsEmpty()) return;
// v8_inspector::V8Inspector *inspector = InspectorClientImpl::InspectorFromContext(context);
//
// v8::Local<v8::StackTrace> stack = message->GetStackTrace();
// int script_id = message->GetScriptOrigin().ScriptID()->Value();
// if (!stack.IsEmpty() && stack->GetFrameCount() > 0)
// {
// int top_script_id = stack->GetFrame(0)->GetScriptId();
// if (top_script_id == script_id) script_id = 0;
// }
// int line_number = message->GetLineNumber(context).FromMaybe(0);
// int column_number = 0;
// if (message->GetStartColumn(context).IsJust())
// column_number = message->GetStartColumn(context).FromJust() + 1;
//
// v8_inspector::StringView detailed_message;
// v8_inspector::String16 message_text_string = ToString16(message->Get());
// v8_inspector::StringView message_text(message_text_string.characters16(),
// message_text_string.length());
// v8_inspector::String16 url_string;
// if (message->GetScriptOrigin().ResourceName()->IsString())
// {
// url_string =
// ToString16(message->GetScriptOrigin().ResourceName().As<v8::String>());
// }
// v8_inspector::StringView url(url_string.characters16(), url_string.length());
//
// inspector->exceptionThrown(context, message_text, exception, detailed_message,
// url, line_number, column_number,
// inspector->createStackTrace(stack), script_id);
}
void JsV8InspectorClient::attachInspectorCallbacks(Isolate* isolate,
Local<ObjectTemplate>& globalObjectTemplate) {
v8::HandleScope scope(isolate);
auto inspectorJSObject = ObjectTemplate::New(isolate);
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "responseReceived"), FunctionTemplate::New(isolate, NetworkDomainCallbackHandlers::ResponseReceivedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "requestWillBeSent"), FunctionTemplate::New(isolate, NetworkDomainCallbackHandlers::RequestWillBeSentCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "dataForRequestId"), FunctionTemplate::New(isolate, NetworkDomainCallbackHandlers::DataForRequestIdCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "loadingFinished"), FunctionTemplate::New(isolate, NetworkDomainCallbackHandlers::LoadingFinishedCallback));
inspectorJSObject->SetAccessor(ArgConverter::ConvertToV8String(isolate, "isConnected"), JsV8InspectorClient::InspectorIsConnectedGetterCallback);
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "documentUpdated"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::DocumentUpdatedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "childNodeInserted"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::ChildNodeInsertedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "childNodeRemoved"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::ChildNodeRemovedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "attributeModified"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::AttributeModifiedCallback));
inspectorJSObject->Set(ArgConverter::ConvertToV8String(isolate, "attributeRemoved"), FunctionTemplate::New(isolate, DOMDomainCallbackHandlers::AttributeRemovedCallback));
globalObjectTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__inspector"), inspectorJSObject);
}
void JsV8InspectorClient::InspectorIsConnectedGetterCallback(v8::Local<v8::String> property, const v8::PropertyCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(JsV8InspectorClient::GetInstance()->isConnected);
}
JsV8InspectorClient* JsV8InspectorClient::instance = nullptr;
jclass JsV8InspectorClient::inspectorClass = nullptr;
jmethodID JsV8InspectorClient::sendMethod = nullptr;
jmethodID JsV8InspectorClient::sendToDevToolsConsoleMethod = nullptr;
jmethodID JsV8InspectorClient::getInspectorMessageMethod = nullptr;