package com.android.org.conscrypt;

import android.compat.annotation.UnsupportedAppUsage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public final class ClientSessionContext extends AbstractSessionContext {
    private SSLClientSessionCache persistentCache;
    private final Map<HostAndPort, List<NativeSslSession>> sessionsByHostAndPort;

    private static final class HostAndPort {
        final String host;
        final int port;

        HostAndPort(String str, int i3) {
            this.host = str;
            this.port = i3;
        }

        public boolean equals(Object obj) {
            if (!(obj instanceof HostAndPort)) {
                return false;
            }
            HostAndPort hostAndPort = (HostAndPort) obj;
            return this.host.equals(hostAndPort.host) && this.port == hostAndPort.port;
        }

        public int hashCode() {
            return (this.host.hashCode() * 31) + this.port;
        }
    }

    ClientSessionContext() {
        super(10);
        this.sessionsByHostAndPort = new HashMap();
    }

    @UnsupportedAppUsage
    private NativeSslSession getSession(String str, int i3) {
        NativeSslSession nativeSslSession;
        byte[] sessionData;
        NativeSslSession nativeSslSessionNewInstance;
        if (str == null) {
            return null;
        }
        HostAndPort hostAndPort = new HostAndPort(str, i3);
        synchronized (this.sessionsByHostAndPort) {
            List<NativeSslSession> list = this.sessionsByHostAndPort.get(hostAndPort);
            nativeSslSession = (list == null || list.size() <= 0) ? null : list.get(0);
        }
        if (nativeSslSession != null && nativeSslSession.isValid()) {
            return nativeSslSession;
        }
        SSLClientSessionCache sSLClientSessionCache = this.persistentCache;
        if (sSLClientSessionCache == null || (sessionData = sSLClientSessionCache.getSessionData(str, i3)) == null || (nativeSslSessionNewInstance = NativeSslSession.newInstance(this, sessionData, str, i3)) == null || !nativeSslSessionNewInstance.isValid()) {
            return null;
        }
        putSession(hostAndPort, nativeSslSessionNewInstance);
        return nativeSslSessionNewInstance;
    }

    private void putSession(HostAndPort hostAndPort, NativeSslSession nativeSslSession) {
        synchronized (this.sessionsByHostAndPort) {
            List<NativeSslSession> arrayList = this.sessionsByHostAndPort.get(hostAndPort);
            if (arrayList == null) {
                arrayList = new ArrayList<>();
                this.sessionsByHostAndPort.put(hostAndPort, arrayList);
            }
            if (arrayList.size() > 0 && arrayList.get(0).isSingleUse() != nativeSslSession.isSingleUse()) {
                while (!arrayList.isEmpty()) {
                    removeSession(arrayList.get(0));
                }
                this.sessionsByHostAndPort.put(hostAndPort, arrayList);
            }
            arrayList.add(nativeSslSession);
        }
    }

    private void removeSession(HostAndPort hostAndPort, NativeSslSession nativeSslSession) {
        synchronized (this.sessionsByHostAndPort) {
            List<NativeSslSession> list = this.sessionsByHostAndPort.get(hostAndPort);
            if (list != null) {
                list.remove(nativeSslSession);
                if (list.isEmpty()) {
                    this.sessionsByHostAndPort.remove(hostAndPort);
                }
            }
        }
    }

    synchronized NativeSslSession getCachedSession(String str, int i3, SSLParametersImpl sSLParametersImpl) {
        boolean z2;
        if (str == null) {
            return null;
        }
        NativeSslSession session = getSession(str, i3);
        if (session == null) {
            return null;
        }
        String protocol = session.getProtocol();
        String[] strArr = sSLParametersImpl.enabledProtocols;
        int length = strArr.length;
        boolean z3 = false;
        int i4 = 0;
        while (true) {
            if (i4 >= length) {
                z2 = false;
                break;
            }
            if (protocol.equals(strArr[i4])) {
                z2 = true;
                break;
            }
            i4++;
        }
        if (!z2) {
            return null;
        }
        String cipherSuite = session.getCipherSuite();
        String[] enabledCipherSuites = sSLParametersImpl.getEnabledCipherSuites();
        int length2 = enabledCipherSuites.length;
        int i5 = 0;
        while (true) {
            if (i5 >= length2) {
                break;
            }
            if (cipherSuite.equals(enabledCipherSuites[i5])) {
                z3 = true;
                break;
            }
            i5++;
        }
        if (!z3) {
            return null;
        }
        if (session.isSingleUse()) {
            removeSession(session);
        }
        return session;
    }

    @Override // com.android.org.conscrypt.AbstractSessionContext
    NativeSslSession getSessionFromPersistentCache(byte[] bArr) {
        return null;
    }

    @Override // com.android.org.conscrypt.AbstractSessionContext
    void onBeforeAddSession(NativeSslSession nativeSslSession) {
        byte[] bytes;
        String peerHost = nativeSslSession.getPeerHost();
        int peerPort = nativeSslSession.getPeerPort();
        if (peerHost == null) {
            return;
        }
        putSession(new HostAndPort(peerHost, peerPort), nativeSslSession);
        if (this.persistentCache == null || nativeSslSession.isSingleUse() || (bytes = nativeSslSession.toBytes()) == null) {
            return;
        }
        this.persistentCache.putSessionData(nativeSslSession.toSSLSession(), bytes);
    }

    @Override // com.android.org.conscrypt.AbstractSessionContext
    void onBeforeRemoveSession(NativeSslSession nativeSslSession) {
        String peerHost = nativeSslSession.getPeerHost();
        if (peerHost == null) {
            return;
        }
        removeSession(new HostAndPort(peerHost, nativeSslSession.getPeerPort()), nativeSslSession);
    }

    @UnsupportedAppUsage
    public void setPersistentCache(SSLClientSessionCache sSLClientSessionCache) {
        this.persistentCache = sSLClientSessionCache;
    }

    int size() {
        int size;
        synchronized (this.sessionsByHostAndPort) {
            Iterator<List<NativeSslSession>> it = this.sessionsByHostAndPort.values().iterator();
            size = 0;
            while (it.hasNext()) {
                size += it.next().size();
            }
        }
        return size;
    }
}
