package com.bytedance.sdk.component.fm;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Base64;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b;

/* JADX INFO: loaded from: classes.dex */
public class wey extends fm {
    static final /* synthetic */ boolean vt = true;

    /* JADX INFO: renamed from: ef, reason: collision with root package name */
    protected WebView f6625ef;
    protected String wu;

    @Override // com.bytedance.sdk.component.fm.fm
    public Context fm(ef efVar) {
        Context context = efVar.jnr;
        if (context != null) {
            return context;
        }
        WebView webView = efVar.fm;
        if (webView != null) {
            return webView.getContext();
        }
        throw new IllegalStateException("WebView cannot be null!");
    }

    @Override // com.bytedance.sdk.component.fm.fm
    @JavascriptInterface
    public void invokeMethod(String str) {
        super.invokeMethod(str);
    }

    @SuppressLint({"AddJavascriptInterface"})
    public void lb() {
        if (!vt && this.f6625ef == null) {
            throw new AssertionError();
        }
        this.f6625ef.addJavascriptInterface(this, this.wu);
    }

    @Override // com.bytedance.sdk.component.fm.fm
    @SuppressLint({"JavascriptInterface", "AddJavascriptInterface"})
    public void ro(ef efVar) {
        this.f6625ef = efVar.fm;
        this.wu = efVar.f6618lb;
        if (efVar.dsz) {
            return;
        }
        lb();
    }

    public void yz() {
        this.f6625ef.removeJavascriptInterface(this.wu);
    }

    @Override // com.bytedance.sdk.component.fm.fm
    public String fm() {
        return this.f6625ef.getUrl();
    }

    @Override // com.bytedance.sdk.component.fm.fm
    public void ro() {
        super.ro();
        yz();
    }

    @Override // com.bytedance.sdk.component.fm.fm
    public void fm(String str, onz onzVar) {
        if (onzVar != null && !TextUtils.isEmpty(onzVar.wu)) {
            String str2 = onzVar.wu;
            fm(str, String.format("javascript:(function(){   const iframe = document.querySelector(atob('%s'));   if (iframe && iframe.contentWindow) {        iframe.contentWindow.postMessage(%s, atob('%s'));   }})()", Base64.encodeToString(String.format("iframe[src=\"%s\"", str2).getBytes(), 2), str, Base64.encodeToString(str2.getBytes(), 2)));
            return;
        }
        super.fm(str, onzVar);
    }

    @Override // com.bytedance.sdk.component.fm.fm
    public void fm(String str) {
        fm(str, b.h(new StringBuilder("javascript:"), this.wu, "._handleMessageFromToutiao(", str, ")"));
    }

    private void fm(String str, final String str2) {
        if (this.ajl || TextUtils.isEmpty(str2)) {
            return;
        }
        Runnable runnable = new Runnable() { // from class: com.bytedance.sdk.component.fm.wey.1
            @Override // java.lang.Runnable
            public void run() {
                if (wey.this.ajl) {
                    return;
                }
                try {
                    wey.this.f6625ef.evaluateJavascript(str2, null);
                } catch (Throwable unused) {
                }
            }
        };
        if (Looper.myLooper() != Looper.getMainLooper()) {
            this.yz.post(runnable);
        } else {
            runnable.run();
        }
    }
}
