Security and other

JSBridge class manages exposure to front-end method, the method should call the front in order to use this class to register. Register is implemented from the Map to find whether there is a key, all of the methods corresponding to the class does not exist, the reflection achieved, the specific method is defined in BridgeImpl, the method includes three parameters were WebView, JSONObject, CallBack. If the conditions are met, then the method satisfies all the conditions put into map.

the Map static Private <String, the HashMap <String, Method, >> = new new exposedMethods the HashMap <> ();
public static void Register (? exposedName String, Class <iBridge the extends> clazz) {
IF (exposedMethods.containsKey (exposedName)!) {
{the try
exposedMethods.put (exposedName, getAllMethod (clazz));
} the catch (Exception E) {
e.printStackTrace (http://www.amjmh.com);
}
}
}

copy the code
JSBridge class callJava approach is js URL parsing pass over, just to find out from the Map established in accordance with the class name to be called, calls the specific method method name, and pass into the resolved three parameters.

public static String callJava(WebView webView, String uriString) {
String methodName = "";
String className = "";
String param = "{}";
String port = "";
if (!TextUtils.isEmpty(uriString) && uriString.startsWith("JSBridge")) {
Uri uri = Uri.parse(uriString);
className = uri.getHost();
param = uri.getQuery();
port = uri.getPort() + "";
String path = uri.getPath();
if (!TextUtils.isEmpty(path)) {
methodName = path.replace("/", "");
}
}


if (exposedMethods.containsKey(className)) {
HashMap<String, Method> methodHashMap = exposedMethods.get(className);

IF (!! = null && methodHashMap methodHashMap.size () = 0 && methodHashMap.containsKey (methodName)) {
Method, Method = methodHashMap.get (methodName);
IF (! Method = null) {
the try {
Method.invoke (null, the webView, the JSONObject new new (param), the callback new new (the webView, Port));
} the catch (Exception E) {
e.printStackTrace ();
}
}
}
}
return null;
}

copy the code
callBack class is used js callbacks callbacks method of the corresponding Java classes. Java layer processing returns good result is achieved by CallBack class. Callback parameters passed in this class is the JSONObject (return result), WebView and port, port should be passed over the corresponding js port.

private static Handler mHandler = new Handler(Looper.getMainLooper());
private static final String CALLBACK_JS_FORMAT = "javascript:JSBridge.onFinish('%s', %s);";
private String mPort;
private WeakReference<WebView> mWebViewRef;

public Callback(WebView view, String port) {
mWebViewRef = new WeakReference<>(view);
mPort = port;
}
public void apply(JSONObject jsonObject) {
final String execJs = String.format(CALLBACK_JS_FORMAT, mPort, String.valueOf(jsonObject));
if (mWebViewRef != null && mWebViewRef.get() != null) {
mHandler.post(new Runnable() {
@Override
public void run() {
mWebViewRef.get().loadUrl(execJs);
}
});
}
}

Guess you like

Origin www.cnblogs.com/ly570/p/11291184.html