1
0
Fork 0

For #12544 - Prevent search engines mixup

Stricter synchronization by always using the same "loadedSearchEngines"
variable.
With "loadedSearchEngines" calling "refreshAsync()" we also get the fallback
engines to contain reddit and youtube (which are programatically added) and
also now we properly remember and display the engines added by user.
master
Mugurell 2020-07-14 16:38:56 +03:00
parent af3c232615
commit d449184faa
1 changed files with 16 additions and 22 deletions

View File

@ -7,7 +7,6 @@ package org.mozilla.fenix.components.searchengine
import android.content.Context import android.content.Context
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.async import kotlinx.coroutines.async
@ -33,14 +32,16 @@ import java.util.Locale
open class FenixSearchEngineProvider( open class FenixSearchEngineProvider(
private val context: Context private val context: Context
) : SearchEngineProvider, CoroutineScope by CoroutineScope(Job() + Dispatchers.IO) { ) : SearchEngineProvider, CoroutineScope by CoroutineScope(Job() + Dispatchers.IO) {
private val locationService: LocationService = if (Config.channel.isDebug) { private val locationService = with(MozillaLocationService(
LocationService.dummy() context,
} else { context.components.core.client,
MozillaLocationService( BuildConfig.MLS_TOKEN
context, )) {
context.components.core.client, if (Config.channel.isDebug || !this.hasRegionCached()) {
BuildConfig.MLS_TOKEN LocationService.dummy()
) } else {
this
}
} }
// We have two search engine types: one based on MLS reported region, one based only on Locale. // We have two search engine types: one based on MLS reported region, one based only on Locale.
@ -93,17 +94,6 @@ open class FenixSearchEngineProvider(
private var loadedSearchEngines = refreshAsync() private var loadedSearchEngines = refreshAsync()
// https://github.com/mozilla-mobile/fenix/issues/9935
// Create new getter that will return the fallback SearchEngineList if
// the main one hasn't completed yet
private val searchEngines: Deferred<SearchEngineList>
get() =
if (isRegionCachedByLocationService) {
loadedSearchEngines
} else {
fallbackEngines
}
fun getDefaultEngine(context: Context): SearchEngine { fun getDefaultEngine(context: Context): SearchEngine {
val engines = installedSearchEngines(context) val engines = installedSearchEngines(context)
val selectedName = context.settings().defaultSearchEngineName val selectedName = context.settings().defaultSearchEngineName
@ -117,7 +107,7 @@ open class FenixSearchEngineProvider(
*/ */
fun installedSearchEngines(context: Context): SearchEngineList = runBlocking { fun installedSearchEngines(context: Context): SearchEngineList = runBlocking {
val installedIdentifiers = installedSearchEngineIdentifiers(context) val installedIdentifiers = installedSearchEngineIdentifiers(context)
val engineList = searchEngines.await() val engineList = loadedSearchEngines.await()
engineList.copy( engineList.copy(
list = engineList.list.filter { list = engineList.list.filter {
@ -188,7 +178,11 @@ open class FenixSearchEngineProvider(
} }
private fun refreshAsync() = async { private fun refreshAsync() = async {
val engineList = baseSearchEngines.await() val engineList = if (isRegionCachedByLocationService) {
baseSearchEngines.await()
} else {
fallbackEngines.await()
}
val bundledList = bundledSearchEngines.await().list val bundledList = bundledSearchEngines.await().list
val customList = customSearchEngines.await().list val customList = customSearchEngines.await().list