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

fix: Rely upon the WebView for completing non-intercepted requests #34

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
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.wordpress.gutenberg

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

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

class DefaultGutenbergRequestInterceptor: GutenbergRequestInterceptor {
override fun interceptRequest(request: WebResourceRequest): WebResourceRequest {
return request
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 @@ -20,11 +20,6 @@ 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"
Expand Down Expand Up @@ -53,13 +48,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
}
Expand Down Expand Up @@ -99,38 +91,18 @@ class GutenbergView : WebView {
}

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
view: WebView,
request: WebResourceRequest
): WebResourceResponse? {
if (request?.url == null) {
if (request.url == null) {
return super.shouldInterceptRequest(view, request)
} else if(request.url.host?.contains("appassets.androidplatform.net") == true) {
} 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
}
} else if (requestInterceptor.canIntercept(request)) {
return requestInterceptor.handleRequest(request)
}

return super.shouldInterceptRequest(view, request)
}
}

Expand Down