diff --git a/lib/index.js b/lib/index.js index 46ad49c6..5b4f3091 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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!'); +} \ No newline at end of file diff --git a/react-native/android/app/build.gradle b/react-native/android/app/build.gradle index 02e5a9bd..6acc3f44 100644 --- a/react-native/android/app/build.gradle +++ b/react-native/android/app/build.gradle @@ -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}" diff --git a/react-native/android/app/src/main/AndroidManifest.xml b/react-native/android/app/src/main/AndroidManifest.xml index 3f4f9d4e..a8596b54 100644 --- a/react-native/android/app/src/main/AndroidManifest.xml +++ b/react-native/android/app/src/main/AndroidManifest.xml @@ -20,8 +20,9 @@ --> - + + diff --git a/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java index b832dc24..67e367d6 100644 --- a/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java +++ b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java @@ -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 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 = "

Hello server

\n"; + // Map parms = session.getParms(); + // if (parms.get("username") == null) { + // msg += "
\n

Your name:

\n" + "
\n"; + // } else { + // msg += "

Hello, " + parms.get("username") + "!

"; + // } + // return newFixedLengthResponse( msg + "\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); } diff --git a/src/android/com_reacttests_RealmReactAndroid.cpp b/src/android/com_reacttests_RealmReactAndroid.cpp index 1c58c7f8..3b8a1a6f 100644 --- a/src/android/com_reacttests_RealmReactAndroid.cpp +++ b/src/android/com_reacttests_RealmReactAndroid.cpp @@ -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"); + } + + diff --git a/src/android/com_reacttests_RealmReactAndroid.h b/src/android/com_reacttests_RealmReactAndroid.h index edfb76a0..7d13b454 100644 --- a/src/android/com_reacttests_RealmReactAndroid.h +++ b/src/android/com_reacttests_RealmReactAndroid.h @@ -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 diff --git a/tests/react-test-app/android/app/build.gradle b/tests/react-test-app/android/app/build.gradle index 2a50a4ae..c25d5835 100644 --- a/tests/react-test-app/android/app/build.gradle +++ b/tests/react-test-app/android/app/build.gradle @@ -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' diff --git a/tests/react-test-app/run-android.sh b/tests/react-test-app/run-android.sh old mode 100644 new mode 100755