Skip to content

Commit

Permalink
fix(android): promise leak after engine destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
iPel authored and zoomchan-cxj committed Mar 16, 2023
1 parent 28fdfa9 commit 7eca478
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@


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 {
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -86,17 +83,22 @@ public boolean needResolveBySelf() {
}

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;
}
Expand All @@ -107,15 +109,15 @@ public void doCallback(int code, Object resultObject) {
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);
hippyMap.pushString("moduleName", mModuleName);
hippyMap.pushString("moduleFunc", mModuleFunc);
hippyMap.pushString("callId", mCallId);
hippyMap.pushObject("params", resultObject);
mContext.getBridgeManager().execCallback(hippyMap, transferType);
context.getBridgeManager().execCallback(hippyMap, transferType);
}
}
}

0 comments on commit 7eca478

Please sign in to comment.