From 8f4d82fedf9bd3d845efab060d53d020921904f7 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Tue, 22 Oct 2024 20:21:16 -0400 Subject: [PATCH 1/6] fix: Rely upon the WebView for completing non-intercepted requests Previously, a manual request was created for _all_ requests. This was unnecessary and resulted in header values being unexpectedly discarded. --- .../gutenberg/GutenbergRequestInterceptor.kt | 8 ++-- .../org/wordpress/gutenberg/GutenbergView.kt | 38 +++++-------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt index cf06bb04..6193f72e 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt @@ -1,13 +1,15 @@ package org.wordpress.gutenberg import android.webkit.WebResourceRequest +import android.webkit.WebResourceResponse +import android.webkit.WebView public interface GutenbergRequestInterceptor { - fun interceptRequest(request: WebResourceRequest): WebResourceRequest + fun modifyRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? } class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor { - override fun interceptRequest(request: WebResourceRequest): WebResourceRequest { - return request + override fun modifyRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? { + return null } } \ No newline at end of file diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index 0d119088..67f86d9e 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -99,38 +99,18 @@ class GutenbergView : WebView { } override fun shouldInterceptRequest( - view: WebView?, - request: WebResourceRequest? + view: WebView, + request: WebResourceRequest ): WebResourceResponse? { - if (request?.url == null) { - return super.shouldInterceptRequest(view, request) - } else if(request.url.host?.contains("appassets.androidplatform.net") == true) { + val requestResponse = requestInterceptor.modifyRequest(view, request); + + if (requestResponse != null) { + return requestResponse + } else if (request.url.host?.contains("appassets.androidplatform.net") == true) { return assetLoader.shouldInterceptRequest(request.url) - } else { - val modifiedRequest = requestInterceptor.interceptRequest(request) - - try { - val okHttpRequest = Request.Builder() - .url(modifiedRequest.url!!.toString()) - .headers(modifiedRequest.requestHeaders.toHeaders()) - .build() - - val response: Response = httpClient.newCall(okHttpRequest).execute() - - val body = if(response.body != null) { response.body!! } else { return null } - val contentType = if(body.contentType() != null) { body.contentType() } else { return null } - - return WebResourceResponse( - contentType.toString(), - response.header("content-encoding", null), - body.byteStream() - ) - } catch (e: IOException) { - // We don't need to handle this ourselves, just tell the WebView that - // we weren't able to fetch the resource - return null - } } + + return super.shouldInterceptRequest(view, request) } } From 90c2c5d73e0e5ba6a681caba3651e3a3d8d8a078 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Tue, 22 Oct 2024 20:40:43 -0400 Subject: [PATCH 2/6] refactor: Remove unused imports --- .../src/main/java/org/wordpress/gutenberg/GutenbergView.kt | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index 67f86d9e..718e9010 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -20,11 +20,7 @@ import android.webkit.WebView import android.webkit.WebViewClient import androidx.webkit.WebViewAssetLoader import androidx.webkit.WebViewAssetLoader.AssetsPathHandler -import okhttp3.Headers.Companion.toHeaders import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.Response -import okio.IOException import org.json.JSONObject const val ASSET_URL = "https://appassets.androidplatform.net/assets/index.html" @@ -53,13 +49,10 @@ class GutenbergView : WebView { var requestInterceptor: GutenbergRequestInterceptor = DefaultGutenbergRequestInterceptor() - private var onFileChooserRequested: ((Intent, Int) -> Unit)? = null private var contentChangeListener: ContentChangeListener? = null private var editorDidBecomeAvailableListener: EditorAvailableListener? = null - private val httpClient = OkHttpClient() - fun setContentChangeListener(listener: ContentChangeListener) { contentChangeListener = listener } From b2ae11f9c3d2d8d4ab7770ec6a12bd77d5e1263f Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Tue, 22 Oct 2024 20:41:09 -0400 Subject: [PATCH 3/6] refactor: Remove redundant semicolon --- .../src/main/java/org/wordpress/gutenberg/GutenbergView.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index 718e9010..eca551a2 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -95,7 +95,7 @@ class GutenbergView : WebView { view: WebView, request: WebResourceRequest ): WebResourceResponse? { - val requestResponse = requestInterceptor.modifyRequest(view, request); + val requestResponse = requestInterceptor.modifyRequest(view, request) if (requestResponse != null) { return requestResponse From 75b6982bf1c96c6778dbaa6bfdd9bfc72b3a49fc Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Tue, 22 Oct 2024 21:04:53 -0400 Subject: [PATCH 4/6] refactor: Remove unused import --- .../src/main/java/org/wordpress/gutenberg/GutenbergView.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index eca551a2..367279cc 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -20,7 +20,6 @@ import android.webkit.WebView import android.webkit.WebViewClient import androidx.webkit.WebViewAssetLoader import androidx.webkit.WebViewAssetLoader.AssetsPathHandler -import okhttp3.OkHttpClient import org.json.JSONObject const val ASSET_URL = "https://appassets.androidplatform.net/assets/index.html" From 946b8c24e4e55e42304fa0821d27307915ef9cb8 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Thu, 24 Oct 2024 11:12:20 -0400 Subject: [PATCH 5/6] refactor: Remove unused view parameter This was added to mirror the `shouldInterceptRequest` method signature, but is unnecessary complexity. --- .../org/wordpress/gutenberg/GutenbergRequestInterceptor.kt | 4 ++-- .../src/main/java/org/wordpress/gutenberg/GutenbergView.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt index 6193f72e..dae4cb47 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt @@ -5,11 +5,11 @@ import android.webkit.WebResourceResponse import android.webkit.WebView public interface GutenbergRequestInterceptor { - fun modifyRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? + fun modifyRequest(request: WebResourceRequest): WebResourceResponse? } class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor { - override fun modifyRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? { + override fun modifyRequest(request: WebResourceRequest): WebResourceResponse? { return null } } \ No newline at end of file diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index 367279cc..4bf1cb2f 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -94,7 +94,7 @@ class GutenbergView : WebView { view: WebView, request: WebResourceRequest ): WebResourceResponse? { - val requestResponse = requestInterceptor.modifyRequest(view, request) + val requestResponse = requestInterceptor.modifyRequest(request) if (requestResponse != null) { return requestResponse From 189ca417f1ca130c579ef70f7006a6a67ae6b842 Mon Sep 17 00:00:00 2001 From: David Calhoun Date: Wed, 30 Oct 2024 11:39:32 -0400 Subject: [PATCH 6/6] refactor: Separate check and handle request methods Mirror the iOS method structure. --- .../wordpress/gutenberg/GutenbergRequestInterceptor.kt | 10 +++++++--- .../main/java/org/wordpress/gutenberg/GutenbergView.kt | 8 ++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt index dae4cb47..4bee1cd9 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergRequestInterceptor.kt @@ -2,14 +2,18 @@ package org.wordpress.gutenberg import android.webkit.WebResourceRequest import android.webkit.WebResourceResponse -import android.webkit.WebView public interface GutenbergRequestInterceptor { - fun modifyRequest(request: WebResourceRequest): WebResourceResponse? + fun canIntercept(request: WebResourceRequest): Boolean + fun handleRequest(request: WebResourceRequest): WebResourceResponse? } class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor { - override fun modifyRequest(request: WebResourceRequest): WebResourceResponse? { + override fun canIntercept(request: WebResourceRequest): Boolean { + return false + } + + override fun handleRequest(request: WebResourceRequest): WebResourceResponse? { return null } } \ No newline at end of file diff --git a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt index 4bf1cb2f..4c038fe7 100644 --- a/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt +++ b/Demo-Android/Gutenberg/src/main/java/org/wordpress/gutenberg/GutenbergView.kt @@ -94,12 +94,12 @@ class GutenbergView : WebView { view: WebView, request: WebResourceRequest ): WebResourceResponse? { - val requestResponse = requestInterceptor.modifyRequest(request) - - if (requestResponse != null) { - return requestResponse + if (request.url == null) { + return super.shouldInterceptRequest(view, request) } else if (request.url.host?.contains("appassets.androidplatform.net") == true) { return assetLoader.shouldInterceptRequest(request.url) + } else if (requestInterceptor.canIntercept(request)) { + return requestInterceptor.handleRequest(request) } return super.shouldInterceptRequest(view, request)