Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 53 additions & 38 deletions runtime/src/main/java/com/tns/AndroidJsV8Inspector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.os.Handler;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
Expand All @@ -27,19 +31,17 @@ public class AndroidJsV8Inspector
protected static native final void disconnect();

protected native final void dispatchMessage(String message);

protected Handler mainHandler;
private LinkedBlockingQueue<String> inspectorMessages = new LinkedBlockingQueue<String>();

public AndroidJsV8Inspector(Context context, Logger logger)
{
public AndroidJsV8Inspector(Context context, Logger logger) {
this.context = context;
this.logger = logger;
}

public void start() throws IOException
{
if (this.server == null)
{
public void start() throws IOException {
if (this.server == null) {
Runtime currentRuntime = Runtime.getCurrentRuntime();

mainHandler = currentRuntime.getHandler();
Expand All @@ -56,20 +58,45 @@ public void start() throws IOException
}
}

private static void send(Object connection, String payload) throws IOException
{
@RuntimeCallable
private static void sendToDevToolsConsole(Object connection, String message, String level) {
try {
JSONObject consoleMessage = new JSONObject();

JSONObject params = new JSONObject();
params.put("type", level);
params.put("executionContextId", 0);
params.put("timestamp", 0.000000000000000);

JSONArray args = new JSONArray();
args.put(message);
params.put("args", args);

consoleMessage.put("method", "Runtime.consoleAPICalled");
consoleMessage.put("params", params);

String sendingText = consoleMessage.toString();
AndroidJsV8Inspector.send(connection, sendingText);

} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@RuntimeCallable
private static void send(Object connection, String payload) throws IOException {
((JsV8InspectorWebSocket) connection).send(payload);
}

private static String getInspectorMessage(Object connection)
{
@RuntimeCallable
private static String getInspectorMessage(Object connection) {
return ((JsV8InspectorWebSocket) connection).getInspectorMessage();
}

class JsV8InspectorServer extends NanoWSD
{
public JsV8InspectorServer(String name)
{
class JsV8InspectorServer extends NanoWSD {
public JsV8InspectorServer(String name) {
super(name);
}

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

@Override
protected WebSocket openWebSocket(IHTTPSession handshake)
{
protected WebSocket openWebSocket(IHTTPSession handshake) {
return new JsV8InspectorWebSocket(handshake);
}
}

class JsV8InspectorWebSocket extends NanoWSD.WebSocket
{
class JsV8InspectorWebSocket extends NanoWSD.WebSocket {

public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest)
{
public JsV8InspectorWebSocket(NanoHTTPD.IHTTPSession handshakeRequest) {
super(handshakeRequest);
}

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

mainHandler.post(new Runnable()
{
mainHandler.post(new Runnable() {
@Override
public void run()
{
Expand Down Expand Up @@ -153,14 +176,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)

inspectorMessages.offer(message.getTextPayload());

mainHandler.post(new Runnable()
{
mainHandler.post(new Runnable() {
@Override
public void run()
{
public void run() {
String nextMessage = inspectorMessages.poll();
while (nextMessage != null)
{
while (nextMessage != null) {
dispatchMessage(nextMessage);
nextMessage = inspectorMessages.poll();
}
Expand All @@ -179,15 +199,11 @@ public void send(String payload) throws IOException
super.send(payload);
}

public String getInspectorMessage()
{
try
{
public String getInspectorMessage() {
try {
String message = inspectorMessages.take();
return message;
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
e.printStackTrace();
}

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

@Override
protected void onException(IOException exception)
{
protected void onException(IOException exception) {
exception.printStackTrace();
disconnect();
}
}
}
}
52 changes: 48 additions & 4 deletions runtime/src/main/jni/JsV8InspectorClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <assert.h>
#include <include/libplatform/libplatform.h>
#include "Runtime.h"
#include "src/inspector/string-16.h"
#include "NativeScriptException.h"

#include "ArgConverter.h"

Expand All @@ -33,6 +33,9 @@ JsV8InspectorClient::JsV8InspectorClient(v8::Isolate *isolate)
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);
}
Expand Down Expand Up @@ -152,8 +155,8 @@ void JsV8InspectorClient::sendProtocolNotification(const v8_inspector::StringVie

JEnv env;
const char *msss = msg.utf8().c_str();
JniLocalRef string(env.NewStringUTF(msg.utf8().c_str()));
env.CallStaticVoidMethod(inspectorClass, sendMethod, this->connection, (jstring) string);
JniLocalRef str(env.NewStringUTF(msg.utf8().c_str()));
env.CallStaticVoidMethod(inspectorClass, sendMethod, this->connection, (jstring) str);
}

void JsV8InspectorClient::flushProtocolNotifications()
Expand Down Expand Up @@ -197,7 +200,6 @@ void JsV8InspectorClient::init()
v8::Local<Context> context = Context::New(isolate_);
v8::Context::Scope context_scope(context);


inspector_ = V8Inspector::create(isolate_, this);

inspector_->contextCreated(v8_inspector::V8ContextInfo(context, 0, v8_inspector::StringView()));
Expand All @@ -218,6 +220,47 @@ JsV8InspectorClient *JsV8InspectorClient::GetInstance()
return instance;
}


void JsV8InspectorClient::sendToFrontEndCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {

if(instance->connection == nullptr) {
return;
}

try
{
if ((args.Length() > 0) && args[0]->IsString())
{
std::string message = ArgConverter::ConvertToString(args[0]->ToString());

std:string level = "log";
if (args.Length() > 1 && args[1]->IsString())
{
level = ArgConverter::ConvertToString(args[1]->ToString());
}

JEnv env;
JniLocalRef str(env.NewStringUTF(message.c_str()));
JniLocalRef lev(env.NewStringUTF(level.c_str()));
env.CallStaticVoidMethod(inspectorClass, sendToDevToolsConsoleMethod, instance->connection, (jstring) str, (jstring)lev);
}
}
catch (NativeScriptException& e)
{
e.ReThrowToV8();
}
catch (std::exception e) {
stringstream ss;
ss << "Error: c++ exception: " << e.what() << endl;
NativeScriptException nsEx(ss.str());
nsEx.ReThrowToV8();
}
catch (...) {
NativeScriptException nsEx(std::string("Error: c++ exception!"));
nsEx.ReThrowToV8();
}
}

void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> exception)
{
// v8::Isolate *isolate = v8::Isolate::GetCurrent();
Expand Down Expand Up @@ -257,6 +300,7 @@ void MessageHandler(v8::Local<v8::Message> message, v8::Local<v8::Value> excepti
JsV8InspectorClient *JsV8InspectorClient::instance = nullptr;
jclass JsV8InspectorClient::inspectorClass = nullptr;
jmethodID JsV8InspectorClient::sendMethod = nullptr;
jmethodID JsV8InspectorClient::sendToDevToolsConsoleMethod = nullptr;
jmethodID JsV8InspectorClient::getInspectorMessageMethod = nullptr;


Expand Down
3 changes: 3 additions & 0 deletions runtime/src/main/jni/JsV8InspectorClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace tns
void sendProtocolNotification(const v8_inspector::StringView &message) override;
void flushProtocolNotifications() override;

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

void runMessageLoopOnPause(int context_group_id) override;
void quitMessageLoopOnPause() override;
v8::Local<v8::Context> ensureDefaultContextInGroup(int contextGroupId) override;
Expand All @@ -46,6 +48,7 @@ namespace tns
static jclass inspectorClass;
static jmethodID sendMethod;
static jmethodID getInspectorMessageMethod;
static jmethodID sendToDevToolsConsoleMethod;

v8::Isolate* isolate_;
v8::Persistent<v8::Context> context_;
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/main/jni/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <sstream>
#include <dlfcn.h>
#include "sys/system_properties.h"
#include "JsV8InspectorClient.h"

using namespace v8;
using namespace std;
Expand Down Expand Up @@ -563,7 +564,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring nativeLibDir
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__log"), FunctionTemplate::New(isolate, CallbackHandlers::LogMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__dumpReferenceTables"), FunctionTemplate::New(isolate, CallbackHandlers::DumpReferenceTablesMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__debugbreak"), FunctionTemplate::New(isolate, JsDebugger::DebugBreakCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsDebugger::ConsoleMessageCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__consoleMessage"), FunctionTemplate::New(isolate, JsV8InspectorClient::sendToFrontEndCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__enableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::EnableVerboseLoggingMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__disableVerboseLogging"), FunctionTemplate::New(isolate, CallbackHandlers::DisableVerboseLoggingMethodCallback));
globalTemplate->Set(ArgConverter::ConvertToV8String(isolate, "__exit"), FunctionTemplate::New(isolate, CallbackHandlers::ExitMethodCallback));
Expand Down
2 changes: 1 addition & 1 deletion test-app/app/src/main/assets/app/shared