From cdbe7983f57c2d1c679e0a94b77c903ba992b9fc Mon Sep 17 00:00:00 2001 From: mcarare Date: Thu, 16 Dec 2021 18:15:50 +0200 Subject: [PATCH] For #22879: Filter default engines when sending telemetry. Filter out custom engines that are not well known search domains. --- app/metrics.yaml | 2 +- .../org/mozilla/fenix/FenixApplication.kt | 21 ++++-- .../org/mozilla/fenix/ext/SearchEngine.kt | 33 +++++++++ .../org/mozilla/fenix/ext/SearchEngineTest.kt | 67 +++++++++++++++++++ 4 files changed, 117 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/mozilla/fenix/ext/SearchEngine.kt create mode 100644 app/src/test/java/org/mozilla/fenix/ext/SearchEngineTest.kt diff --git a/app/metrics.yaml b/app/metrics.yaml index 2b1f413a4..3821118f3 100644 --- a/app/metrics.yaml +++ b/app/metrics.yaml @@ -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: diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 74f964c01..8fa482e91 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -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") + } } } } diff --git a/app/src/main/java/org/mozilla/fenix/ext/SearchEngine.kt b/app/src/main/java/org/mozilla/fenix/ext/SearchEngine.kt new file mode 100644 index 000000000..d9dc513ff --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/ext/SearchEngine.kt @@ -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 diff --git a/app/src/test/java/org/mozilla/fenix/ext/SearchEngineTest.kt b/app/src/test/java/org/mozilla/fenix/ext/SearchEngineTest.kt new file mode 100644 index 000000000..6222783f7 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/SearchEngineTest.kt @@ -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()) + } +}