[fenix] For https://github.com/mozilla-mobile/fenix/issues/22879: Filter default engines when sending telemetry.

Filter out custom engines that are not well known search domains.
pull/600/head
mcarare 3 years ago committed by mergify[bot]
parent cb307f7bd3
commit 1facaaf775

@ -1842,7 +1842,7 @@ search.default_engine:
URL we use to build the search query for the search engine. For example:
https://mysearchengine.com/?query=%s. If it's a custom search engine
(defined: https://github.com/mozilla-mobile/fenix/issues/1607) the value
will be "custom"
will not be set.
send_in_pings:
- metrics
bugs:

@ -80,6 +80,8 @@ import org.mozilla.fenix.components.Core
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MozillaProductDetector
import org.mozilla.fenix.components.toolbar.ToolbarPosition
import org.mozilla.fenix.ext.isCustomEngine
import org.mozilla.fenix.ext.isKnownSearchDomain
import org.mozilla.fenix.perf.MarkersActivityLifecycleCallbacks
import org.mozilla.fenix.utils.Settings
@ -643,11 +645,20 @@ open class FenixApplication : LocaleAwareApplication(), Provider {
}
browserStore.waitForSelectedOrDefaultSearchEngine { searchEngine ->
if (searchEngine != null) {
SearchDefaultEngine.apply {
code.set(searchEngine.id)
name.set(searchEngine.name)
searchUrl.set(searchEngine.buildSearchUrl(""))
searchEngine?.let {
val sendSearchUrl =
!searchEngine.isCustomEngine() || searchEngine.isKnownSearchDomain()
if (sendSearchUrl) {
SearchDefaultEngine.apply {
code.set(searchEngine.id)
name.set(searchEngine.name)
searchUrl.set(searchEngine.buildSearchUrl(""))
}
} else {
SearchDefaultEngine.apply {
code.set(searchEngine.id)
name.set("custom")
}
}
}
}

@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.ext
import mozilla.components.browser.state.search.SearchEngine
// List of well known search domains, taken from
// https://searchfox.org/mozilla-central/source/toolkit/components/search/SearchService.jsm#2405
private val wellKnownSearchDomains = setOf(
"aol",
"ask",
"baidu",
"bing",
"duckduckgo",
"google",
"yahoo",
"yandex",
"startpage"
)
/**
* Whether or not the search engine is a custom engine added by the user.
*/
fun SearchEngine.isCustomEngine(): Boolean =
this.type == SearchEngine.Type.CUSTOM
/**
* Whether or not the search engine is a known search domain.
*/
fun SearchEngine.isKnownSearchDomain(): Boolean =
this.resultUrls[0].findAnyOf(wellKnownSearchDomains, 0, true) != null

@ -0,0 +1,67 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.ext
import mozilla.components.browser.state.search.SearchEngine
import mozilla.components.support.test.mock
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.util.UUID
class SearchEngineTest {
@Test
fun `custom search engines are identified correctly`() {
val searchEngine = SearchEngine(
id = UUID.randomUUID().toString(),
name = "Not custom",
icon = mock(),
type = SearchEngine.Type.BUNDLED,
resultUrls = listOf(
"https://www.startpage.com/sp/search?q={searchTerms}"
)
)
val customSearchEngine = SearchEngine(
id = UUID.randomUUID().toString(),
name = "Custom",
icon = mock(),
type = SearchEngine.Type.CUSTOM,
resultUrls = listOf(
"https://www.startpage.com/sp/search?q={searchTerms}"
)
)
assertFalse(searchEngine.isCustomEngine())
assertTrue(customSearchEngine.isCustomEngine())
}
@Test
fun `well known search engines are identified correctly`() {
val searchEngine = SearchEngine(
id = UUID.randomUUID().toString(),
name = "Not well known",
icon = mock(),
type = SearchEngine.Type.BUNDLED,
resultUrls = listOf(
"https://www.random.com/sp/search?q={searchTerms}"
)
)
val wellKnownSearchEngine = SearchEngine(
id = UUID.randomUUID().toString(),
name = "Well known",
icon = mock(),
type = SearchEngine.Type.CUSTOM,
resultUrls = listOf(
"https://www.startpage.com/sp/search?q={searchTerms}"
)
)
assertFalse(searchEngine.isKnownSearchDomain())
assertTrue(wellKnownSearchEngine.isKnownSearchDomain())
}
}
Loading…
Cancel
Save