Skip to content

Commit

Permalink
feat(android): support dynamic load
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 authored and churchill-zhang committed Dec 25, 2020
1 parent 50c427a commit 67b0b3f
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.mtt.hippy.bridge.bundleloader.HippyAssetBundleLoader;
import com.tencent.mtt.hippy.bridge.bundleloader.HippyBundleLoader;
import com.tencent.mtt.hippy.bridge.bundleloader.HippyFileBundleLoader;
import com.tencent.mtt.hippy.bridge.bundleloader.HippyRemoteBundleLoader;
import com.tencent.mtt.hippy.bridge.libraryloader.LibraryLoader;
import com.tencent.mtt.hippy.common.Callback;
import com.tencent.mtt.hippy.common.HippyJsException;
Expand All @@ -45,6 +46,7 @@
import com.tencent.mtt.hippy.utils.TimeMonitor;
import com.tencent.mtt.hippy.utils.UIThreadUtils;
import java.io.File;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -134,7 +136,7 @@ else if (!TextUtils.isEmpty(params.coreJSFilePath))
this.mPreloadBundleLoader = preloadBundleLoader;
this.mAPIProviders = params.providers;
this.mDebugMode = params.debugMode;
this.mServerBundleName = params.debugMode ? params.debugBundleName : "";;
this.mServerBundleName = params.debugMode ? params.debugBundleName : "";
this.mStartTimeMonitor = new TimeMonitor(!params.debugMode);
this.mEnableHippyBuffer = params.enableBuffer;
this.mServerHost = params.debugServerHost;
Expand Down Expand Up @@ -169,10 +171,11 @@ public void initEngine(EngineListener listener)
mDevSupportManager = new DevSupportManager(mGlobalConfigs, mDebugMode, mServerHost, mServerBundleName);
mDevSupportManager.setDevCallback(this);

if(mDebugMode)
{
if(mDebugMode) {
mDevSupportManager.init(null);
return;
String url = mDevSupportManager.createResourceUrl(mServerBundleName);
mCoreBundleLoader = new HippyRemoteBundleLoader(url);
((HippyRemoteBundleLoader)mCoreBundleLoader).setIsDebugMode(true);
}

LogUtils.d(TAG, "start restartEngineInBackground...");
Expand Down Expand Up @@ -679,11 +682,21 @@ public void onInstancePause(int id)
}
}

@Override
public void onDevBundleLoadReady(InputStream inputStream) {

}

@Override
public void onDevBundleReLoad() {
restartEngineInBackground();
}

