Skip to content

Commit

Permalink
fixup! Bug 40925: Implemented the Security Level component
Browse files Browse the repository at this point in the history
Bug 43128: Use DuckDuckGo HTML on the Safest security level for Android
  • Loading branch information
cypherpunks1 authored and PieroV committed Sep 12, 2024
1 parent 89777aa commit 40a262a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.state.selectedOrDefaultSearchEngine
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.feature.search.ext.buildSearchUrl
import mozilla.components.feature.session.SessionUseCases
import mozilla.components.feature.tabs.TabsUseCases
Expand All @@ -25,6 +27,7 @@ class SearchUseCases(
store: BrowserStore,
tabsUseCases: TabsUseCases,
sessionUseCases: SessionUseCases,
settings: Settings? = null,
) {
interface SearchUseCase {
/**
Expand All @@ -41,6 +44,7 @@ class SearchUseCases(
private val store: BrowserStore,
private val tabsUseCases: TabsUseCases,
private val sessionUseCases: SessionUseCases,
private val settings: Settings? = null,
) : SearchUseCase {
private val logger = Logger("DefaultSearchUseCase")

Expand Down Expand Up @@ -72,9 +76,15 @@ class SearchUseCases(
flags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none(),
additionalHeaders: Map<String, String>? = null,
) {
var securityLevel: Int
try {
securityLevel = settings?.torSecurityLevel ?: 0
} catch (e: UnsupportedSettingException) {
securityLevel = 0
}
val searchUrl = searchEngine?.let {
searchEngine.buildSearchUrl(searchTerms)
} ?: store.state.search.selectedOrDefaultSearchEngine?.buildSearchUrl(searchTerms)
searchEngine.buildSearchUrl(searchTerms, securityLevel)
} ?: store.state.search.selectedOrDefaultSearchEngine?.buildSearchUrl(searchTerms, securityLevel)

if (searchUrl == null) {
logger.warn("No default search engine available to perform search")
Expand Down Expand Up @@ -124,6 +134,7 @@ class SearchUseCases(
private val store: BrowserStore,
private val tabsUseCases: TabsUseCases,
private val isPrivate: Boolean,
private val settings: Settings? = null,
) : SearchUseCase {
private val logger = Logger("NewTabSearchUseCase")

Expand Down Expand Up @@ -161,9 +172,15 @@ class SearchUseCases(
flags: EngineSession.LoadUrlFlags = EngineSession.LoadUrlFlags.none(),
additionalHeaders: Map<String, String>? = null,
) {
var securityLevel: Int
try {
securityLevel = settings?.torSecurityLevel ?: 0
} catch (e: UnsupportedSettingException) {
securityLevel = 0
}
val searchUrl = searchEngine?.let {
searchEngine.buildSearchUrl(searchTerms)
} ?: store.state.search.selectedOrDefaultSearchEngine?.buildSearchUrl(searchTerms)
searchEngine.buildSearchUrl(searchTerms, securityLevel)
} ?: store.state.search.selectedOrDefaultSearchEngine?.buildSearchUrl(searchTerms, securityLevel)

if (searchUrl == null) {
logger.warn("No default search engine available to perform search")
Expand Down Expand Up @@ -310,15 +327,15 @@ class SearchUseCases(
}

val defaultSearch: DefaultSearchUseCase by lazy {
DefaultSearchUseCase(store, tabsUseCases, sessionUseCases)
DefaultSearchUseCase(store, tabsUseCases, sessionUseCases, settings)
}

val newTabSearch: NewTabSearchUseCase by lazy {
NewTabSearchUseCase(store, tabsUseCases, false)
NewTabSearchUseCase(store, tabsUseCases, false, settings)
}

val newPrivateTabSearch: NewTabSearchUseCase by lazy {
NewTabSearchUseCase(store, tabsUseCases, true)
NewTabSearchUseCase(store, tabsUseCases, true, settings)
}

val addSearchEngine: AddNewSearchEngineUseCase by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ fun SearchEngine.buildSuggestionsURL(query: String): String? {
/**
* Builds a URL to search for the given search terms with this search engine.
*/
fun SearchEngine.buildSearchUrl(searchTerm: String): String {
fun SearchEngine.buildSearchUrl(searchTerm: String, securityLevel: Int = 0): String {
val builder = SearchUrlBuilder(this)
return builder.buildSearchUrl(searchTerm)
return builder.buildSearchUrl(searchTerm, securityLevel)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ private const val OS_PARAM_OPTIONAL = "\\{" + "(?:\\w+:)?\\w+?" + "\\}"
internal class SearchUrlBuilder(
private val searchEngine: SearchEngine,
) {
fun buildSearchUrl(searchTerms: String): String {
fun buildSearchUrl(searchTerms: String, securityLevel: Int): String {
// The parser should have put the best URL for this device at the beginning of the list.
val template = searchEngine.resultUrls[0]
var template = searchEngine.resultUrls[0]
if (securityLevel == 1 && (searchEngine.id == "ddg" || searchEngine.id == "ddg-onion")) {
template = template.replaceFirst("/?", "/html/?")
}
return buildUrl(template, searchTerms)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class UseCases(
store,
tabsUseCases,
sessionUseCases,
engine.settings,
)
}

Expand Down

0 comments on commit 40a262a

Please sign in to comment.