adding NanoHTTP server for chrome debug

This commit is contained in:
Nabil Hachicha 2016-01-13 17:56:38 +00:00
parent 27bae32361
commit 55bde66ef8
8 changed files with 110 additions and 16 deletions

View File

@ -5,7 +5,11 @@
'use strict';
if (typeof Realm != 'undefined') {
// The global Realm constructor should be available on device (using JavaScriptCore).
module.exports = Realm; // eslint-disable-line no-undef
} else {
} else if (navigator.userAgent) {
// The userAgent will be defined when running in a browser (such as Chrome debugging mode).
module.exports = require('./realm');
}
} else {
throw new Error('Missing Realm constructor - please ensure RealmReact framework is included!');
}

View File

@ -116,7 +116,6 @@ task buildReactNdkLib(dependsOn: [prepareJSC], type: Exec) {
"NDK_LIBS_OUT=$buildDir/react-ndk/all",
"THIRD_PARTY_NDK_DIR=$buildDir/third-party-ndk",
'-C', file('src/main/jni').absolutePath,
'-B',
'NDK_LOG=1',
'NDK_DEBUG=1',
'--jobs', Runtime.runtime.availableProcessors(),
@ -185,6 +184,7 @@ dependencies {
compile 'org.webkit:android-jsc:r174650'
compile "com.facebook.react:react-native:0.16.+"
compile 'com.github.KeepSafe:ReLinker:1.1'
compile 'org.nanohttpd:nanohttpd:2.2.0'
testCompile "junit:junit:${JUNIT_VERSION}"
testCompile "org.powermock:powermock-api-mockito:${POWERMOCK_VERSION}"

View File

@ -20,8 +20,9 @@
</application>
</manifest> -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.reacttests">
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.reacttests">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<application />

View File

@ -12,12 +12,18 @@ import android.widget.Toast;
import com.facebook.react.bridge.Callback;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
public class RealmReactAndroid extends ReactContextBaseJavaModule {
private static final String DURATION_SHORT_KEY = "SHORT";
private static final String DURATION_LONG_KEY = "LONG";
private String filesDirPath;
private static final int DEFAULT_PORT = 8082;
private AndroidWebServer webServer;
private long rpcServerPtr;
public RealmReactAndroid(ReactApplicationContext reactContext) {
super(reactContext);
try {
@ -36,24 +42,67 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
@Override
public Map<String, Object> getConstants() {
Log.w("RealmReactAndroid", injectRealmJsContext(filesDirPath));
startWebServer();
return new HashMap<>();
}
@ReactMethod
public void resultOfJsContextInjection(Callback successCallback) {
// Inject our JS Context
Exception exception = new Exception();
exception.fillInStackTrace();
exception.printStackTrace();
successCallback.invoke(injectRealmJsContext(filesDirPath));
@Override
public void onCatalystInstanceDestroy() {
if (webServer != null) {
Log.w("RealmReactAndroid", "Stopping the webserver");
webServer.stop();
}
}
// @ReactMethod
// public void setUpChromeDebugMode() {
// startWebServer();
// // setupChromeDebugModeRealmJsContext();
// }
///FIXME find the right callback to call webServerstop
private void startWebServer() {
rpcServerPtr = setupChromeDebugModeRealmJsContext();
webServer = new AndroidWebServer(DEFAULT_PORT);
try {
webServer.start();
Log.w("RealmReactAndroid", "Starting WebServer, Host: " + webServer.getHostname() + " Port: " + webServer.getListeningPort());
} catch (IOException e) {
e.printStackTrace();
}
}
@ReactMethod
public void show(String message, int duration) {
Toast.makeText(getReactApplicationContext(), message, duration).show();
// WebServer
class AndroidWebServer extends NanoHTTPD {
public AndroidWebServer(int port) {
super(port);
}
public AndroidWebServer(String hostname, int port) {
super(hostname, port);
}
@Override
public Response serve(IHTTPSession session) {
String cmdUri = session.getUri();
Log.w("AndroidWebServer", "Session Uri: " + cmdUri + " Mehtod: " + session.getMethod().name());
// String msg = "<html><body><h1>Hello server</h1>\n";
// Map<String, String> parms = session.getParms();
// if (parms.get("username") == null) {
// msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
// } else {
// msg += "<p>Hello, " + parms.get("username") + "!</p>";
// }
// return newFixedLengthResponse( msg + "</body></html>\n" );
String jsonResponse = processChromeDebugCommand(rpcServerPtr, cmdUri);
Response response = newFixedLengthResponse(jsonResponse);
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
return response;
}
}
// fileDir: path of the internal storage of the application
private native String injectRealmJsContext(String fileDir);
// responsible for creating the rpcServer that will accept thw chrome Websocket command
private native long setupChromeDebugModeRealmJsContext();
// this receives one command from Chrome debug, & return the processing we should post back
private native String processChromeDebugCommand(long rpcServerPointer, String cmd);
}

View File

@ -45,7 +45,7 @@ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsCon
msg << "Got the globalContext map, size=" << s_globalContextRefToJSCExecutor.size();
for (auto pair : s_globalContextRefToJSCExecutor) {
RJSInitializeInContext(pair.first);
RJSInitializeInContext(pair.first);
}
return env->NewStringUTF(msg.str().c_str());
} else {
@ -53,3 +53,27 @@ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsCon
}
}
/*
* Class: com_reacttests_RealmReactAndroid
* Method: setupChromeDebugModeRealmJsContext
*/
JNIEXPORT jlong JNICALL Java_com_reacttests_RealmReactAndroid_setupChromeDebugModeRealmJsContext
(JNIEnv *, jclass)
{
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Java_com_reacttests_RealmReactAndroid_setupChromeDebugModeRealmJsContext");
return 0;
}
/*
* Class: com_reacttests_RealmReactAndroid
* Method: processChromeDebugCommand
*/
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_processChromeDebugCommand
(JNIEnv *env, jclass, jlong rpc_server_ptr, jstring chrome_cmd)
{
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Java_com_reacttests_RealmReactAndroid_processChromeDebugCommand");
return env->NewStringUTF("Echo");
}

View File

@ -15,6 +15,21 @@ extern "C" {
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext
(JNIEnv *, jclass, jstring);
/*
* Class: com_reacttests_RealmReactAndroid
* Method: setupChromeDebugModeRealmJsContext
*/
JNIEXPORT jlong JNICALL Java_com_reacttests_RealmReactAndroid_setupChromeDebugModeRealmJsContext
(JNIEnv *, jclass);
/*
* Class: com_reacttests_RealmReactAndroid
* Method: processsetupChromeDebugCommand
*/
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_processChromeDebugCommand
(JNIEnv *, jclass, jlong, jstring);
#ifdef __cplusplus
}
#endif

View File

@ -78,6 +78,7 @@ dependencies {
compile 'com.facebook.react:ReactAndroid-debug@aar'
compile 'com.reacttests:app-debug@aar'
compile 'com.github.KeepSafe:ReLinker:1.1'
compile 'org.nanohttpd:nanohttpd:2.2.0'
compile 'com.google.code.findbugs:jsr305:3.0.0'
compile 'com.facebook.stetho:stetho-okhttp:1.2.0'

0
tests/react-test-app/run-android.sh Normal file → Executable file
View File