2019-02-12 20:13:09 +01:00
|
|
|
/* 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
|
|
|
|
|
|
|
|
import android.content.Context
|
2019-12-02 19:26:06 +01:00
|
|
|
import android.net.ConnectivityManager
|
2019-12-17 07:03:11 +01:00
|
|
|
import androidx.core.content.getSystemService
|
2019-02-12 20:13:09 +01:00
|
|
|
import mozilla.components.browser.errorpages.ErrorPages
|
|
|
|
import mozilla.components.browser.errorpages.ErrorType
|
|
|
|
import mozilla.components.concept.engine.EngineSession
|
|
|
|
import mozilla.components.concept.engine.request.RequestInterceptor
|
2019-05-14 23:17:58 +02:00
|
|
|
import org.mozilla.fenix.components.metrics.Event
|
2019-04-04 21:41:06 +02:00
|
|
|
import org.mozilla.fenix.ext.components
|
2019-12-17 07:03:11 +01:00
|
|
|
import org.mozilla.fenix.ext.isOnline
|
2020-01-22 21:53:26 +01:00
|
|
|
import org.mozilla.fenix.ext.settings
|
2019-02-12 20:13:09 +01:00
|
|
|
|
|
|
|
class AppRequestInterceptor(private val context: Context) : RequestInterceptor {
|
2019-10-02 06:45:04 +02:00
|
|
|
override fun onLoadRequest(
|
2019-12-20 14:27:19 +01:00
|
|
|
engineSession: EngineSession,
|
|
|
|
uri: String,
|
|
|
|
hasUserGesture: Boolean,
|
|
|
|
isSameDomain: Boolean
|
2019-10-02 06:45:04 +02:00
|
|
|
): RequestInterceptor.InterceptionResponse? {
|
2019-12-20 14:27:19 +01:00
|
|
|
adjustTrackingProtection(context, engineSession)
|
|
|
|
var result: RequestInterceptor.InterceptionResponse? = null
|
|
|
|
|
2019-08-23 01:56:13 +02:00
|
|
|
// WebChannel-driven authentication does not require a separate redirect interceptor.
|
2020-01-13 19:38:32 +01:00
|
|
|
@Suppress("ConstantConditionIf")
|
|
|
|
if (FeatureFlags.asFeatureWebChannelsDisabled) {
|
2019-12-20 14:27:19 +01:00
|
|
|
result = context.components.services.accountsAuthFeature.interceptor.onLoadRequest(
|
|
|
|
engineSession, uri, hasUserGesture, isSameDomain)
|
2019-08-23 01:56:13 +02:00
|
|
|
}
|
2019-12-20 14:27:19 +01:00
|
|
|
|
|
|
|
if (result == null) {
|
|
|
|
result = context.components.services.appLinksInterceptor.onLoadRequest(
|
|
|
|
engineSession, uri, hasUserGesture, isSameDomain)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2019-04-04 21:41:06 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 06:45:04 +02:00
|
|
|
private fun adjustTrackingProtection(context: Context, session: EngineSession) {
|
2019-09-24 19:33:46 +02:00
|
|
|
val trackingProtectionEnabled = context.settings().shouldUseTrackingProtection
|
2019-10-02 06:45:04 +02:00
|
|
|
if (!trackingProtectionEnabled) {
|
2019-05-14 19:15:00 +02:00
|
|
|
session.disableTrackingProtection()
|
2019-05-07 22:28:38 +02:00
|
|
|
} else {
|
2019-05-14 19:15:00 +02:00
|
|
|
val core = context.components.core
|
2019-05-07 22:28:38 +02:00
|
|
|
val policy = core.createTrackingProtectionPolicy(normalMode = true)
|
|
|
|
core.engine.settings.trackingProtectionPolicy = policy
|
2019-05-14 19:15:00 +02:00
|
|
|
session.enableTrackingProtection(policy)
|
2019-05-07 22:28:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 20:13:09 +01:00
|
|
|
override fun onErrorRequest(
|
|
|
|
session: EngineSession,
|
|
|
|
errorType: ErrorType,
|
|
|
|
uri: String?
|
|
|
|
): RequestInterceptor.ErrorResponse? {
|
2019-12-02 19:26:06 +01:00
|
|
|
val improvedErrorType = improveErrorType(errorType)
|
|
|
|
val riskLevel = getRiskLevel(improvedErrorType)
|
|
|
|
|
|
|
|
context.components.analytics.metrics.track(Event.ErrorPageVisited(improvedErrorType))
|
2019-10-30 19:08:45 +01:00
|
|
|
|
2020-01-22 21:53:26 +01:00
|
|
|
val errorPageUri = ErrorPages.createUrlEncodedErrorPage(
|
|
|
|
context = context,
|
|
|
|
errorType = improvedErrorType,
|
|
|
|
uri = uri,
|
|
|
|
htmlResource = riskLevel.htmlRes
|
2019-05-07 22:28:38 +02:00
|
|
|
)
|
2020-01-22 21:53:26 +01:00
|
|
|
|
|
|
|
return RequestInterceptor.ErrorResponse.Uri(errorPageUri)
|
2019-04-09 20:01:06 +02:00
|
|
|
}
|
|
|
|
|
2019-12-02 19:26:06 +01:00
|
|
|
/**
|
|
|
|
* Where possible, this will make the error type more accurate by including information not
|
|
|
|
* available to AC.
|
|
|
|
*/
|
|
|
|
private fun improveErrorType(errorType: ErrorType): ErrorType {
|
|
|
|
// This is not an ideal solution. For context, see:
|
|
|
|
// https://github.com/mozilla-mobile/android-components/pull/5068#issuecomment-558415367
|
2019-12-17 07:03:11 +01:00
|
|
|
|
|
|
|
val isConnected: Boolean = context.getSystemService<ConnectivityManager>()!!.isOnline()
|
2019-12-02 19:26:06 +01:00
|
|
|
|
|
|
|
return when {
|
|
|
|
errorType == ErrorType.ERROR_UNKNOWN_HOST && !isConnected -> ErrorType.ERROR_NO_INTERNET
|
|
|
|
else -> errorType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 19:08:45 +01:00
|
|
|
private fun getRiskLevel(errorType: ErrorType): RiskLevel = when (errorType) {
|
|
|
|
ErrorType.UNKNOWN,
|
|
|
|
ErrorType.ERROR_NET_INTERRUPT,
|
|
|
|
ErrorType.ERROR_NET_TIMEOUT,
|
|
|
|
ErrorType.ERROR_CONNECTION_REFUSED,
|
|
|
|
ErrorType.ERROR_UNKNOWN_SOCKET_TYPE,
|
|
|
|
ErrorType.ERROR_REDIRECT_LOOP,
|
|
|
|
ErrorType.ERROR_OFFLINE,
|
|
|
|
ErrorType.ERROR_NET_RESET,
|
|
|
|
ErrorType.ERROR_UNSAFE_CONTENT_TYPE,
|
|
|
|
ErrorType.ERROR_CORRUPTED_CONTENT,
|
|
|
|
ErrorType.ERROR_CONTENT_CRASHED,
|
|
|
|
ErrorType.ERROR_INVALID_CONTENT_ENCODING,
|
|
|
|
ErrorType.ERROR_UNKNOWN_HOST,
|
|
|
|
ErrorType.ERROR_MALFORMED_URI,
|
|
|
|
ErrorType.ERROR_FILE_NOT_FOUND,
|
|
|
|
ErrorType.ERROR_FILE_ACCESS_DENIED,
|
|
|
|
ErrorType.ERROR_PROXY_CONNECTION_REFUSED,
|
|
|
|
ErrorType.ERROR_UNKNOWN_PROXY_HOST,
|
2019-11-28 00:02:47 +01:00
|
|
|
ErrorType.ERROR_NO_INTERNET,
|
2019-10-30 19:08:45 +01:00
|
|
|
ErrorType.ERROR_UNKNOWN_PROTOCOL -> RiskLevel.Low
|
2019-04-09 20:01:06 +02:00
|
|
|
|
2019-10-30 19:08:45 +01:00
|
|
|
ErrorType.ERROR_SECURITY_BAD_CERT,
|
|
|
|
ErrorType.ERROR_SECURITY_SSL,
|
|
|
|
ErrorType.ERROR_PORT_BLOCKED -> RiskLevel.Medium
|
2019-04-09 20:01:06 +02:00
|
|
|
|
2019-10-30 19:08:45 +01:00
|
|
|
ErrorType.ERROR_SAFEBROWSING_HARMFUL_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_MALWARE_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_PHISHING_URI,
|
|
|
|
ErrorType.ERROR_SAFEBROWSING_UNWANTED_URI -> RiskLevel.High
|
2019-04-09 20:01:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:53:26 +01:00
|
|
|
internal enum class RiskLevel(val htmlRes: String) {
|
|
|
|
Low(LOW_AND_MEDIUM_RISK_ERROR_PAGES),
|
|
|
|
Medium(LOW_AND_MEDIUM_RISK_ERROR_PAGES),
|
|
|
|
High(HIGH_RISK_ERROR_PAGES),
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
internal const val LOW_AND_MEDIUM_RISK_ERROR_PAGES = "low_and_medium_risk_error_pages.html"
|
|
|
|
internal const val HIGH_RISK_ERROR_PAGES = "high_risk_error_pages.html"
|
2019-02-12 20:13:09 +01:00
|
|
|
}
|
|
|
|
}
|