fix(android): promise leak after engine destroy

This commit is contained in:
iPel
2023-03-16 17:30:31 +08:00
committed by zoomchan-cxj
parent 28fdfa925f
commit 7eca478b86
@@ -17,16 +17,13 @@ package com.tencent.mtt.hippy.modules;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.tencent.mtt.hippy.HippyEngine.BridgeTransferType;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.adapter.monitor.HippyEngineMonitorAdapter;
import com.tencent.mtt.hippy.common.HippyMap;
import com.tencent.mtt.hippy.runtime.builtins.JSMap;
import com.tencent.mtt.hippy.runtime.builtins.JSObject;
import com.tencent.mtt.hippy.runtime.builtins.JSValue;
import java.util.HashMap;
import java.lang.ref.WeakReference;
@SuppressWarnings({"deprecation", "unused"})
public class PromiseImpl implements Promise {
@@ -35,7 +32,7 @@ public class PromiseImpl implements Promise {
public static final int PROMISE_CODE_NORMAN_ERROR = 1;
public static final int PROMISE_CODE_OTHER_ERROR = 2;
private static final String CALL_ID_NO_CALLBACK = "-1";
private HippyEngineContext mContext;
private WeakReference<HippyEngineContext> mContextRef;
private final String mModuleName;
private final String mModuleFunc;
private final String mCallId;
@@ -44,14 +41,14 @@ public class PromiseImpl implements Promise {
public PromiseImpl(HippyEngineContext context, String moduleName, String moduleFunc,
String callId) {
this.mContext = context;
this.mContextRef = new WeakReference<>(context);
this.mModuleName = moduleName;
this.mModuleFunc = moduleFunc;
this.mCallId = callId;
}
public void setContext(HippyEngineContext context) {
mContext = context;
mContextRef = new WeakReference<>(context);
}
public String getCallId() {
@@ -86,17 +83,22 @@ public class PromiseImpl implements Promise {
}
private boolean onInterceptPromiseCallBack(Object resultObject) {
HippyEngineMonitorAdapter adapter = mContext.getGlobalConfigs().getEngineMonitorAdapter();
final HippyEngineContext context = mContextRef.get();
if (context == null) {
return false;
}
HippyEngineMonitorAdapter adapter = context.getGlobalConfigs().getEngineMonitorAdapter();
if (adapter == null) {
return false;
}
return adapter
.onInterceptPromiseCallback(mContext.getComponentName(), mModuleName, mModuleFunc,
.onInterceptPromiseCallback(context.getComponentName(), mModuleName, mModuleFunc,
mCallId, resultObject);
}
public void doCallback(int code, Object resultObject) {
if (onInterceptPromiseCallBack(resultObject) || TextUtils
final HippyEngineContext context = mContextRef.get();
if (context == null || onInterceptPromiseCallBack(resultObject) || TextUtils
.equals(CALL_ID_NO_CALLBACK, mCallId)) {
return;
}
@@ -107,7 +109,7 @@ public class PromiseImpl implements Promise {
jsObject.set("moduleFunc", mModuleFunc);
jsObject.set("callId", mCallId);
jsObject.set("params", resultObject);
mContext.getBridgeManager().execCallback(jsObject, transferType);
context.getBridgeManager().execCallback(jsObject, transferType);
} else {
HippyMap hippyMap = new HippyMap();
hippyMap.pushInt("result", code);
@@ -115,7 +117,7 @@ public class PromiseImpl implements Promise {
hippyMap.pushString("moduleFunc", mModuleFunc);
hippyMap.pushString("callId", mCallId);
hippyMap.pushObject("params", resultObject);
mContext.getBridgeManager().execCallback(hippyMap, transferType);
context.getBridgeManager().execCallback(hippyMap, transferType);
}
}
}