feat:Android and iOS monitor use DoricContext as parameter

This commit is contained in:
pengfei.zhou 2020-03-07 10:38:42 +08:00 committed by osborn
parent 5a956950bd
commit 3b0a245129
19 changed files with 73 additions and 69 deletions

View File

@ -59,13 +59,33 @@ public class DoricNativeDriver implements IDoricDriver {
@Override
public AsyncResult<JSDecoder> invokeContextEntityMethod(final String contextId, final String method, final Object... args) {
final AsyncResult<JSDecoder> asyncResult = new AsyncResult<>();
final Object[] nArgs = new Object[args.length + 2];
nArgs[0] = contextId;
nArgs[1] = method;
if (args.length > 0) {
System.arraycopy(args, 0, nArgs, 2, args.length);
}
return invokeDoricMethod(DoricConstant.DORIC_CONTEXT_INVOKE, nArgs);
invokeDoricMethod(DoricConstant.DORIC_CONTEXT_INVOKE, nArgs).setCallback(new AsyncResult.Callback<JSDecoder>() {
@Override
public void onResult(JSDecoder result) {
asyncResult.setResult(result);
}
@Override
public void onError(Throwable t) {
asyncResult.setError(t);
getRegistry().onException(
DoricContextManager.getContext(contextId),
t instanceof Exception ? (Exception) t : new RuntimeException(t));
}
@Override
public void onFinish() {
}
});
return asyncResult;
}
@Override
@ -91,14 +111,6 @@ public class DoricNativeDriver implements IDoricDriver {
}
}
private String sourceWithContextId(String contextId) {
DoricContext context = DoricContextManager.getContext(contextId);
if (context == null) {
return "Unknown:" + contextId;
}
return context.getSource();
}
@Override
public AsyncResult<Boolean> createContext(final String contextId, final String script, final String source) {
return AsyncCall.ensureRunInHandler(mJSHandler, new Callable<Boolean>() {
@ -108,7 +120,7 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.prepareContext(contextId, script, source);
return true;
} catch (Exception e) {
doricJSEngine.getRegistry().onException(sourceWithContextId(contextId), e);
doricJSEngine.getRegistry().onException(DoricContextManager.getContext(contextId), e);
doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("createContext %s error is %s", source, e.getLocalizedMessage()));
return false;
}
@ -125,7 +137,7 @@ public class DoricNativeDriver implements IDoricDriver {
doricJSEngine.destroyContext(contextId);
return true;
} catch (Exception e) {
doricJSEngine.getRegistry().onException(sourceWithContextId(contextId), e);
doricJSEngine.getRegistry().onException(DoricContextManager.getContext(contextId), e);
doricJSEngine.getRegistry().onLog(Log.ERROR, String.format("destroyContext %s error is %s", contextId, e.getLocalizedMessage()));
return false;
}

View File

@ -162,9 +162,9 @@ public class DoricRegistry {
this.monitors.add(monitor);
}
public void onException(String source, Exception e) {
public void onException(DoricContext context, Exception e) {
for (IDoricMonitor monitor : this.monitors) {
monitor.onException(source, e);
monitor.onException(context, e);
}
}

View File

@ -24,11 +24,11 @@ public interface IDoricMonitor {
/**
* Called when native or js exception occurred in doric
*
* @param source Which source file
* @param context Which context
* @param e exception which is thrown within doric sdk
* @see com.github.pengfeizhou.jscore.JSRuntimeException
*/
void onException(String source, Exception e);
void onException(DoricContext context, Exception e);
/**
* @param type The priority/type of this log message.

View File

@ -25,6 +25,8 @@ import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JavaFunction;
@ -132,7 +134,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
break;
}
} catch (Exception e) {
mDoricRegistry.onException("Log", e);
mDoricRegistry.onException(null, e);
}
return null;
}
@ -156,7 +158,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
mDoricJSE.loadJS(packageModuleScript(name, content), "Module://" + name);
return new JavaValue(true);
} catch (Exception e) {
mDoricRegistry.onException("Require", e);
mDoricRegistry.onException(null, e);
return new JavaValue(false);
}
}
@ -170,7 +172,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
args[1].number().longValue(),
args[2].bool());
} catch (Exception e) {
mDoricRegistry.onException("Timer", e);
mDoricRegistry.onException(null, e);
}
return null;
}
@ -181,7 +183,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
try {
mTimerExtension.clearTimer(args[0].number().longValue());
} catch (Exception e) {
mDoricRegistry.onException("Timer", e);
mDoricRegistry.onException(null, e);
}
return null;
}
@ -198,7 +200,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
JSDecoder jsDecoder = args[4];
return mDoricBridgeExtension.callNative(contextId, module, method, callbackId, jsDecoder);
} catch (Exception e) {
mDoricRegistry.onException("Bridge", e);
mDoricRegistry.onException(null, e);
}
return null;
}
@ -212,7 +214,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
String libJS = DoricUtils.readAssetFile(DoricConstant.DORIC_BUNDLE_LIB);
mDoricJSE.loadJS(packageModuleScript(libName, libJS), "Module://" + libName);
} catch (Exception e) {
mDoricRegistry.onException("Init Environment", e);
mDoricRegistry.onException(null, e);
}
}
@ -263,7 +265,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
try {
invokeDoricMethod(DoricConstant.DORIC_TIMER_CALLBACK, timerId);
} catch (Exception e) {
mDoricRegistry.onException("Timer", e);
mDoricRegistry.onException(null, e);
mDoricRegistry.onLog(
Log.ERROR,
String.format("Timer Callback error:%s", e.getLocalizedMessage()));
@ -275,8 +277,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
}
@Override
public void onException(String source, Exception e) {
Log.e(DoricJSEngine.class.getSimpleName(), "In source file: " + source);
public void onException(@Nullable DoricContext context, Exception e) {
Log.e(DoricJSEngine.class.getSimpleName(), "In source file: " + (context != null ? context.getSource() : "Unknown"));
e.printStackTrace();
}

View File

@ -98,7 +98,7 @@ public class DoricBridgeExtension {
@Override
public void onError(Throwable t) {
context.getDriver().getRegistry().onException(
context.getSource(),
context,
t instanceof Exception ? (Exception) t : new RuntimeException(t));
}
@ -120,7 +120,7 @@ public class DoricBridgeExtension {
try {
return DoricUtils.toJavaObject(clz, jsDecoder);
} catch (Exception e) {
context.getDriver().getRegistry().onException(context.getSource(), e);
context.getDriver().getRegistry().onException(context, e);
context.getDriver().getRegistry().onLog(
Log.ERROR,
String.format("createParam error:%s", e.getLocalizedMessage()));

View File

@ -55,7 +55,7 @@ public class DoricPromise {
@Override
public void onError(Throwable t) {
context.getDriver().getRegistry().onException(context.getSource(), t instanceof Exception ? (Exception) t : new RuntimeException(t));
context.getDriver().getRegistry().onException(context, t instanceof Exception ? (Exception) t : new RuntimeException(t));
}
@Override
@ -82,7 +82,7 @@ public class DoricPromise {
@Override
public void onError(Throwable t) {
context.getDriver().getRegistry().onException(context.getSource(), t instanceof Exception ? (Exception) t : new RuntimeException(t));
context.getDriver().getRegistry().onException(context, t instanceof Exception ? (Exception) t : new RuntimeException(t));
}
@Override

View File

@ -79,7 +79,7 @@ public class ShaderPlugin extends DoricJavaPlugin {
@Override
public void onError(Throwable t) {
getDoricContext().getDriver().getRegistry().onException(
getDoricContext().getSource(),
getDoricContext(),
t instanceof Exception ? (Exception) t : new RuntimeException(t));
getDoricContext().getDriver().getRegistry().onLog(
Log.ERROR,
@ -92,7 +92,7 @@ public class ShaderPlugin extends DoricJavaPlugin {
}
});
} catch (Exception e) {
getDoricContext().getDriver().getRegistry().onException(getDoricContext().getSource(), e);
getDoricContext().getDriver().getRegistry().onException(getDoricContext(), e);
getDoricContext().getDriver().getRegistry().onLog(
Log.ERROR,
String.format("Shader.render:error%s", e.getLocalizedMessage())
@ -173,7 +173,7 @@ public class ShaderPlugin extends DoricJavaPlugin {
}
}
} catch (ArchiveException e) {
getDoricContext().getDriver().getRegistry().onException(getDoricContext().getSource(), e);
getDoricContext().getDriver().getRegistry().onException(getDoricContext(), e);
}
return new JavaValue(true);
}

View File

@ -156,7 +156,7 @@ public class ImageNode extends ViewNode<ImageView> {
if (errorDrawable != null) {
requestBuilder = requestBuilder.apply(RequestOptions.errorOf(errorDrawable));
}
} catch (Exception e) {
} catch (Throwable e) {
e.printStackTrace();
DoricLog.e("ImageNode blend error, please check the glide version");
}

View File

@ -52,10 +52,6 @@ - (DoricRegistry *)registry {
return ret;
}
- (NSString *)aliasWithContextId:(NSString *)contextId {
return [[DoricContextManager instance] getContext:contextId].source;
}
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
@ -104,7 +100,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -127,7 +123,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -144,7 +140,7 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:source];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -161,7 +157,7 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId {
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;

View File

@ -33,14 +33,14 @@ - (instancetype)initWithScript:(NSString *)script source:(NSString *)source extr
if (self = [super init]) {
_driver = [DoricNativeDriver instance];
_pluginInstanceMap = [NSMutableDictionary new];
[[DoricContextManager instance] createContext:self script:script source:source];
_headNodes = [NSMutableDictionary new];
DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self];
_rootNode = rootNode;
_script = script;
_source = source;
_initialParams = [@{@"width": @(0), @"height": @(0)} mutableCopy];
_extra = extra;
[[DoricContextManager instance] createContext:self script:script source:source];
_headNodes = [NSMutableDictionary new];
DoricRootNode *rootNode = [[DoricRootNode alloc] initWithContext:self];
_rootNode = rootNode;
[self callEntity:DORIC_ENTITY_CREATE, nil];
}
return self;

View File

@ -21,7 +21,6 @@
//
#import "DoricContextManager.h"
#import "DoricContext.h"
@interface DoricContextManager ()
@ -52,11 +51,11 @@ + (instancetype)instance {
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source {
context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter];
[context.driver createContext:context.contextId script:script source:source];
dispatch_sync(self.mapQueue, ^() {
NSValue *value = [NSValue valueWithNonretainedObject:context];
self.doricContextMap[context.contextId] = value;
});
[context.driver createContext:context.contextId script:script source:source];
}
- (DoricContext *)getContext:(NSString *)contextId {

View File

@ -42,6 +42,4 @@
- (void)ensureSyncInMainQueue:(dispatch_block_t)block;
- (NSString *)aliasWithContextId:(NSString *)contextId;
@end

View File

@ -9,6 +9,7 @@ typedef NS_ENUM(NSInteger, DoricLogType) {
DoricLogTypeWarning = 1,
DoricLogTypeError = 2,
};
@class DoricContext;
@protocol DoricMonitorProtocol <NSObject>
/**
@ -17,7 +18,7 @@ typedef NS_ENUM(NSInteger, DoricLogType) {
* @param source Which source file
* @param e exception which is thrown within doric sdk
*/
- (void)onException:(NSException *)exception source:(NSString *)source;
- (void)onException:(NSException *)exception inContext:(DoricContext *)context;
/**
* @param type The priority/type of this log message.

View File

@ -61,10 +61,6 @@ + (instancetype)instance {
return ret;
}
- (NSString *)aliasWithContextId:(NSString *)contextId {
return [[DoricContextManager instance] getContext:contextId].source;
}
- (DoricAsyncResult<JSValue *> *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
DoricAsyncResult *ret = [[DoricAsyncResult alloc] init];
NSMutableArray *array = [[NSMutableArray alloc] init];
@ -113,7 +109,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -136,7 +132,7 @@ - (DoricAsyncResult *)invokeContextEntity:(NSString *)contextId method:(NSString
[ret setupResult:jsValue];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -153,7 +149,7 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:source];
[self.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;
@ -170,7 +166,7 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId {
[ret setupResult:@YES];
} @catch (NSException *exception) {
[ret setupError:exception];
[self.jsExecutor.registry onException:exception source:[self aliasWithContextId:contextId]];
[self.jsExecutor.registry onException:exception inContext:[[DoricContextManager instance] getContext:contextId]];
}
});
return ret;

View File

@ -80,8 +80,8 @@ @interface DoricDefaultMonitor : NSObject <DoricMonitorProtocol>
@end
@implementation DoricDefaultMonitor
- (void)onException:(NSException *)exception source:(NSString *)source {
DoricLog(@"DefaultMonitor - source: %@- onException - %@", source, exception.reason);
- (void)onException:(NSException *)exception inContext:(DoricContext *)context {
DoricLog(@"DefaultMonitor - source: %@- onException - %@", context.source, exception.reason);
}
- (void)onLog:(DoricLogType)type message:(NSString *)message {
@ -187,9 +187,9 @@ - (void)registerMonitor:(id <DoricMonitorProtocol>)monitor {
[self.monitors addObject:monitor];
}
- (void)onException:(NSException *)exception source:(NSString *)source {
- (void)onException:(NSException *)exception inContext:(DoricContext *)context {
for (id <DoricMonitorProtocol> monitor in self.monitors) {
[monitor onException:exception source:source];
[monitor onException:exception inContext:context];
}
}

View File

@ -140,7 +140,7 @@ - (void)initDoricEnvironment {
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
} @catch (NSException *exception) {
[self.registry onException:exception source:@"InitEnvironment"];
[self.registry onException:exception inContext:nil];
}
}
@ -207,7 +207,7 @@ - (void)callbackTimer:(NSTimer *)timer {
@try {
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
} @catch (NSException *exception) {
[self.registry onException:exception source:@"Timer"];
[self.registry onException:exception inContext:nil];
[self.registry onLog:DoricLogTypeError
message:[NSString stringWithFormat:@"Timer Callback error:%@", exception.reason]];
}

View File

@ -87,7 +87,7 @@ - (id)findClass:(Class)clz target:(id)target context:(DoricContext *)context met
[invocation invoke];
} @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason);
[context.driver.registry onException:exception source:context.source];
[context.driver.registry onException:exception inContext:context];
}
};

View File

@ -44,7 +44,7 @@ - (void)resolve:(id)result {
setExceptionCallback:^(NSException *e) {
[self.context.driver.registry
onException:e
source:self.context.source];
inContext:self.context];
}];
}
@ -53,7 +53,7 @@ - (void)reject:(id)result {
setExceptionCallback:^(NSException *e) {
[self.context.driver.registry
onException:e
source:self.context.source];
inContext:self.context];
}];
}
@end

View File

@ -108,7 +108,7 @@ - (id)findClass:(Class)clz target:(id)target method:(NSString *)name promise:(Do
[invocation invoke];
} @catch (NSException *exception) {
DoricLog(@"CallNative Error:%@", exception.reason);
[self.doricContext.driver.registry onException:exception source:self.doricContext.source];
[self.doricContext.driver.registry onException:exception inContext:self.doricContext];
}
};
dispatch_async(dispatch_get_main_queue(), ^{