From 2ade5f37817e4884e0f205ce51c3091606f4252d Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Fri, 20 Jan 2017 11:50:20 -0800 Subject: [PATCH] stash the Source URL when loading a script. Reviewed By: javache Differential Revision: D4437195 fbshipit-source-id: 14698fc81bbe24cab81668bfb54578fc434abc58 --- .../react/cxxbridge/CatalystInstanceImpl.java | 40 ++++++++++++++++--- .../react/cxxbridge/JSBundleLoader.java | 2 +- .../react/modules/debug/SourceCodeModule.java | 19 ++++----- .../jni/xreact/jni/CatalystInstanceImpl.cpp | 28 ++++++------- .../jni/xreact/jni/CatalystInstanceImpl.h | 8 ++-- 5 files changed, 61 insertions(+), 36 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java index bb628be19..1bc07aa44 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java @@ -181,10 +181,40 @@ public class CatalystInstanceImpl implements CatalystInstance { MessageQueueThread moduleQueue, ModuleRegistryHolder registryHolder); - /* package */ native void setSourceURL(String sourceURL); - /* package */ native void loadScriptFromAssets(AssetManager assetManager, String assetURL); - /* package */ native void loadScriptFromFile(String fileName, String sourceURL); - /* package */ native void loadScriptFromOptimizedBundle(String path, String sourceURL, int flags); + /** + * This API is used in situations where the JS bundle is being executed not on + * the device, but on a host machine. In that case, we must provide two source + * URLs for the JS bundle: One to be used on the device, and one to be used on + * the remote debugging machine. + * + * @param deviceURL A source URL that is accessible from this device. + * @param remoteURL A source URL that is accessible from the remote machine + * executing the JS. + */ + /* package */ void setSourceURLs(String deviceURL, String remoteURL) { + mSourceURL = deviceURL; + jniSetSourceURL(remoteURL); + } + + /* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL) { + mSourceURL = assetURL; + jniLoadScriptFromAssets(assetManager, assetURL); + } + + /* package */ void loadScriptFromFile(String fileName, String sourceURL) { + mSourceURL = sourceURL; + jniLoadScriptFromFile(fileName, sourceURL); + } + + /* package */ void loadScriptFromOptimizedBundle(String path, String sourceURL, int flags) { + mSourceURL = sourceURL; + jniLoadScriptFromOptimizedBundle(path, sourceURL, flags); + } + + private native void jniSetSourceURL(String sourceURL); + private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL); + private native void jniLoadScriptFromFile(String fileName, String sourceURL); + private native void jniLoadScriptFromOptimizedBundle(String path, String sourceURL, int flags); @Override public void runJSBundle() { @@ -192,7 +222,7 @@ public class CatalystInstanceImpl implements CatalystInstance { mJSBundleHasLoaded = true; // incrementPendingJSCalls(); - mSourceURL = mJSBundleLoader.loadScript(CatalystInstanceImpl.this); + mJSBundleLoader.loadScript(CatalystInstanceImpl.this); synchronized (mJSCallsPendingInitLock) { // Loading the bundle is queued on the JS thread, but may not have diff --git a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java index 261ff009f..30a6ac07e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java @@ -83,7 +83,7 @@ public abstract class JSBundleLoader { return new JSBundleLoader() { @Override public String loadScript(CatalystInstanceImpl instance) { - instance.setSourceURL(proxySourceURL); + instance.setSourceURLs(realSourceURL, proxySourceURL); return realSourceURL; } }; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.java index 1023d8e3d..d738f348b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/debug/SourceCodeModule.java @@ -26,22 +26,11 @@ import com.facebook.react.module.annotations.ReactModule; public class SourceCodeModule extends BaseJavaModule { private final ReactContext mReactContext; - private @Nullable String mSourceUrl; public SourceCodeModule(ReactContext reactContext) { mReactContext = reactContext; } - @Override - public void initialize() { - super.initialize(); - - mSourceUrl = - Assertions.assertNotNull( - mReactContext.getCatalystInstance().getSourceURL(), - "No source URL loaded, have you initialised the instance?"); - } - @Override public String getName() { return "RCTSourceCode"; @@ -50,7 +39,13 @@ public class SourceCodeModule extends BaseJavaModule { @Override public @Nullable Map getConstants() { HashMap constants = new HashMap<>(); - constants.put("scriptURL", mSourceUrl); + + String sourceURL = + Assertions.assertNotNull( + mReactContext.getCatalystInstance().getSourceURL(), + "No source URL loaded, have you initialised the instance?"); + + constants.put("scriptURL", sourceURL); return constants; } } diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp index bce767829..cb04af1ec 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp @@ -103,13 +103,13 @@ void CatalystInstanceImpl::registerNatives() { registerHybrid({ makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid), makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge), - makeNativeMethod("setSourceURL", CatalystInstanceImpl::setSourceURL), - makeNativeMethod("loadScriptFromAssets", + makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL), + makeNativeMethod("jniLoadScriptFromAssets", "(Landroid/content/res/AssetManager;Ljava/lang/String;)V", - CatalystInstanceImpl::loadScriptFromAssets), - makeNativeMethod("loadScriptFromFile", CatalystInstanceImpl::loadScriptFromFile), - makeNativeMethod("loadScriptFromOptimizedBundle", - CatalystInstanceImpl::loadScriptFromOptimizedBundle), + CatalystInstanceImpl::jniLoadScriptFromAssets), + makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile), + makeNativeMethod("jniLoadScriptFromOptimizedBundle", + CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle), makeNativeMethod("callJSFunction", CatalystInstanceImpl::callJSFunction), makeNativeMethod("callJSCallback", CatalystInstanceImpl::callJSCallback), makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken), @@ -159,12 +159,12 @@ void CatalystInstanceImpl::initializeBridge( mrh->getModuleRegistry()); } -void CatalystInstanceImpl::setSourceURL(const std::string& sourceURL) { +void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) { instance_->setSourceURL(sourceURL); } -void CatalystInstanceImpl::loadScriptFromAssets(jobject assetManager, - const std::string& assetURL) { +void CatalystInstanceImpl::jniLoadScriptFromAssets(jobject assetManager, + const std::string& assetURL) { const int kAssetsLength = 9; // strlen("assets://"); auto sourceURL = assetURL.substr(kAssetsLength); @@ -192,8 +192,8 @@ bool CatalystInstanceImpl::isIndexedRAMBundle(const char *sourcePath) { return parseTypeFromHeader(header) == ScriptTag::RAMBundle; } -void CatalystInstanceImpl::loadScriptFromFile(const std::string& fileName, - const std::string& sourceURL) { +void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName, + const std::string& sourceURL) { auto zFileName = fileName.c_str(); if (isIndexedRAMBundle(zFileName)) { auto bundle = folly::make_unique(zFileName); @@ -207,9 +207,9 @@ void CatalystInstanceImpl::loadScriptFromFile(const std::string& fileName, } } -void CatalystInstanceImpl::loadScriptFromOptimizedBundle(const std::string& bundlePath, - const std::string& sourceURL, - jint flags) { +void CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, + const std::string& sourceURL, + jint flags) { return instance_->loadScriptFromOptimizedBundle(std::move(bundlePath), std::move(sourceURL), flags); diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h index a0a292036..a04a17081 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h @@ -53,11 +53,11 @@ class CatalystInstanceImpl : public jni::HybridClass { /** * Sets the source URL of the underlying bridge without loading any JS code. */ - void setSourceURL(const std::string& sourceURL); + void jniSetSourceURL(const std::string& sourceURL); - void loadScriptFromAssets(jobject assetManager, const std::string& assetURL); - void loadScriptFromFile(const std::string& fileName, const std::string& sourceURL); - void loadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags); + void jniLoadScriptFromAssets(jobject assetManager, const std::string& assetURL); + void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL); + void jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags); void callJSFunction(JExecutorToken* token, std::string module, std::string method, NativeArray* arguments); void callJSCallback(JExecutorToken* token, jint callbackId, NativeArray* arguments); local_ref getMainExecutorToken();