package com.google.firebase.firestore.remote; import com.google.firebase.firestore.model.SnapshotVersion; import com.google.firebase.firestore.model.mutation.Mutation; import com.google.firebase.firestore.model.mutation.MutationResult; import com.google.firebase.firestore.remote.Stream; import com.google.firebase.firestore.util.Assert; import com.google.firebase.firestore.util.AsyncQueue; import com.google.firebase.firestore.util.Preconditions; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import o.AQX; import o.C1151WuV; import o.GMq; import o.QwV; import o.mnR; /* loaded from: classes2.dex */ public class WriteStream extends AbstractStream { public static final QwV EMPTY_STREAM_TOKEN = QwV.a; protected boolean handshakeComplete; private QwV lastStreamToken; private final RemoteSerializer serializer; /* loaded from: classes2.dex */ public interface Callback extends Stream.StreamCallback { void onHandshakeComplete(); void onWriteResponse(SnapshotVersion snapshotVersion, List list); } @Override // com.google.firebase.firestore.remote.AbstractStream public /* bridge */ /* synthetic */ void inhibitBackoff() { super.inhibitBackoff(); } @Override // com.google.firebase.firestore.remote.AbstractStream public /* bridge */ /* synthetic */ boolean isOpen() { return super.isOpen(); } @Override // com.google.firebase.firestore.remote.AbstractStream public /* bridge */ /* synthetic */ boolean isStarted() { return super.isStarted(); } @Override // com.google.firebase.firestore.remote.AbstractStream public /* bridge */ /* synthetic */ void stop() { super.stop(); } /* JADX INFO: Access modifiers changed from: package-private */ public WriteStream(FirestoreChannel firestoreChannel, AsyncQueue asyncQueue, RemoteSerializer remoteSerializer, Callback callback) { super(firestoreChannel, GMq.d(), asyncQueue, AsyncQueue.TimerId.WRITE_STREAM_CONNECTION_BACKOFF, AsyncQueue.TimerId.WRITE_STREAM_IDLE, AsyncQueue.TimerId.HEALTH_CHECK_TIMEOUT, callback); this.handshakeComplete = false; this.lastStreamToken = EMPTY_STREAM_TOKEN; this.serializer = remoteSerializer; } @Override // com.google.firebase.firestore.remote.AbstractStream public void start() { this.handshakeComplete = false; super.start(); } @Override // com.google.firebase.firestore.remote.AbstractStream protected void tearDown() { if (this.handshakeComplete) { writeMutations(Collections.emptyList()); } } /* JADX INFO: Access modifiers changed from: package-private */ public void setLastStreamToken(QwV qwV) { this.lastStreamToken = (QwV) Preconditions.checkNotNull(qwV); } /* JADX INFO: Access modifiers changed from: package-private */ public void writeHandshake() { Assert.hardAssert(isOpen(), "Writing handshake requires an opened stream", new Object[0]); Assert.hardAssert(!this.handshakeComplete, "Handshake already completed", new Object[0]); writeRequest(mnR.d().a(this.serializer.databaseName()).build()); } /* JADX INFO: Access modifiers changed from: package-private */ public void writeMutations(List list) { Assert.hardAssert(isOpen(), "Writing mutations requires an opened stream", new Object[0]); Assert.hardAssert(this.handshakeComplete, "Handshake must be complete before writing mutations", new Object[0]); mnR.HBt d = mnR.d(); Iterator it = list.iterator(); while (it.hasNext()) { d.d(this.serializer.encodeMutation(it.next())); } d.b(this.lastStreamToken); writeRequest(d.build()); } @Override // com.google.firebase.firestore.remote.AbstractStream public void onNext(C1151WuV c1151WuV) { this.lastStreamToken = c1151WuV.streamToken_; if (!this.handshakeComplete) { this.handshakeComplete = true; ((Callback) this.listener).onHandshakeComplete(); return; } this.backoff.reset(); RemoteSerializer remoteSerializer = this.serializer; AQX aqx = c1151WuV.commitTime_; if (aqx == null) { aqx = AQX.c(); } SnapshotVersion decodeVersion = remoteSerializer.decodeVersion(aqx); int size = c1151WuV.writeResults_.size(); ArrayList arrayList = new ArrayList(size); for (int i = 0; i < size; i++) { arrayList.add(this.serializer.decodeMutationResult(c1151WuV.writeResults_.get(i), decodeVersion)); } ((Callback) this.listener).onWriteResponse(decodeVersion, arrayList); } /* JADX INFO: Access modifiers changed from: package-private */ public boolean isHandshakeComplete() { return this.handshakeComplete; } /* JADX INFO: Access modifiers changed from: package-private */ public QwV getLastStreamToken() { return this.lastStreamToken; } }