From 8c56a1d7b84d898714d7ca7d167bd60df033ab57 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 24 Aug 2021 21:33:29 +0530 Subject: Update AndroidX deps (#1491) --- .../autofillparser/PublicSuffixListCache.kt | 19 ++++++++++++++----- .../lib/publicsuffixlist/PublicSuffixList.kt | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'autofill-parser/src/main') diff --git a/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/PublicSuffixListCache.kt b/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/PublicSuffixListCache.kt index be3cbe66..2a35e8c7 100644 --- a/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/PublicSuffixListCache.kt +++ b/autofill-parser/src/main/java/com/github/androidpasswordstore/autofillparser/PublicSuffixListCache.kt @@ -5,6 +5,8 @@ package com.github.androidpasswordstore.autofillparser import android.content.Context +import android.net.InetAddresses +import android.os.Build import android.util.Patterns import kotlinx.coroutines.runBlocking import mozilla.components.lib.publicsuffixlist.PublicSuffixList @@ -17,7 +19,7 @@ private object PublicSuffixListCache { if (!::publicSuffixList.isInitialized) { publicSuffixList = PublicSuffixList(context) // Trigger loading the actual public suffix list, but don't block. - @Suppress("DeferredResultUnused") publicSuffixList.prefetch() + @Suppress("DeferredResultUnused") publicSuffixList.prefetchAsync() } return publicSuffixList } @@ -43,15 +45,21 @@ internal fun getPublicSuffixPlusOne( // We do not check whether the domain actually exists (actually, not even whether its TLD // exists). As long as we restrict ourselves to syntactically valid domain names, // getPublicSuffixPlusOne will return non-colliding results. - if (!Patterns.DOMAIN_NAME.matcher(domain).matches() || - Patterns.IP_ADDRESS.matcher(domain).matches() - ) { + if (!Patterns.DOMAIN_NAME.matcher(domain).matches() || isNumericAddress(domain)) { domain } else { getCanonicalSuffix(context, domain, customSuffixes) } } +private fun isNumericAddress(domain: String): Boolean { + return if (Build.VERSION.SDK_INT >= 29) { + InetAddresses.isNumericAddress(domain) + } else { + @Suppress("DEPRECATION") Patterns.IP_ADDRESS.matcher(domain).matches() + } +} + /** * Returns: * - [domain], if [domain] equals [suffix]; @@ -72,7 +80,8 @@ private suspend fun getCanonicalSuffix( customSuffixes: Sequence ): String { val publicSuffixList = PublicSuffixListCache.getOrCachePublicSuffixList(context) - val publicSuffixPlusOne = publicSuffixList.getPublicSuffixPlusOne(domain).await() ?: return domain + val publicSuffixPlusOne = + publicSuffixList.getPublicSuffixPlusOneAsync(domain).await() ?: return domain var longestSuffix = publicSuffixPlusOne for (customSuffix in customSuffixes) { val suffixPlusUpToOne = getSuffixPlusUpToOne(domain, customSuffix) ?: continue diff --git a/autofill-parser/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixList.kt b/autofill-parser/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixList.kt index 6f8c6d80..cdf9d8de 100644 --- a/autofill-parser/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixList.kt +++ b/autofill-parser/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixList.kt @@ -38,7 +38,7 @@ internal class PublicSuffixList( } /** Prefetch the public suffix list from disk so that it is available in memory. */ - fun prefetch(): Deferred = scope.async { data.run { Unit } } + fun prefetchAsync(): Deferred = scope.async { data.run {} } /** * Returns the public suffix and one more level; known as the registrable domain. Returns `null` @@ -55,7 +55,7 @@ internal class PublicSuffixList( * unexpected values are passed (e.g., a full URL, a domain with a trailing '/', etc) this may * return an incorrect result. */ - fun getPublicSuffixPlusOne(domain: String): Deferred = + fun getPublicSuffixPlusOneAsync(domain: String): Deferred = scope.async { when (val offset = data.getPublicSuffixOffset(domain)) { is PublicSuffixOffset.Offset -> -- cgit v1.2.3