diff --git a/autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/securestorage/SecureStorageDatabaseFactory.kt b/autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/securestorage/SecureStorageDatabaseFactory.kt index 539f25d6fa47..b5446dd461ff 100644 --- a/autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/securestorage/SecureStorageDatabaseFactory.kt +++ b/autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/securestorage/SecureStorageDatabaseFactory.kt @@ -17,7 +17,9 @@ package com.duckduckgo.autofill.impl.securestorage import android.content.Context +import androidx.lifecycle.LifecycleOwner import androidx.room.Room +import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver import com.duckduckgo.di.scopes.AppScope import com.duckduckgo.library.loader.LibraryLoader import com.duckduckgo.securestorage.store.db.ALL_MIGRATIONS @@ -26,6 +28,7 @@ import com.squareup.anvil.annotations.ContributesBinding import dagger.SingleInstanceIn import javax.inject.Inject import net.zetetic.database.sqlcipher.SupportOpenHelperFactory +import timber.log.Timber interface SecureStorageDatabaseFactory { fun getDatabase(): SecureStorageDatabase? @@ -36,9 +39,19 @@ interface SecureStorageDatabaseFactory { class RealSecureStorageDatabaseFactory @Inject constructor( private val context: Context, private val keyProvider: SecureStorageKeyProvider, -) : SecureStorageDatabaseFactory { +) : SecureStorageDatabaseFactory, MainProcessLifecycleObserver { private var _database: SecureStorageDatabase? = null + override fun onCreate(owner: LifecycleOwner) { + Timber.d("Loading the sqlcipher native library") + try { + LibraryLoader.loadLibrary(context, "sqlcipher") + } catch (t: Throwable) { + // error loading the library, return null db + Timber.e(t, "Error loading sqlcipher library") + } + } + override fun getDatabase(): SecureStorageDatabase? { // If we have already the DB instance then let's use it // use double-check locking optimisation @@ -48,14 +61,6 @@ class RealSecureStorageDatabaseFactory @Inject constructor( synchronized(this) { if (_database == null) { - // Ensure the library is loaded before database creation - try { - LibraryLoader.loadLibrary(context, "sqlcipher") - } catch (t: Throwable) { - // error loading the library, return null db - return null - } - if (keyProvider.canAccessKeyStore()) { _database = Room.databaseBuilder( context,