@@ -18,8 +18,11 @@ package com.google.firebase.sessions
1818
1919import  android.content.Context 
2020import  android.util.Log 
21+ import  androidx.datastore.core.DataMigration 
2122import  androidx.datastore.core.DataStore 
23+ import  androidx.datastore.core.DataStoreFactory 
2224import  androidx.datastore.core.MultiProcessDataStoreFactory 
25+ import  androidx.datastore.core.Serializer 
2326import  androidx.datastore.core.handlers.ReplaceFileCorruptionHandler 
2427import  androidx.datastore.dataStoreFile 
2528import  com.google.android.datatransport.TransportFactory 
@@ -43,6 +46,7 @@ import dagger.BindsInstance
4346import  dagger.Component 
4447import  dagger.Module 
4548import  dagger.Provides 
49+ import  java.io.File 
4650import  javax.inject.Qualifier 
4751import  javax.inject.Singleton 
4852import  kotlin.coroutines.CoroutineContext 
@@ -137,7 +141,7 @@ internal interface FirebaseSessionsComponent {
137141        appContext :  Context ,
138142        @Blocking blockingDispatcher :  CoroutineContext ,
139143      ): DataStore <SessionConfigs > = 
140-         MultiProcessDataStoreFactory .create (
144+         createDataStore (
141145          serializer =  SessionConfigsSerializer ,
142146          corruptionHandler = 
143147            ReplaceFileCorruptionHandler  { ex -> 
@@ -154,7 +158,7 @@ internal interface FirebaseSessionsComponent {
154158        appContext :  Context ,
155159        @Blocking blockingDispatcher :  CoroutineContext ,
156160      ): DataStore <SessionData > = 
157-         MultiProcessDataStoreFactory .create (
161+         createDataStore (
158162          serializer =  SessionDataSerializer ,
159163          corruptionHandler = 
160164            ReplaceFileCorruptionHandler  { ex -> 
@@ -164,6 +168,36 @@ internal interface FirebaseSessionsComponent {
164168          scope =  CoroutineScope (blockingDispatcher),
165169          produceFile =  { appContext.dataStoreFile(" aqs/sessionDataStore.data"  ) },
166170        )
171+ 
172+       private  fun  <T > createDataStore (
173+         serializer :  Serializer <T >,
174+         corruptionHandler :  ReplaceFileCorruptionHandler <T >,
175+         migrations :  List <DataMigration <T >> = listOf(),
176+         scope :  CoroutineScope ,
177+         produceFile :  () ->  File ,
178+       ): DataStore <T > = 
179+         if  (loadDataStoreSharedCounter()) {
180+           MultiProcessDataStoreFactory .create(
181+             serializer,
182+             corruptionHandler,
183+             migrations,
184+             scope,
185+             produceFile,
186+           )
187+         } else  {
188+           DataStoreFactory .create(serializer, corruptionHandler, migrations, scope, produceFile)
189+         }
190+ 
191+       /* * This native library in unavailable in some conditions, for example, Robolectric tests */ 
192+       private  fun  loadDataStoreSharedCounter (): Boolean  = 
193+         try  {
194+           System .loadLibrary(" datastore_shared_counter"  )
195+           true 
196+         } catch  (_:  UnsatisfiedLinkError ) {
197+           false 
198+         } catch  (_:  SecurityException ) {
199+           false 
200+         }
167201    }
168202  }
169203}
0 commit comments