Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Improve loading URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 authored and mergify[bot] committed Nov 25, 2021
1 parent f6fda4c commit 309af24
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import mozilla.components.browser.engine.gecko.prompt.GeckoPromptDelegate
import mozilla.components.browser.engine.gecko.window.GeckoWindowRequest
import mozilla.components.browser.errorpages.ErrorType
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSession.LoadUrlFlags.Companion.ALLOW_JAVASCRIPT_URL
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.Settings
Expand Down Expand Up @@ -57,7 +58,6 @@ import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoSession.NavigationDelegate
import org.mozilla.geckoview.GeckoSession.PermissionDelegate
import org.mozilla.geckoview.GeckoSession.PermissionDelegate.ContentPermission
import org.mozilla.geckoview.GeckoSessionSettings
import org.mozilla.geckoview.WebRequestError
Expand Down Expand Up @@ -154,7 +154,7 @@ class GeckoEngineSession(
additionalHeaders: Map<String, String>?
) {
val scheme = Uri.parse(url).normalizeScheme().scheme
if (BLOCKED_SCHEMES.contains(scheme)) {
if (BLOCKED_SCHEMES.contains(scheme) && !shouldLoadJSSchemes(scheme, flags)) {
logger.error("URL scheme not allowed. Aborting load.")
return
}
Expand All @@ -165,7 +165,7 @@ class GeckoEngineSession(

val loader = GeckoSession.Loader()
.uri(url)
.flags(flags.value)
.flags(flags.getGeckoFlags())

if (additionalHeaders != null) {
loader.additionalHeaders(additionalHeaders)
Expand All @@ -180,6 +180,11 @@ class GeckoEngineSession(
Fact(Component.BROWSER_ENGINE_GECKO, Action.IMPLEMENTATION_DETAIL, "GeckoSession.load").collect()
}

private fun shouldLoadJSSchemes(
scheme: String?,
flags: LoadUrlFlags
) = scheme?.startsWith(JS_SCHEME) == true && flags.contains(ALLOW_JAVASCRIPT_URL)

/**
* See [EngineSession.loadData]
*/
Expand All @@ -206,7 +211,7 @@ class GeckoEngineSession(
// successfully loaded a page. Calling reload now would just reload
// about:blank. To prevent that we trigger the initial load again.
loadUrl(it.url, it.parent, it.flags, it.additionalHeaders)
} ?: geckoSession.reload(flags.value)
} ?: geckoSession.reload(flags.getGeckoFlags())
}

/**
Expand Down Expand Up @@ -1105,7 +1110,8 @@ class GeckoEngineSession(
internal const val PROGRESS_STOP = 100
internal const val MOZ_NULL_PRINCIPAL = "moz-nullprincipal:"
internal const val ABOUT_BLANK = "about:blank"
internal val BLOCKED_SCHEMES = listOf("content", "file", "resource") // See 1684761 and 1684947
internal const val JS_SCHEME = "javascript"
internal val BLOCKED_SCHEMES = listOf("content", "file", "resource", JS_SCHEME) // See 1684761 and 1684947

/**
* Provides an ErrorType corresponding to the error code provided.
Expand Down Expand Up @@ -1143,3 +1149,13 @@ class GeckoEngineSession(
}
}
}

/**
* Provides all gecko flags ignoring flags that only exists on AC.
**/
@VisibleForTesting
internal fun EngineSession.LoadUrlFlags.getGeckoFlags(): Int = if (contains(ALLOW_JAVASCRIPT_URL)) {
value - ALLOW_JAVASCRIPT_URL
} else {
value
}
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,10 @@ abstract class EngineSession(
const val BYPASS_CLASSIFIER: Int = 1 shl 4
const val LOAD_FLAGS_FORCE_ALLOW_DATA_URI: Int = 1 shl 5
const val LOAD_FLAGS_REPLACE_HISTORY: Int = 1 shl 6
const val ALLOW_JAVASCRIPT_URL: Int = 1 shl 16
internal const val ALL = BYPASS_CACHE + BYPASS_PROXY + EXTERNAL + ALLOW_POPUPS +
BYPASS_CLASSIFIER + LOAD_FLAGS_FORCE_ALLOW_DATA_URI + LOAD_FLAGS_REPLACE_HISTORY
BYPASS_CLASSIFIER + LOAD_FLAGS_FORCE_ALLOW_DATA_URI + LOAD_FLAGS_REPLACE_HISTORY +
ALLOW_JAVASCRIPT_URL

fun all() = LoadUrlFlags(ALL)
fun none() = LoadUrlFlags(NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import mozilla.components.browser.icons.BrowserIcons
import mozilla.components.browser.icons.IconRequest
import mozilla.components.concept.awesomebar.AwesomeBar
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarksStorage
import mozilla.components.feature.awesomebar.facts.emitBookmarkSuggestionClickedFact
Expand Down Expand Up @@ -72,7 +73,8 @@ class BookmarksStorageSuggestionProvider(
description = result.url,
editSuggestion = result.url,
onSuggestionClicked = {
loadUrlUseCase.invoke(result.url!!)
val flags = LoadUrlFlags.select(LoadUrlFlags.ALLOW_JAVASCRIPT_URL)
loadUrlUseCase.invoke(result.url!!, flags = flags)
emitBookmarkSuggestionClickedFact()
}
)
Expand Down

0 comments on commit 309af24

Please sign in to comment.