QQ浏览器 v12.2.3.7053版本的 MD5 值为:8285ab3059e5c8b521a264dfbc5c3685

以下内容为反编译后的 HippyBridgeImpl.java 源代码,内容仅作参考


package com.tencent.mtt.hippy.bridge;

import android.content.res.AssetManager;
import android.text.TextUtils;
import com.tencent.mtt.hippy.HippyEngine;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.bridge.HippyBridge;
import com.tencent.mtt.hippy.common.HippyArray;
import com.tencent.mtt.hippy.devsupport.DebugWebSocketClient;
import com.tencent.mtt.hippy.devsupport.DevRemoteDebugProxy;
import com.tencent.mtt.hippy.devsupport.DevServerCallBack;
import com.tencent.mtt.hippy.devsupport.DevSupportManager;
import com.tencent.mtt.hippy.qb.adapter.monitor.QBHippyEngineMonitorAdapter;
import com.tencent.mtt.hippy.serialization.compatible.Deserializer;
import com.tencent.mtt.hippy.serialization.nio.reader.BinaryReader;
import com.tencent.mtt.hippy.serialization.nio.reader.SafeDirectReader;
import com.tencent.mtt.hippy.serialization.nio.reader.SafeHeapReader;
import com.tencent.mtt.hippy.serialization.string.InternalizedStringTable;
import com.tencent.mtt.hippy.utils.ArgumentUtils;
import com.tencent.mtt.hippy.utils.FileUtils;
import com.tencent.mtt.hippy.utils.LogUtils;
import com.tencent.mtt.hippy.utils.UIThreadUtils;
import com.tencent.mtt.hippy.utils.UrlUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

public class HippyBridgeImpl implements HippyBridge, DevRemoteDebugProxy.OnReceiveDataListener {
    private static volatile String mCodeCacheRootDir;
    private static final Object sBridgeSyncLock = new Object();
    private Deserializer deserializer;
    private final boolean enableV8Serialization;
    private HippyBridge.BridgeCallback mBridgeCallback;
    private final HippyEngineContext mContext;
    private String mDebugGlobalConfig;
    private NativeCallback mDebugInitJSFrameworkCallback;
    private String mDebugServerHost;
    private DebugWebSocketClient mDebugWebSocketClient;
    private final boolean mIsDevModule;
    private final boolean mSingleThreadMode;
    private BinaryReader safeDirectReader;
    private BinaryReader safeHeapReader;
    private final HippyEngine.V8InitParams v8InitParams;
    private long mV8RuntimeId = 0;
    private boolean mInit = false;

    public HippyBridgeImpl(HippyEngineContext hippyEngineContext, HippyBridge.BridgeCallback bridgeCallback, boolean z, boolean z2, boolean z3, String str, HippyEngine.V8InitParams v8InitParams) {
        File hippyFile;
        this.mBridgeCallback = bridgeCallback;
        this.mSingleThreadMode = z;
        this.enableV8Serialization = z2;
        this.mIsDevModule = z3;
        this.mDebugServerHost = str;
        this.mContext = hippyEngineContext;
        this.v8InitParams = v8InitParams;
        synchronized (sBridgeSyncLock) {
            if (mCodeCacheRootDir == null && (hippyFile = FileUtils.getHippyFile(this.mContext.getGlobalConfigs().getContext())) != null) {
                mCodeCacheRootDir = hippyFile.getAbsolutePath() + File.separator + "codecache" + File.separator;
            }
        }
        if (z2) {
            this.deserializer = new Deserializer(null, new InternalizedStringTable());
        }
    }

    private HippyArray bytesToArgument(ByteBuffer byteBuffer) {
        byte[] array;
        HippyArray parseToArray;
        BinaryReader binaryReader;
        if (this.enableV8Serialization) {
            LogUtils.d("hippy_bridge", "bytesToArgument using Buffer");
            try {
                if (byteBuffer.isDirect()) {
                    if (this.safeDirectReader == null) {
                        this.safeDirectReader = new SafeDirectReader();
                    }
                    binaryReader = this.safeDirectReader;
                } else {
                    if (this.safeHeapReader == null) {
                        this.safeHeapReader = new SafeHeapReader();
                    }
                    binaryReader = this.safeHeapReader;
                }
                binaryReader.a(byteBuffer);
                this.deserializer.a(binaryReader);
                this.deserializer.q();
                this.deserializer.r();
                Object s = this.deserializer.s();
                parseToArray = s instanceof HippyArray ? (HippyArray) s : null;
            } catch (Throwable th) {
                th.printStackTrace();
                LogUtils.e("compatible.Deserializer", "Error Parsing Buffer", th);
                return new HippyArray();
            }
        } else {
            LogUtils.d("hippy_bridge", "bytesToArgument using JSON");
            if (byteBuffer.isDirect()) {
                array = new byte[byteBuffer.limit()];
                byteBuffer.get(array);
            } else {
                array = byteBuffer.array();
            }
            parseToArray = ArgumentUtils.parseToArray(new String(array));
        }
        return parseToArray == null ? new HippyArray() : parseToArray;
    }

