Skip to content

Commit

Permalink
fix(android): error loading resource redirects webview to resource url (
Browse files Browse the repository at this point in the history
#1354)

* Add check for main frame

* add change files, fix redirect

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
millermk and lucasfernog authored Sep 11, 2024
1 parent 170095b commit 5915341
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-android-error-handling-page-visible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fixes Android webview error page flashing when a redirect to the app is performed.
5 changes: 5 additions & 0 deletions .changes/fix-request-error-handling-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fix navigation error handling to trigger custom protocol on Android.
36 changes: 26 additions & 10 deletions src/android/kotlin/RustWebViewClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import android.net.Uri
import android.webkit.*
import android.content.Context
import android.graphics.Bitmap
import android.os.Handler
import android.os.Looper
import androidx.webkit.WebViewAssetLoader

class RustWebViewClient(context: Context): WebViewClient() {
private val interceptedState = mutableMapOf<String, Boolean>()
var currentUrl: String = "about:blank"
var lastInterceptedUrl: Uri? = null
private var lastInterceptedUrl: Uri? = null
private var pendingUrlRedirect: String? = null

private val assetLoader = WebViewAssetLoader.Builder()
.setDomain(assetLoaderDomain())
Expand All @@ -24,6 +27,14 @@ class RustWebViewClient(context: Context): WebViewClient() {
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
pendingUrlRedirect?.let {
Handler(Looper.getMainLooper()).post {
view.loadUrl(it)
}
pendingUrlRedirect = null
return null
}

lastInterceptedUrl = request.url
return if (withAssetLoader()) {
assetLoader.shouldInterceptRequest(request.url)
Expand Down Expand Up @@ -53,22 +64,27 @@ class RustWebViewClient(context: Context): WebViewClient() {
}

override fun onPageFinished(view: WebView, url: String) {
return onPageLoaded(url)
onPageLoaded(url)
}

override fun onReceivedError(
view: WebView,
request: WebResourceRequest,
error: WebResourceError
) {
// we get a net::ERR_CONNECTION_REFUSED when an external URL redirects to a custom protocol
// e.g. oauth flow, because shouldInterceptRequest is not called on redirects
// so we must force retry here with loadUrl() to get a chance of the custom protocol to kick in
if (error.errorCode == ERROR_CONNECT && request.url != lastInterceptedUrl) {
view.loadUrl(request.url.toString())
} else {
super.onReceivedError(view, request, error)
}
// we get a net::ERR_CONNECTION_REFUSED when an external URL redirects to a custom protocol
// e.g. oauth flow, because shouldInterceptRequest is not called on redirects
// so we must force retry here with loadUrl() to get a chance of the custom protocol to kick in
if (error.errorCode == ERROR_CONNECT && request.isForMainFrame && request.url != lastInterceptedUrl) {
// prevent the default error page from showing
view.stopLoading()
// without this initial loadUrl the app is stuck
view.loadUrl(request.url.toString())
// ensure the URL is actually loaded - for some reason there's a race condition and we need to call loadUrl() again later
pendingUrlRedirect = request.url.toString()
} else {
super.onReceivedError(view, request, error)
}
}

companion object {
Expand Down

0 comments on commit 5915341

Please sign in to comment.