Skip to content

Commit d3de77b

Browse files
committed
enabled showing messages in dev tools console
when __toFrontEnd(message) is called from JS the messages is sent to Chrome Dev Tools still need to choose if we wan't to export this new function or use the old one - __consoleMessage
1 parent fac0db9 commit d3de77b

File tree

5 files changed

+103
-43
lines changed

5 files changed

+103
-43
lines changed

runtime/src/main/java/com/tns/AndroidJsV8Inspector.java

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import android.os.Handler;
55
import android.util.Log;
66

7+
import org.json.JSONArray;
8+
import org.json.JSONException;
9+
import org.json.JSONObject;
10+
711
import java.io.IOException;
812
import java.util.concurrent.ConcurrentLinkedQueue;
913
import java.util.concurrent.LinkedBlockingQueue;
@@ -27,19 +31,17 @@ public class AndroidJsV8Inspector
2731
protected static native final void disconnect();
2832

2933
protected native final void dispatchMessage(String message);
34+
3035
protected Handler mainHandler;
3136
private LinkedBlockingQueue<String> inspectorMessages = new LinkedBlockingQueue<String>();
3237

33-
public AndroidJsV8Inspector(Context context, Logger logger)
34-
{
38+
public AndroidJsV8Inspector(Context context, Logger logger) {
3539
this.context = context;
3640
this.logger = logger;
3741
}
3842

39-
public void start() throws IOException
40-
{
41-
if (this.server == null)
42-
{
43+
public void start() throws IOException {
44+
if (this.server == null) {
4345
Runtime currentRuntime = Runtime.getCurrentRuntime();
4446

4547
mainHandler = currentRuntime.getHandler();
@@ -56,20 +58,46 @@ public void start() throws IOException
5658
}
5759
}
5860

59-
private static void send(Object connection, String payload) throws IOException
60-
{
61+
@RuntimeCallable
62+
private static void sendToDevToolsConsole(Object connection, String message, String level) {
63+
//{"method":"Runtime.consoleAPICalled","params":{"type":"log","args":["asdjasdkljasd"],"executionContextId":0,"timestamp":0.000000000000000}}
64+
try {
65+
JSONObject consoleMessage = new JSONObject();
66+
67+
JSONObject params = new JSONObject();
68+
params.put("type", level);
69+
params.put("executionContextId", 0);
70+
params.put("timestamp", 0.000000000000000);
71+
72+
JSONArray args = new JSONArray();
73+
args.put(message);
74+
params.put("args", args);
75+
76+
consoleMessage.put("method", "Runtime.consoleAPICalled");
77+
consoleMessage.put("params", params);
78+
79+
String sendingText = consoleMessage.toString();
80+
AndroidJsV8Inspector.send(connection, sendingText);
81+
82+
} catch (JSONException e) {
83+
e.printStackTrace();
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
89+
@RuntimeCallable
90+
private static void send(Object connection, String payload) throws IOException {
6191
((JsV8InspectorWebSocket) connection).send(payload);
6292
}
6393

64-
private static String getInspectorMessage(Object connection)
65-
{
94+
@RuntimeCallable
95+
private static String getInspectorMessage(Object connection) {
6696
return ((JsV8InspectorWebSocket) connection).getInspectorMessage();
6797
}
6898

69-
class JsV8InspectorServer extends NanoWSD
70-
{
71-
public JsV8InspectorServer(String name)
72-
{
99+
class JsV8InspectorServer extends NanoWSD {
100+
public JsV8InspectorServer(String name) {
73101
super(name);
74102
}
75103

@@ -84,17 +112,14 @@ protected Response serveHttp(IHTTPSession session)
84112
}
85113

86114
@Override
87-
protected WebSocket openWebSocket(IHTTPSession handshake)
88-
{
115+
protected WebSocket openWebSocket(IHTTPSession handshake) {
89116
return new JsV8InspectorWebSocket(handshake);
90117
}
91118
}
92119

93-
class JsV8InspectorWebSocket extends NanoWSD.WebSocket
94-
{
120+
class JsV8InspectorWebSocket extends NanoWSD.WebSocket {
95121

96-
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
97-
{
122+
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
98123
super(handshakeRequest);
99124
}
100125

@@ -106,8 +131,7 @@ protected void onOpen()
106131
Log.d("V8Inspector", "onOpen: ThreadID: " + Thread.currentThread().getId());
107132
}
108133

109-
mainHandler.post(new Runnable()
110-
{
134+
mainHandler.post(new Runnable() {
111135
@Override
112136
public void run()
113137
{
@@ -153,14 +177,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153177

154178
inspectorMessages.offer(message.getTextPayload());
155179

156-
mainHandler.post(new Runnable()
157-
{
180+
mainHandler.post(new Runnable() {
158181
@Override
159-
public void run()
160-
{
182+
public void run() {
161183
String nextMessage = inspectorMessages.poll();
162-
while (nextMessage != null)
163-
{
184+
while (nextMessage != null) {
164185
dispatchMessage(nextMessage);
165186
nextMessage = inspectorMessages.poll();
166187
}
@@ -179,15 +200,11 @@ public void send(String payload) throws IOException
179200
super.send(payload);
180201
}
181202

182-
public String getInspectorMessage()
183-
{
184-
try
185-
{
203+
public String getInspectorMessage() {
204+
try {
186205
String message = inspectorMessages.take();
187206
return message;
188-
}
189-
catch (InterruptedException e)
190-
{
207+
} catch (InterruptedException e) {
191208
e.printStackTrace();
192209
}
193210

@@ -200,10 +217,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200217
}
201218

202219
@Override
203-
protected void onException(IOException exception)
204-
{
220+
protected void onException(IOException exception) {
205221
exception.printStackTrace();
206222
disconnect();
207223
}
208224
}
209-
}
225+
}

runtime/src/main/jni/JsV8InspectorClient.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <assert.h>
99
#include <include/libplatform/libplatform.h>
1010
#include "Runtime.h"
11-
#include "src/inspector/string-16.h"
11+
#include "NativeScriptException.h"
1212

1313
#include "ArgConverter.h"
1414

@@ -33,6 +33,9 @@ JsV8InspectorClient::JsV8InspectorClient(v8::Isolate *isolate)
3333
sendMethod = env.GetStaticMethodID(inspectorClass, "send", "(Ljava/lang/Object;Ljava/lang/String;)V");
3434
assert(sendMethod != nullptr);
3535

36+
sendToDevToolsConsoleMethod = env.GetStaticMethodID(inspectorClass, "sendToDevToolsConsole", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V");
37+
assert(sendToDevToolsConsoleMethod != nullptr);
38+
3639
getInspectorMessageMethod = env.GetStaticMethodID(inspectorClass, "getInspectorMessage", "(Ljava/lang/Object;)Ljava/lang/String;");
3740
assert(getInspectorMessageMethod != nullptr);
3841
}
@@ -152,8 +155,8 @@ void JsV8InspectorClient::sendProtocolNotification(const v8_inspector::StringVie
152155

153156
JEnv env;
154157
const char *msss = msg.utf8().c_str();
155-
JniLocalRef string(env.NewStringUTF(msg.utf8().c_str()));
156-
env.CallStaticVoidMethod(inspectorClass, sendMethod, this->connection, (jstring) string);
158+
JniLocalRef str(env.NewStringUTF(msg.utf8().c_str()));
159+
env.CallStaticVoidMethod(inspectorClass, sendMethod, this->connection, (jstring) str);
157160
}
158161

159162
void JsV8InspectorClient::flushProtocolNotifications()
@@ -197,7 +200,6 @@ void JsV8InspectorClient::init()
197200
v8::Local<Context> context = Context::New(isolate_);
198201
v8::Context::Scope context_scope(context);
199202

200-
201203
inspector_ = V8Inspector::create(isolate_, this);
202204

203205
inspector_->contextCreated(v8_inspector::V8ContextInfo(context, 0, v8_inspector::StringView()));
@@ -218,6 +220,42 @@ JsV8InspectorClient *JsV8InspectorClient::GetInstance()
218220
return instance;
219221
}
220222

223+
224+
void JsV8InspectorClient::sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
225+
try
226+
{
227+
if ((args.Length() > 0) && args[0]->IsString())
228+
{
229+
std::string message = ArgConverter::ConvertToString(args[0]->ToString());
230+
231+
std:string level = "log";
232+
if (args.Length() > 1 && args[1]->IsString())
233+
{
234+
level = ArgConverter::ConvertToString(args[1]->ToString());
235+
}
236+
237+
JEnv env;
238+
JniLocalRef str(env.NewStringUTF(message.c_str()));
239+
JniLocalRef lev(env.NewStringUTF(level.c_str()));
240+
env.CallStaticVoidMethod(inspectorClass, sendToDevToolsConsoleMethod, instance->connection, (jstring) str, (jstring)lev);
241+
}
242+
}
243+
catch (NativeScriptException& e)
244+
{
245+
e.ReThrowToV8();
246+
}
247+
catch (std::exception e) {
248+
stringstream ss;
249+
ss << "Error: c++ exception: " << e.what() << endl;
250+
NativeScriptException nsEx(ss.str());
251+
nsEx.ReThrowToV8();
252+
}
253+
catch (...) {
254+
NativeScriptException nsEx(std::string("Error: c++ exception!"));
255+
nsEx.ReThrowToV8();
256+
}
257+
}
258+
221259
void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> exception)
222260
{
223261
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
@@ -257,6 +295,7 @@ void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> excepti
257295
JsV8InspectorClient *JsV8InspectorClient::instance = nullptr;
258296
jclass JsV8InspectorClient::inspectorClass = nullptr;
259297
jmethodID JsV8InspectorClient::sendMethod = nullptr;
298+
jmethodID JsV8InspectorClient::sendToDevToolsConsoleMethod = nullptr;
260299
jmethodID JsV8InspectorClient::getInspectorMessageMethod = nullptr;
261300

262301

runtime/src/main/jni/JsV8InspectorClient.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace tns
3535
void sendProtocolNotification(const v8_inspector::StringView &message) override;
3636
void flushProtocolNotifications() override;
3737

38+
static void sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
39+
3840
void runMessageLoopOnPause(int context_group_id) override;
3941
void quitMessageLoopOnPause() override;
4042
v8::Local<v8::Context> ensureDefaultContextInGroup(int contextGroupId) override;
@@ -46,6 +48,7 @@ namespace tns
4648
static jclass inspectorClass;
4749
static jmethodID sendMethod;
4850
static jmethodID getInspectorMessageMethod;
51+
static jmethodID sendToDevToolsConsoleMethod;
4952

5053
v8::Isolate* isolate_;
5154
v8::Persistent<v8::Context> context_;

runtime/src/main/jni/Runtime.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sstream>
2525
#include <dlfcn.h>
2626
#include "sys/system_properties.h"
27+
#include "JsV8InspectorClient.h"
2728

2829
using namespace v8;
2930
using namespace std;
@@ -564,6 +565,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring nativeLibDir
564565
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__dumpReferenceTables"), FunctionTemplate::New(isolate, CallbackHandlers::DumpReferenceTablesMethodCallback));
565566
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__debugbreak"), FunctionTemplate::New(isolate, JsDebugger::DebugBreakCallback));
566567
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsDebugger::ConsoleMessageCallback));
568+
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__toFrontEnd"), FunctionTemplate::New(isolate, JsV8InspectorClient::sendToFrontEndCallback));
567569
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__enableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::EnableVerboseLoggingMethodCallback));
568570
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback));
569571
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback));

0 commit comments

Comments
 (0)