use of tech.easily.easybridge.lib.handler.BridgeHandler in project EasyBridge by easilycoder.
the class EasyBridge method enqueue.
/**
* js invoke java async
*
* @param handlerName name
* @param parameters values from js
* @param callbackId the unique id to invoke js callback function
*/
@JavascriptInterface
public void enqueue(String handlerName, String currentPageUrl, final String parameters, String callbackId) {
final Gson gson = new GsonBuilder().create();
final ResultCallBack callBack = new ResultCallBack(callbackId) {
@Override
public void onResult(Object result) {
dispatchResult(getCallbackId(), gson.toJson(CallBackMessage.generateSuccessMessage(result)));
}
};
if (TextUtils.isEmpty(handlerName)) {
callBack.onResult(CallBackMessage.generateErrorMessage(CallBackMessage.CODE_INVALID_HANDLER, "the handlerName is not invalid"));
return;
}
final BridgeHandler handler = findTargetHandler(handlerName);
if (handler == null) {
callBack.onResult(CallBackMessage.generateErrorMessage(CallBackMessage.CODE_NO_HANDLER, "handler with name " + handlerName + " is not registered in Java code"));
return;
}
// global security check
if (!checkGlobalSecurity(currentPageUrl, parameters)) {
callBack.onResult(CallBackMessage.generateErrorMessage(CODE_SECURITY_FORBIDDEN, "handler with name " + handlerName + " is not allowed to invoke in page:" + currentPageUrl + " by the global Security Checker"));
return;
}
// handler security check
if (handler.securityPolicyChecker() != null && !handler.securityPolicyChecker().check(currentPageUrl, parameters)) {
callBack.onResult(CallBackMessage.generateErrorMessage(CODE_SECURITY_FORBIDDEN, "handler with name " + handlerName + " is not allowed to invoke in page:" + currentPageUrl));
return;
}
// invoke the handler in main thread
callBackHandler.post(new Runnable() {
@Override
public void run() {
handler.onCall(parameters, callBack);
}
});
}
Aggregations