Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge #5853
Browse files Browse the repository at this point in the history
5853: Closes #4466, #5278: Migration: Filter about:home tabs and rewrite about:reader URLs. r=grigoryk a=pocmo

For #4466 an #5278.

We are now:
- Skipping tabs that are pointing to `about:home` - Fenix can't handle that URL.
- Rewriting `about:reader` URLs to the original URL. Fenix can't handle `about:reader` URLs but can just load the original URL.

Co-authored-by: Sebastian Kaspari <s.kaspari@gmail.com>
  • Loading branch information
MozLando and pocmo committed Feb 10, 2020
2 parents 52bc2f4 + dca2779 commit af1ae47
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 7 deletions.
Expand Up @@ -4,6 +4,7 @@

package mozilla.components.support.migration

import android.net.Uri
import mozilla.components.browser.session.SessionManager
import mozilla.components.lib.crash.CrashReporter
import mozilla.components.support.base.log.logger.Logger
Expand Down Expand Up @@ -36,9 +37,9 @@ internal object FennecSessionMigration {

for (file in sessionFiles) {
try {
return StreamingSessionStoreParser.parse(file)
return StreamingSessionStoreParser.parse(file).filter(logger, crashReporter)
} catch (e: FileNotFoundException) {
logger.warn("FileNotFoundException while trying to parse sessopm stpre", e)
logger.warn("FileNotFoundException while trying to parse session store", e)
} catch (e: IOException) {
crashReporter.submitCaughtException(e)
logger.error("IOException while parsing Fennec session store", e)
Expand Down Expand Up @@ -73,3 +74,55 @@ internal object FennecSessionMigration {
return files
}
}

@Suppress("TooGenericExceptionCaught")
private fun Result.Success<SessionManager.Snapshot>.filter(
logger: Logger,
crashReporter: CrashReporter
): Result.Success<SessionManager.Snapshot> {
var selectedIndex = value.selectedSessionIndex

logger.debug("Filtering migrated sessions (size=${value.sessions.size}, index=$selectedIndex)")

val sessions = value.sessions.mapIndexedNotNull { index, item ->
when {
// We filter out about:home tabs since Fenix does not handle those URLs.
item.session.url == ABOUT_HOME -> {
logger.debug("- Filtering about:home URL")
if (index < selectedIndex) {
selectedIndex--
}
null
}

// We rewrite about:reader URLs to their actual URLs. Fenix does not handle about:reader
// URLs, but it can load the original URL.
item.session.url.startsWith(ABOUT_READER) -> {
try {
val url = Uri.decode(item.session.url.substring(ABOUT_READER.length))

logger.debug("- Rewriting about:reader URL (${item.session.url}): $url")

item.session.url = url
item
} catch (e: Exception) {
crashReporter.submitCaughtException(e)
null
}
}

else -> item
}
}

if (sessions.isEmpty()) {
selectedIndex = -1
}

logger.debug("Migrated sessions filtered (size=${sessions.size}, index=$selectedIndex)")

return Result.Success(SessionManager.Snapshot(sessions, selectedIndex))
}

private const val ABOUT_HOME = "about:home"
private const val ABOUT_READER = "about:reader?url="
Expand Up @@ -59,7 +59,7 @@ class FennecSessionMigrationTest {
}

snapshot.sessions[4].also {
assertEquals("about:reader?url=https%3A%2F%2Fwww.theverge.com%2F2019%2F9%2F18%2F20871860%2Fhuawei-mate-30-photos-videos-leak-watch-gt-2-fitness-band-tv-android-tablet-harmony-os",
assertEquals("https://www.theverge.com/2019/9/18/20871860/huawei-mate-30-photos-videos-leak-watch-gt-2-fitness-band-tv-android-tablet-harmony-os",
it.session.url)

assertEquals("Huawei’s Thursday event lineup apparently leaks in full",
Expand Down Expand Up @@ -124,8 +124,6 @@ class FennecSessionMigrationTest {
val profilePath = File(getTestPath("sessions"), "test-case2")
val result = FennecSessionMigration.migrate(profilePath, mock())

assertTrue(result is Result.Success)

assertTrue(result is Result.Success)
val snapshot = (result as Result.Success).value
assertEquals(2, snapshot.sessions.size)
Expand Down Expand Up @@ -154,11 +152,60 @@ class FennecSessionMigrationTest {

val result = FennecSessionMigration.migrate(profilePath, mock())

assertTrue(result is Result.Success)

assertTrue(result is Result.Success)
val snapshot = (result as Result.Success).value
assertEquals(21, snapshot.sessions.size)
assertEquals(12, snapshot.selectedSessionIndex)
}

/**
* We expect in this test run to:
* - Filter out the about:home tab.
* - Rewrite the about:reader tab to its original URL.
* - Rewrite the selected index after about:home was filtered out.
*/
@Test
fun `about-home and about-reader URLs`() {
val profilePath = File(getTestPath("sessions"), "about-urls")

val result = FennecSessionMigration.migrate(profilePath, mock())

assertTrue(result is Result.Success)
val snapshot = (result as Result.Success).value
assertEquals(2, snapshot.sessions.size)
assertEquals(1, snapshot.selectedSessionIndex)

snapshot.sessions[0].also {
assertEquals("https://www.spiegel.de/",
it.session.url)

assertEquals("DER SPIEGEL | Online-Nachrichten",
it.session.title)
}

snapshot.sessions[1].also {
assertEquals("https://www.spiegel.de/politik/deutschland/fdp-parteivorstand-spricht-lindner-nach-kemmerich-wahl-vertrauen-aus-a-47e0a21c-7617-4549-b6dc-716c0363cbc2",
it.session.url)

assertEquals("FDP-Parteivorstand spricht Lindner Vertrauen aus - DER SPIEGEL - Politik",
it.session.title)
}
}

/**
* We expect in this test run to:
* - Filter out the single about:home tab.
* - Reset the selected index to -1.
*/
@Test
fun `only about-home`() {
val profilePath = File(getTestPath("sessions"), "only-about-home")

val result = FennecSessionMigration.migrate(profilePath, mock())

assertTrue(result is Result.Success)
val snapshot = (result as Result.Success).value
assertEquals(0, snapshot.sessions.size)
assertEquals(-1, snapshot.selectedSessionIndex)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af1ae47

Please sign in to comment.