    public void initJSEngine(int i) {
        synchronized (HippyBridgeImpl.class) {
            try {
                this.mV8RuntimeId = initJSFramework(this.mDebugGlobalConfig.getBytes(StandardCharsets.UTF_16LE), this.mSingleThreadMode, this.enableV8Serialization, this.mIsDevModule, this.mDebugInitJSFrameworkCallback, i, this.v8InitParams);
                this.mInit = true;
            } finally {
            }
        }
    }

    public void InspectorChannel(byte[] bArr) {
        String str = new String(bArr, Charset.forName(ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN ? "UTF-16BE" : "UTF-16LE"));
        DebugWebSocketClient debugWebSocketClient = this.mDebugWebSocketClient;
        if (debugWebSocketClient != null) {
            debugWebSocketClient.a(str);
        }
    }

    public native void callFunction(String str, long j, NativeCallback nativeCallback, ByteBuffer byteBuffer, int i, int i2);

    public native void callFunction(String str, long j, NativeCallback nativeCallback, byte[] bArr, int i, int i2);

    @Override
    public void callFunction(String str, NativeCallback nativeCallback, ByteBuffer byteBuffer) {
        if (!this.mInit || TextUtils.isEmpty(str) || byteBuffer == null || byteBuffer.limit() == 0) {
            return;
        }
        int position = byteBuffer.position();
        int limit = byteBuffer.limit() - byteBuffer.position();
        if (byteBuffer.isDirect()) {
            callFunction(str, this.mV8RuntimeId, nativeCallback, byteBuffer, position, limit);
        } else {
            callFunction(str, this.mV8RuntimeId, nativeCallback, byteBuffer.array(), position + byteBuffer.arrayOffset(), limit);
        }
    }

    @Override
    public void callFunction(String str, NativeCallback nativeCallback, byte[] bArr) {
        callFunction(str, nativeCallback, bArr, 0, bArr.length);
    }

    @Override
    public void callFunction(String str, NativeCallback nativeCallback, byte[] bArr, int i, int i2) {
        if (!this.mInit || TextUtils.isEmpty(str) || bArr == null || i < 0 || i2 < 0 || i + i2 > bArr.length) {
            return;
        }
        callFunction(str, this.mV8RuntimeId, nativeCallback, bArr, i, i2);
    }

    public void callNatives(String str, String str2, String str3, ByteBuffer byteBuffer) {
        LogUtils.d("jni_callback", "callNatives [moduleName:" + str + " , moduleFunc: " + str2 + "]");
        if (this.mBridgeCallback != null) {
            this.mBridgeCallback.a(str, str2, str3, bytesToArgument(byteBuffer));
        }
    }

    public void callNatives(String str, String str2, String str3, byte[] bArr) {
        callNatives(str, str2, str3, ByteBuffer.wrap(bArr));
    }

    public native void destroy(long j, boolean z, NativeCallback nativeCallback);

    @Override
    public void destroy(NativeCallback nativeCallback) {
        destroy(this.mV8RuntimeId, this.mSingleThreadMode, nativeCallback);
    }

