Skip to content

Commit 2ef701b

Browse files
authored
Merge pull request #677 from NativeScript/plamen5kov/console_api
Plamen5kov/console api
2 parents fac0db9 + 9a93bd2 commit 2ef701b

File tree

5 files changed

+107
-44
lines changed

5 files changed

+107
-44
lines changed

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

Lines changed: 53 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,45 @@ 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+
try {
64+
JSONObject consoleMessage = new JSONObject();
65+
66+
JSONObject params = new JSONObject();
67+
params.put("type", level);
68+
params.put("executionContextId", 0);
69+
params.put("timestamp", 0.000000000000000);
70+
71+
JSONArray args = new JSONArray();
72+
args.put(message);
73+
params.put("args", args);
74+
75+
consoleMessage.put("method", "Runtime.consoleAPICalled");
76+
consoleMessage.put("params", params);
77+
78+
String sendingText = consoleMessage.toString();
79+
AndroidJsV8Inspector.send(connection, sendingText);
80+
81+
} catch (JSONException e) {
82+
e.printStackTrace();
83+
} catch (IOException e) {
84+
e.printStackTrace();
85+
}
86+
}
87+
88+
@RuntimeCallable
89+
private static void send(Object connection, String payload) throws IOException {
6190
((JsV8InspectorWebSocket) connection).send(payload);
6291
}
6392

64-
private static String getInspectorMessage(Object connection)
65-
{
93+
@RuntimeCallable
94+
private static String getInspectorMessage(Object connection) {
6695
return ((JsV8InspectorWebSocket) connection).getInspectorMessage();
6796
}
6897

69-
class JsV8InspectorServer extends NanoWSD
70-
{
71-
public JsV8InspectorServer(String name)
72-
{
98+
class JsV8InspectorServer extends NanoWSD {
99+
public JsV8InspectorServer(String name) {
73100
super(name);
74101
}
75102

@@ -84,17 +111,14 @@ protected Response serveHttp(IHTTPSession session)
84111
}
85112

86113
@Override
87-
protected WebSocket openWebSocket(IHTTPSession handshake)
88-
{
114+
protected WebSocket openWebSocket(IHTTPSession handshake) {
89115
return new JsV8InspectorWebSocket(handshake);
90116
}
91117
}
92118

93-
class JsV8InspectorWebSocket extends NanoWSD.WebSocket
94-
{
119+
class JsV8InspectorWebSocket extends NanoWSD.WebSocket {
95120

96-
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
97-
{
121+
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
98122
super(handshakeRequest);
99123
}
100124

@@ -106,8 +130,7 @@ protected void onOpen()
106130
Log.d("V8Inspector", "onOpen: ThreadID: " + Thread.currentThread().getId());
107131
}
108132

109-
mainHandler.post(new Runnable()
110-
{
133+
mainHandler.post(new Runnable() {
111134
@Override
112135
public void run()
113136
{
@@ -153,14 +176,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153176

154177
inspectorMessages.offer(message.getTextPayload());
155178

156-
mainHandler.post(new Runnable()
157-
{
179+
mainHandler.post(new Runnable() {
158180
@Override
159-
public void run()
160-
{
181+
public void run() {
161182
String nextMessage = inspectorMessages.poll();
162-
while (nextMessage != null)
163-
{
183+
while (nextMessage != null) {
164184
dispatchMessage(nextMessage);
165185
nextMessage = inspectorMessages.poll();
166186
}
@@ -179,15 +199,11 @@ public void send(String payload) throws IOException
179199
super.send(payload);
180200
}
181201

182-
public String getInspectorMessage()
183-
{
184-
try
185-
{
202+
public String getInspectorMessage() {
203+
try {
186204
String message = inspectorMessages.take();
187205
return message;
188-
}
189-
catch (InterruptedException e)
190-
{
206+
} catch (InterruptedException e) {
191207
e.printStackTrace();
192208
}
193209

@@ -200,10 +216,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200216
}
201217

202218
@Override
203-
protected void onException(IOException exception)
204-
{
219+
protected void onException(IOException exception) {
205220
exception.printStackTrace();
206221
disconnect();
207222
}
208223
}
209-
}
224+
}

runtime/src/main/jni/JsV8InspectorClient.cpp

Lines changed: 48 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,47 @@ JsV8InspectorClient *JsV8InspectorClient::GetInstance()
218220
return instance;
219221
}
220222

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

262306

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 & 1 deletion
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;
@@ -563,7 +564,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring nativeLibDir
563564
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__log"), FunctionTemplate::New(isolate, CallbackHandlers::LogMethodCallback));
564565
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__dumpReferenceTables"), FunctionTemplate::New(isolate, CallbackHandlers::DumpReferenceTablesMethodCallback));
565566
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__debugbreak"), FunctionTemplate::New(isolate, JsDebugger::DebugBreakCallback));
566-
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsDebugger::ConsoleMessageCallback));
567+
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsV8InspectorClient::sendToFrontEndCallback));
567568
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__enableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::EnableVerboseLoggingMethodCallback));
568569
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback));
569570
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback));

0 commit comments

Comments
 (0)