Rename "hasActiveCatalystInstance" to "hasActiveReactInstance" for clarification

Summary:
Sometimes ```hasActiveCatalystInstance()``` is used to check if it's safe to access the CatalystInstance, which will still crash in Venice.

Previously we mitigate this by changing ```reactContext.hasActiveCatalystInstance()``` to ```reactContext.hasActiveCatalystInstance() || reactContext.isBridgeless()```.

To solve this for all and good the plan is:
1, Rename ```hasActiveCatalystInstance()``` to ```hasActiveReactInstance()``` so it won't sounds like CatalystInstance-only.
2, Implement hasActiveReactInstance() for Venice. D27343867
3, Remove previous mitigation. D27343952

This diff is the first step, by xbgs there are **58** non-generated callsites of  ```hasActiveCatalystInstance()``` in code base which are all renamed in this diff.

Changelog:
[Android][Changed] - Rename "hasActiveCatalystInstance" to "hasActiveReactInstance"

Reviewed By: mdvacca

Differential Revision: D27335055

fbshipit-source-id: 5b8ff5e09b79a492e910bb8f197e70fa1360bcef
This commit is contained in:
Lulu Wu
2021-03-31 06:32:08 -07:00
committed by Facebook GitHub Bot
parent b86e52a9ec
commit dfa8eb0558
19 changed files with 27 additions and 26 deletions
@@ -518,7 +518,7 @@ public class ReactInstanceManager {
private void toggleElementInspector() {
ReactContext currentContext = getCurrentReactContext();
if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
if (currentContext != null && currentContext.hasActiveReactInstance()) {
currentContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("toggleElementInspector", null);
@@ -859,7 +859,7 @@ public class ReactInstanceManager {
if (mAttachedReactRoots.contains(reactRoot)) {
ReactContext currentContext = getCurrentReactContext();
mAttachedReactRoots.remove(reactRoot);
if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
if (currentContext != null && currentContext.hasActiveReactInstance()) {
detachViewFromInstance(reactRoot, currentContext.getCatalystInstance());
}
}
@@ -894,7 +894,7 @@ public class ReactInstanceManager {
ReactApplicationContext context;
synchronized (mReactContextLock) {
context = (ReactApplicationContext) getCurrentReactContext();
if (context == null || !context.hasActiveCatalystInstance()) {
if (context == null || !context.hasActiveReactInstance()) {
return null;
}
}
@@ -920,7 +920,7 @@ public class ReactInstanceManager {
ReactApplicationContext context;
synchronized (mReactContextLock) {
context = (ReactApplicationContext) getCurrentReactContext();
if (context == null || !context.hasActiveCatalystInstance()) {
if (context == null || !context.hasActiveReactInstance()) {
return null;
}
}
@@ -170,7 +170,8 @@ public class ReactContext extends ContextWrapper {
return Assertions.assertNotNull(mCatalystInstance);
}
public boolean hasActiveCatalystInstance() {
/** @return true if there is an non-null, alive react native instance */
public boolean hasActiveReactInstance() {
return mCatalystInstance != null && !mCatalystInstance.isDestroyed();
}
@@ -184,7 +185,7 @@ public class ReactContext extends ContextWrapper {
public void addLifecycleEventListener(final LifecycleEventListener listener) {
mLifecycleEventListeners.add(listener);
if (hasActiveCatalystInstance() || isBridgeless()) {
if (hasActiveReactInstance() || isBridgeless()) {
switch (mLifecycleState) {
case BEFORE_CREATE:
case BEFORE_RESUME:
@@ -452,7 +453,7 @@ public class ReactContext extends ContextWrapper {
}
public @Nullable JSIModule getJSIModule(JSIModuleType moduleType) {
if (!hasActiveCatalystInstance()) {
if (!hasActiveReactInstance()) {
throw new IllegalStateException(
"Unable to retrieve a JSIModule if CatalystInstance is not active.");
}
@@ -52,7 +52,7 @@ public abstract class ReactContextBaseJavaModule extends BaseJavaModule {
*/
@ThreadConfined(ANY)
protected @Nullable final ReactApplicationContext getReactApplicationContextIfActiveOrWarn() {
if (mReactApplicationContext.hasActiveCatalystInstance()
if (mReactApplicationContext.hasActiveReactInstance()
|| mReactApplicationContext.isBridgeless()) {
return mReactApplicationContext;
}
@@ -915,7 +915,7 @@ public abstract class DevSupportManagerBase
@Nullable ReactContext context = mCurrentContext;
if (context == null
|| (!context.isBridgeless() && !context.hasActiveCatalystInstance())) {
|| (!context.isBridgeless() && !context.hasActiveReactInstance())) {
return;
}
@@ -107,7 +107,7 @@ public class HeadlessJsTaskContext {
}
mActiveTasks.add(taskId);
mActiveTaskConfigs.put(taskId, new HeadlessJsTaskConfig(taskConfig));
if (reactContext.hasActiveCatalystInstance()) {
if (reactContext.hasActiveReactInstance()) {
reactContext
.getJSModule(AppRegistry.class)
.startHeadlessTask(taskId, taskConfig.getTaskKey(), taskConfig.getData());
@@ -56,7 +56,7 @@ public class AccessibilityInfoModule extends NativeAccessibilityInfoSpec
@Override
public void onChange(boolean selfChange, Uri uri) {
if (getReactApplicationContext().hasActiveCatalystInstance()) {
if (getReactApplicationContext().hasActiveReactInstance()) {
AccessibilityInfoModule.this.updateAndSendReduceMotionChangeEvent();
}
}
@@ -100,7 +100,7 @@ public class AppStateModule extends NativeAppStateSpec
// We don't gain anything interesting from logging here, and it's an extremely common
// race condition for an AppState event to be triggered as the Catalyst instance is being
// set up or torn down. So, just fail silently here.
if (!reactApplicationContext.hasActiveCatalystInstance()) {
if (!reactApplicationContext.hasActiveReactInstance()) {
return;
}
reactApplicationContext.getJSModule(RCTDeviceEventEmitter.class).emit(eventName, data);
@@ -61,7 +61,7 @@ public class DatePickerDialogModule extends NativeDatePickerAndroidSpec {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", ACTION_DATE_SET);
result.putInt("year", year);
@@ -74,7 +74,7 @@ public class DatePickerDialogModule extends NativeDatePickerAndroidSpec {
@Override
public void onDismiss(DialogInterface dialog) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveCatalystInstance()) {
if (!mPromiseResolved && getReactApplicationContext().hasActiveReactInstance()) {
WritableMap result = new WritableNativeMap();
result.putString("action", ACTION_DISMISSED);
mPromise.resolve(result);
@@ -83,7 +83,7 @@ public class DeviceInfoModule extends NativeDeviceInfoSpec implements LifecycleE
return;
}
if (mReactApplicationContext.hasActiveCatalystInstance()) {
if (mReactApplicationContext.hasActiveReactInstance()) {
// Don't emit an event to JS if the dimensions haven't changed
WritableNativeMap displayMetrics =
DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale);
@@ -130,7 +130,7 @@ public class DialogModule extends NativeDialogManagerAndroidSpec implements Life
public void onClick(DialogInterface dialog, int which) {
if (!mCallbackConsumed) {
if (getReactApplicationContext().isBridgeless()
|| getReactApplicationContext().hasActiveCatalystInstance()) {
|| getReactApplicationContext().hasActiveReactInstance()) {
mCallback.invoke(ACTION_BUTTON_CLICKED, which);
mCallbackConsumed = true;
}
@@ -141,7 +141,7 @@ public class DialogModule extends NativeDialogManagerAndroidSpec implements Life
public void onDismiss(DialogInterface dialog) {
if (!mCallbackConsumed) {
if (getReactApplicationContext().isBridgeless()
|| getReactApplicationContext().hasActiveCatalystInstance()) {
|| getReactApplicationContext().hasActiveReactInstance()) {
mCallback.invoke(ACTION_DISMISSED);
mCallbackConsumed = true;
}
@@ -292,7 +292,7 @@ public class ReactAccessibilityDelegate extends AccessibilityDelegateCompat {
final WritableMap event = Arguments.createMap();
event.putString("actionName", mAccessibilityActionsMap.get(action));
ReactContext reactContext = (ReactContext) host.getContext();
if (reactContext.hasActiveCatalystInstance()) {
if (reactContext.hasActiveReactInstance()) {
final int reactTag = host.getId();
final int surfaceId = UIManagerHelper.getSurfaceId(reactContext);
UIManager uiManager = UIManagerHelper.getUIManager(reactContext, reactTag);
@@ -72,7 +72,7 @@ public class UIManagerHelper {
}
// TODO T60461551: add tests to verify emission of events when the ReactContext is being turn
// down.
if (!context.hasActiveCatalystInstance()) {
if (!context.hasActiveReactInstance()) {
ReactSoftException.logSoftException(
"UIManagerHelper",
new ReactNoCrashSoftException(
@@ -89,7 +89,7 @@ public class ReactEventEmitter implements RCTModernEventEmitter {
int type = ViewUtil.getUIManagerType(reactTag);
assert type == UIManagerType.DEFAULT;
if (mRCTEventEmitter == null) {
if (mReactContext.hasActiveCatalystInstance()) {
if (mReactContext.hasActiveReactInstance()) {
mRCTEventEmitter = mReactContext.getJSModule(RCTEventEmitter.class);
} else {
ReactSoftException.logSoftException(
@@ -111,7 +111,7 @@ public class ReactTextShadowNode extends ReactBaseTextShadowNode {
text, layout, sTextPaintInstance, themedReactContext);
WritableMap event = Arguments.createMap();
event.putArray("lines", lines);
if (themedReactContext.hasActiveCatalystInstance()) {
if (themedReactContext.hasActiveReactInstance()) {
themedReactContext
.getJSModule(RCTEventEmitter.class)
.receiveEvent(getReactTag(), "topTextLayout", event);