From 76c4faee5e1f8ac65bf31c17c9db7ddf71473c0a Mon Sep 17 00:00:00 2001 From: Ashok Menon Date: Wed, 18 Jan 2017 08:55:46 -0800 Subject: [PATCH] CatalystInstanceImpl.setSourceURL Reviewed By: javache Differential Revision: D4422416 fbshipit-source-id: bc49485ac64064909f32375b6b8360a0a505975b --- .../react/cxxbridge/CatalystInstanceImpl.java | 1 + .../react/cxxbridge/JSBundleLoader.java | 2 +- .../jni/xreact/jni/CatalystInstanceImpl.cpp | 19 ++++++++++------- .../jni/xreact/jni/CatalystInstanceImpl.h | 8 ++++++- ReactCommon/cxxreact/Instance.cpp | 21 +++++++++++-------- ReactCommon/cxxreact/Instance.h | 3 +++ 6 files changed, 35 insertions(+), 19 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 9f4499198..bb628be19 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/CatalystInstanceImpl.java @@ -181,6 +181,7 @@ 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); 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 629a1d381..261ff009f 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.loadScriptFromFile(null, proxySourceURL); + instance.setSourceURL(proxySourceURL); return realSourceURL; } }; diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp index 8ba8f98f3..bce767829 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.cpp @@ -101,8 +101,9 @@ CatalystInstanceImpl::CatalystInstanceImpl() void CatalystInstanceImpl::registerNatives() { registerHybrid({ - makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid), + makeNativeMethod("initHybrid", CatalystInstanceImpl::initHybrid), makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge), + makeNativeMethod("setSourceURL", CatalystInstanceImpl::setSourceURL), makeNativeMethod("loadScriptFromAssets", "(Landroid/content/res/AssetManager;Ljava/lang/String;)V", CatalystInstanceImpl::loadScriptFromAssets), @@ -158,6 +159,10 @@ void CatalystInstanceImpl::initializeBridge( mrh->getModuleRegistry()); } +void CatalystInstanceImpl::setSourceURL(const std::string& sourceURL) { + instance_->setSourceURL(sourceURL); +} + void CatalystInstanceImpl::loadScriptFromAssets(jobject assetManager, const std::string& assetURL) { const int kAssetsLength = 9; // strlen("assets://"); @@ -187,20 +192,18 @@ bool CatalystInstanceImpl::isIndexedRAMBundle(const char *sourcePath) { return parseTypeFromHeader(header) == ScriptTag::RAMBundle; } -void CatalystInstanceImpl::loadScriptFromFile(jni::alias_ref fileName, +void CatalystInstanceImpl::loadScriptFromFile(const std::string& fileName, const std::string& sourceURL) { - - std::string file = fileName ? fileName->toStdString() : ""; - - if (isIndexedRAMBundle(file.c_str())) { - auto bundle = folly::make_unique(file.c_str()); + auto zFileName = fileName.c_str(); + if (isIndexedRAMBundle(zFileName)) { + auto bundle = folly::make_unique(zFileName); auto startupScript = bundle->getStartupCode(); instance_->loadUnbundle( std::move(bundle), std::move(startupScript), sourceURL); } else { - instance_->loadScriptFromFile(file, sourceURL); + instance_->loadScriptFromFile(fileName, sourceURL); } } diff --git a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h index 674ca8766..a0a292036 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h +++ b/ReactAndroid/src/main/jni/xreact/jni/CatalystInstanceImpl.h @@ -49,8 +49,14 @@ class CatalystInstanceImpl : public jni::HybridClass { jni::alias_ref jsQueue, jni::alias_ref moduleQueue, ModuleRegistryHolder* mrh); + + /** + * Sets the source URL of the underlying bridge without loading any JS code. + */ + void setSourceURL(const std::string& sourceURL); + void loadScriptFromAssets(jobject assetManager, const std::string& assetURL); - void loadScriptFromFile(jni::alias_ref fileName, const std::string& sourceURL); + void loadScriptFromFile(const std::string& fileName, const std::string& sourceURL); void loadScriptFromOptimizedBundle(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); diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index 1ac0885f2..fafa07914 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -50,6 +50,14 @@ void Instance::initializeBridge( CHECK(nativeToJsBridge_); } +void Instance::setSourceURL(std::string sourceURL) { + callback_->incrementPendingJSCalls(); + SystraceSection s("reactbridge_xplat_setSourceURL", + "sourceURL", sourceURL); + + nativeToJsBridge_->loadApplication(nullptr, nullptr, std::move(sourceURL)); +} + void Instance::loadScriptFromString(std::unique_ptr string, std::string sourceURL) { callback_->incrementPendingJSCalls(); @@ -74,15 +82,10 @@ void Instance::loadScriptFromFile(const std::string& filename, std::unique_ptr script; - // This function can be called in order to change the Bridge's Source URL. - // In that case, the filename will be empty, and we should not attempt to - // load it. - if (!filename.empty()) { - RecoverableError::runRethrowingAsRecoverable( - [&filename, &script]() { - script = JSBigFileString::fromPath(filename); - }); - } + RecoverableError::runRethrowingAsRecoverable( + [&filename, &script]() { + script = JSBigFileString::fromPath(filename); + }); nativeToJsBridge_->loadApplication(nullptr, std::move(script), sourceURL); } diff --git a/ReactCommon/cxxreact/Instance.h b/ReactCommon/cxxreact/Instance.h index 0a3290406..39a132d65 100644 --- a/ReactCommon/cxxreact/Instance.h +++ b/ReactCommon/cxxreact/Instance.h @@ -34,6 +34,9 @@ class Instance { std::shared_ptr jsQueue, std::unique_ptr nativeQueue, std::shared_ptr moduleRegistry); + + void setSourceURL(std::string sourceURL); + void loadScriptFromString(std::unique_ptr string, std::string sourceURL); void loadScriptFromStringSync(std::unique_ptr string, std::string sourceURL); void loadScriptFromFile(const std::string& filename, const std::string& sourceURL);