Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android Request Interception #30

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Demo-Android/Gutenberg/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.webkit)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.wordpress.gutenberg

import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse

public interface GutenbergRequestInterceptor {
fun canIntercept(request: WebResourceRequest): Boolean
fun handleRequest(request: WebResourceRequest): WebResourceResponse?
}

class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor {
override fun canIntercept(request: WebResourceRequest): Boolean {
return false
}

override fun handleRequest(request: WebResourceRequest): WebResourceResponse? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class GutenbergView : WebView {
private var editorDidBecomeAvailable: ((GutenbergView) -> Unit)? = null
var filePathCallback: ValueCallback<Array<Uri?>?>? = null
val pickImageRequestCode = 1

var requestInterceptor: GutenbergRequestInterceptor = DefaultGutenbergRequestInterceptor()

private var onFileChooserRequested: ((Intent, Int) -> Unit)? = null
private var contentChangeListener: ContentChangeListener? = null
private var editorDidBecomeAvailableListener: EditorAvailableListener? = null
Expand Down Expand Up @@ -88,14 +91,18 @@ class GutenbergView : WebView {
}

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
return if (request?.url != null) {
assetLoader.shouldInterceptRequest(request.url)
} else {
super.shouldInterceptRequest(view, request)
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)
}
}

Expand Down