Skip to content

Commit 34e7358

Browse files
authored
Use stored specification rather than etag download check for https binary (#326)
1 parent 3314922 commit 34e7358

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

app/src/main/java/com/duckduckgo/app/httpsupgrade/api/HttpsUpgradeDataDownloader.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.duckduckgo.app.httpsupgrade.api
1818

1919
import com.duckduckgo.app.global.api.isCached
20+
import com.duckduckgo.app.global.db.AppDatabase
2021
import com.duckduckgo.app.global.store.BinaryDataStore
2122
import com.duckduckgo.app.httpsupgrade.HttpsUpgrader
2223
import 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

app/src/main/java/com/duckduckgo/app/httpsupgrade/model/HttpsBloomFilterSpec.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import android.arch.persistence.room.PrimaryKey
2121

2222

2323
@Entity(tableName = "https_bloom_filter_spec")
24-
class HttpsBloomFilterSpec(
24+
data class HttpsBloomFilterSpec(
2525
@PrimaryKey val id: Int = 1,
2626
val errorRate: Double,
2727
val totalEntries: Int,

0 commit comments

Comments
 (0)