From 0c2fdf4b6af3c4193d351ed615b9fc12c941173a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gregorczyk?= Date: Tue, 6 Sep 2016 19:39:27 -0700 Subject: [PATCH] Add separate JSBunldeLoader for assets Reviewed By: mhorowitz Differential Revision: D3735897 fbshipit-source-id: 990d45e9cb40a9afce1df8f8fd0b73c62e13158a --- .../react/testing/ReactTestHelper.java | 2 +- .../facebook/react/ReactInstanceManager.java | 27 +++++++++------- .../react/cxxbridge/JSBundleLoader.java | 32 +++++++++++++------ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java index f75d3f281..c701c2b83 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java @@ -73,7 +73,7 @@ public class ReactTestHelper { .setJSExecutor(executor) .setRegistry(mNativeModuleRegistryBuilder.build()) .setJSModuleRegistry(mJSModuleRegistryBuilder.build()) - .setJSBundleLoader(JSBundleLoader.createFileLoader( + .setJSBundleLoader(JSBundleLoader.createAssetLoader( mContext, "assets://AndroidTestBundle.js")) .setNativeModuleCallExceptionHandler( diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index f5d1b9f57..cd5774fce 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -220,7 +220,7 @@ public abstract class ReactInstanceManager { protected final List mPackages = new ArrayList<>(); - protected @Nullable String mJSBundleFile; + protected @Nullable String mJSBundleAssetUrl; protected @Nullable JSBundleLoader mJSBundleLoader; protected @Nullable String mJSMainModuleName; protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener; @@ -252,7 +252,9 @@ public abstract class ReactInstanceManager { * Example: {@code "index.android.js"} */ public Builder setBundleAssetName(String bundleAssetName) { - return this.setJSBundleFile(bundleAssetName == null ? null : "assets://" + bundleAssetName); + mJSBundleAssetUrl = (bundleAssetName == null ? null : "assets://" + bundleAssetName); + mJSBundleLoader = null; + return this; } /** @@ -261,9 +263,12 @@ public abstract class ReactInstanceManager { * Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"} */ public Builder setJSBundleFile(String jsBundleFile) { - mJSBundleFile = jsBundleFile; - mJSBundleLoader = null; - return this; + if (jsBundleFile.startsWith("assets://")) { + mJSBundleAssetUrl = jsBundleFile; + mJSBundleLoader = null; + return this; + } + return setJSBundleLoader(JSBundleLoader.createFileLoader(jsBundleFile)); } /** @@ -274,7 +279,7 @@ public abstract class ReactInstanceManager { */ public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) { mJSBundleLoader = jsBundleLoader; - mJSBundleFile = null; + mJSBundleAssetUrl = null; return this; } @@ -376,11 +381,11 @@ public abstract class ReactInstanceManager { "Application property has not been set with this builder"); Assertions.assertCondition( - mUseDeveloperSupport || mJSBundleFile != null || mJSBundleLoader != null, - "JS Bundle File has to be provided when dev support is disabled"); + mUseDeveloperSupport || mJSBundleAssetUrl != null || mJSBundleLoader != null, + "JS Bundle File or Asset URL has to be provided when dev support is disabled"); Assertions.assertCondition( - mJSMainModuleName != null || mJSBundleFile != null || mJSBundleLoader != null, + mJSMainModuleName != null || mJSBundleAssetUrl != null || mJSBundleLoader != null, "Either MainModuleName or JS Bundle File needs to be provided"); if (mUIImplementationProvider == null) { @@ -392,8 +397,8 @@ public abstract class ReactInstanceManager { mApplication, mCurrentActivity, mDefaultHardwareBackBtnHandler, - (mJSBundleLoader == null && mJSBundleFile != null) ? - JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader, + (mJSBundleLoader == null && mJSBundleAssetUrl != null) ? + JSBundleLoader.createAssetLoader(mApplication, mJSBundleAssetUrl) : mJSBundleLoader, mJSMainModuleName, mPackages, mUseDeveloperSupport, 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 1f064212a..1c3df2ca2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/cxxbridge/JSBundleLoader.java @@ -24,20 +24,34 @@ public abstract class JSBundleLoader { /** * This loader is recommended one for release version of your app. In that case local JS executor - * should be used. JS bundle will be read from assets directory in native code to save on passing - * large strings from java to native memory. + * should be used. JS bundle will be read from assets in native code to save on passing large + * strings from java to native memory. */ - public static JSBundleLoader createFileLoader( + public static JSBundleLoader createAssetLoader( final Context context, - final String fileName) { + final String assetUrl) { return new JSBundleLoader() { @Override public void loadScript(CatalystInstanceImpl instance) { - if (fileName.startsWith("assets://")) { - instance.loadScriptFromAssets(context.getAssets(), fileName); - } else { - instance.loadScriptFromFile(fileName, fileName); - } + instance.loadScriptFromAssets(context.getAssets(), assetUrl); + } + + @Override + public String getSourceUrl() { + return assetUrl; + } + }; + } + + /** + * This loader loads bundle from file system. The bundle will be read in native code to save on + * passing large strings from java to native memorory. + */ + public static JSBundleLoader createFileLoader(final String fileName) { + return new JSBundleLoader() { + @Override + public void loadScript(CatalystInstanceImpl instance) { + instance.loadScriptFromFile(fileName, fileName); } @Override