@@ -21,6 +21,8 @@ import android.graphics.Bitmap
2121import android.net.Uri
2222import android.net.http.SslError
2323import android.os.Build
24+ import android.support.annotation.AnyThread
25+ import android.support.annotation.UiThread
2426import android.support.annotation.WorkerThread
2527import android.webkit.*
2628import androidx.core.net.toUri
@@ -32,6 +34,7 @@ import com.duckduckgo.app.statistics.pixels.Pixel.PixelName.HTTPS_UPGRADE_SITE_E
3234import com.duckduckgo.app.statistics.pixels.Pixel.PixelParameter
3335import timber.log.Timber
3436import javax.inject.Inject
37+ import kotlin.concurrent.thread
3538
3639
3740class BrowserWebViewClient @Inject constructor(
@@ -117,40 +120,52 @@ class BrowserWebViewClient @Inject constructor(
117120 return webViewRequestInterceptor.shouldIntercept(request, webView, currentUrl, webViewClientListener)
118121 }
119122
123+ @UiThread
120124 @Suppress(" OverridingDeprecatedMember" )
121125 override fun onReceivedError (view : WebView , errorCode : Int , description : String , failingUrl : String ) {
122126 if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
123127 val url = failingUrl.toUri()
124- if (isHttpsUpgradeSite(url)) {
125- reportHttpsUpgradeSiteError(url, statusCode = null , error = " WEB_RESOURCE_ERROR_$errorCode " )
126- }
128+ reportHttpsErrorIfInUpgradeList(url, statusCode = null , error = " WEB_RESOURCE_ERROR_$errorCode " )
127129 }
128130 super .onReceivedError(view, errorCode, description, failingUrl)
129131 }
130132
133+ @UiThread
131134 @TargetApi(Build .VERSION_CODES .M )
132135 override fun onReceivedError (view : WebView , request : WebResourceRequest , error : WebResourceError ) {
133- if (request.isForMainFrame && isHttpsUpgradeSite(request.url) ) {
134- reportHttpsUpgradeSiteError (request.url, statusCode = null , error = " WEB_RESOURCE_ERROR_${error.errorCode} " )
136+ if (request.isForMainFrame) {
137+ reportHttpsErrorIfInUpgradeList (request.url, statusCode = null , error = " WEB_RESOURCE_ERROR_${error.errorCode} " )
135138 }
136139 super .onReceivedError(view, request, error)
137140 }
138141
142+ @UiThread
139143 override fun onReceivedHttpError (view : WebView , request : WebResourceRequest , errorResponse : WebResourceResponse ) {
140- if (request.isForMainFrame && isHttpsUpgradeSite(request.url) ) {
141- reportHttpsUpgradeSiteError (request.url, errorResponse.statusCode, error = null )
144+ if (request.isForMainFrame) {
145+ reportHttpsErrorIfInUpgradeList (request.url, errorResponse.statusCode, error = null )
142146 }
143147 super .onReceivedHttpError(view, request, errorResponse)
144148 }
145149
150+ @UiThread
146151 override fun onReceivedSslError (view : WebView , handler : SslErrorHandler , error : SslError ) {
147152 val uri = error.url.toUri()
148- if (isHttpsUpgradeSite(uri)) {
149- reportHttpsUpgradeSiteError(uri, null , " SSL_ERROR_${error.primaryError} " )
150- }
153+ reportHttpsErrorIfInUpgradeList(uri, null , " SSL_ERROR_${error.primaryError} " )
151154 super .onReceivedSslError(view, handler, error)
152155 }
153156
157+ @AnyThread
158+ private fun reportHttpsErrorIfInUpgradeList (url : Uri , statusCode : Int? , error : String? ) {
159+
160+ if (! url.isHttps) return
161+
162+ thread {
163+ if (httpsUpgrader.isInUpgradeList(url)) {
164+ reportHttpsUpgradeSiteError(url, statusCode, error)
165+ }
166+ }
167+ }
168+
154169 private fun reportHttpsUpgradeSiteError (url : Uri , statusCode : Int? , error : String? ) {
155170 val params = mapOf (
156171 PixelParameter .URL to url.simpleUrl,
@@ -160,10 +175,6 @@ class BrowserWebViewClient @Inject constructor(
160175 pixel.fire(HTTPS_UPGRADE_SITE_ERROR , params)
161176 }
162177
163- private fun isHttpsUpgradeSite (url : Uri ): Boolean {
164- return url.isHttps && httpsUpgrader.shouldUpgrade(url)
165- }
166-
167178 /* *
168179 * Utility to function to execute a function, and then return true
169180 *
0 commit comments