diff --git a/app/src/main/java/org/mozilla/fenix/ext/String.kt b/app/src/main/java/org/mozilla/fenix/ext/String.kt index 659c6e326..304290303 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/String.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/String.kt @@ -4,40 +4,21 @@ package org.mozilla.fenix.ext +import android.content.Context import android.net.Uri import android.util.Patterns import android.webkit.URLUtil import androidx.core.net.toUri import kotlinx.coroutines.runBlocking import mozilla.components.lib.publicsuffixlist.PublicSuffixList +import mozilla.components.lib.publicsuffixlist.ext.urlToTrimmedHost import mozilla.components.support.ktx.android.net.hostWithoutCommonPrefixes import java.net.IDN -import java.net.MalformedURLException -import java.net.URL import java.util.Locale const val FILE_PREFIX = "file://" const val MAX_VALID_PORT = 65_535 -/** - * Replaces the keys with the values with the map provided. - */ -fun String.replace(pairs: Map): String { - var result = this - pairs.forEach { (l, r) -> result = result.replace(l, r) } - return result -} - -/** - * Tries to parse and get host part if this [String] is valid URL. - * Otherwise returns the string. - */ -fun String.tryGetHostFromUrl(): String = try { - URL(this).host -} catch (e: MalformedURLException) { - this -} - /** * Shortens URLs to be more user friendly. * @@ -110,15 +91,8 @@ private fun Uri.isIpv6(): Boolean { /** * Trim a host's prefix and suffix */ -fun String.urlToTrimmedHost(publicSuffixList: PublicSuffixList): String { - return try { - val host = toUri().hostWithoutCommonPrefixes ?: return this - runBlocking { - publicSuffixList.stripPublicSuffix(host).await() - } - } catch (e: MalformedURLException) { - this - } +fun String.urlToTrimmedHost(context: Context): String = runBlocking { + urlToTrimmedHost(context.components.publicSuffixList).await() } /** diff --git a/app/src/main/java/org/mozilla/fenix/library/history/HistoryDataSource.kt b/app/src/main/java/org/mozilla/fenix/library/history/HistoryDataSource.kt index f6e371968..2c5d52176 100644 --- a/app/src/main/java/org/mozilla/fenix/library/history/HistoryDataSource.kt +++ b/app/src/main/java/org/mozilla/fenix/library/history/HistoryDataSource.kt @@ -6,8 +6,8 @@ package org.mozilla.fenix.library.history import androidx.paging.ItemKeyedDataSource import mozilla.components.concept.storage.VisitInfo +import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl import org.mozilla.fenix.components.history.PagedHistoryProvider -import org.mozilla.fenix.ext.tryGetHostFromUrl class HistoryDataSource( private val historyProvider: PagedHistoryProvider diff --git a/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt b/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt index 4d429b9fd..7cc52ec42 100644 --- a/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt +++ b/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt @@ -7,7 +7,7 @@ package org.mozilla.fenix.trackingprotection import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.content.blocking.TrackerLog -import org.mozilla.fenix.ext.tryGetHostFromUrl +import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CROSS_SITE_TRACKING_COOKIES import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS diff --git a/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt b/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt index 3f222d5c9..f090416c9 100644 --- a/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt +++ b/app/src/test/java/org/mozilla/fenix/ext/StringTest.kt @@ -24,25 +24,10 @@ class StringTest { private val publicSuffixList = testContext.components.publicSuffixList - @Test - fun replace() { - val chars = mapOf("Mozilla Corporation" to "moco", "Mozilla Foundation" to "mofo") - val sentence = "Mozilla Corporation and Mozilla Foundation are committed to the future of the internet" - val new = sentence.replace(chars) - assertEquals(new, "moco and mofo are committed to the future of the internet") - } - - @Test - fun `Try Get Host From Url`() { - val urlTest = "http://www.example.com:1080/docs/resource1.html" - val new = urlTest.tryGetHostFromUrl() - assertEquals(new, "www.example.com") - } - @Test fun `Url To Trimmed Host`() { val urlTest = "http://www.example.com:1080/docs/resource1.html" - val new = urlTest.urlToTrimmedHost(testContext.components.publicSuffixList) + val new = urlTest.urlToTrimmedHost(testContext) assertEquals(new, "example") }