what-the-bank/sources/com/google/firebase/firestore/util/AsyncQueue.java

358 lines
14 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.firestore.util;
import android.os.Handler;
import android.os.Looper;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.firestore.util.AsyncQueue;
import java.lang.Thread;
import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/* loaded from: classes2.dex */
public class AsyncQueue {
private final ArrayList<TimerId> timerIdsToSkip = new ArrayList<>();
private final ArrayList<DelayedTask> delayedTasks = new ArrayList<>();
private final SynchronizedShutdownAwareExecutor executor = new SynchronizedShutdownAwareExecutor(this);
/* loaded from: classes2.dex */
public enum TimerId {
ALL,
LISTEN_STREAM_IDLE,
LISTEN_STREAM_CONNECTION_BACKOFF,
WRITE_STREAM_IDLE,
WRITE_STREAM_CONNECTION_BACKOFF,
HEALTH_CHECK_TIMEOUT,
ONLINE_STATE_TIMEOUT,
GARBAGE_COLLECTION,
RETRY_TRANSACTION,
CONNECTIVITY_ATTEMPT_TIMER,
INDEX_BACKFILL
}
/* loaded from: classes2.dex */
public class DelayedTask {
private ScheduledFuture scheduledFuture;
private final long targetTimeMs;
private final Runnable task;
final AsyncQueue this$0;
private final TimerId timerId;
private DelayedTask(AsyncQueue asyncQueue, TimerId timerId, long j, Runnable runnable) {
this.this$0 = asyncQueue;
this.timerId = timerId;
this.targetTimeMs = j;
this.task = runnable;
}
/* JADX INFO: Access modifiers changed from: private */
public void start(long j) {
this.scheduledFuture = this.this$0.executor.schedule(new Runnable(this) { // from class: com.google.firebase.firestore.util.AsyncQueue$DelayedTask$$ExternalSyntheticLambda0
public final AsyncQueue.DelayedTask f$0;
@Override // java.lang.Runnable
public final void run() {
this.f$0.handleDelayElapsed();
}
{
this.f$0 = this;
}
}, j, TimeUnit.MILLISECONDS);
}
public void cancel() {
this.this$0.verifyIsCurrentThread();
ScheduledFuture scheduledFuture = this.scheduledFuture;
if (scheduledFuture != null) {
scheduledFuture.cancel(false);
markDone();
}
}
/* JADX INFO: Access modifiers changed from: private */
public void handleDelayElapsed() {
this.this$0.verifyIsCurrentThread();
if (this.scheduledFuture != null) {
markDone();
this.task.run();
}
}
private void markDone() {
Assert.hardAssert(this.scheduledFuture != null, "Caller should have verified scheduledFuture is non-null.", new Object[0]);
this.scheduledFuture = null;
this.this$0.removeDelayedTask(this);
}
}
/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class SynchronizedShutdownAwareExecutor implements Executor {
private final ScheduledThreadPoolExecutor internalExecutor;
private boolean isShuttingDown;
final AsyncQueue this$0;
private final Thread thread;
/* loaded from: classes2.dex */
class DelayedStartFactory implements Runnable, ThreadFactory {
private Runnable delegate;
private final CountDownLatch latch;
final SynchronizedShutdownAwareExecutor this$1;
private DelayedStartFactory(SynchronizedShutdownAwareExecutor synchronizedShutdownAwareExecutor) {
this.this$1 = synchronizedShutdownAwareExecutor;
this.latch = new CountDownLatch(1);
}
@Override // java.lang.Runnable
public void run() {
try {
this.latch.await();
} catch (InterruptedException unused) {
Thread.currentThread().interrupt();
}
this.delegate.run();
}
@Override // java.util.concurrent.ThreadFactory
public Thread newThread(Runnable runnable) {
Assert.hardAssert(this.delegate == null, "Only one thread may be created in an AsyncQueue.", new Object[0]);
this.delegate = runnable;
this.latch.countDown();
return this.this$1.thread;
}
}
SynchronizedShutdownAwareExecutor(AsyncQueue asyncQueue) {
this.this$0 = asyncQueue;
DelayedStartFactory delayedStartFactory = new DelayedStartFactory();
Thread newThread = java.util.concurrent.Executors.defaultThreadFactory().newThread(delayedStartFactory);
this.thread = newThread;
newThread.setName("FirestoreWorker");
newThread.setDaemon(true);
newThread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(this) { // from class: com.google.firebase.firestore.util.AsyncQueue$SynchronizedShutdownAwareExecutor$$ExternalSyntheticLambda0
public final AsyncQueue.SynchronizedShutdownAwareExecutor f$0;
@Override // java.lang.Thread.UncaughtExceptionHandler
public final void uncaughtException(Thread thread, Throwable th) {
this.f$0.m255x23fada6e(thread, th);
}
{
this.f$0 = this;
}
});
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(this, 1, delayedStartFactory, asyncQueue) { // from class: com.google.firebase.firestore.util.AsyncQueue.SynchronizedShutdownAwareExecutor.1
final SynchronizedShutdownAwareExecutor this$1;
final AsyncQueue val$this$0;
{
this.this$1 = this;
this.val$this$0 = asyncQueue;
}
@Override // java.util.concurrent.ThreadPoolExecutor
protected void afterExecute(Runnable runnable, Throwable th) {
super.afterExecute(runnable, th);
if (th == null && (runnable instanceof Future)) {
Future future = (Future) runnable;
try {
if (future.isDone()) {
future.get();
}
} catch (InterruptedException unused) {
Thread.currentThread().interrupt();
} catch (CancellationException unused2) {
} catch (ExecutionException e) {
th = e.getCause();
}
}
if (th != null) {
this.this$1.this$0.panic(th);
}
}
};
this.internalExecutor = scheduledThreadPoolExecutor;
scheduledThreadPoolExecutor.setKeepAliveTime(3L, TimeUnit.SECONDS);
this.isShuttingDown = false;
}
/* JADX INFO: Access modifiers changed from: package-private */
/* renamed from: lambda$new$0$com-google-firebase-firestore-util-AsyncQueue$SynchronizedShutdownAwareExecutor, reason: not valid java name */
public /* synthetic */ void m255x23fada6e(Thread thread, Throwable th) {
this.this$0.panic(th);
}
/* JADX INFO: Access modifiers changed from: private */
public boolean isShuttingDown() {
boolean z;
synchronized (this) {
z = this.isShuttingDown;
}
return z;
}
@Override // java.util.concurrent.Executor
public void execute(Runnable runnable) {
synchronized (this) {
if (!this.isShuttingDown) {
this.internalExecutor.execute(runnable);
}
}
}
/* JADX INFO: Access modifiers changed from: private */
public <T> Task<T> executeAndReportResult(final Callable<T> callable) {
final TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
try {
execute(new Runnable(taskCompletionSource, callable) { // from class: com.google.firebase.firestore.util.AsyncQueue$SynchronizedShutdownAwareExecutor$$ExternalSyntheticLambda2
public final TaskCompletionSource f$0;
public final Callable f$1;
@Override // java.lang.Runnable
public final void run() {
AsyncQueue.SynchronizedShutdownAwareExecutor.lambda$executeAndReportResult$1(this.f$0, this.f$1);
}
{
this.f$0 = taskCompletionSource;
this.f$1 = callable;
}
});
} catch (RejectedExecutionException unused) {
Logger.warn("AsyncQueue", "Refused to enqueue task after panic", new Object[0]);
}
return taskCompletionSource.getTask();
}
/* JADX INFO: Access modifiers changed from: package-private */
public static /* synthetic */ void lambda$executeAndReportResult$1(TaskCompletionSource taskCompletionSource, Callable callable) {
try {
taskCompletionSource.setResult(callable.call());
} catch (Exception e) {
taskCompletionSource.setException(e);
throw new RuntimeException(e);
}
}
/* JADX INFO: Access modifiers changed from: private */
public ScheduledFuture<?> schedule(Runnable runnable, long j, TimeUnit timeUnit) {
synchronized (this) {
if (this.isShuttingDown) {
return null;
}
return this.internalExecutor.schedule(runnable, j, timeUnit);
}
}
/* JADX INFO: Access modifiers changed from: private */
public void shutdownNow() {
this.internalExecutor.shutdownNow();
}
}
public void verifyIsCurrentThread() {
Thread currentThread = Thread.currentThread();
if (this.executor.thread == currentThread) {
return;
}
String name = this.executor.thread.getName();
long id = this.executor.thread.getId();
throw Assert.fail("We are running on the wrong thread. Expected to be on the AsyncQueue thread %s/%d but was %s/%d", name, Long.valueOf(id), currentThread.getName(), Long.valueOf(currentThread.getId()));
}
public <T> Task<T> enqueue(Callable<T> callable) {
return this.executor.executeAndReportResult(callable);
}
public Task<Void> enqueue(final Runnable runnable) {
return enqueue(new Callable(runnable) { // from class: com.google.firebase.firestore.util.AsyncQueue$$ExternalSyntheticLambda0
public final Runnable f$0;
@Override // java.util.concurrent.Callable
public final Object call() {
return AsyncQueue.lambda$enqueue$2(this.f$0);
}
{
this.f$0 = runnable;
}
});
}
/* JADX INFO: Access modifiers changed from: package-private */
public static /* synthetic */ Void lambda$enqueue$2(Runnable runnable) throws Exception {
runnable.run();
return null;
}
public boolean isShuttingDown() {
return this.executor.isShuttingDown();
}
public void enqueueAndForget(Runnable runnable) {
enqueue(runnable);
}
public DelayedTask enqueueAfterDelay(TimerId timerId, long j, Runnable runnable) {
if (this.timerIdsToSkip.contains(timerId)) {
j = 0;
}
DelayedTask createAndScheduleDelayedTask = createAndScheduleDelayedTask(timerId, j, runnable);
this.delayedTasks.add(createAndScheduleDelayedTask);
return createAndScheduleDelayedTask;
}
public void panic(final Throwable th) {
this.executor.shutdownNow();
new Handler(Looper.getMainLooper()).post(new Runnable(th) { // from class: com.google.firebase.firestore.util.AsyncQueue$$ExternalSyntheticLambda6
public final Throwable f$0;
@Override // java.lang.Runnable
public final void run() {
AsyncQueue.lambda$panic$3(this.f$0);
}
{
this.f$0 = th;
}
});
}
/* JADX INFO: Access modifiers changed from: package-private */
public static /* synthetic */ void lambda$panic$3(Throwable th) {
if (th instanceof OutOfMemoryError) {
OutOfMemoryError outOfMemoryError = new OutOfMemoryError("Firestore (24.3.0) ran out of memory. Check your queries to make sure they are not loading an excessive amount of data.");
outOfMemoryError.initCause(th);
throw outOfMemoryError;
}
throw new RuntimeException("Internal error in Cloud Firestore (24.3.0).", th);
}
private DelayedTask createAndScheduleDelayedTask(TimerId timerId, long j, Runnable runnable) {
DelayedTask delayedTask = new DelayedTask(timerId, System.currentTimeMillis() + j, runnable);
delayedTask.start(j);
return delayedTask;
}
/* JADX INFO: Access modifiers changed from: private */
public void removeDelayedTask(DelayedTask delayedTask) {
Assert.hardAssert(this.delayedTasks.remove(delayedTask), "Delayed task not found.", new Object[0]);
}
public Executor getExecutor() {
return this.executor;
}
}