Skip to content

Commit 1cb406d

Browse files
committed
Enhance SecureStorageDatabaseFactory to load sqlcipher library asynchronously
1 parent e6ceeb4 commit 1cb406d

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/securestorage/SecureStorageDatabaseFactory.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,41 @@ class RealSecureStorageDatabaseFactory @Inject constructor(
4747
private val context: Context,
4848
private val keyProvider: SecureStorageKeyProvider,
4949
private val autofillFeature: AutofillFeature,
50-
) : SecureStorageDatabaseFactory {
50+
) : SecureStorageDatabaseFactory, LibraryLoader.LibraryLoaderListener {
5151
private var _database: SecureStorageDatabase? = null
5252

5353
private val mutex = Mutex()
5454

5555
init {
5656
logcat { "Loading the sqlcipher native library" }
5757
try {
58-
LibraryLoader.loadLibrary(context, "sqlcipher")
59-
logcat { "sqlcipher native library loaded ok" }
58+
LibraryLoader.loadLibrary(context, "sqlcipher", this)
6059
} catch (t: Throwable) {
61-
// error loading the library
62-
logcat(ERROR) { "Error loading sqlcipher library: ${t.asLog()}" }
60+
logcat(ERROR) { "Error when calling LibraryLoader.loadLibrary for sqlcipher: ${t.asLog()}" }
6361
}
6462
}
6563

6664
override suspend fun getDatabase(): SecureStorageDatabase? {
67-
return if (autofillFeature.createAsyncPreferences().isEnabled()) {
68-
getAsyncDatabase()
69-
} else {
70-
getDatabaseSynchronized()
65+
return try {
66+
if (autofillFeature.createAsyncPreferences().isEnabled()) {
67+
getAsyncDatabase()
68+
} else {
69+
getDatabaseSynchronized()
70+
}
71+
} catch (e: Exception) {
72+
logcat(ERROR) { "Error getting secure storage database instance: ${e.asLog()}" }
73+
null
7174
}
7275
}
7376

77+
override fun success() {
78+
logcat { "sqlcipher native library loaded ok" }
79+
}
80+
81+
override fun failure(throwable: Throwable) {
82+
logcat(ERROR) { "Error loading sqlcipher library: ${throwable.asLog()}" }
83+
}
84+
7485
@Synchronized
7586
private fun getDatabaseSynchronized(): SecureStorageDatabase? {
7687
return runBlocking {

0 commit comments

Comments
 (0)