web: closing with a private tab active no longer restores to a dead tab bar
- Commit
17df7f701df38a2ed51d7cc197cae3d9f6001e67- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 23:22 UTC (5 days ago)
- Parents
2e80798
Restoring from prefs could leave the browser with no active tab: persistTabsSync filtered private tabs out of the saved list but saved the activeTabId unconditionally, so closing with a private tab active persisted an ID that restore could never find; restore then trusted it (and the legacy migration 'repaired' the invalid ID by assigning it again). activeTab was null with tabs non-empty — BrowserPage read that as a new tab, navigateActiveTab returned early and QuickAccess was a no-op: a dead address bar until the switcher was used. The writer now only persists an active ID among the saved tabs (falling back to the last saved tab), and restore validates the saved ID against the restored list at all four assignment sites — which also heals already-corrupted prefs on devices. Two regression tests failed pre-fix on both halves. History/bookmarks/downloads/omnibox were audited and cleared. Verified: :web:testDevUnitTest 358 tests pass; lint errors are pre-existing on untouched files.
Diff
web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.ktindex 0e1c482d82..8833be7810 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModel.kt@@ -356,15 +356,15 @@ class WebViewModel( ) { preExistingActive } else {- activeId ?: tabs.firstOrNull()?.id+ activeId?.takeIf { id -> tabs.any { it.id == id } } ?: tabs.firstOrNull()?.id } } else {- activeTabId = activeId ?: tabs.firstOrNull()?.id+ activeTabId = activeId?.takeIf { id -> tabs.any { it.id == id } } ?: tabs.firstOrNull()?.id } } else { tabs.clear() tabs.addAll(decodedSaved)- activeTabId = activeId ?: tabs.firstOrNull()?.id+ activeTabId = activeId?.takeIf { id -> tabs.any { it.id == id } } ?: tabs.firstOrNull()?.id } } preExisting.isNotEmpty() -> {@@ -393,7 +393,7 @@ class WebViewModel( } // Ensure active id is still valid after migration if (activeTabId == null || tabs.none { it.id == activeTabId }) {- activeTabId = activeId ?: tabs.firstOrNull()?.id+ activeTabId = activeId?.takeIf { id -> tabs.any { it.id == id } } ?: tabs.firstOrNull()?.id } }
web/src/main/java/com/vayunmathur/web/platform/WebViewModelPersistence.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelPersistence.kt b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelPersistence.ktindex 74ce28eed4..4deb96026f 100644--- a/web/src/main/java/com/vayunmathur/web/platform/WebViewModelPersistence.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/WebViewModelPersistence.kt@@ -23,9 +23,15 @@ internal fun WebViewModel.persistTabsSync() { runCatching { val sp = context.getSharedPreferences("web_prefs", Context.MODE_PRIVATE) val toSave = tabs.filter { !it.isPrivate }+ // Private tabs are not saved, so their id must not be saved as the+ // active id either: restore would point at a tab that is not in the+ // saved list and leave the browser with no active tab. Fall back to+ // the last saved tab, which is the most recent non-private one.+ val savedActiveId = activeTabId?.takeIf { id -> toSave.any { it.id == id } }+ ?: toSave.lastOrNull()?.id sp.edit { putString(savedTabsKey, json.encodeToString(toSave))- putString(activeTabKey, activeTabId)+ putString(activeTabKey, savedActiveId) } }.onFailure { e -> Log.error(TAG, "persistTabs failed", e) } }
web/src/test/java/com/vayunmathur/web/platform/RestoreAndFilterTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/platform/RestoreAndFilterTest.kt b/web/src/test/java/com/vayunmathur/web/platform/RestoreAndFilterTest.ktindex 9a9a652fd5..e001b26458 100644--- a/web/src/test/java/com/vayunmathur/web/platform/RestoreAndFilterTest.kt+++ b/web/src/test/java/com/vayunmathur/web/platform/RestoreAndFilterTest.kt@@ -140,6 +140,40 @@ class RestoreAndFilterTest { assertEquals(v.activeTabId, sp.getString("web_active_tab_id_roundtrip-window", null)) } + @Test fun privateActiveTabIsNotPersistedAsRestoredActiveId() {+ prefs().edit().clear().commit()+ val v = WebViewModel(stores, context, windowId = "private-active-window", incognito = false)+ createdVms += v+ await("initial tab") { v.tabs.isNotEmpty() }+ v.newTab(url = "https://normal.example/page")+ val normalId = v.activeTabId+ v.newTab(url = "https://secret.example/page", isPrivate = true)+ assertTrue(v.activeTab?.isPrivate == true)+ v.onClearedPersist()+ val decoded = json.decodeFromString<List<BrowserTab>>(+ prefs().getString("web_saved_tabs_private-active-window", null)!!+ )+ assertTrue(decoded.none { it.isPrivate })+ assertEquals(normalId, prefs().getString("web_active_tab_id_private-active-window", null))+ assertTrue(decoded.any { it.id == normalId })+ }++ @Test fun restoreIgnoresSavedActiveIdThatIsNotAmongSavedTabs() {+ val saved = listOf(+ BrowserTab(id = "t1", url = "https://one.example", title = "One"),+ BrowserTab(id = "t2", url = "https://two.example", title = "Two"),+ )+ prefs().edit()+ .putString("web_saved_tabs", json.encodeToString(saved))+ .putString("web_active_tab_id", "private-tab-not-saved")+ .commit()+ val v = WebViewModel(stores, context, windowId = WebViewModel.DEFAULT_WINDOW_ID, incognito = false)+ createdVms += v+ await("tabs restored") { v.tabs.map { it.id }.containsAll(listOf("t1", "t2")) }+ assertEquals("t1", v.activeTabId)+ assertEquals("https://one.example", v.activeTab?.url)+ }+ private fun request(url: String, mainFrame: Boolean): android.webkit.WebResourceRequest = object : android.webkit.WebResourceRequest { override fun getUrl(): Uri = Uri.parse(url)