package io.netty.handler.codec.http2;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.EventExecutor;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.ObjectUtil;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.nio.channels.ClosedChannelException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public final class Http2StreamChannelBootstrap {
    public static final /* synthetic */ boolean $assertionsDisabled = false;
    private static final InternalLogger logger = InternalLoggerFactory.getInstance((Class<?>) Http2StreamChannelBootstrap.class);
    private final Channel channel;
    private volatile ChannelHandler handler;
    private final Map<ChannelOption<?>, Object> options = new LinkedHashMap();
    private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap();

    public Http2StreamChannelBootstrap(Channel channel) {
        this.channel = (Channel) ObjectUtil.checkNotNull(channel, "channel");
    }

    private void init(Channel channel) {
        ChannelPipeline channelPipelinePipeline = channel.pipeline();
        ChannelHandler channelHandler = this.handler;
        if (channelHandler != null) {
            channelPipelinePipeline.addLast(channelHandler);
        }
        synchronized (this.options) {
            setChannelOptions(channel, this.options, logger);
        }
        synchronized (this.attrs) {
            for (Map.Entry<AttributeKey<?>, Object> entry : this.attrs.entrySet()) {
                channel.attr(entry.getKey()).set(entry.getValue());
            }
        }
    }

    private static void setChannelOption(Channel channel, ChannelOption<?> channelOption, Object obj, InternalLogger internalLogger) {
        try {
            if (channel.config().setOption(channelOption, obj)) {
                return;
            }
            internalLogger.warn("Unknown channel option '{}' for channel '{}'", channelOption, channel);
        } catch (Throwable th) {
            internalLogger.warn("Failed to set channel option '{}' with value '{}' for channel '{}'", channelOption, obj, channel, th);
        }
    }

    private static void setChannelOptions(Channel channel, Map<ChannelOption<?>, Object> map, InternalLogger internalLogger) {
        for (Map.Entry<ChannelOption<?>, Object> entry : map.entrySet()) {
            setChannelOption(channel, entry.getKey(), entry.getValue(), internalLogger);
        }
    }

    public <T> Http2StreamChannelBootstrap attr(AttributeKey<T> attributeKey, T t2) {
        Objects.requireNonNull(attributeKey, "key");
        synchronized (this.attrs) {
            if (t2 == null) {
                this.attrs.remove(attributeKey);
            } else {
                this.attrs.put(attributeKey, t2);
            }
        }
        return this;
    }

    public Http2StreamChannelBootstrap handler(ChannelHandler channelHandler) {
        this.handler = (ChannelHandler) ObjectUtil.checkNotNull(channelHandler, "handler");
        return this;
    }

    public Future<Http2StreamChannel> open() {
        return open(this.channel.eventLoop().newPromise());
    }

    public Future<Http2StreamChannel> open(final Promise<Http2StreamChannel> promise) {
        final ChannelHandlerContext channelHandlerContextContext = this.channel.pipeline().context(Http2MultiplexCodec.class);
        if (channelHandlerContextContext != null) {
            EventExecutor eventExecutorExecutor = channelHandlerContextContext.executor();
            if (eventExecutorExecutor.inEventLoop()) {
                open0(channelHandlerContextContext, promise);
            } else {
                eventExecutorExecutor.execute(new Runnable() { // from class: io.netty.handler.codec.http2.Http2StreamChannelBootstrap.1
                    @Override // java.lang.Runnable
                    public void run() {
                        Http2StreamChannelBootstrap.this.open0(channelHandlerContextContext, promise);
                    }
                });
            }
        } else if (this.channel.isActive()) {
            promise.setFailure(new IllegalStateException(StringUtil.simpleClassName((Class<?>) Http2MultiplexCodec.class) + " must be in the ChannelPipeline of Channel " + this.channel));
        } else {
            promise.setFailure(new ClosedChannelException());
        }
        return promise;
    }

    public void open0(ChannelHandlerContext channelHandlerContext, final Promise<Http2StreamChannel> promise) {
        final Http2StreamChannel http2StreamChannelNewOutboundStream = ((Http2MultiplexCodec) channelHandlerContext.handler()).newOutboundStream();
        try {
            init(http2StreamChannelNewOutboundStream);
            channelHandlerContext.channel().eventLoop().register(http2StreamChannelNewOutboundStream).addListener((GenericFutureListener<? extends Future<? super Void>>) new ChannelFutureListener() { // from class: io.netty.handler.codec.http2.Http2StreamChannelBootstrap.2
                @Override // io.netty.util.concurrent.GenericFutureListener
                public void operationComplete(ChannelFuture channelFuture) {
                    if (channelFuture.isSuccess()) {
                        promise.setSuccess(http2StreamChannelNewOutboundStream);
                        return;
                    }
                    if (channelFuture.isCancelled()) {
                        promise.cancel(false);
                        return;
                    }
                    if (http2StreamChannelNewOutboundStream.isRegistered()) {
                        http2StreamChannelNewOutboundStream.close();
                    } else {
                        http2StreamChannelNewOutboundStream.unsafe().closeForcibly();
                    }
                    promise.setFailure(channelFuture.cause());
                }
            });
        } catch (Exception e2) {
            http2StreamChannelNewOutboundStream.unsafe().closeForcibly();
            promise.setFailure(e2);
        }
    }

    public <T> Http2StreamChannelBootstrap option(ChannelOption<T> channelOption, T t2) {
        Objects.requireNonNull(channelOption, "option");
        synchronized (this.options) {
            if (t2 == null) {
                this.options.remove(channelOption);
            } else {
                this.options.put(channelOption, t2);
            }
        }
        return this;
    }
}
