Android Studio是如何实现AddJavascript

2017-04-19  本文已影响238人  sunger
效果图

源码是AddJavascriptInterfaceDetector.java

接下来我们来看看是如何实现的

Detector.JavaPsiScanner

   private static final String WEB_VIEW = "android.webkit.WebView";
    private static final String ADD_JAVASCRIPT_INTERFACE = "addJavascriptInterface";

    // ---- Implements JavaScanner ----

    @Nullable
    @Override
    public List<String> getApplicableMethodNames() {
        return Collections.singletonList(ADD_JAVASCRIPT_INTERFACE);
    }

    @Override
    public void visitMethod(@NonNull JavaContext context, @Nullable JavaElementVisitor visitor,
            @NonNull PsiMethodCallExpression node, @NonNull PsiMethod method) {
        // Ignore the issue if we never build for any API less than 17.
        if (context.getMainProject().getMinSdk() >= 17) {
            return;
        }

        JavaEvaluator evaluator = context.getEvaluator();
        if (!evaluator.methodMatches(method, WEB_VIEW, true, TYPE_OBJECT, TYPE_STRING)) {
            return;
        }

        String message = "`WebView.addJavascriptInterface` should not be called with minSdkVersion < 17 for security reasons: " +
                "JavaScript can use reflection to manipulate application";
        context.report(ISSUE, node, context.getNameLocation(node), message);
    }

核心代码

这个方法用来返回你感兴趣的那些方法调用列表,所以这里设置
的是字符addJavascriptInterface,此方法配合以下方法使用

addJavascriptInterface被调用时,会触发此方法。
JavaContext 上下文

需要注意的一些方法

接下来判断调用addJavascriptInterface的对象是不是Android系统提供的WebView

public boolean methodMatches(PsiMethod method,String className, boolean allowInherit,String... argumentTypes)

method
visitMethod中的method

className
addJavascriptInterface的类名如android.webkit.WebView

allowInherit
是否支持指定类的子类校验,比如WebView的子类

argumentTypes
指定方法中参数的类型
public void addJavascriptInterface(Object object, String name)
所以上面第一个参数为TYPE_OBJECT和TYPE_STRING

创建提示类Issue

Issue类提供了工厂方法创建issue
public static Issue create(String id,String briefDescription,String
explanation,Category category,int priority, Severity
severity,Implementation implementation)

发送报告

Context(子类JavaContext,ClassContext)
public void report(Issue issue,Location location,String message)

上一篇下一篇

猜你喜欢

热点阅读