Skip to content

Commit

Permalink
fix: inject webview
Browse files Browse the repository at this point in the history
  • Loading branch information
styluo committed Nov 10, 2021
1 parent f552681 commit 95eb1df
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 24 deletions.
10 changes: 10 additions & 0 deletions growingio-autotracker-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ android {
lintOptions {
abortOnError false
}

defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
GRADLE_PLUGIN_DIR : ("$rootDir/growingio-autotracker-gradle-plugin".toString())
]
}
}
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public static void webkitWebViewLoadData(WebView webView, String data, String mi
bridgeForWebView(webView);
}

@Before(clazz = WebView.class, method = "loadDataWithBaseURL", parameterTypes = {String.class, String.class, String.class, String.class, String.class})
public static void webkitWebViewLoadDataWithBaseURL(WebView webView, String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
Logger.d(TAG, "webkitWebViewLoadDataWithBaseURL: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = WebView.class, method = "postUrl", parameterTypes = {String.class, byte[].class})
public static void webkitWebViewPostUrl(WebView webView, String url, byte[] postData) {
Logger.d(TAG, "webkitWebViewPostUrl: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.tencent.smtt.sdk.WebView.class, method = "loadUrl", parameterTypes = {String.class})
public static void x5WebViewLoadUrl(com.tencent.smtt.sdk.WebView webView, String url) {
Logger.d(TAG, "x5WebViewLoadUrl: webView = " + webView.getClass().getName() + ", url = " + url);
Expand All @@ -83,6 +95,18 @@ public static void x5WebViewLoadData(com.tencent.smtt.sdk.WebView webView, Strin
bridgeForWebView(webView);
}

@Before(clazz = com.tencent.smtt.sdk.WebView.class, method = "loadDataWithBaseURL", parameterTypes = {String.class, String.class, String.class, String.class, String.class})
public static void x5WebViewLoadDataWithBaseURL(com.tencent.smtt.sdk.WebView webView, String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
Logger.d(TAG, "x5WebViewLoadDataWithBaseURL: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.tencent.smtt.sdk.WebView.class, method = "postUrl", parameterTypes = {String.class, byte[].class})
public static void x5WebViewPostUrl(com.tencent.smtt.sdk.WebView webView, String url, byte[] postData) {
Logger.d(TAG, "x5WebViewPostUrl: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.uc.webview.export.WebView.class, method = "loadUrl", parameterTypes = {String.class})
public static void ucWebViewLoadUrl(com.uc.webview.export.WebView webView, String url) {
Logger.d(TAG, "ucWebViewLoadUrl: webView = " + webView.getClass().getName() + ", url = " + url);
Expand All @@ -101,21 +125,15 @@ public static void ucWebViewLoadData(com.uc.webview.export.WebView webView, Stri
bridgeForWebView(webView);
}

@Before(clazz = WebView.class, method = "loadDataWithBaseURL", parameterTypes = {String.class, String.class, String.class, String.class, String.class})
public static void webkitWebViewLoadDataWithBaseURL(WebView webView, String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
Logger.d(TAG, "webkitWebViewLoadDataWithBaseURL: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.tencent.smtt.sdk.WebView.class, method = "loadDataWithBaseURL", parameterTypes = {String.class, String.class, String.class, String.class, String.class})
public static void x5WebViewLoadDataWithBaseURL(com.tencent.smtt.sdk.WebView webView, String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
Logger.d(TAG, "x5WebViewLoadDataWithBaseURL: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.uc.webview.export.WebView.class, method = "loadDataWithBaseURL", parameterTypes = {String.class, String.class, String.class, String.class, String.class})
public static void ucWebViewLoadDataWithBaseURL(com.uc.webview.export.WebView webView, String baseUrl, String data, String mimeType, String encoding, String historyUrl) {
Logger.d(TAG, "ucWebViewLoadDataWithBaseURL: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}

@Before(clazz = com.uc.webview.export.WebView.class, method = "postUrl", parameterTypes = {String.class, byte[].class})
public static void ucWebViewPostUrl(com.uc.webview.export.WebView webView, String url, byte[] postData) {
Logger.d(TAG, "ucWebViewPostUrl: webView = " + webView.getClass().getName());
bridgeForWebView(webView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,25 @@ public void setup() {
public void webInjectTest() {
RobolectricActivity activity = Robolectric.buildActivity(RobolectricActivity.class).create().get();
WebView webView = new WebView(activity);
WebViewInjector.webkitWebViewLoadData(webView, "<html></html>", "text", "UTF8");
WebViewInjector.webkitWebViewLoadUrl(webView, "https://www.baidu.com/");
WebViewInjector.webkitWebViewLoadUrl(webView, "https://www.baidu.com/", new HashMap<>());
WebViewInjector.webkitWebViewLoadData(webView, "<html></html>", "text", "UTF8");
WebViewInjector.webkitWebViewLoadDataWithBaseURL(webView, "https://www.baidu.com/", "<p>", "text", "UTF8", "https://www/growingio.com/");
WebViewInjector.webkitWebViewPostUrl(webView, "https://www.baidu.com/", null);

com.uc.webview.export.WebView ucWebView = new com.uc.webview.export.WebView(activity);
WebViewInjector.ucWebViewLoadData(ucWebView, "<html></html>", "text", "UTF8");
WebViewInjector.ucWebViewLoadUrl(ucWebView, "https://www.baidu.com/");
WebViewInjector.ucWebViewLoadUrl(ucWebView, "https://www.baidu.com/", new HashMap<>());
WebViewInjector.ucWebViewLoadData(ucWebView, "<html></html>", "text", "UTF8");
WebViewInjector.ucWebViewLoadDataWithBaseURL(ucWebView, "https://www.baidu.com/", "<p>", "text", "UTF8", "https://www/growingio.com/");
WebViewInjector.ucWebViewPostUrl(ucWebView, "https://www.baidu.com/", null);

com.tencent.smtt.sdk.WebView x5WebView = new com.tencent.smtt.sdk.WebView(activity);
WebViewInjector.x5WebViewLoadData(x5WebView, "<html></html>", "text", "UTF8");
WebViewInjector.x5WebViewLoadUrl(x5WebView, "https://www.baidu.com/");
WebViewInjector.x5WebViewLoadUrl(x5WebView, "https://www.baidu.com/", new HashMap<>());
WebViewInjector.x5WebViewLoadData(x5WebView, "<html></html>", "text", "UTF8");
WebViewInjector.x5WebViewLoadDataWithBaseURL(x5WebView, "https://www.baidu.com/", "<p>", "text", "UTF8", "https://www/growingio.com/");
WebViewInjector.x5WebViewPostUrl(x5WebView, "https://www.baidu.com/", null);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ public class HookClassesConfig {
putAroundHookMethod("android/webkit/WebView", "loadUrl", "(Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewLoadUrl", "(Landroid/webkit/WebView;Ljava/lang/String;)V", false);
putAroundHookMethod("android/webkit/WebView", "loadUrl", "(Ljava/lang/String;Ljava/util/Map;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewLoadUrl", "(Landroid/webkit/WebView;Ljava/lang/String;Ljava/util/Map;)V", false);
putAroundHookMethod("android/webkit/WebView", "loadData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewLoadData", "(Landroid/webkit/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("android/webkit/WebView", "loadDataWithBaseURL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewLoadDataWithBaseURL", "(Landroid/webkit/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("android/webkit/WebView", "postUrl", "(Ljava/lang/String;[B)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewPostUrl", "(Landroid/webkit/WebView;Ljava/lang/String;[B)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "loadUrl", "(Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewLoadUrl", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "loadUrl", "(Ljava/lang/String;Ljava/util/Map;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewLoadUrl", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;Ljava/util/Map;)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "loadData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewLoadData", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "loadDataWithBaseURL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewLoadDataWithBaseURL", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "postUrl", "(Ljava/lang/String;[B)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewPostUrl", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;[B)V", false);
putAroundHookMethod("com/uc/webview/export/WebView", "loadUrl", "(Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "ucWebViewLoadUrl", "(Lcom/uc/webview/export/WebView;Ljava/lang/String;)V", false);
putAroundHookMethod("com/uc/webview/export/WebView", "loadUrl", "(Ljava/lang/String;Ljava/util/Map;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "ucWebViewLoadUrl", "(Lcom/uc/webview/export/WebView;Ljava/lang/String;Ljava/util/Map;)V", false);
putAroundHookMethod("com/uc/webview/export/WebView", "loadData", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "ucWebViewLoadData", "(Lcom/uc/webview/export/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("android/webkit/WebView", "loadDataWithBaseURL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "webkitWebViewLoadDataWithBaseURL", "(Landroid/webkit/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("com/tencent/smtt/sdk/WebView", "loadDataWithBaseURL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "x5WebViewLoadDataWithBaseURL", "(Lcom/tencent/smtt/sdk/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("com/uc/webview/export/WebView", "loadDataWithBaseURL", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "ucWebViewLoadDataWithBaseURL", "(Lcom/uc/webview/export/WebView;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V", false);
putAroundHookMethod("com/uc/webview/export/WebView", "postUrl", "(Ljava/lang/String;[B)V", "com/growingio/android/sdk/autotrack/inject/WebViewInjector", "ucWebViewPostUrl", "(Lcom/uc/webview/export/WebView;Ljava/lang/String;[B)V", false);
putAroundHookMethod("android/app/AlertDialog", "show", "()V", "com/growingio/android/sdk/autotrack/click/ViewClickInjector", "alertDialogShow", "(Landroid/app/AlertDialog;)V", true);
putSuperHookMethod("android/view/View$OnClickListener", "onClick", "(Landroid/view/View;)V", "com/growingio/android/sdk/autotrack/click/ViewClickInjector", "viewOnClick", "(Landroid/view/View$OnClickListener;Landroid/view/View;)V", false);
putSuperHookMethod("android/content/DialogInterface$OnClickListener", "onClick", "(Landroid/content/DialogInterface;I)V", "com/growingio/android/sdk/autotrack/click/ViewClickInjector", "dialogOnClick", "(Landroid/content/DialogInterface$OnClickListener;Landroid/content/DialogInterface;I)V", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ public void unregisterDomChangedListener(OnDomChangedListener listener) {
@SuppressLint("SetJavaScriptEnabled")
public void bridgeForWebView(SuperWebView<?> webView) {
webView.setJavaScriptEnabled(true);
if (webView.hasAddJavaScripted()) return;
if (webView.hasAddJavaScripted()) {
Logger.d(TAG, "JavascriptInterface has already been added to the WebView");
return;
}
webView.addJavascriptInterface(new WebViewBridgeJavascriptInterface(getJavascriptBridgeConfiguration()), WebViewBridgeJavascriptInterface.JAVASCRIPT_INTERFACE_NAME);
webView.setAddJavaScript(true);
webView.setAddJavaScript();
}

public Disposable getWebViewDomTree(SuperWebView<?> webView, final Callback<JSONObject> callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public int getWidth() {
public abstract void evaluateJavascript(String script, ValueCallback<String> resultCallback);

public boolean hasAddJavaScripted() {
return hasAddJavaScript;
return mRealWebView.getTag(R.id.growing_tracker_has_add_java_script) != null;
}

public void setAddJavaScript(boolean hasAddJavaScript) {
this.hasAddJavaScript = hasAddJavaScript;
public void setAddJavaScript() {
mRealWebView.setTag(R.id.growing_tracker_has_add_java_script, new Object());
}

public static SuperWebView<WebView> make(WebView webView) {
Expand Down
5 changes: 5 additions & 0 deletions growingio-hybrid/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>
<item name="growing_tracker_has_add_java_script" type="id" />
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Types;

import org.apache.commons.io.IOUtils;
import org.objectweb.asm.Type;
Expand Down Expand Up @@ -205,7 +206,17 @@ private void stashHookClassesArgs(Element element, AnnotationMirror annotationMi
Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) element;
List<Symbol.VarSymbol> parameters = methodSymbol.getParameters();
for (Symbol.VarSymbol parameter : parameters) {
injectParameters.add(parameter.asType().asElement().flatName().toString());
injectParameters.add(parameter.asType().accept(new Types.DefaultTypeVisitor<String, Void>() {
@Override
public String visitArrayType(com.sun.tools.javac.code.Type.ArrayType t, Void unused) {
return t.elemtype.accept(this, null) + "[]";
}

@Override
public String visitType(com.sun.tools.javac.code.Type type, Void unused) {
return type.asElement().flatName().toString();
}
}, null));
}
argumentTypes = new Type[injectParameters.size()];
for (int i = 0; i < injectParameters.size(); i++) {
Expand Down Expand Up @@ -366,7 +377,11 @@ private void generateJavaFile() {
}

private void generatePerfectJava(JavaFile javaFile) throws IOException {
String classPath = getProjectRootPath() + "/src/main/java/" + javaFile.packageName.replace(".", "/") + "/" + javaFile.typeSpec.name + ".java";
String rootDir = processingEnv.getOptions().get("GRADLE_PLUGIN_DIR");
if (rootDir == null) {
rootDir = getProjectRootPath();
}
String classPath = rootDir + "/src/main/java/" + javaFile.packageName.replace(".", "/") + "/" + javaFile.typeSpec.name + ".java";
File file = new File(classPath);
OutputStream out = new FileOutputStream(file);

Expand Down

0 comments on commit 95eb1df

Please sign in to comment.