From 73f17908e64cff7105d05118d6f61ea8f99fc79e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gregorczyk?= Date: Mon, 21 Aug 2017 13:32:46 -0700 Subject: [PATCH] Make JavaScriptExecutor.Factory a configurable property of ReactInstanceManager Reviewed By: mhorowitz Differential Revision: D5662431 fbshipit-source-id: 17dca2744de645740cef252efbf83902acde5046 --- .../react/testing/ReactTestHelper.java | 4 ++-- .../facebook/react/ReactInstanceManager.java | 19 +++++++++++-------- .../react/ReactInstanceManagerBuilder.java | 15 +++++++++++++++ .../react/bridge/JSCJavaScriptExecutor.java | 19 ++----------------- .../bridge/JSCJavaScriptExecutorFactory.java | 19 +++++++++++++++++++ .../react/bridge/JavaScriptExecutor.java | 4 ---- .../bridge/JavaScriptExecutorFactory.java | 14 ++++++++++++++ .../react/bridge/ProxyJavaScriptExecutor.java | 2 +- 8 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutorFactory.java create mode 100644 ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutorFactory.java 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 79f10a822..212397f00 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactTestHelper.java @@ -30,7 +30,7 @@ import com.facebook.react.bridge.WritableNativeMap; import com.facebook.react.bridge.queue.ReactQueueConfigurationSpec; import com.facebook.react.bridge.CatalystInstanceImpl; import com.facebook.react.bridge.JSBundleLoader; -import com.facebook.react.bridge.JSCJavaScriptExecutor; +import com.facebook.react.bridge.JSCJavaScriptExecutorFactory; import com.facebook.react.bridge.JavaScriptExecutor; import com.facebook.react.modules.core.ReactChoreographer; @@ -73,7 +73,7 @@ public class ReactTestHelper { } JavaScriptExecutor executor = null; try { - executor = new JSCJavaScriptExecutor.Factory(new WritableNativeMap()).create(); + executor = new JSCJavaScriptExecutorFactory().create(); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index ade12c27f..88f245a6a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -43,9 +43,9 @@ import com.facebook.infer.annotation.ThreadSafe; import com.facebook.react.bridge.CatalystInstance; import com.facebook.react.bridge.CatalystInstanceImpl; import com.facebook.react.bridge.JSBundleLoader; -import com.facebook.react.bridge.JSCJavaScriptExecutor; import com.facebook.react.bridge.JavaJSExecutor; import com.facebook.react.bridge.JavaScriptExecutor; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.NativeArray; import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NativeModuleRegistry; @@ -127,7 +127,8 @@ public class ReactInstanceManager { private volatile @Nullable Thread mCreateReactContextThread; /* accessed from any thread */ - private final @Nullable JSBundleLoader mBundleLoader; /* path to JS bundle on file system */ + private final JavaScriptExecutorFactory mJavaScriptExecutorFactory; + private final @Nullable JSBundleLoader mBundleLoader; private final @Nullable String mJSMainModulePath; /* path to JS bundle root on packager server */ private final List mPackages; private final List mInitFunctions; @@ -178,17 +179,17 @@ public class ReactInstanceManager { }; private class ReactContextInitParams { - private final JavaScriptExecutor.Factory mJsExecutorFactory; + private final JavaScriptExecutorFactory mJsExecutorFactory; private final JSBundleLoader mJsBundleLoader; public ReactContextInitParams( - JavaScriptExecutor.Factory jsExecutorFactory, + JavaScriptExecutorFactory jsExecutorFactory, JSBundleLoader jsBundleLoader) { mJsExecutorFactory = Assertions.assertNotNull(jsExecutorFactory); mJsBundleLoader = Assertions.assertNotNull(jsBundleLoader); } - public JavaScriptExecutor.Factory getJsExecutorFactory() { + public JavaScriptExecutorFactory getJsExecutorFactory() { return mJsExecutorFactory; } @@ -208,6 +209,7 @@ public class ReactInstanceManager { Context applicationContext, @Nullable Activity currentActivity, @Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, + JavaScriptExecutorFactory javaScriptExecutorFactory, @Nullable JSBundleLoader bundleLoader, @Nullable String jsMainModulePath, List packages, @@ -233,6 +235,7 @@ public class ReactInstanceManager { mApplicationContext = applicationContext; mCurrentActivity = currentActivity; mDefaultBackButtonImpl = defaultHardwareBackBtnHandler; + mJavaScriptExecutorFactory = javaScriptExecutorFactory; mBundleLoader = bundleLoader; mJSMainModulePath = jsMainModulePath; mPackages = new ArrayList<>(); @@ -444,7 +447,7 @@ public class ReactInstanceManager { Log.d( ReactConstants.TAG, "ReactInstanceManager.recreateReactContextInBackgroundFromBundleLoader()"); - recreateReactContextInBackground(new JSCJavaScriptExecutor.Factory(), mBundleLoader); + recreateReactContextInBackground(mJavaScriptExecutorFactory, mBundleLoader); } /** @@ -790,14 +793,14 @@ public class ReactInstanceManager { private void onJSBundleLoadedFromServer() { Log.d(ReactConstants.TAG, "ReactInstanceManager.onJSBundleLoadedFromServer()"); recreateReactContextInBackground( - new JSCJavaScriptExecutor.Factory(), + mJavaScriptExecutorFactory, JSBundleLoader.createCachedBundleFromNetworkLoader( mDevSupportManager.getSourceUrl(), mDevSupportManager.getDownloadedJSBundleFile())); } @ThreadConfined(UI) private void recreateReactContextInBackground( - JavaScriptExecutor.Factory jsExecutorFactory, + JavaScriptExecutorFactory jsExecutorFactory, JSBundleLoader jsBundleLoader) { Log.d(ReactConstants.TAG, "ReactInstanceManager.recreateReactContextInBackground()"); UiThreadUtil.assertOnUiThread(); diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index e906cb13c..56cba793f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -5,7 +5,9 @@ package com.facebook.react; import android.app.Activity; import android.app.Application; import com.facebook.infer.annotation.Assertions; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.JSBundleLoader; +import com.facebook.react.bridge.JSCJavaScriptExecutorFactory; import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener; import com.facebook.react.common.LifecycleState; @@ -40,6 +42,7 @@ public class ReactInstanceManagerBuilder { private boolean mLazyNativeModulesEnabled; private boolean mLazyViewManagersEnabled; private @Nullable DevBundleDownloadListener mDevBundleDownloadListener; + private @Nullable JavaScriptExecutorFactory mJavaScriptExecutorFactory; private boolean mUseSeparateUIBackgroundThread; private int mMinNumShakes = 1; private boolean mEnableSplitPackage; @@ -59,6 +62,15 @@ public class ReactInstanceManagerBuilder { return this; } + /** + * Factory for desired implementation of JavaScriptExecutor. + */ + public ReactInstanceManagerBuilder setJavaScriptExecutorFactory( + @Nullable JavaScriptExecutorFactory javaScriptExecutorFactory) { + mJavaScriptExecutorFactory = javaScriptExecutorFactory; + return this; + } + /** * Name of the JS bundle file to be loaded from application's raw assets. * Example: {@code "index.android.js"} @@ -249,6 +261,9 @@ public class ReactInstanceManagerBuilder { mApplication, mCurrentActivity, mDefaultHardwareBackBtnHandler, + mJavaScriptExecutorFactory == null + ? new JSCJavaScriptExecutorFactory() + : mJavaScriptExecutorFactory, (mJSBundleLoader == null && mJSBundleAssetUrl != null) ? JSBundleLoader.createAssetLoader( mApplication, mJSBundleAssetUrl, false /*Asynchronous*/) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutor.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutor.java index b123ff9bd..e329cf5db 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutor.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutor.java @@ -13,27 +13,12 @@ import com.facebook.jni.HybridData; import com.facebook.proguard.annotations.DoNotStrip; @DoNotStrip -public class JSCJavaScriptExecutor extends JavaScriptExecutor { - public static class Factory implements JavaScriptExecutor.Factory { - private ReadableNativeMap mJSCConfig; - - public Factory() { - WritableNativeMap jscConfig = new WritableNativeMap(); - jscConfig.putString("OwnerIdentity", "ReactNative"); - mJSCConfig = jscConfig; - } - - @Override - public JavaScriptExecutor create() throws Exception { - return new JSCJavaScriptExecutor(mJSCConfig); - } - } - +/* package */ class JSCJavaScriptExecutor extends JavaScriptExecutor { static { ReactBridge.staticInit(); } - private JSCJavaScriptExecutor(ReadableNativeMap jscConfig) { + /* package */ JSCJavaScriptExecutor(ReadableNativeMap jscConfig) { super(initHybrid(jscConfig)); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutorFactory.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutorFactory.java new file mode 100644 index 000000000..b02a2f94d --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JSCJavaScriptExecutorFactory.java @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.bridge; + +public class JSCJavaScriptExecutorFactory implements JavaScriptExecutorFactory { + @Override + public JavaScriptExecutor create() throws Exception { + WritableNativeMap jscConfig = new WritableNativeMap(); + jscConfig.putString("OwnerIdentity", "ReactNative"); + return new JSCJavaScriptExecutor(jscConfig); + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutor.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutor.java index f03218ff5..562e4fde6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutor.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutor.java @@ -14,10 +14,6 @@ import com.facebook.proguard.annotations.DoNotStrip; @DoNotStrip public abstract class JavaScriptExecutor { - public interface Factory { - JavaScriptExecutor create() throws Exception; - } - private final HybridData mHybridData; protected JavaScriptExecutor(HybridData hybridData) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutorFactory.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutorFactory.java new file mode 100644 index 000000000..7e12b9f59 --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaScriptExecutorFactory.java @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.bridge; + +public interface JavaScriptExecutorFactory { + JavaScriptExecutor create() throws Exception; +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ProxyJavaScriptExecutor.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ProxyJavaScriptExecutor.java index ae25e0cd9..08a225948 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ProxyJavaScriptExecutor.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ProxyJavaScriptExecutor.java @@ -24,7 +24,7 @@ import com.facebook.proguard.annotations.DoNotStrip; */ @DoNotStrip public class ProxyJavaScriptExecutor extends JavaScriptExecutor { - public static class Factory implements JavaScriptExecutor.Factory { + public static class Factory implements JavaScriptExecutorFactory { private final JavaJSExecutor.Factory mJavaJSExecutorFactory; public Factory(JavaJSExecutor.Factory javaJSExecutorFactory) {