package io.grpc.internal; import com.google.android.gms.common.internal.ServiceSpecificExtraArgs; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import io.grpc.Attributes; import io.grpc.ClientCall; import io.grpc.Context; import io.grpc.Deadline; import io.grpc.Metadata; import io.grpc.Status; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public class DelayedClientCall extends ClientCall { static final boolean $assertionsDisabled = false; private final Executor callExecutor; private final Context context; private DelayedListener delayedListener; private Status error; private final ScheduledFuture initialDeadlineMonitor; private ClientCall.Listener listener; private volatile boolean passThrough; private List pendingRunnables = new ArrayList(); private ClientCall realCall; private static final Logger logger = Logger.getLogger(DelayedClientCall.class.getName()); private static final ClientCall NOOP_CALL = new ClientCall() { // from class: io.grpc.internal.DelayedClientCall.7 @Override // io.grpc.ClientCall public void cancel(String str, Throwable th) { } @Override // io.grpc.ClientCall public void halfClose() { } @Override // io.grpc.ClientCall public boolean isReady() { return false; } @Override // io.grpc.ClientCall public void request(int i) { } @Override // io.grpc.ClientCall public void sendMessage(Object obj) { } @Override // io.grpc.ClientCall public void start(ClientCall.Listener listener, Metadata metadata) { } }; /* JADX INFO: Access modifiers changed from: protected */ public void callCancelled() { } /* JADX INFO: Access modifiers changed from: protected */ public DelayedClientCall(Executor executor, ScheduledExecutorService scheduledExecutorService, Deadline deadline) { this.callExecutor = (Executor) Preconditions.checkNotNull(executor, "callExecutor"); Preconditions.checkNotNull(scheduledExecutorService, "scheduler"); this.context = Context.current(); this.initialDeadlineMonitor = scheduleDeadlineIfNeeded(scheduledExecutorService, deadline); } private ScheduledFuture scheduleDeadlineIfNeeded(ScheduledExecutorService scheduledExecutorService, Deadline deadline) { Deadline deadline2 = this.context.getDeadline(); if (deadline == null && deadline2 == null) { return null; } long min = deadline != null ? Math.min(Long.MAX_VALUE, deadline.timeRemaining(TimeUnit.NANOSECONDS)) : Long.MAX_VALUE; if (deadline2 != null && deadline2.timeRemaining(TimeUnit.NANOSECONDS) < min) { min = deadline2.timeRemaining(TimeUnit.NANOSECONDS); Logger logger2 = logger; if (logger2.isLoggable(Level.FINE)) { StringBuilder sb = new StringBuilder(String.format("Call timeout set to '%d' ns, due to context deadline.", Long.valueOf(min))); if (deadline == null) { sb.append(" Explicit call timeout was not set."); } else { sb.append(String.format(" Explicit call timeout was '%d' ns.", Long.valueOf(deadline.timeRemaining(TimeUnit.NANOSECONDS)))); } logger2.fine(sb.toString()); } } long abs = Math.abs(min) / TimeUnit.SECONDS.toNanos(1L); long abs2 = Math.abs(min); long nanos = TimeUnit.SECONDS.toNanos(1L); StringBuilder sb2 = new StringBuilder(); if (min < 0) { sb2.append("ClientCall started after deadline exceeded. Deadline exceeded after -"); } else { sb2.append("Deadline exceeded after "); } sb2.append(abs); sb2.append(String.format(Locale.US, ".%09d", Long.valueOf(abs2 % nanos))); sb2.append("s. "); return scheduledExecutorService.schedule(new Runnable(this, sb2) { // from class: io.grpc.internal.DelayedClientCall.1DeadlineExceededRunnable final DelayedClientCall this$0; final StringBuilder val$buf; { this.this$0 = this; this.val$buf = sb2; } @Override // java.lang.Runnable public void run() { this.this$0.cancel(Status.DEADLINE_EXCEEDED.withDescription(this.val$buf.toString()), true); } }, min, TimeUnit.NANOSECONDS); } public final void setCall(ClientCall clientCall) { synchronized (this) { if (this.realCall != null) { return; } setRealCall((ClientCall) Preconditions.checkNotNull(clientCall, "call")); drainPendingCalls(); } } @Override // io.grpc.ClientCall public final void start(ClientCall.Listener listener, Metadata metadata) { Status status; boolean z; Preconditions.checkState(this.listener == null, "already started"); synchronized (this) { this.listener = (ClientCall.Listener) Preconditions.checkNotNull(listener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER); status = this.error; z = this.passThrough; if (!z) { DelayedListener delayedListener = new DelayedListener<>(listener); this.delayedListener = delayedListener; listener = delayedListener; } } if (status != null) { this.callExecutor.execute(new CloseListenerRunnable(this, listener, status)); } else if (z) { this.realCall.start(listener, metadata); } else { delayOrExecute(new Runnable(this, listener, metadata) { // from class: io.grpc.internal.DelayedClientCall.1 final DelayedClientCall this$0; final ClientCall.Listener val$finalListener; final Metadata val$headers; { this.this$0 = this; this.val$finalListener = listener; this.val$headers = metadata; } @Override // java.lang.Runnable public void run() { this.this$0.realCall.start(this.val$finalListener, this.val$headers); } }); } } @Override // io.grpc.ClientCall public final void cancel(String str, Throwable th) { Status withDescription; Status status = Status.CANCELLED; if (str != null) { withDescription = status.withDescription(str); } else { withDescription = status.withDescription("Call cancelled without message"); } if (th != null) { withDescription = withDescription.withCause(th); } cancel(withDescription, false); } /* JADX INFO: Access modifiers changed from: private */ /* JADX WARN: Multi-variable type inference failed */ public void cancel(Status status, boolean z) { boolean z2; ClientCall.Listener listener; synchronized (this) { if (this.realCall == null) { setRealCall(NOOP_CALL); listener = this.listener; this.error = status; z2 = false; } else { if (z) { return; } z2 = true; listener = null; } if (z2) { delayOrExecute(new Runnable(this, status) { // from class: io.grpc.internal.DelayedClientCall.2 final DelayedClientCall this$0; final Status val$status; { this.this$0 = this; this.val$status = status; } @Override // java.lang.Runnable public void run() { this.this$0.realCall.cancel(this.val$status.getDescription(), this.val$status.getCause()); } }); } else { if (listener != null) { this.callExecutor.execute(new CloseListenerRunnable(this, listener, status)); } drainPendingCalls(); } callCancelled(); } } private void delayOrExecute(Runnable runnable) { synchronized (this) { if (!this.passThrough) { this.pendingRunnables.add(runnable); } else { runnable.run(); } } } /* JADX WARN: Code restructure failed: missing block: B:10:0x0031, code lost: if (r0.hasNext() == false) goto L24; */ /* JADX WARN: Code restructure failed: missing block: B:11:0x0033, code lost: ((java.lang.Runnable) r0.next()).run(); */ /* JADX WARN: Code restructure failed: missing block: B:8:0x0029, code lost: r0 = r1.iterator(); */ /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Removed duplicated region for block: B:18:0x0019 */ /* JADX WARN: Removed duplicated region for block: B:21:? A[RETURN, SYNTHETIC] */ /* Code decompiled incorrectly, please refer to instructions dump. To view partially-correct add '--show-bad-code' argument */ private void drainPendingCalls() { /* r3 = this; java.util.ArrayList r0 = new java.util.ArrayList r0.() L5: monitor-enter(r3) java.util.List r1 = r3.pendingRunnables // Catch: java.lang.Throwable -> L42 boolean r1 = r1.isEmpty() // Catch: java.lang.Throwable -> L42 if (r1 == 0) goto L24 r0 = 0 r3.pendingRunnables = r0 // Catch: java.lang.Throwable -> L42 r0 = 1 r3.passThrough = r0 // Catch: java.lang.Throwable -> L42 io.grpc.internal.DelayedClientCall$DelayedListener r0 = r3.delayedListener // Catch: java.lang.Throwable -> L42 monitor-exit(r3) if (r0 == 0) goto L23 java.util.concurrent.Executor r1 = r3.callExecutor io.grpc.internal.DelayedClientCall$1DrainListenerRunnable r2 = new io.grpc.internal.DelayedClientCall$1DrainListenerRunnable r2.(r3, r0) r1.execute(r2) L23: return L24: java.util.List r1 = r3.pendingRunnables // Catch: java.lang.Throwable -> L42 r3.pendingRunnables = r0 // Catch: java.lang.Throwable -> L42 monitor-exit(r3) java.util.Iterator r0 = r1.iterator() L2d: boolean r2 = r0.hasNext() if (r2 == 0) goto L3d java.lang.Object r2 = r0.next() java.lang.Runnable r2 = (java.lang.Runnable) r2 r2.run() goto L2d L3d: r1.clear() r0 = r1 goto L5 L42: r0 = move-exception monitor-exit(r3) throw r0 */ throw new UnsupportedOperationException("Method not decompiled: io.grpc.internal.DelayedClientCall.drainPendingCalls():void"); } private void setRealCall(ClientCall clientCall) { ClientCall clientCall2 = this.realCall; Preconditions.checkState(clientCall2 == null, "realCall already set to %s", clientCall2); ScheduledFuture scheduledFuture = this.initialDeadlineMonitor; if (scheduledFuture != null) { scheduledFuture.cancel(false); } this.realCall = clientCall; } @Override // io.grpc.ClientCall public final void sendMessage(ReqT reqt) { if (this.passThrough) { this.realCall.sendMessage(reqt); } else { delayOrExecute(new Runnable(this, reqt) { // from class: io.grpc.internal.DelayedClientCall.3 final DelayedClientCall this$0; final Object val$message; { this.this$0 = this; this.val$message = reqt; } /* JADX WARN: Multi-variable type inference failed */ @Override // java.lang.Runnable public void run() { this.this$0.realCall.sendMessage(this.val$message); } }); } } @Override // io.grpc.ClientCall public final void setMessageCompression(boolean z) { if (this.passThrough) { this.realCall.setMessageCompression(z); } else { delayOrExecute(new Runnable(this, z) { // from class: io.grpc.internal.DelayedClientCall.4 final DelayedClientCall this$0; final boolean val$enable; { this.this$0 = this; this.val$enable = z; } @Override // java.lang.Runnable public void run() { this.this$0.realCall.setMessageCompression(this.val$enable); } }); } } @Override // io.grpc.ClientCall public final void request(int i) { if (this.passThrough) { this.realCall.request(i); } else { delayOrExecute(new Runnable(this, i) { // from class: io.grpc.internal.DelayedClientCall.5 final DelayedClientCall this$0; final int val$numMessages; { this.this$0 = this; this.val$numMessages = i; } @Override // java.lang.Runnable public void run() { this.this$0.realCall.request(this.val$numMessages); } }); } } @Override // io.grpc.ClientCall public final void halfClose() { delayOrExecute(new Runnable(this) { // from class: io.grpc.internal.DelayedClientCall.6 final DelayedClientCall this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.realCall.halfClose(); } }); } @Override // io.grpc.ClientCall public final boolean isReady() { if (this.passThrough) { return this.realCall.isReady(); } return false; } @Override // io.grpc.ClientCall public final Attributes getAttributes() { ClientCall clientCall; synchronized (this) { clientCall = this.realCall; } if (clientCall != null) { return clientCall.getAttributes(); } return Attributes.EMPTY; } public String toString() { return MoreObjects.toStringHelper(this).add("realCall", this.realCall).toString(); } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class CloseListenerRunnable extends ContextRunnable { final ClientCall.Listener listener; final Status status; final DelayedClientCall this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ CloseListenerRunnable(DelayedClientCall delayedClientCall, ClientCall.Listener listener, Status status) { super(delayedClientCall.context); this.this$0 = delayedClientCall; this.listener = listener; this.status = status; } @Override // io.grpc.internal.ContextRunnable public final void runInContext() { this.listener.onClose(this.status, new Metadata()); } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static final class DelayedListener extends ClientCall.Listener { static final boolean $assertionsDisabled = false; private volatile boolean passThrough; private List pendingCallbacks = new ArrayList(); private final ClientCall.Listener realListener; public DelayedListener(ClientCall.Listener listener) { this.realListener = listener; } private void delayOrExecute(Runnable runnable) { synchronized (this) { if (!this.passThrough) { this.pendingCallbacks.add(runnable); } else { runnable.run(); } } } @Override // io.grpc.ClientCall.Listener public final void onHeaders(Metadata metadata) { if (this.passThrough) { this.realListener.onHeaders(metadata); } else { delayOrExecute(new Runnable(this, metadata) { // from class: io.grpc.internal.DelayedClientCall.DelayedListener.1 final DelayedListener this$0; final Metadata val$headers; { this.this$0 = this; this.val$headers = metadata; } @Override // java.lang.Runnable public void run() { this.this$0.realListener.onHeaders(this.val$headers); } }); } } @Override // io.grpc.ClientCall.Listener public final void onMessage(RespT respt) { if (this.passThrough) { this.realListener.onMessage(respt); } else { delayOrExecute(new Runnable(this, respt) { // from class: io.grpc.internal.DelayedClientCall.DelayedListener.2 final DelayedListener this$0; final Object val$message; { this.this$0 = this; this.val$message = respt; } @Override // java.lang.Runnable public void run() { this.this$0.realListener.onMessage(this.val$message); } }); } } @Override // io.grpc.ClientCall.Listener public final void onClose(Status status, Metadata metadata) { delayOrExecute(new Runnable(this, status, metadata) { // from class: io.grpc.internal.DelayedClientCall.DelayedListener.3 final DelayedListener this$0; final Status val$status; final Metadata val$trailers; { this.this$0 = this; this.val$status = status; this.val$trailers = metadata; } @Override // java.lang.Runnable public void run() { this.this$0.realListener.onClose(this.val$status, this.val$trailers); } }); } @Override // io.grpc.ClientCall.Listener public final void onReady() { if (this.passThrough) { this.realListener.onReady(); } else { delayOrExecute(new Runnable(this) { // from class: io.grpc.internal.DelayedClientCall.DelayedListener.4 final DelayedListener this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.realListener.onReady(); } }); } } /* JADX WARN: Multi-variable type inference failed */ final void drainPendingCallbacks() { List list; List arrayList = new ArrayList(); while (true) { synchronized (this) { if (this.pendingCallbacks.isEmpty()) { this.pendingCallbacks = null; this.passThrough = true; return; } else { list = this.pendingCallbacks; this.pendingCallbacks = arrayList; } } Iterator it = list.iterator(); while (it.hasNext()) { ((Runnable) it.next()).run(); } list.clear(); arrayList = list; } } } final ClientCall getRealCall() { return this.realCall; } }