Skip to content

Commit

Permalink
Load sqlcipher library at process creation
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorvs committed Jan 21, 2025
1 parent d4e9852 commit 10d7e5e
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand All @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 10d7e5e

Please sign in to comment.