Skip to content

Commit 534e5b3

Browse files
Revert "Remove overrideChannelBuilder" (#756)
1 parent 7533855 commit 534e5b3

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/remote/GrpcCallProvider.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.firestore.remote;
1616

1717
import android.content.Context;
18+
import androidx.annotation.VisibleForTesting;
1819
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
1920
import com.google.android.gms.common.GooglePlayServicesRepairableException;
2021
import com.google.android.gms.security.ProviderInstaller;
@@ -24,6 +25,7 @@
2425
import com.google.firebase.firestore.util.AsyncQueue;
2526
import com.google.firebase.firestore.util.Executors;
2627
import com.google.firebase.firestore.util.Logger;
28+
import com.google.firebase.firestore.util.Supplier;
2729
import com.google.firestore.v1.FirestoreGrpc;
2830
import io.grpc.CallCredentials;
2931
import io.grpc.CallOptions;
@@ -41,11 +43,26 @@ public class GrpcCallProvider {
4143

4244
private static final String LOG_TAG = "GrpcCallProvider";
4345

46+
private static Supplier<ManagedChannelBuilder<?>> overrideChannelBuilderSupplier;
47+
4448
private final Task<ManagedChannel> channelTask;
4549
private final AsyncQueue asyncQueue;
4650

4751
private CallOptions callOptions;
4852

53+
/**
54+
* Helper function to globally override the channel that RPCs use. Useful for testing when you
55+
* want to bypass SSL certificate checking.
56+
*
57+
* @param channelBuilderSupplier The supplier for a channel builder that is used to create gRPC
58+
* channels.
59+
*/
60+
@VisibleForTesting
61+
public static void overrideChannelBuilder(
62+
Supplier<ManagedChannelBuilder<?>> channelBuilderSupplier) {
63+
overrideChannelBuilderSupplier = channelBuilderSupplier;
64+
}
65+
4966
GrpcCallProvider(
5067
AsyncQueue asyncQueue,
5168
Context context,
@@ -88,12 +105,16 @@ private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) {
88105
Logger.warn(LOG_TAG, "Failed to update ssl context: %s", e);
89106
}
90107

91-
ManagedChannelBuilder<?> channelBuilder =
92-
ManagedChannelBuilder.forTarget(databaseInfo.getHost());
93-
if (!databaseInfo.isSslEnabled()) {
94-
// Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
95-
// It merely turns off SSL encryption.
96-
channelBuilder.usePlaintext();
108+
ManagedChannelBuilder<?> channelBuilder;
109+
if (overrideChannelBuilderSupplier != null) {
110+
channelBuilder = overrideChannelBuilderSupplier.get();
111+
} else {
112+
channelBuilder = ManagedChannelBuilder.forTarget(databaseInfo.getHost());
113+
if (!databaseInfo.isSslEnabled()) {
114+
// Note that the boolean flag does *NOT* switch the wire format from Protobuf to Plaintext.
115+
// It merely turns off SSL encryption.
116+
channelBuilder.usePlaintext();
117+
}
97118
}
98119

99120
// Ensure gRPC recovers from a dead connection. (Not typically necessary, as the OS will

0 commit comments

Comments
 (0)