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
|
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,
|
2020-07-28 01:03:45 +02:00
|
|
|
lastUri: String?,
|
2019-12-20 14:27:19 +01:00
|
|
|
hasUserGesture: Boolean,
|
2020-06-27 15:43:34 +02:00
|
|
|
isSameDomain: Boolean,
|
2020-07-05 16:20:28 +02:00
|
|
|
isRedirect: Boolean,
|
2020-07-14 21:25:05 +02:00
|
|
|
isDirectNavigation: Boolean,
|
|
|
|
isSubframeRequest: Boolean
|
2019-10-02 06:45:04 +02:00
|
|
|
): RequestInterceptor.InterceptionResponse? {
|
2020-06-16 00:48:12 +02:00
|
|
|
return context.components.services.appLinksInterceptor
|
2020-07-14 21:25:05 +02:00
|
|
|
.onLoadRequest(
|
2020-08-11 23:24:38 +02:00
|
|
|
engineSession,
|
|
|
|
uri,
|
|
|
|
lastUri,
|
|
|
|
hasUserGesture,
|
|
|
|
isSameDomain,
|
|
|
|
isRedirect,
|
|
|
|
isDirectNavigation,
|
|
|
|
isSubframeRequest
|
2020-07-14 21:25:05 +02:00
|
|
|
)
|
2019-04-04 21:41:06 +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
|
|
|
}
|
|
|
|
}
|