From 76981229abe2b72239d8f48b3c3bfef745d34d37 Mon Sep 17 00:00:00 2001 From: Nabil Hachicha Date: Fri, 19 Feb 2016 19:05:07 +0000 Subject: [PATCH 1/2] start debug server only on when using chrome debug --- .../java/io/realm/react/RealmReactModule.java | 12 ++++++++++-- src/android/io_realm_react_RealmReactModule.cpp | 17 +++++++++++++++-- src/android/io_realm_react_RealmReactModule.h | 15 +++++++++++++++ src/android/jsc_override.cpp | 3 +++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/react-native/android/src/main/java/io/realm/react/RealmReactModule.java b/react-native/android/src/main/java/io/realm/react/RealmReactModule.java index 37345b90..42c4f08d 100644 --- a/react-native/android/src/main/java/io/realm/react/RealmReactModule.java +++ b/react-native/android/src/main/java/io/realm/react/RealmReactModule.java @@ -9,7 +9,6 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.soloader.SoLoader; import java.io.IOException; -import java.lang.IllegalStateException; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -56,13 +55,16 @@ public class RealmReactModule extends ReactContextBaseJavaModule { @Override public Map getConstants() { - // FIXME: Only start web server when in Chrome debug mode! + if (!isContextInjected()) { + startWebServer(); + } startWebServer(); return Collections.EMPTY_MAP; } @Override public void onCatalystInstanceDestroy() { + clearFlag(); stopWebServer(); } @@ -127,6 +129,12 @@ public class RealmReactModule extends ReactContextBaseJavaModule { } } + // return true if the Realm API was injected (return false when running in Chrome Debug) + private native boolean isContextInjected(); + + // clear the flag set when injecting Realm API + private native void clearFlag(); + // fileDir: path of the internal storage of the application private native void setDefaultRealmFileDirectory(String fileDir); diff --git a/src/android/io_realm_react_RealmReactModule.cpp b/src/android/io_realm_react_RealmReactModule.cpp index 801b2be9..999058a5 100644 --- a/src/android/io_realm_react_RealmReactModule.cpp +++ b/src/android/io_realm_react_RealmReactModule.cpp @@ -10,6 +10,7 @@ #include "platform.hpp" static realm_js::RPCServer *s_rpc_server; +extern bool realmContextInjected; JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_setDefaultRealmFileDirectory (JNIEnv *env, jclass, jstring fileDir) @@ -19,7 +20,7 @@ JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_setDefaultRealmFileD // Setting the internal storage path for the application const char* strFileDir = env->GetStringUTFChars(fileDir, NULL); realm::set_default_realm_file_directory(strFileDir); - env->ReleaseStringUTFChars(fileDir , strFileDir); + env->ReleaseStringUTFChars(fileDir, strFileDir); __android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Absolute path: %s", realm::default_realm_file_directory().c_str()); } @@ -38,7 +39,6 @@ JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactModule_setupChromeDebugMod JNIEXPORT jstring JNICALL Java_io_realm_react_RealmReactModule_processChromeDebugCommand (JNIEnv *env, jclass, jstring chrome_cmd, jstring chrome_args) { - __android_log_print(ANDROID_LOG_VERBOSE, "JSRealm", "processChromeDebugCommand"); const char* cmd = env->GetStringUTFChars(chrome_cmd, NULL); const char* args = env->GetStringUTFChars(chrome_args, NULL); realm_js::json json = realm_js::json::parse(args); @@ -47,3 +47,16 @@ JNIEXPORT jstring JNICALL Java_io_realm_react_RealmReactModule_processChromeDebu env->ReleaseStringUTFChars(chrome_args, args); return env->NewStringUTF(response.dump().c_str()); } + +JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_isContextInjected + (JNIEnv *env, jclass) + { + return realmContextInjected; + } + +JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearFlag + (JNIEnv *env, jclass) + { + realmContextInjected = false;//might not be needed + + } \ No newline at end of file diff --git a/src/android/io_realm_react_RealmReactModule.h b/src/android/io_realm_react_RealmReactModule.h index 98fb03b0..a33c7b5e 100644 --- a/src/android/io_realm_react_RealmReactModule.h +++ b/src/android/io_realm_react_RealmReactModule.h @@ -29,6 +29,21 @@ JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactModule_setupChromeDebugMod JNIEXPORT jstring JNICALL Java_io_realm_react_RealmReactModule_processChromeDebugCommand (JNIEnv *, jclass, jstring, jstring); +/* + * Class: io_realm_react_RealmReactModule + * Method: isContextInjected + */ +JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_isContextInjected + (JNIEnv *, jclass); + +/* + * Class: io_realm_react_RealmReactModule + * Method: clearFlag + */ +JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearFlag + (JNIEnv *, jclass); + + #ifdef __cplusplus } #endif diff --git a/src/android/jsc_override.cpp b/src/android/jsc_override.cpp index 1d5f90c0..03b1cdd8 100644 --- a/src/android/jsc_override.cpp +++ b/src/android/jsc_override.cpp @@ -18,6 +18,8 @@ #define HOOK_SIZE 5 #endif +bool realmContextInjected; + static void swap_function() __attribute__((constructor)); static JSGlobalContextRef create_context(JSContextGroupRef group, JSClassRef global_class) @@ -36,6 +38,7 @@ static JSGlobalContextRef create_context(JSContextGroupRef group, JSClassRef glo realm::Realm::s_global_cache.clear(); RJSInitializeInContext(ctx); + realmContextInjected = true; return ctx; } From 9bcebfaf292734a73e2e163bb89696c38ba43b1e Mon Sep 17 00:00:00 2001 From: Nabil Hachicha Date: Fri, 19 Feb 2016 22:50:30 +0000 Subject: [PATCH 2/2] PR feedback --- .../java/io/realm/react/RealmReactModule.java | 5 ++--- src/android/io_realm_react_RealmReactModule.cpp | 15 +++++++-------- src/android/io_realm_react_RealmReactModule.h | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/react-native/android/src/main/java/io/realm/react/RealmReactModule.java b/react-native/android/src/main/java/io/realm/react/RealmReactModule.java index 42c4f08d..07ebfeb8 100644 --- a/react-native/android/src/main/java/io/realm/react/RealmReactModule.java +++ b/react-native/android/src/main/java/io/realm/react/RealmReactModule.java @@ -58,13 +58,12 @@ public class RealmReactModule extends ReactContextBaseJavaModule { if (!isContextInjected()) { startWebServer(); } - startWebServer(); return Collections.EMPTY_MAP; } @Override public void onCatalystInstanceDestroy() { - clearFlag(); + clearContextInjectedFlag(); stopWebServer(); } @@ -133,7 +132,7 @@ public class RealmReactModule extends ReactContextBaseJavaModule { private native boolean isContextInjected(); // clear the flag set when injecting Realm API - private native void clearFlag(); + private native void clearContextInjectedFlag(); // fileDir: path of the internal storage of the application private native void setDefaultRealmFileDirectory(String fileDir); diff --git a/src/android/io_realm_react_RealmReactModule.cpp b/src/android/io_realm_react_RealmReactModule.cpp index 999058a5..dd4d0696 100644 --- a/src/android/io_realm_react_RealmReactModule.cpp +++ b/src/android/io_realm_react_RealmReactModule.cpp @@ -50,13 +50,12 @@ JNIEXPORT jstring JNICALL Java_io_realm_react_RealmReactModule_processChromeDebu JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_isContextInjected (JNIEnv *env, jclass) - { - return realmContextInjected; - } +{ + return realmContextInjected; +} -JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearFlag +JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearContextInjectedFlag (JNIEnv *env, jclass) - { - realmContextInjected = false;//might not be needed - - } \ No newline at end of file +{ + realmContextInjected = false; +} diff --git a/src/android/io_realm_react_RealmReactModule.h b/src/android/io_realm_react_RealmReactModule.h index a33c7b5e..fb6404ee 100644 --- a/src/android/io_realm_react_RealmReactModule.h +++ b/src/android/io_realm_react_RealmReactModule.h @@ -38,9 +38,9 @@ JNIEXPORT jboolean JNICALL Java_io_realm_react_RealmReactModule_isContextInjecte /* * Class: io_realm_react_RealmReactModule - * Method: clearFlag + * Method: clearContextInjectedFlag */ -JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearFlag +JNIEXPORT void JNICALL Java_io_realm_react_RealmReactModule_clearContextInjectedFlag (JNIEnv *, jclass);