@Override
public void onDevBundleLoadReady(File bundle)
{
mCoreBundleLoader = new HippyFileBundleLoader(bundle.getAbsolutePath());
((HippyFileBundleLoader)mCoreBundleLoader).setIsDebugMode(true);
restartEngineInBackground();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@
*/
public interface HippyBridge
{
static final String URI_SCHEME_ASSETS = "asset:";
static final String URI_SCHEME_FILE = "file:";

public void initJSBridge(String gobalConfig, NativeCallback callback, int groupId);

public boolean runScriptFromFile(String filePath, String scriptName, boolean canUseCodeCache, String codeCacheTag, NativeCallback callback);

public boolean runScriptFromAssets(String fileName, AssetManager assetManager, boolean canUseCodeCache, String codeCacheTag, NativeCallback callback);

public boolean runScriptFromUri(String uri, AssetManager assetManager, boolean canUseCodeCache, String codeCacheTag, NativeCallback callback);

public void destroy(NativeCallback callback);

public void callFunction(String action, String params, NativeCallback callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
*/
package com.tencent.mtt.hippy.bridge;

import android.util.Log;
import com.tencent.mtt.hippy.HippyEngineContext;
import com.tencent.mtt.hippy.devsupport.DevServerCallBack;
import com.tencent.mtt.hippy.devsupport.DevServerConfig;
import com.tencent.mtt.hippy.devsupport.DevSupportManager;
import com.tencent.mtt.hippy.utils.ContextHolder;
import com.tencent.mtt.hippy.utils.UIThreadUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Locale;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand All @@ -40,6 +50,7 @@ public class HippyBridgeImpl implements HippyBridge, DevRemoteDebugProxy.OnRecei
private static volatile ThreadPoolExecutor mCodeCacheThreadExecutor = null;
private static volatile int sBridgeNum = 0;
private static Object sBridgeSyncLock;
private static int TIMEOUT = 3000;

static
{
Expand All @@ -59,22 +70,24 @@ public class HippyBridgeImpl implements HippyBridge, DevRemoteDebugProxy.OnRecei
private DebugWebSocketClient mDebugWebSocketClient;
private String mDebugGobalConfig;
private NativeCallback mDebugInitJSFrameworkCallback;
private HippyEngineContext mContext;

public HippyBridgeImpl(Context context, BridgeCallback callback, boolean singleThreadMode,
boolean jsonBrige, boolean isDevModule, String debugServerHost)
public HippyBridgeImpl(HippyEngineContext engineContext, BridgeCallback callback, boolean singleThreadMode, boolean jsonBrige, boolean isDevModule, String debugServerHost)
{
this.mBridgeCallback = callback;
this.mSingleThreadMode = singleThreadMode;
this.mBridgeParamJson = jsonBrige;
this.mIsDevModule = isDevModule;
this.mDebugServerHost = debugServerHost;
this.mContext = engineContext;

synchronized (sBridgeSyncLock)
{
++sBridgeNum;

if (mCodeCacheRootDir == null)
{
Context context = mContext.getGlobalConfigs().getContext();
File hippyFile = FileUtils.getHippyFile(context);
if (hippyFile != null)
{
Expand Down Expand Up @@ -129,6 +142,8 @@ public void onSuccess(String response)
public void onFailure(final Throwable cause)
{
LogUtils.e("hippyCore", "js debug socket connect failed");

initJSEngine(groupId);
}
});
}
Expand Down Expand Up @@ -177,6 +192,7 @@ public boolean runScriptFromAssets(String fileName, AssetManager assetManager, b
{
return false;
}

if (!TextUtils.isEmpty(codeCacheTag) && !TextUtils.isEmpty(mCodeCacheRootDir))
{
LogUtils.e("HippyEngineMonitor", "runScriptFromAssets ======core====== " + codeCacheTag + ", canUseCodeCache == " + canUseCodeCache);
Expand All @@ -192,6 +208,29 @@ public boolean runScriptFromAssets(String fileName, AssetManager assetManager, b
}
}

@Override
public boolean runScriptFromUri(String uri, AssetManager assetManager, boolean canUseCodeCache, String codeCacheTag, NativeCallback callback)
{
if (!mInit)
{
return false;
}

if (!TextUtils.isEmpty(codeCacheTag) && !TextUtils.isEmpty(mCodeCacheRootDir))
{
LogUtils.e("HippyEngineMonitor", "runScriptFromAssets ======core====== " + codeCacheTag + ", canUseCodeCache == " + canUseCodeCache);
String codeCacheDir = mCodeCacheRootDir + codeCacheTag + File.separator;
File file = new File(codeCacheDir);
LogUtils.d("HippyEngineMonitor", "codeCacheDir file size : " + (file.listFiles() != null ? file.listFiles().length : 0));
return runScriptFromUri(uri, assetManager, canUseCodeCache, codeCacheDir, mV8RuntimeId, callback);
}
else
{
LogUtils.e("HippyEngineMonitor", "runScriptFromAssets codeCacheTag is null");
return runScriptFromUri(uri, assetManager, false, "" + codeCacheTag + File.separator, mV8RuntimeId, callback);
}
}

@Override
public void callFunction(String action, String params, NativeCallback callback)
{
Expand Down Expand Up @@ -266,6 +305,8 @@ public void destroy(NativeCallback callback)

public native boolean runScriptFromAssets(String fileName, AssetManager assetManager, boolean canUseCodeCache, String codeCacheDir, long V8RuntimId, NativeCallback callback);

public native boolean runScriptFromUri(String uri, AssetManager assetManager, boolean canUseCodeCache, String codeCacheDir, long V8RuntimId, NativeCallback callback);

public native void destroy(long V8RuntimId, boolean useLowMemoryMode, NativeCallback callback);

public native void callFunction(String action, byte[] params, int offset, int length, long V8RuntimId, NativeCallback callback);
Expand Down Expand Up @@ -302,6 +343,65 @@ public void InspectorChannel(byte[] params)
}
}

public byte[] fetchResourceWithUri(String uri) {
if (mContext == null || TextUtils.isEmpty(uri) || !uri.startsWith("http://")) {
return null;
}

final String resUrl = uri;
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final CountDownLatch countDownLatch = new CountDownLatch(1);
UIThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
DevSupportManager devManager = mContext.getDevSupportManager();
if (devManager == null || !devManager.supportDev()) {
countDownLatch.countDown();
return;
}

devManager.loadRemoteResource(resUrl, new DevServerCallBack() {
@Override
public void onDevBundleLoadReady(File bundle) {}

@Override
public void onDevBundleReLoad() {}

@Override
public void onDevBundleLoadReady(InputStream inputStream) {
try {
byte[] b = new byte[2048];
int size = 0;
while ((size = inputStream.read(b)) > 0) {
output.write(b, 0, size);
}
} catch (Exception e) {
LogUtils.e("hippy", "requireSubResource: " + e.getMessage());
} finally {
countDownLatch.countDown();
}

}

@Override
public void onInitDevError(Throwable e) {
LogUtils.e("hippy", "requireSubResource: " + e.getMessage());
countDownLatch.countDown();
}
});
}
});

try {
//countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
countDownLatch.await();
} catch (InterruptedException e) {
LogUtils.e("hippy", "requireSubResource: " + e.getMessage());
}

return output.toByteArray();
}

private HippyArray bytesToArgument(byte param[])
{
HippyArray hippyParam = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ public boolean handleMessage(Message msg)
final com.tencent.mtt.hippy.common.Callback<Boolean> callback = (com.tencent.mtt.hippy.common.Callback<Boolean>) msg.obj;
try
{
mHippyBridge = new HippyBridgeImpl(mContext.getGlobalConfigs().getContext(), HippyBridgeManagerImpl.this,
mHippyBridge = new HippyBridgeImpl(mContext, HippyBridgeManagerImpl.this,
mBridgeType == BRIDGE_TYPE_SINGLE_THREAD, !mEnableHippyBuffer, this.mIsDevModule, this.mDebugServerHost);

mHippyBridge.initJSBridge(getGlobalConfigs(), new NativeCallback(mHandler) {
@Override
public void Call(long value, Message msg, String action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.bridge.bundleloader;

import static com.tencent.mtt.hippy.bridge.HippyBridge.URI_SCHEME_ASSETS;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;
Expand All @@ -29,7 +31,6 @@
*/
public class HippyAssetBundleLoader implements HippyBundleLoader
{
private static final String ASSETS_STR = "assets://";
private Context mContext;

private String mAssetPath;
Expand Down Expand Up @@ -60,21 +61,27 @@ public void setCodeCache(boolean canUseCodeCache, String codeCacheTag)
@Override
public boolean load(HippyBridge bridge, NativeCallback callback)
{
if (TextUtils.isEmpty(mAssetPath))
{
if (TextUtils.isEmpty(mAssetPath)) {
return false;
}

AssetManager assetManager = mContext.getAssets();
return bridge.runScriptFromAssets(mAssetPath, assetManager,mCanUseCodeCache,mCodeCacheTag, callback);
return bridge.runScriptFromUri(getPath(), assetManager, mCanUseCodeCache, mCodeCacheTag, callback);
//return bridge.runScriptFromAssets(mAssetPath, assetManager,mCanUseCodeCache,mCodeCacheTag, callback);
}

@Override
public String getPath()
{
if (mAssetPath != null && !mAssetPath.startsWith(ASSETS_STR))
return ASSETS_STR + mAssetPath;
else
if (mAssetPath != null && !mAssetPath.startsWith(URI_SCHEME_ASSETS)) {
if (mAssetPath.startsWith("/")) {
return URI_SCHEME_ASSETS + mAssetPath;
} else {
return URI_SCHEME_ASSETS + "/" + mAssetPath;
}
} else {
return mAssetPath;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.tencent.mtt.hippy.bridge.bundleloader;

import static com.tencent.mtt.hippy.bridge.HippyBridge.URI_SCHEME_FILE;

import android.text.TextUtils;
import com.tencent.mtt.hippy.bridge.HippyBridge;
import com.tencent.mtt.hippy.bridge.NativeCallback;
Expand All @@ -26,10 +28,10 @@
*/
public class HippyFileBundleLoader implements HippyBundleLoader
{
private static final String FILE_STR = "file://";

String mFilePath;

boolean mIsDebugMode = false;

private boolean mCanUseCodeCache;

private String mCodeCacheTag;
Expand All @@ -52,21 +54,27 @@ public void setCodeCache(boolean canUseCodeCache, String codeCacheTag)
this.mCodeCacheTag = codeCacheTag;
}

public void setIsDebugMode(boolean debugMode) {
mIsDebugMode = debugMode;
}

@Override
public boolean load(HippyBridge bridge, NativeCallback callback)
{
if (TextUtils.isEmpty(mFilePath))
{
if (TextUtils.isEmpty(mFilePath)) {
return false;
}
return bridge.runScriptFromFile(mFilePath, mFilePath,mCanUseCodeCache,mCodeCacheTag, callback);

String uri = getPath();
return bridge.runScriptFromUri(uri, null, mCanUseCodeCache, mCodeCacheTag, callback);
//return bridge.runScriptFromFile(mFilePath, mFilePath,mCanUseCodeCache,mCodeCacheTag, callback);
}

@Override
public String getPath()
{
if (mFilePath != null && !mFilePath.startsWith(FILE_STR))
return FILE_STR + mFilePath;
if (mFilePath != null && !mFilePath.startsWith(URI_SCHEME_FILE))
return URI_SCHEME_FILE + mFilePath;
else
return mFilePath;
}
Expand Down

0 comments on commit 67b0b3f

Please sign in to comment.