15
15
package com .google .firebase .firestore .remote ;
16
16
17
17
import android .content .Context ;
18
+ import androidx .annotation .VisibleForTesting ;
18
19
import com .google .android .gms .common .GooglePlayServicesNotAvailableException ;
19
20
import com .google .android .gms .common .GooglePlayServicesRepairableException ;
20
21
import com .google .android .gms .security .ProviderInstaller ;
24
25
import com .google .firebase .firestore .util .AsyncQueue ;
25
26
import com .google .firebase .firestore .util .Executors ;
26
27
import com .google .firebase .firestore .util .Logger ;
28
+ import com .google .firebase .firestore .util .Supplier ;
27
29
import com .google .firestore .v1 .FirestoreGrpc ;
28
30
import io .grpc .CallCredentials ;
29
31
import io .grpc .CallOptions ;
@@ -41,11 +43,26 @@ public class GrpcCallProvider {
41
43
42
44
private static final String LOG_TAG = "GrpcCallProvider" ;
43
45
46
+ private static Supplier <ManagedChannelBuilder <?>> overrideChannelBuilderSupplier ;
47
+
44
48
private final Task <ManagedChannel > channelTask ;
45
49
private final AsyncQueue asyncQueue ;
46
50
47
51
private CallOptions callOptions ;
48
52
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
+
49
66
GrpcCallProvider (
50
67
AsyncQueue asyncQueue ,
51
68
Context context ,
@@ -88,12 +105,16 @@ private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) {
88
105
Logger .warn (LOG_TAG , "Failed to update ssl context: %s" , e );
89
106
}
90
107
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
+ }
97
118
}
98
119
99
120
// Ensure gRPC recovers from a dead connection. (Not typically necessary, as the OS will
0 commit comments