Skip to content

Commit

Permalink
修复自定义注入命回调Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Nov 7, 2014
1 parent 69ceeee commit 004006e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Safe Java-JS WebView Bridge
<dependency>
<groupId>cn.pedant.safewebviewbridge</groupId>
<artifactId>library</artifactId>
<version>1.2</version>
<version>1.3</version>
<type>aar</type>
</dependency>

Expand All @@ -26,7 +26,7 @@ Safe Java-JS WebView Bridge
}

dependencies {
compile 'cn.pedant.safewebviewbridge:library:1.2'
compile 'cn.pedant.safewebviewbridge:library:1.3'
}

## Sample
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_NAME=1.2
VERSION_CODE=3
VERSION_NAME=1.3
VERSION_CODE=4
GROUP=cn.pedant.safewebviewbridge

POM_DESCRIPTION=Provide a safe and reliable scheme for Java and JavaScript in Android WebView.
Expand Down
26 changes: 19 additions & 7 deletions library/src/main/java/cn/pedant/SafeWebViewBridge/JsCallJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ public class JsCallJava {
private final static String TAG = "JsCallJava";
private final static String RETURN_RESULT_FORMAT = "{\"code\": %d, \"result\": %s}";
private HashMap<String, Method> mMethodsMap;
private String mInjectedName;
private String mPreloadInterfaceJS;
private Gson mGson;

public JsCallJava (String injectedName, Class injectedCls) {
try {
if (TextUtils.isEmpty(injectedName)) {
throw new Exception("injected name can not be null");
}
mInjectedName = injectedName;
mMethodsMap = new HashMap<String, Method>();
//获取自身声明的所有方法(包括public private protected), getMethods会获得所有继承与非继承的方法
Method[] methods = injectedCls.getDeclaredMethods();
StringBuilder sb = new StringBuilder("javascript:(function(b){console.log(\"HostApp initialization begin\");var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};");

StringBuilder sb = new StringBuilder("javascript:(function(b){console.log(\"");
sb.append(mInjectedName);
sb.append(" initialization begin\");var a={queue:[],callback:function(){var d=Array.prototype.slice.call(arguments,0);var c=d.shift();var e=d.shift();this.queue[c].apply(this,d);if(!e){delete this.queue[c]}}};");
for (Method method : methods) {
String sign;
if (method.getModifiers() != (Modifier.PUBLIC | Modifier.STATIC) || (sign = genJavaMethodSign(method)) == null) {
Expand All @@ -36,9 +42,15 @@ public JsCallJava (String injectedName, Class injectedCls) {
sb.append(String.format("a.%s=", method.getName()));
}

sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"HostApp call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"HostApp call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b.");
sb.append(injectedName);
sb.append("=a;console.log(\"HostApp initialization end\")})(window);");
sb.append("function(){var f=Array.prototype.slice.call(arguments,0);if(f.length<1){throw\"");
sb.append(mInjectedName);
sb.append(" call error, message:miss method name\"}var e=[];for(var h=1;h<f.length;h++){var c=f[h];var j=typeof c;e[e.length]=j;if(j==\"function\"){var d=a.queue.length;a.queue[d]=c;f[h]=d}}var g=JSON.parse(prompt(JSON.stringify({method:f.shift(),types:e,args:f})));if(g.code!=200){throw\"");
sb.append(mInjectedName);
sb.append(" call error, code:\"+g.code+\", message:\"+g.result}return g.result};Object.getOwnPropertyNames(a).forEach(function(d){var c=a[d];if(typeof c===\"function\"&&d!==\"callback\"){a[d]=function(){return c.apply(a,[d].concat(Array.prototype.slice.call(arguments,0)))}}});b.");
sb.append(mInjectedName);
sb.append("=a;console.log(\"");
sb.append(mInjectedName);
sb.append(" initialization end\")})(window);");
mPreloadInterfaceJS = sb.toString();
} catch(Exception e){
Log.e(TAG, "init js error:" + e.getMessage());
Expand Down Expand Up @@ -110,7 +122,7 @@ public String call(WebView webView, String jsonStr) {
values[k + 1] = argsVals.isNull(k) ? null : argsVals.getJSONObject(k);
} else if ("function".equals(currType)) {
sign += "_F";
values[k + 1] = new JsCallback(webView, argsVals.getInt(k));
values[k + 1] = new JsCallback(webView, mInjectedName, argsVals.getInt(k));
} else {
sign += "_P";
}
Expand Down Expand Up @@ -176,7 +188,7 @@ private String getReturn (String reqJson, int stateCode, Object result) {
insertRes = String.valueOf(result);
}
String resStr = String.format(RETURN_RESULT_FORMAT, stateCode, insertRes);
Log.d(TAG, "HostApp call json: " + reqJson + " result:" + resStr);
Log.d(TAG, mInjectedName + " call json: " + reqJson + " result:" + resStr);
return resStr;
}
}
22 changes: 12 additions & 10 deletions library/src/main/java/cn/pedant/SafeWebViewBridge/JsCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
import android.util.Log;

public class JsCallback {
private static final String CALLBACK_JS_FORMAT = "javascript:HostApp.callback(%d, %d %s);";
private int index;
private WebView webView;
private int isPermanent;
private static final String CALLBACK_JS_FORMAT = "javascript:%s.callback(%d, %d %s);";
private int mIndex;
private WebView mWebView;
private int mIsPermanent;
private String mInjectedName;

public JsCallback (WebView view, int index) {
this.index = index;
this.webView = view;
public JsCallback (WebView view, String injectedName, int index) {
mWebView = view;
mInjectedName = injectedName;
mIndex = index;
}

public void apply (Object... args) {
Expand All @@ -35,12 +37,12 @@ public void apply (Object... args) {
sb.append("\"");
}
}
String execJs = String.format(CALLBACK_JS_FORMAT, index, isPermanent, sb.toString());
String execJs = String.format(CALLBACK_JS_FORMAT, mInjectedName, mIndex, mIsPermanent, sb.toString());
Log.d("JsCallBack", execJs);
webView.loadUrl(execJs);
mWebView.loadUrl(execJs);
}

public void setPermanent (boolean value) {
isPermanent = value ? 1 : 0;
mIsPermanent = value ? 1 : 0;
}
}
2 changes: 1 addition & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ android {
}

dependencies {
//compile 'cn.pedant.safewebviewbridge:library:1.2'
//compile 'cn.pedant.safewebviewbridge:library:1.3'
compile project(':library')
}

0 comments on commit 004006e

Please sign in to comment.