Commit 6fe9bf5b authored by mimi89999's avatar mimi89999 Committed by Pier Angelo Vendrame
Browse files

Bug 1969937 - Add handling of blob URIs in Android toolbar URLRenderer. r=petru,android-reviewers

parent 04b24b99
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.ktx.android.net.isHttpOrHttps
import mozilla.components.support.ktx.kotlin.isIpv4OrIpv6

private const val BLOB_URL_PREFIX = "blob:"

/**
 * Asynchronous URL renderer.
 *
@@ -133,6 +135,7 @@ internal suspend fun getRegistrableDomainSpanInHost(
 *
 * @param url The complete URL to analyze
 * @param publicSuffixList The [PublicSuffixList] used to get the eTLD+1 for the host
 * @param allowBlobUnwrapping Whether to allow unwrapping blob URLs
 * @return A Pair of (startIndex, endIndex) for either:
 *         - The registrable domain's position within the URL, or
 *         - The host's position within the URL if no registrable domain was found, or
@@ -143,7 +146,21 @@ internal suspend fun getRegistrableDomainSpanInHost(
internal suspend fun getRegistrableDomainOrHostSpan(
    url: String,
    publicSuffixList: PublicSuffixList,
    allowBlobUnwrapping: Boolean = true,
): Pair<Int, Int>? {
    if (url.startsWith(BLOB_URL_PREFIX)) {
        if (!allowBlobUnwrapping) return null

        val innerUrl = url.substring(BLOB_URL_PREFIX.length)
        return getRegistrableDomainOrHostSpan(
            innerUrl,
            publicSuffixList,
            allowBlobUnwrapping = false,
        )?.let { (start, end) ->
            BLOB_URL_PREFIX.length + start to BLOB_URL_PREFIX.length + end
        }
    }

    val uri = url.toUri()
    if (!uri.isHttpOrHttps) return null

+24 −0
Original line number Diff line number Diff line
@@ -356,6 +356,30 @@ class URLRendererTest {
        }
    }

    @Test
    fun `GIVEN a blob URI WHEN getting registrable domain or host span THEN domain span is returned`() {
        runTestOnMain {
            val span = getRegistrableDomainOrHostSpan(
                url = "blob:https://www.mozilla.org/69a29afb-938c-4b9e-9fca-b2f79755047a",
                publicSuffixList = PublicSuffixList(testContext, Dispatchers.Unconfined),
            )

            assertEquals(17 to 28, span)
        }
    }

    @Test
    fun `GIVEN a blob URI with duplicated blob prefix WHEN getting registrable domain or host span THEN null is returned`() {
        runTestOnMain {
            val span = getRegistrableDomainOrHostSpan(
                url = "blob:blob:https://www.mozilla.org/69a29afb-938c-4b9e-9fca-b2f79755047a",
                publicSuffixList = PublicSuffixList(testContext, Dispatchers.Unconfined),
            )

            assertNull(span)
        }
    }

    @Test
    fun `GIVEN a simple URL WHEN rendering it THEN registrable domain is colored`() {
        runTestOnMain {