Skip to content

Commit 7819973

Browse files
committed
Fix the issue when profiles are lost when migration fails
1 parent 0e4810e commit 7819973

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

browser-mode/browser-mode-impl/src/main/java/com/duckduckgo/browsermode/impl/profile/RealWebViewProfileManager.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,20 @@ class RealWebViewProfileManager @Inject constructor(
105105

106106
return mutex.withLock {
107107
val oldName = activeProfiles[mode]?.name ?: return@withLock false
108-
val newIndex = dataStore.incrementProfileIndex(mode)
108+
val newIndex = dataStore.getProfileIndex(mode) + 1
109109
val newName = mode.prefix() + newIndex
110-
val migrationData = withContext(dispatchers.main()) {
110+
val (old, new) = withContext(dispatchers.main()) {
111111
val store = ProfileStore.getInstance()
112-
113-
val old = store.getOrCreateProfile(oldName)
114-
val new = store.getOrCreateProfile(newName)
115-
activeProfiles = activeProfiles + (mode to ActiveProfile(newName, new.webStorage, new.cookieManager))
116-
old to new
112+
store.getOrCreateProfile(oldName) to store.getOrCreateProfile(newName)
117113
}
118114

119115
if (mode == BrowserMode.REGULAR) {
120-
migrationManager.migrate(migrationData.first, migrationData.second)
116+
migrationManager.migrate(old, new)
121117
}
122118

119+
dataStore.incrementProfileIndex(mode)
120+
activeProfiles = activeProfiles + (mode to ActiveProfile(newName, new.webStorage, new.cookieManager))
121+
123122
appScope.launch { cleanupStaleProfiles() }
124123
true
125124
}
@@ -128,7 +127,9 @@ class RealWebViewProfileManager @Inject constructor(
128127
override suspend fun cleanupStaleProfiles() {
129128
initLatch.await()
130129

131-
if (fireModeAvailability.isAvailable()) {
130+
if (!fireModeAvailability.isAvailable()) return
131+
132+
mutex.withLock {
132133
withContext(dispatchers.main()) {
133134
val active = activeProfiles.values.map { it.name }.toSet()
134135
val store = ProfileStore.getInstance()

0 commit comments

Comments
 (0)