1717package com.duckduckgo.app.httpsupgrade.api
1818
1919import com.duckduckgo.app.global.api.isCached
20+ import com.duckduckgo.app.global.db.AppDatabase
2021import com.duckduckgo.app.global.store.BinaryDataStore
2122import com.duckduckgo.app.httpsupgrade.HttpsUpgrader
2223import com.duckduckgo.app.httpsupgrade.db.HttpsBloomFilterSpecDao
@@ -34,7 +35,8 @@ class HttpsUpgradeDataDownloader @Inject constructor(
3435 private val httpsUpgrader : HttpsUpgrader ,
3536 private val httpsBloomSpecDao : HttpsBloomFilterSpecDao ,
3637 private val whitelistDao : HttpsWhitelistDao ,
37- private val binaryDataStore : BinaryDataStore
38+ private val binaryDataStore : BinaryDataStore ,
39+ private val appDatabase : AppDatabase
3840) {
3941
4042 fun download (): Completable {
@@ -53,29 +55,31 @@ class HttpsUpgradeDataDownloader @Inject constructor(
5355
5456 private fun downloadBloomFilter (specification : HttpsBloomFilterSpec ): Completable {
5557 return fromAction {
58+
5659 Timber .d(" Downloading https bloom filter binary" )
57- val call = service.httpsBloomFilter()
58- val response = call.execute()
59- val fileName = HTTPS_BINARY_FILE
6060
61- if (response.isCached && binaryDataStore.verifyCheckSum(fileName , specification.sha256)) {
62- Timber .d(" Https bloom data already cached and stored for this spec" )
61+ if (specification == httpsBloomSpecDao.get() && binaryDataStore.verifyCheckSum(HTTPS_BINARY_FILE , specification.sha256)) {
62+ Timber .d(" Https bloom data already stored for this spec" )
6363 return @fromAction
6464 }
6565
66+ val call = service.httpsBloomFilter()
67+ val response = call.execute()
6668 if (! response.isSuccessful) {
6769 throw IOException (" Status: ${response.code()} - ${response.errorBody()?.string()} " )
6870 }
6971
7072 val bytes = response.body()!! .bytes()
7173 if (! binaryDataStore.verifyCheckSum(bytes, specification.sha256)) {
72- throw IOException (" Https binary has incorrect checksum, throwisng away file" )
74+ throw IOException (" Https binary has incorrect checksum, throwing away file" )
7375 }
7476
7577 Timber .d(" Updating https bloom data store with new data" )
76- httpsBloomSpecDao.insert(specification)
77- binaryDataStore.saveData(fileName, bytes)
78- httpsUpgrader.reloadData()
78+ appDatabase.runInTransaction {
79+ httpsBloomSpecDao.insert(specification)
80+ binaryDataStore.saveData(HTTPS_BINARY_FILE , bytes)
81+ httpsUpgrader.reloadData()
82+ }
7983 }
8084 }
8185
0 commit comments