光速虚拟机 v3.8.2版本的 MD5 值为:e0eecbee2ded4bfb58a5ca72845ca90b

以下内容为反编译后的 SafeDKWebAppInterface.java 源代码,内容仅作参考


package com.safedk.android.internal;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.support.multidex.MultiDexApplication;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.safedk.android.SafeDK;
import com.safedk.android.analytics.brandsafety.BrandSafetyUtils;
import com.safedk.android.analytics.brandsafety.creatives.AdNetworkConfiguration;
import com.safedk.android.analytics.brandsafety.creatives.CreativeInfoManager;
import com.safedk.android.analytics.brandsafety.creatives.infos.CreativeInfo;
import com.safedk.android.utils.Logger;
import com.safedk.android.utils.SdksMapping;
import com.safedk.android.utils.l;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class SafeDKWebAppInterface {
    public static final String a = "SafeDKWebAppInterface";
    public static final String b = "status";
    public static final String c = "playing";
    public static final String d = "paused";
    public static final String e = "sdk_id";
    public static final String f = "javascript:";
    public static final String g = "(function(){var isMainFrame=function(){return window.location===window.parent.location};var log=function(message){try{window.webkit.messageHandlers.safedkDebug.postMessage(message)}catch(error){}};var sdkId=\"{{SDK_ID}}\";var endCardDetected=false;var isElementHidden=function(element){try{while(element){if(getComputedStyle(element).display==\"none\")return true;element=element.parentElement}}catch(error){window.webkit.messageHandlers.safedkVideoExc.postMessage(error.message+\"\\n\"+error.stack)}return false};var isElementInEventHidden=function(event){return isElementHidden(event.srcElement)};var getVideoPath=function(event){try{var videoElement=event.srcElement;if(videoElement.tagName!=\"VIDEO\")return null;var path=videoElement.src;if(!path){var sourceNodes=videoElement.getElementsByTagName(\"SOURCE\");for(var sourceNode of sourceNodes){if(sourceNode.src){path=sourceNode.src;break}}}if(path){if(path.length>=200){path=path.substring(0,199)}}return path}catch(error){window.webkit.messageHandlers.safedkVideoExc.postMessage(error.message+\"\\n\"+error.stack)}return null};var notifyPlay=function(event){if(!endCardDetected){if(isElementInEventHidden(event))return;event.srcElement.safedkVideoPlaying=true;var message={status:\"playing\",sdk_id:sdkId};var src=getVideoPath(event);if(src)message[\"src\"]=src;log(\"Notifying client that video is playing for sdkId: \"+sdkId+\", video ID: \"+event.srcElement.id);window.webkit.messageHandlers.safedkVideo.postMessage(JSON.stringify(message))}};var notifyPause=function(event){if(!endCardDetected){event.srcElement.safedkVideoPlaying=false;var message={status:\"paused\",sdk_id:sdkId};var src=getVideoPath(event);if(src)message[\"src\"]=src;log(\"Notifying client that video is paused for sdkId: \"+sdkId+\", video ID: \"+event.srcElement.id);window.webkit.messageHandlers.safedkVideo.postMessage(JSON.stringify(message))}};var notifyEndCard=function(){if(!endCardDetected){endCardDetected=true;log(\"Notifying client that end-card detected for sdkId: \"+sdkId);var message={status:\"end-card\",sdk_id:sdkId};window.webkit.messageHandlers.safedkVideo.postMessage(JSON.stringify(message))}};var addVideoListeners=function(doc){try{if(doc.nodeName==\"#text\")return;var videoElements=doc.getElementsByTagName(\"VIDEO\");if(videoElements.length==0){if(doc.tagName&&doc.tagName==\"VIDEO\"){videoElements=[doc]}}for(i=0;i<videoElements.length;i++){var videoElement=videoElements[i];if(videoElement.safedkVideoListeners!=true){log(\"Found VIDEO tag, adding event listeners. sdkId: \"+sdkId+\", video ID: \"+videoElement.id);videoElement.safedkVideoListeners=true;videoElement.addEventListener(\"play\",notifyPlay);videoElement.addEventListener(\"pause\",notifyPause)}}}catch(error){}};var addEndCardObserver=function(){try{var endCardElement=document.getElementById(\"al_endCardDiv\");if(endCardElement){var computedStyle=getComputedStyle(endCardElement);if(computedStyle&&computedStyle.display==\"flex\"){log(\"Found visible al_endCardDiv tag. sdkId: \"+sdkId);notifyEndCard()}else{if(endCardElement.safedkEndCardObserver!=true){endCardElement.safedkEndCardObserver=true;log(\"Found al_endCardDiv tag, adding end-card observer. sdkId: \"+sdkId);try{var MutationObserver=window.WebKitMutationObserver||window.MutationObserver;var observer=new MutationObserver(function(mutations){mutations.forEach(function(mutation){switch(mutation.type){case\"attributes\":computedStyle=getComputedStyle(mutation.target);if(computedStyle&&computedStyle.display==\"flex\"){notifyEndCard()}break}})});var config={attributes:true,attributeOldValue:true,attributeFilter:[\"style\"]};observer.observe(endCardElement,config)}catch(error){window.webkit.messageHandlers.safedkVideoExc.postMessage(error.message+\"\\n\"+error.stack)}}}}}catch(error){window.webkit.messageHandlers.safedkVideoExc.postMessage(error.message+\"\\n\"+error.stack)}};log(\"Initializing video observer\");addVideoListeners(document);if(sdkId.startsWith(\"com.applovin\")){addEndCardObserver()}try{var MutationObserver=window.WebKitMutationObserver||window.MutationObserver;var observer=new MutationObserver(function(mutations){mutations.forEach(function(mutation){for(var node of mutation.addedNodes){log(\"Add video listeners for added elements: \"+node);addVideoListeners(node)}for(var node of mutation.removedNodes){if(node.tagName){var removedVideoElements=node.getElementsByTagName(\"VIDEO\");for(var removedVideoElement of removedVideoElements){if(removedVideoElement.safedkVideoPlaying){log(\"VIDEO tag removed. Calling notifyPause\");var event={srcElement:removedVideoElement};notifyPause(event)}}}}if(sdkId.startsWith(\"com.applovin\")){addEndCardObserver()}})});var config={childList:true,subtree:true};observer.observe(document,config)}catch(error){window.webkit.messageHandlers.safedkVideoExc.postMessage(error.message+\"\\n\"+error.stack)}})();\n";
    public static final String h = "(function(){var webviewAddress=\"{{webviewAddress}}\";var sdkPackage=\"{{sdkPackage}}\";var log=function(message){try{window.webkit.messageHandlers.safedkDebug.postMessage(\"get_text \"+sdkPackage+\" \"+webviewAddress+\" : \"+message)}catch(error){}};var textContentUnder=function(element){var n;var s=new Set;try{if(element){var walk=document.createTreeWalker(element,NodeFilter.SHOW_TEXT,null,false);while(n=walk.nextNode()){var parent=n.parentElement;if(!parent)continue;if(parent.nodeType!=1)continue;if(parent.tagName==\"SCRIPT\"||parent.tagName==\"NOSCRIPT\"||parent.tagName==\"STYLE\"||parent.tagName==\"IFRAME\")continue;if(isHidden(parent))continue;var text=n.textContent.trim();if(text==\"\")continue;log(\"textContentUnder added \"+text);s.add(text)}}}catch(error){log(\"error : \"+error)}return s};var isHidden=function(element){try{while(element){if(getComputedStyle(element).display==\"none\")return true;element=element.parentElement}}catch(error){console.log(\"error : \"+error)}return false};var collectText=function(element){var textArray=textContentUnder(document.body);var text=Array.from(textArray).join(\"---\");log(\"sdkPackage = \"+sdkPackage+\", address = \"+webviewAddress+\", text = \"+text);try{window.webkit.messageHandlers.safedkDebug.setContent(sdkPackage,webviewAddress,text)}catch(error){log(\"collectText error : \"+error)}};var i=0;var interval=1e3;setInterval(function(){if(i<10){log(\"debug setInterval i = \"+i);i++;collectText()}},interval)})();\n";
    private static final String k = "privacy";
    private static final String l = "end_of_video";
    private static final String m = "extract_text";
    Context j;
    private static final CharSequence n = "";
    private static final CharSequence o = "UnityAdsWebApp.html";
    private static final List<String> p = Arrays.asList("document.write", "(function()");
    public static final Map<String, WeakReference<WebView>> i = new HashMap();

    public SafeDKWebAppInterface(Context context) {
        this.j = context;
    }

    public static String a(String str, String str2) {
        return f + g.replace("window.webkit.", "window.").replace("messageHandlers.safedkDebug.postMessage", "SafeDKWebAppInterface.logMessage").replace("messageHandlers.safedkVideoExc.postMessage", "SafeDKWebAppInterface.logMessage").replace("messageHandlers.safedkNoSampling.postMessage", "SafeDKWebAppInterface.logMessage").replace("messageHandlers.safedkVideo", a).replace("{{SDK_ID}}", str + "~" + str2);
    }

    public static String a(String str, String str2, String str3) {
        return f + str3.replace("window.webkit.", "window.").replace("messageHandlers.safedkNoSamplingExc.postMessage", "SafeDKWebAppInterface.logMessage").replace("messageHandlers.safedkNoSampling.postMessage", "SafeDKWebAppInterface.requestNoSampling").replace("messageHandlers.safedkDebug.postMessage", "SafeDKWebAppInterface.logMessage").replace("{{SDK_ID}}", str).replace("{{ADDRESS}}", str2);
    }

    public static void a(String str) {
        synchronized (i) {
            i.remove(str);
        }
        Logger.d(a, "WebView address removed : " + str);
    }

    public static void a(String str, WebView webView, String str2) {
        String a2 = BrandSafetyUtils.a(webView);
        SafeDK safeDK = SafeDK.getInstance();
        Logger.d(a, "addJScript started sdkPackage=" + str + ", webView=" + a2 + " ,url=" + str2 + " , isMaxInit=" + SafeDK.Z());
        if (!SafeDK.Z() || safeDK == null || str2 == null) {
            Logger.d(a, "addJScript loaded url is empty or app not initialized yet, skipping.");
            return;
        }
        String mainSdkPackage = SdksMapping.getMainSdkPackage(str);
        if (CreativeInfoManager.g(mainSdkPackage) == null) {
            Logger.d(a, "addJScript not s supported sdkPackage :  " + mainSdkPackage + ", existing");
        } else if ((!SafeDK.Z() || safeDK == null || safeDK.h()) && !str2.contains(n) && !str2.contains(o)) {
            Logger.d(a, "addJScript skipped url " + str2);
        } else if (str2.startsWith(f)) {
            Logger.d(a, "addJScript loaded data is javascript, ignoring. url : " + str2);
        } else {
            if (SafeDK.P() && Build.VERSION.SDK_INT >= 19) {
                WebView.setWebContentsDebuggingEnabled(true);
            }
            webView.addJavascriptInterface(new SafeDKWebAppInterface(webView.getContext()), a);
            Logger.d(a, "addJScript javascript interface added, WebView address is " + a2);
            if (SafeDK.P() && Build.VERSION.SDK_INT >= 19) {
                Logger.d(a, "addJScript testing javascript call. WebView address is " + a2);
                webView.evaluateJavascript("javascript:console.log('adding js interface');", null);
                webView.evaluateJavascript("javascript:console.log(document.getElementByName);", null);
                webView.evaluateJavascript("javascript:window.SafeDKWebAppInterface.logMessage('adding js interface');", null);
            }
            b(mainSdkPackage, webView, str2);
        }
    }

    public static String b(String str, String str2) {
        Logger.d(a, "getting js html extr script for webviewAddress " + str2);
        return f + h.replace("{{webviewAddress}}", str2).replace("{{sdkPackage}}", str).replace("window.webkit.", "window.").replace("messageHandlers.safedkDebug.postMessage", "SafeDKWebAppInterface.logMessage").replace("messageHandlers.safedkDebug.setContent", "SafeDKWebAppInterface.setContent");
    }

    private static void b(final String str, final WebView webView, final String str2) {
        final String a2 = BrandSafetyUtils.a(webView);
        SafeDK safeDK = SafeDK.getInstance();
        if (!SafeDK.Z() || safeDK == null || SafeDK.getInstance().h()) {
            return;
        }
        if (str2 == null || str2.equalsIgnoreCase("about:blank") || str2.trim().length() == 0) {
            Logger.d(a, "attachJsScript url is blank, skipping scripts injection. sdkPackage = " + str + ", webView = " + webView + ", url = " + str2);
            return;
        }
        final HashMap hashMap = new HashMap();
        Logger.d(a, "attachJsScript sdk: " + str + ", address: " + a2 + " resource: " + str2);
        com.safedk.android.analytics.brandsafety.creatives.a h2 = CreativeInfoManager.h(str);
        if (h2 == null) {
            Logger.d(a, "attachJsScript configBundle is empty, existing");
            return;
        }
        if (h2 != null) {
            Logger.d(a, "attachJsScript sdk " + str + " items: js privacy fullscreen  : " + h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_FULLSCREEN_ADS, false) + ", js privacy banner  : " + h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_BANNERS, false) + ", js privacy mrec  : " + h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_MRECS, false) + ", js video : " + h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_VIDEO_STATE_DETECTION, false) + ", js text extraction : " + h2.a(AdNetworkConfiguration.SHOULD_INJECT_JS_TO_WEBVIEWS_FOR_TEXT_EXTRACTION, true));
        } else {
            Logger.d(a, "attachJsScript sdk " + str + " config bundle is empty");
        }
        boolean isInterstitialActivity = b.getInstance().isInterstitialActivity(webView.getContext());
        boolean z = h2.a(AdNetworkConfiguration.SHOULD_INJECT_JS_TO_WEBVIEWS_NOT_YET_ATTACHED_TO_ACTIVITY, false) || (webView.getContext() instanceof Application) || (webView.getContext() instanceof MultiDexApplication);
        Logger.d(a, "attachJsScript interstitialActivity is " + isInterstitialActivity + ", context = " + webView.getContext());
        if (((isInterstitialActivity || z) && h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_FULLSCREEN_ADS, false)) || (!isInterstitialActivity && (h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_MRECS, false) || h2.a(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_PRIVACY_BUTTON_CLICK_DETECTION_IN_BANNERS, false)))) {
            String a3 = h2.a(AdNetworkConfiguration.JS_PRIVACY_BUTTON_CLICK_DETECTION_SCRIPT, (String) null);
            if (a3 == null || a3.length() <= 0) {
                Logger.d(a, "attachJsScript privacy button click detection script is empty, skipping.");
            } else {
                hashMap.put(k, a(str, a2, a3));
                Logger.d(a, "attachJsScript added " + str + " privacy button click detection script to list.");
            }
        }
        if (h2.b(AdNetworkConfiguration.SUPPORTS_JAVASCRIPT_BASED_VIDEO_STATE_DETECTION)) {
            hashMap.put(l, a(str, a2));
            Logger.d(a, "attachJsScript added " + str + " video state detection script to list.");
        } else {
            Logger.d(a, "attachJsScript no need to add video state detection script. WebView : " + a2);
        }
        if (!h2.b(AdNetworkConfiguration.SUPPORTS_BANNER_IMPRESSION_TRACKING) || !h2.a(AdNetworkConfiguration.SHOULD_INJECT_JS_TO_WEBVIEWS_FOR_TEXT_EXTRACTION, true)) {
            Logger.d(a, "attachJsScript no need to add text extraction script. sdkPackage = " + str + ", WebView : " + a2);
        } else if (isInterstitialActivity) {
            Logger.d(a, "attachJsScript WebView is in an interstitial, skipping text extraction script. sdk: " + str + ", context = " + webView.getContext());
        } else {
            synchronized (i) {
                i.put(a2, new WeakReference<>(webView));
            }
            hashMap.put(m, b(str, a2));
            Logger.d(a, "attachJsScript added " + str + " text extraction script to list.");
        }
        webView.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT < 19) {
                    for (String str3 : hashMap.keySet()) {
                        String str4 = (String) hashMap.get(str3);
                        if (str4 != null && str4.length() > 0 && webView != null) {
                            Logger.d(SafeDKWebAppInterface.a, "attachJsScript Injecting script for " + str + " WebView : " + a2 + ", url = " + str2 + ", script : " + str3);
                            try {
                                webView.loadUrl(str4, null);
                            } catch (Throwable th) {
                                Logger.d(SafeDKWebAppInterface.a, "attachJsScript Exception injecting text extraction script : " + th.getMessage(), th);
                            }
                        }
                    }
                    return;
                }
                if (webView != null) {
                    for (String str5 : hashMap.keySet()) {
                        if (hashMap.get(str5) != null && ((String) hashMap.get(str5)).length() > 0) {
                            String str6 = (String) hashMap.get(str5);
                            l.b(SafeDKWebAppInterface.a, "Injecting script for " + str + " WebView : " + a2 + ", url = " + str2 + ", script : " + str5);
                            try {
                                webView.evaluateJavascript(str6, null);
                            } catch (Throwable th2) {
                                Logger.d(SafeDKWebAppInterface.a, "attachJsScript Exception injecting text extraction script : " + th2.getMessage(), th2);
                            }
                        }
                    }
                }
                hashMap.clear();
            }
        });
    }

    private boolean b(String str) {
        for (String str2 : p) {
            if (str != null && str.contains(str2)) {
                Logger.d(a, "should add text : skipping text " + str);
                return false;
            }
        }
        Logger.d(a, "should add text : eligible text " + str);
        return true;
    }

    private void c(String str, String str2) {
        List<CreativeInfo> a2 = SafeDK.getInstance().y().a(str, str2);
        List<CreativeInfo> a3 = SafeDK.getInstance().z().a(str, str2);
        List<CreativeInfo> a4 = SafeDK.getInstance().A().a(str, str2);
        if (a2 != null && a2.size() > 0) {
            Logger.d(a, "requestNoSamplingUnity calling interstitial requestNoSampling, : address = " + str2);
            CreativeInfoManager.b(str, str2);
        } else if (a3 != null && a3.size() > 0) {
            Logger.d(a, "requestNoSamplingUnity calling banner requestNoSampling, : address = " + str2);
            CreativeInfoManager.a(BrandSafetyUtils.AdType.BANNER, str2);
        } else if (a4 == null || a4.size() <= 0) {
            Logger.d(a, "requestNoSamplingUnity WebView is not a banner nor an MREC nor an interstitial, : address = " + str2);
        } else {
            Logger.d(a, "requestNoSamplingUnity calling MREC requestNoSampling, : address = " + str2);
            CreativeInfoManager.a(BrandSafetyUtils.AdType.MREC, str2);
        }
    }

    @JavascriptInterface
    public void logMessage(String str) {
        Logger.d(a, "logMessage msg = " + str);
    }

    @JavascriptInterface
    public void postMessage(String str) {
        boolean z = true;
        Logger.d(a, "postMessage : msg = " + str);
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (!jSONObject.has("status")) {
                z = false;
            } else if (jSONObject.get("status").equals(c)) {
                z = false;
            }
            String string = jSONObject.has(e) ? jSONObject.getString(e) : null;
            Logger.d(a, "postMessage : status = " + z + ", sdk_id = " + string + ", src = " + (jSONObject.has("src") ? jSONObject.get("src") : "NA"));
            if (string != null) {
                String[] split = string.split("~");
                CreativeInfoManager.a(split[0], split[1], z);
            }
        } catch (JSONException e2) {
            Logger.d(a, "Received msg is not a JSON object : " + str);
        }
    }

    @JavascriptInterface
    public void requestNoSampling(String str, String str2, int i2, int i3) {
        Logger.d(a, "requestNoSampling : address = " + str2 + ", height = " + i2 + " , width = " + i3);
        if (i2 == 0 && i3 == 0) {
            c(str, str2);
        } else if (l.a(i3, i2)) {
            Logger.d(a, "requestNoSampling calling banner requestNoSampling, : address = " + str2);
            CreativeInfoManager.a(BrandSafetyUtils.AdType.BANNER, str2);
        } else if (l.b(i3, i2)) {
            Logger.d(a, "requestNoSampling calling MREC requestNoSampling, : address = " + str2);
            CreativeInfoManager.a(BrandSafetyUtils.AdType.MREC, str2);
        } else if (!b.getInstance().isInterstitialActivity(b.getInstance().getForegroundActivity())) {
            Logger.d(a, "requestNoSampling WebView is non a banner nor an interstitial, probably MREC, : address = " + str2);
        } else {
            Logger.d(a, "requestNoSampling calling interstitial requestNoSampling, : address = " + str2);
            CreativeInfoManager.b(str, str2);
        }
    }

    @JavascriptInterface
    public void setContent(String str, String str2, String str3) {
        WeakReference<WebView> weakReference;
        String[] split;
        try {
            if (com.safedk.android.utils.a.a(str)) {
                if (str3 == null || str3.trim().length() <= 0) {
                    Logger.d(a, "setContent WebView address " + str2 + " text is empty");
                    return;
                }
                Logger.d(a, "setContent packageName = " + str + ", webviewAddress = " + str2 + ", text = " + str3);
                if (!i.containsKey(str2) || !b(str3) || (weakReference = i.get(str2)) == null || weakReference.get() == null) {
                    return;
                }
                for (String str4 : str3.split("---")) {
                    if (str4 != null && str4.trim().length() > 0) {
                        com.safedk.android.analytics.brandsafety.creatives.e.a(str, weakReference.get(), "text:" + str4, null);
                    }
                }
            }
        } catch (Throwable th) {
            Logger.d(a, "setContent exception : " + th.getMessage() + ", webviewAddress = " + str2, th);
        }
    }
}