diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java index 50aff5fcb..aba8879f6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java @@ -21,7 +21,7 @@ import javax.annotation.Nullable; */ @DoNotStrip public interface CatalystInstance - extends MemoryPressureListener, JSInstance { + extends MemoryPressureListener, JSInstance, JSBundleLoaderDelegate { void runJSBundle(); // Returns the status of running the JS bundle; waits for an answer if runJSBundle is running diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java index b0cf871ad..d1aff6c8b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java @@ -201,17 +201,8 @@ public class CatalystInstanceImpl implements CatalystInstance { Collection javaModules, Collection cxxModules); - /** - * 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) { + @Override + public void setSourceURLs(String deviceURL, String remoteURL) { mSourceURL = deviceURL; jniSetSourceURL(remoteURL); } @@ -221,17 +212,20 @@ public class CatalystInstanceImpl implements CatalystInstance { jniRegisterSegment(segmentId, path); } - /* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) { + @Override + public void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) { mSourceURL = assetURL; jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously); } - /* package */ void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) { + @Override + public void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) { mSourceURL = sourceURL; jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously); } - /* package */ void loadScriptFromDeltaBundle( + @Override + public void loadScriptFromDeltaBundle( String sourceURL, NativeDeltaClient deltaClient, boolean loadSynchronously) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/FallbackJSBundleLoader.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/FallbackJSBundleLoader.java index 93f14f1f4..50c47aa8a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/FallbackJSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/FallbackJSBundleLoader.java @@ -50,10 +50,10 @@ public final class FallbackJSBundleLoader extends JSBundleLoader { * it is replaced by the next most preferred loader. */ @Override - public String loadScript(CatalystInstanceImpl instance) { + public String loadScript(JSBundleLoaderDelegate delegate) { while (true) { try { - return getDelegateLoader().loadScript(instance); + return getDelegateLoader().loadScript(delegate); } catch (Exception e) { if (e.getMessage() == null || !e.getMessage().startsWith(RECOVERABLE)) { throw e; diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java index 44164e32e..3aff0da69 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java @@ -8,12 +8,11 @@ package com.facebook.react.bridge; import android.content.Context; -import com.facebook.react.bridge.NativeDeltaClient; import com.facebook.react.common.DebugServerException; /** - * A class that stores JS bundle information and allows {@link CatalystInstance} to load a correct - * bundle through {@link ReactBridge}. + * A class that stores JS bundle information and allows a {@link JSBundleLoaderDelegate} + * (e.g. {@link CatalystInstance}) to load a correct bundle through {@link ReactBridge}. */ public abstract class JSBundleLoader { @@ -28,8 +27,8 @@ public abstract class JSBundleLoader { final boolean loadSynchronously) { return new JSBundleLoader() { @Override - public String loadScript(CatalystInstanceImpl instance) { - instance.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously); + public String loadScript(JSBundleLoaderDelegate delegate) { + delegate.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously); return assetUrl; } }; @@ -49,8 +48,8 @@ public abstract class JSBundleLoader { final boolean loadSynchronously) { return new JSBundleLoader() { @Override - public String loadScript(CatalystInstanceImpl instance) { - instance.loadScriptFromFile(fileName, assetUrl, loadSynchronously); + public String loadScript(JSBundleLoaderDelegate delegate) { + delegate.loadScriptFromFile(fileName, assetUrl, loadSynchronously); return fileName; } }; @@ -68,9 +67,9 @@ public abstract class JSBundleLoader { final String cachedFileLocation) { return new JSBundleLoader() { @Override - public String loadScript(CatalystInstanceImpl instance) { + public String loadScript(JSBundleLoaderDelegate delegate) { try { - instance.loadScriptFromFile(cachedFileLocation, sourceURL, false); + delegate.loadScriptFromFile(cachedFileLocation, sourceURL, false); return sourceURL; } catch (Exception e) { throw DebugServerException.makeGeneric(e.getMessage(), e); @@ -90,9 +89,9 @@ public abstract class JSBundleLoader { final NativeDeltaClient nativeDeltaClient) { return new JSBundleLoader() { @Override - public String loadScript(CatalystInstanceImpl instance) { + public String loadScript(JSBundleLoaderDelegate delegate) { try { - instance.loadScriptFromDeltaBundle(sourceURL, nativeDeltaClient, false); + delegate.loadScriptFromDeltaBundle(sourceURL, nativeDeltaClient, false); return sourceURL; } catch (Exception e) { throw DebugServerException.makeGeneric(e.getMessage(), e); @@ -110,13 +109,13 @@ public abstract class JSBundleLoader { final String realSourceURL) { return new JSBundleLoader() { @Override - public String loadScript(CatalystInstanceImpl instance) { - instance.setSourceURLs(realSourceURL, proxySourceURL); + public String loadScript(JSBundleLoaderDelegate delegate) { + delegate.setSourceURLs(realSourceURL, proxySourceURL); return realSourceURL; } }; } /** Loads the script, returning the URL of the source it loaded. */ - public abstract String loadScript(CatalystInstanceImpl instance); + public abstract String loadScript(JSBundleLoaderDelegate delegate); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoaderDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoaderDelegate.java new file mode 100644 index 000000000..29c3af69d --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoaderDelegate.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.bridge; + +import android.content.Context; +import android.content.res.AssetManager; + +/** + * An interface for classes that initialize JavaScript using {@link JSBundleLoader} + */ +public interface JSBundleLoaderDelegate { + + /** + * Load a JS bundle from Android assets. See {@link JSBundleLoader#createAssetLoader(Context, String, boolean)} + * @param assetManager + * @param assetURL + * @param loadSynchronously + */ + void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously); + + /** + * Load a JS bundle from the filesystem. + * See {@link JSBundleLoader#createFileLoader(String)} and {@link JSBundleLoader#createCachedBundleFromNetworkLoader(String, String)} + * @param fileName + * @param sourceURL + * @param loadSynchronously + */ + void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously); + + /** + * Load a delta bundle from Metro. + * See {@link JSBundleLoader#createDeltaFromNetworkLoader(String, NativeDeltaClient)} + * @param sourceURL + * @param deltaClient + * @param loadSynchronously + */ + void loadScriptFromDeltaBundle( + String sourceURL, + NativeDeltaClient deltaClient, + boolean loadSynchronously); + + /** + * 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. + */ + void setSourceURLs(String deviceURL, String remoteURL); +}