    public void fetchResourceWithUri(final String str, final long j) {
        UIThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                DevSupportManager devSupportManager = HippyBridgeImpl.this.mContext.getDevSupportManager();
                if (!TextUtils.isEmpty(str) && UrlUtils.isWebUrl(str) && devSupportManager != null) {
                    devSupportManager.a(str, new DevServerCallBack() {
                        @Override
                        public void onDevBundleLoadReady(InputStream inputStream) {
                            try {
                                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                byte[] bArr = new byte[2048];
                                while (true) {
                                    int read = inputStream.read(bArr);
                                    if (read <= 0) {
                                        byte[] byteArray = byteArrayOutputStream.toByteArray();
                                        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(byteArray.length);
                                        allocateDirect.put(byteArray);
                                        HippyBridgeImpl.this.onResourceReady(allocateDirect, HippyBridgeImpl.this.mV8RuntimeId, j);
                                        return;
                                    }
                                    byteArrayOutputStream.write(bArr, 0, read);
                                }
                            } catch (Throwable th) {
                                if (HippyBridgeImpl.this.mBridgeCallback != null) {
                                    HippyBridgeImpl.this.mBridgeCallback.a(th);
                                }
                                HippyBridgeImpl.this.onResourceReady(null, HippyBridgeImpl.this.mV8RuntimeId, j);
                            }
                        }

                        @Override
                        public void onDevBundleReLoad() {
                        }

                        @Override
                        public void onInitDevError(Throwable th) {
                            LogUtils.e("hippy", "requireSubResource: " + th.getMessage());
                            HippyBridgeImpl.this.onResourceReady(null, HippyBridgeImpl.this.mV8RuntimeId, j);
                        }
                    });
                    return;
                }
                LogUtils.e("HippyBridgeImpl", "fetchResourceWithUri: can not call loadRemoteResource with " + str);
            }
        });
    }

    @Override
    public long getV8RuntimeId() {
        return this.mV8RuntimeId;
    }

    @Override
    public void initJSBridge(String str, NativeCallback nativeCallback, final int i) {
        this.mDebugGlobalConfig = str;
        this.mDebugInitJSFrameworkCallback = nativeCallback;
        if (!this.mIsDevModule) {
            initJSEngine(i);
            return;
        }
        this.mDebugWebSocketClient = new DebugWebSocketClient();
        this.mDebugWebSocketClient.a(this);
        if (TextUtils.isEmpty(this.mDebugServerHost)) {
            this.mDebugServerHost = "localhost:38989";
        }
        this.mDebugWebSocketClient.a(String.format(Locale.US, "ws://%s/debugger-proxy?role=android_client", this.mDebugServerHost), new DebugWebSocketClient.JSDebuggerCallback() {
            @Override
            public void a(String str2) {
                LogUtils.d("hippyCore", "js debug socket connect success");
                HippyBridgeImpl.this.initJSEngine(i);
            }

            @Override
            public void a(Throwable th) {
                LogUtils.e("hippyCore", "js debug socket connect failed");
                HippyBridgeImpl.this.initJSEngine(i);
            }
        });
    }

    public native long initJSFramework(byte[] bArr, boolean z, boolean z2, boolean z3, NativeCallback nativeCallback, long j, HippyEngine.V8InitParams v8InitParams);

    @Override
    public void onDestroy() {
        DebugWebSocketClient debugWebSocketClient = this.mDebugWebSocketClient;
        if (debugWebSocketClient != null) {
            debugWebSocketClient.a();
            this.mDebugWebSocketClient = null;
        }
        if (this.mInit) {
            this.mInit = false;
            if (this.enableV8Serialization) {
                this.deserializer.p().a();
            }
            this.mV8RuntimeId = 0L;
            this.mBridgeCallback = null;
        }
    }

    @Override
    public void onReceiveData(String str) {
        if (this.mIsDevModule) {
            callFunction("onWebsocketMsg", (NativeCallback) null, str.getBytes(StandardCharsets.UTF_16LE));
        }
    }

    public native void onResourceReady(ByteBuffer byteBuffer, long j, long j2);

    public void reportException(String str, String str2) {
        LogUtils.e("reportException", "!!!!!!!!!!!!!!!!!!!");
        LogUtils.e("reportException", str);
        LogUtils.e("reportException", str2);
        HippyBridge.BridgeCallback bridgeCallback = this.mBridgeCallback;
        if (bridgeCallback != null) {
            bridgeCallback.a(str, str2);
        }
    }

    public native boolean runScriptFromUri(String str, AssetManager assetManager, boolean z, String str2, long j, NativeCallback nativeCallback);

    @Override
    public boolean runScriptFromUri(String str, AssetManager assetManager, boolean z, String str2, NativeCallback nativeCallback) {
        boolean z2;
        String str3;
        if (!this.mInit) {
            return false;
        }
        if (TextUtils.isEmpty(str2) || TextUtils.isEmpty(mCodeCacheRootDir)) {
            LogUtils.d(QBHippyEngineMonitorAdapter.TAG, "runScriptFromAssets codeCacheTag is null");
            try {
                return runScriptFromUri(str, assetManager, false, "" + str2 + File.separator, this.mV8RuntimeId, nativeCallback);
            } catch (Throwable th) {
                HippyBridge.BridgeCallback bridgeCallback = this.mBridgeCallback;
                if (bridgeCallback == null) {
                    return false;
                }
                bridgeCallback.a(th);
                return false;
            }
        }
        String str4 = mCodeCacheRootDir + str2 + File.separator;
        File file = new File(str4);
        if (file.exists() || file.mkdirs()) {
            z2 = z;
            str3 = str4;
        } else {
            str3 = "";
            z2 = false;
        }
        return runScriptFromUri(str, assetManager, z2, str3, this.mV8RuntimeId, nativeCallback);
    }
}