@@ -24,6 +24,8 @@ namespace Firebase.VertexAI.Internal {
2424
2525// Contains internal helper methods for interacting with other Firebase libraries.
2626internal static class FirebaseInterops {
27+ // The cached fields for FirebaseApp reflection.
28+ private static PropertyInfo _dataCollectionProperty = null ;
2729
2830 // The various App Check types needed to retrieve the token, cached via reflection on startup.
2931 private static Type _appCheckType ;
@@ -48,6 +50,7 @@ internal static class FirebaseInterops {
4850 private const string authHeader = "Authorization" ;
4951
5052 static FirebaseInterops ( ) {
53+ InitializeAppReflection ( ) ;
5154 InitializeAppCheckReflection ( ) ;
5255 InitializeAuthReflection ( ) ;
5356 }
@@ -58,6 +61,72 @@ private static void LogError(string message) {
5861#endif
5962 }
6063
64+ // Cache the methods needed for FirebaseApp reflection.
65+ private static void InitializeAppReflection ( ) {
66+ try {
67+ _dataCollectionProperty = typeof ( FirebaseApp ) . GetProperty (
68+ "IsDataCollectionDefaultEnabled" ,
69+ BindingFlags . Instance | BindingFlags . NonPublic ) ;
70+ if ( _dataCollectionProperty == null ) {
71+ LogError ( "Could not find FirebaseApp.IsDataCollectionDefaultEnabled property via reflection." ) ;
72+ return ;
73+ }
74+ if ( _dataCollectionProperty . PropertyType != typeof ( bool ) ) {
75+ LogError ( "FirebaseApp.IsDataCollectionDefaultEnabled is not a bool, " +
76+ $ "but is { _dataCollectionProperty . PropertyType } ") ;
77+ return ;
78+ }
79+ } catch ( Exception e ) {
80+ LogError ( $ "Failed to initialize FirebaseApp reflection: { e } ") ;
81+ }
82+ }
83+
84+ // Gets the property FirebaseApp.IsDataCollectionDefaultEnabled.
85+ public static bool GetIsDataCollectionDefaultEnabled ( FirebaseApp firebaseApp ) {
86+ if ( firebaseApp == null || _dataCollectionProperty == null ) {
87+ return false ;
88+ }
89+
90+ try {
91+ return ( bool ) _dataCollectionProperty . GetValue ( firebaseApp ) ;
92+ } catch ( Exception e ) {
93+ LogError ( $ "Error accessing 'IsDataCollectionDefaultEnabled': { e } ") ;
94+ return false ;
95+ }
96+ }
97+
98+ // SDK version to use if unable to find it.
99+ private const string _unknownSdkVersion = "unknown" ;
100+ private static readonly Lazy < string > _sdkVersionFetcher = new ( ( ) => {
101+ try {
102+ // Get the type Firebase.VersionInfo from the assembly that defines FirebaseApp.
103+ Type versionInfoType = typeof ( FirebaseApp ) . Assembly . GetType ( "Firebase.VersionInfo" ) ;
104+ if ( versionInfoType == null ) {
105+ LogError ( "Firebase.VersionInfo type not found via reflection" ) ;
106+ return _unknownSdkVersion ;
107+ }
108+
109+ // Firebase.VersionInfo.SdkVersion
110+ PropertyInfo sdkVersionProperty = versionInfoType . GetProperty (
111+ "SdkVersion" ,
112+ BindingFlags . Static | BindingFlags . NonPublic ) ;
113+ if ( sdkVersionProperty == null ) {
114+ LogError ( "Firebase.VersionInfo.SdkVersion property not found via reflection." ) ;
115+ return _unknownSdkVersion ;
116+ }
117+
118+ return sdkVersionProperty . GetValue ( null ) as string ?? _unknownSdkVersion ;
119+ } catch ( Exception e ) {
120+ LogError ( $ "Error accessing SdkVersion via reflection: { e } ") ;
121+ return _unknownSdkVersion ;
122+ }
123+ } ) ;
124+
125+ // Gets the internal property Firebase.VersionInfo.SdkVersion
126+ internal static string GetVersionInfoSdkVersion ( ) {
127+ return _sdkVersionFetcher . Value ;
128+ }
129+
61130 // Cache the various types and methods needed for AppCheck token retrieval.
62131 private static void InitializeAppCheckReflection ( ) {
63132 const string firebaseAppCheckTypeName = "Firebase.AppCheck.FirebaseAppCheck, Firebase.AppCheck" ;
@@ -301,7 +370,6 @@ internal static async Task AddFirebaseTokensAsync(ClientWebSocket socket, Fireba
301370 socket . Options . SetRequestHeader ( authHeader , $ "Firebase { authToken } ") ;
302371 }
303372 }
304-
305373}
306374
307375}
0 commit comments