Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[Android] Fix Android Crash in PostTaskToMsgLoop (#2830)
Browse files Browse the repository at this point in the history
* [Android] Fix Android Crash in `EagleBridge::WeexCoreHandler::PostTaskToMsgLoop`

As `WeexCoreManager::Instance()->script_thread()` is initialized in `WeexCore::InitFramework`, `EagleBridge::WeexCoreHandler::PostTaskToMsgLoop` will crash if `WeexCore::InitFramework` is not invoked.

Fix this problem by not supporting `EagleBridge::WeexCoreHandler::PostTaskToMsgLoop` anymore.

```
libweexcore.so (_ZN8WeexCore11EagleBridge15WeexCoreHandler17PostTaskToMsgLoopERKNSt6__ndk18functionIFvvEEE+13)
```

* [Android] Downgrade if JSC is not initialized in eagle mode

* [Android] Add `isJSFrameworkInit` before calling `nativeInvokeOnSuccess`
  • Loading branch information
YorkShen authored and Darin726 committed Aug 15, 2019
1 parent 4274a32 commit 16e476e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public void send(String instanceId, String url, long nativeCallback) {

@Keep
@CalledByNative
public void getBundleType(String instanceId, String content, long nativeCallback){
public void getBundleType(String instanceId, final String content, final long nativeCallback){
BundType bundleType = WXBridgeManager.getInstance().getBundleType("", content);
String bundleTypeStr = bundleType == null ? "Others" : bundleType.toString();
final String bundleTypeStr = bundleType == null ? "Others" : bundleType.toString();
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
if ("Others".equalsIgnoreCase(bundleTypeStr) && null != instance){
WXExceptionUtils.commitCriticalExceptionRT(
Expand All @@ -99,7 +99,17 @@ public void getBundleType(String instanceId, String content, long nativeCallback
null
);
}
nativeInvokeOnSuccess(nativeCallback, content, bundleTypeStr);
WXBridgeManager.getInstance().post(new Runnable() {
@Override
public void run() {
if(WXBridgeManager.getInstance().isJSFrameworkInit()) {
nativeInvokeOnSuccess(nativeCallback, content, bundleTypeStr);
}
else {
nativeInvokeOnFailed(nativeCallback);
}
}
});
}

class OnHttpListenerInner extends WXHttpListener {
Expand All @@ -112,9 +122,9 @@ class OnHttpListenerInner extends WXHttpListener {

@Override
public void onSuccess(WXResponse response) {
String script = new String(response.originalData);
final String script = new String(response.originalData);
BundType bundleType = WXBridgeManager.getInstance().getBundleType("", script);
String bundleTypeStr = bundleType == null ? "Others" : bundleType.toString();
final String bundleTypeStr = bundleType == null ? "Others" : bundleType.toString();
if ("Others".equalsIgnoreCase(bundleTypeStr) && null != getInstance()){
WXExceptionUtils.commitCriticalExceptionRT(
getInstance().getInstanceId(),
Expand All @@ -124,7 +134,17 @@ public void onSuccess(WXResponse response) {
null
);
}
nativeInvokeOnSuccess(sNativeCallback, script, bundleTypeStr);
WXBridgeManager.getInstance().post(new Runnable() {
@Override
public void run() {
if(WXBridgeManager.getInstance().isJSFrameworkInit()) {
nativeInvokeOnSuccess(sNativeCallback, script, bundleTypeStr);
}
else{
nativeInvokeOnFailed(sNativeCallback);
}
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package com.taobao.weex.bridge;

import static com.taobao.weex.bridge.WXModuleManager.createDomModule;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
Expand All @@ -27,12 +29,13 @@
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RestrictTo;
import android.support.annotation.RestrictTo.Scope;
import android.support.annotation.UiThread;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
Expand Down Expand Up @@ -96,8 +99,6 @@
import com.taobao.weex.utils.batch.BactchExecutor;
import com.taobao.weex.utils.batch.Interceptor;
import com.taobao.weex.utils.tools.LogDetail;
import com.taobao.weex.utils.tools.TimeCalculator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -109,7 +110,6 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -122,8 +122,6 @@
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static com.taobao.weex.bridge.WXModuleManager.createDomModule;

/**
* Manager class for communication between JavaScript and Android.
* <ol>
Expand Down Expand Up @@ -152,6 +150,8 @@ public class WXBridgeManager implements Callback, BactchExecutor {
public static final String METHOD_CREATE_INSTANCE = "createInstance";
public static final String METHOD_CREATE_PAGE_WITH_CONTENT = "CreatePageWithContent";
public static final String METHOD_UPDATE_COMPONENT_WITH_DATA = "UpdateComponentData";
private static final String METHOD_POST_TASK_TO_MSG_LOOP = "PostTaskToMsgLoop";
private static final String METHOD_JSFM_NOT_INIT_IN_EAGLE_MODE = "JsfmNotInitInEagleMode";
public static final String METHOD_DESTROY_INSTANCE = "destroyInstance";
public static final String METHOD_CALL_JS = "callJS";
public static final String METHOD_SET_TIMEOUT = "setTimeoutCallback";
Expand Down Expand Up @@ -369,7 +369,8 @@ public void run() {

// setJSFrameworkInit and isJSFrameworkInit may use on diff thread
// use volatile
private boolean isJSFrameworkInit() {
@RestrictTo(Scope.LIBRARY)
boolean isJSFrameworkInit() {
return mInit;
}

Expand Down Expand Up @@ -2552,7 +2553,12 @@ public void reportJSException(String instanceId, String function,
reportErrorCode = WXErrorCode.WX_RENDER_ERR_JS_CREATE_INSTANCE;
} else if ( METHOD_CREATE_INSTANCE_CONTEXT.equals(function) && !instance.getApmForInstance().hasAddView){
reportErrorCode = WXErrorCode.WX_RENDER_ERR_JS_CREATE_INSTANCE_CONTEXT;
} else if ((METHOD_UPDATE_COMPONENT_WITH_DATA.equals(function) || METHOD_CREATE_PAGE_WITH_CONTENT.equals(function)) && !instance.getApmForInstance().hasAddView){
} else if (
(METHOD_UPDATE_COMPONENT_WITH_DATA.equals(function) ||
METHOD_CREATE_PAGE_WITH_CONTENT.equals(function) ||
METHOD_POST_TASK_TO_MSG_LOOP.equals(function) ||
METHOD_JSFM_NOT_INIT_IN_EAGLE_MODE.equals(function) )
&& !instance.getApmForInstance().hasAddView){
reportErrorCode = WXErrorCode.WX_DEGRAD_EAGLE_RENDER_ERROR;
}
instance.onJSException(reportErrorCode.getErrorCode(), function, exception);
Expand Down
7 changes: 4 additions & 3 deletions weex_core/Source/core/bridge/eagle_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ namespace WeexCore {
);
}

//FIXME Please don't call this method, which will cause downgrade of Weex.
void EagleBridge::WeexCoreHandler::PostTaskToMsgLoop(const weex::base::Closure& closure){
#ifdef OS_ANDROID
WeexCoreManager::Instance()->script_thread()->message_loop()->PostTask(closure);
#endif
WeexCore::WeexCoreManager::Instance()->getPlatformBridge()->platform_side()->ReportException(
"", "PostTaskToMsgLoop",
"PostTaskToMsgLoop is not supported anymore, please update to the latest version of Weex.");
}

int EagleBridge::DataRenderHandler::DestroyInstance(const char *instanceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "android/base/string/scoped_jstring_utf8.h"
#include "base/android/jniprebuild/jniheader/RequestHandler_jni.h"
#include "base/android/jni/android_jni.h"
#include "core/manager/weex_core_manager.h"

using namespace weex::core::network;

Expand All @@ -44,6 +45,9 @@ static void InvokeOnFailed(JNIEnv* env, jobject jcaller, jlong callback) {
CallbackWrapper* callback_wrapper =
reinterpret_cast<CallbackWrapper*>(callback);
delete callback_wrapper;
WeexCore::WeexCoreManager::Instance()->getPlatformBridge()->platform_side()->ReportException(
"", "JsfmNotInitInEagleMode",
"JSFramework is not initialized when executing bundle JS in eagle mode");
}

namespace weex {
Expand Down

0 comments on commit 16e476e

Please sign in to comment.