From e632025917a000bf3fb5d296c6712a81435626d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gregorczyk?= Date: Tue, 12 Jul 2016 08:03:04 -0700 Subject: [PATCH] Allow apps to provide JSBundleLoader of their choice Reviewed By: tadeuzagallo Differential Revision: D3522798 fbshipit-source-id: 90324e44a02ad78885ff3c2a33ba58d4ee6a021a --- .../facebook/react/ReactInstanceManager.java | 38 ++++++++++++++----- .../react/XReactInstanceManagerImpl.java | 19 ++++------ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index 71818459b..ee4a8a2c3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -23,6 +23,7 @@ import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.cxxbridge.JSBundleLoader; import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.devsupport.DevSupportManager; import com.facebook.react.devsupport.RedBoxHandler; @@ -184,6 +185,7 @@ public abstract class ReactInstanceManager { protected final List mPackages = new ArrayList<>(); protected @Nullable String mJSBundleFile; + protected @Nullable JSBundleLoader mJSBundleLoader; protected @Nullable String mJSMainModuleName; protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener; protected @Nullable Application mApplication; @@ -225,6 +227,19 @@ public abstract class ReactInstanceManager { */ public Builder setJSBundleFile(String jsBundleFile) { mJSBundleFile = jsBundleFile; + mJSBundleLoader = null; + return this; + } + + /** + * Bundle loader to use when setting up JS environment. This supersedes + * prior invcations of {@link setJSBundleFile} and {@link setBundleAssetName}. + * + * Example: {@code JSBundleLoader.createFileLoader(application, bundleFile)} + */ + public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) { + mJSBundleLoader = jsBundleLoader; + mJSBundleFile = null; return this; } @@ -326,12 +341,20 @@ public abstract class ReactInstanceManager { * */ public ReactInstanceManager build() { + Assertions.assertNotNull( + mApplication, + "Application property has not been set with this builder"); + Assertions.assertCondition( - mUseDeveloperSupport || mJSBundleFile != null, + mJSBundleLoader == null || !mUseOldBridge, + "JSBundleLoader can't be used with the old bridge"); + + Assertions.assertCondition( + mUseDeveloperSupport || mJSBundleFile != null || mJSBundleLoader != null, "JS Bundle File has to be provided when dev support is disabled"); Assertions.assertCondition( - mJSMainModuleName != null || mJSBundleFile != null, + mJSMainModuleName != null || mJSBundleFile != null || mJSBundleLoader != null, "Either MainModuleName or JS Bundle File needs to be provided"); if (mUIImplementationProvider == null) { @@ -341,9 +364,7 @@ public abstract class ReactInstanceManager { if (mUseOldBridge) { return new ReactInstanceManagerImpl( - Assertions.assertNotNull( - mApplication, - "Application property has not been set with this builder"), + mApplication, mCurrentActivity, mDefaultHardwareBackBtnHandler, mJSBundleFile, @@ -358,12 +379,11 @@ public abstract class ReactInstanceManager { mRedBoxHandler); } else { return new XReactInstanceManagerImpl( - Assertions.assertNotNull( - mApplication, - "Application property has not been set with this builder"), + mApplication, mCurrentActivity, mDefaultHardwareBackBtnHandler, - mJSBundleFile, + (mJSBundleLoader == null && mJSBundleFile != null) ? + JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader, mJSMainModuleName, mPackages, mUseDeveloperSupport, diff --git a/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java index 42911f885..ee29bc079 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java @@ -109,7 +109,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; private @Nullable ReactContextInitAsyncTask mReactContextInitAsyncTask; /* accessed from any thread */ - private @Nullable String mJSBundleFile; /* path to JS bundle on file system */ + private final @Nullable JSBundleLoader mBundleLoader; /* path to JS bundle on file system */ private final @Nullable String mJSMainModuleName; /* path to JS bundle root on packager server */ private final List mPackages; private final DevSupportManager mDevSupportManager; @@ -275,7 +275,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; Context applicationContext, @Nullable Activity currentActivity, @Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, - @Nullable String jsBundleFile, + @Nullable JSBundleLoader bundleLoader, @Nullable String jsMainModuleName, List packages, boolean useDeveloperSupport, @@ -295,7 +295,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; mApplicationContext = applicationContext; mCurrentActivity = currentActivity; mDefaultBackButtonImpl = defaultHardwareBackBtnHandler; - mJSBundleFile = jsBundleFile; + mBundleLoader = bundleLoader; mJSMainModuleName = jsMainModuleName; mPackages = packages; mUseDeveloperSupport = useDeveloperSupport; @@ -382,7 +382,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; // If there is a up-to-date bundle downloaded from server, // with remote JS debugging disabled, always use that. onJSBundleLoadedFromServer(); - } else if (mJSBundleFile == null) { + } else if (mBundleLoader == null) { mDevSupportManager.handleReloadJS(); } else { mDevSupportManager.isPackagerRunning( @@ -398,7 +398,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; } else { // If dev server is down, disable the remote JS debugging. devSettings.setRemoteJSDebugEnabled(false); - recreateReactContextInBackgroundFromBundleFile(); + recreateReactContextInBackgroundFromBundleLoader(); } } }); @@ -408,16 +408,13 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; return; } - recreateReactContextInBackgroundFromBundleFile(); + recreateReactContextInBackgroundFromBundleLoader(); } - private void recreateReactContextInBackgroundFromBundleFile() { - boolean useLazyBundle = mJSCConfig.getConfigMap().hasKey("useLazyBundle") ? - mJSCConfig.getConfigMap().getBoolean("useLazyBundle") : false; - + private void recreateReactContextInBackgroundFromBundleLoader() { recreateReactContextInBackground( new JSCJavaScriptExecutor.Factory(mJSCConfig.getConfigMap()), - JSBundleLoader.createFileLoader(mApplicationContext, mJSBundleFile, useLazyBundle)); + mBundleLoader); } /**