-
Notifications
You must be signed in to change notification settings - Fork 1
[Android] Replace JavascriptInterface with WebMessageListener #424
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
Open
kiftio
wants to merge
1
commit into
main
Choose a base branch
from
dk/web-message-listener
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,14 +11,25 @@ import android.webkit.WebResourceRequest | |
| import android.webkit.WebResourceResponse | ||
| import android.webkit.WebView | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.webkit.WebMessageCompat | ||
| import com.shopify.checkoutkit.ShopifyCheckoutKit.log | ||
| import java.util.concurrent.CountDownLatch | ||
|
|
||
| internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = null) : | ||
| BaseWebView(context, attributeSet) { | ||
| internal class CheckoutWebView private constructor( | ||
| context: Context, | ||
| attributeSet: AttributeSet?, | ||
| private val webMessageTransport: WebMessageTransport, | ||
| ) : BaseWebView(context, attributeSet) { | ||
|
|
||
| constructor(context: Context, attributeSet: AttributeSet? = null) : | ||
| this(context, attributeSet, AndroidXWebMessageTransport) | ||
|
|
||
| internal constructor(context: Context, webMessageTransport: WebMessageTransport) : | ||
| this(context, null, webMessageTransport) | ||
|
|
||
| private var listener = CheckoutWebViewListener(NoopCheckoutListener()) | ||
| private val embeddedCheckoutProtocol = EmbeddedCheckoutProtocolBridge(this) | ||
| private var webMessageListenerAttached = false | ||
| private var loadComplete = false | ||
| internal var isPresented = false | ||
| private set | ||
|
|
@@ -27,7 +38,7 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
|
|
||
| init { | ||
| webViewClient = CheckoutWebViewClient() | ||
| addJavascriptInterface(embeddedCheckoutProtocol, EmbeddedCheckoutProtocolBridge.INTERFACE_NAME) | ||
| attachProtocolBridge() | ||
| settings.userAgentString = "${settings.userAgentString} ${userAgentSuffix()}" | ||
| } | ||
|
|
||
|
|
@@ -53,14 +64,14 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
|
|
||
| override fun onAttachedToWindow() { | ||
| super.onAttachedToWindow() | ||
| log.d(LOG_TAG, "Attached to window. Adding JavaScript interfaces.") | ||
| addJavascriptInterface(embeddedCheckoutProtocol, EmbeddedCheckoutProtocolBridge.INTERFACE_NAME) | ||
| log.d(LOG_TAG, "Attached to window. Adding protocol bridge.") | ||
| attachProtocolBridge() | ||
| } | ||
|
|
||
| override fun onDetachedFromWindow() { | ||
| super.onDetachedFromWindow() | ||
| log.d(LOG_TAG, "Detached from window. Removing JavaScript interfaces.") | ||
| removeJavascriptInterface(EmbeddedCheckoutProtocolBridge.INTERFACE_NAME) | ||
| log.d(LOG_TAG, "Detached from window. Removing protocol bridge.") | ||
| detachProtocolBridge() | ||
| } | ||
|
|
||
| fun loadCheckout(url: String, isPreload: Boolean = false) { | ||
|
|
@@ -78,6 +89,50 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
| } | ||
| } | ||
|
|
||
| internal fun receiveWebMessage(message: String?, isMainFrame: Boolean = true) { | ||
| if (!isMainFrame) { | ||
| log.d(LOG_TAG, "Ignoring ECP WebMessage from a child frame.") | ||
| return | ||
| } | ||
|
|
||
| if (message == null) { | ||
| log.d(LOG_TAG, "Ignoring ECP WebMessage with null payload.") | ||
| return | ||
| } | ||
|
|
||
| embeddedCheckoutProtocol.receiveMessage(message) | ||
| } | ||
|
|
||
| private fun attachProtocolBridge() { | ||
| if (webMessageListenerAttached) return | ||
|
|
||
| log.d(LOG_TAG, "Adding ECP WebMessageListener bridge.") | ||
| val attached = webMessageTransport.attach( | ||
| webView = this, | ||
| jsObjectName = EmbeddedCheckoutProtocolBridge.INTERFACE_NAME, | ||
| allowedOriginRules = WEB_MESSAGE_ALLOWED_ORIGIN_RULES, | ||
| ) { _, webMessage, _, isMainFrame, _ -> | ||
| if (webMessage.type != WebMessageCompat.TYPE_STRING) { | ||
| log.d(LOG_TAG, "Ignoring ECP WebMessage with non-string payload.") | ||
| } else { | ||
| receiveWebMessage(webMessage.data, isMainFrame) | ||
| } | ||
| } | ||
| if (!attached) { | ||
| val error = UnsupportedWebViewException() | ||
| log.w(LOG_TAG, error.checkoutError.errorDescription) | ||
| throw error | ||
| } | ||
| webMessageListenerAttached = true | ||
| } | ||
|
|
||
| private fun detachProtocolBridge() { | ||
| if (!webMessageListenerAttached) return | ||
|
|
||
| webMessageTransport.detach(this, EmbeddedCheckoutProtocolBridge.INTERFACE_NAME) | ||
| webMessageListenerAttached = false | ||
| } | ||
|
|
||
| internal fun markPreloadConsumed() { | ||
| isPreloadRequest = false | ||
| } | ||
|
|
@@ -145,31 +200,45 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
| private const val LOG_TAG = "CheckoutWebView" | ||
| private const val SHOPIFY_PURPOSE_HEADER = "Shopify-Purpose" | ||
| private const val PREFETCH_PURPOSE = "prefetch" | ||
| private val WEB_MESSAGE_ALLOWED_ORIGIN_RULES = setOf("*") | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beginning wide-open, but with the option to tighten up in the future. We could either apply here or add a sourceOrigin check on receipt of each message |
||
|
|
||
| private val preloadCache = PreloadCache() | ||
|
|
||
| internal var cacheClock: PreloadCache.Clock | ||
| get() = preloadCache.clock | ||
| set(value) { | ||
| preloadCache.clock = value | ||
| } | ||
|
|
||
| fun preload(url: String, activity: ComponentActivity) { | ||
| fun preload( | ||
| url: String, | ||
| activity: ComponentActivity, | ||
| webMessageTransport: WebMessageTransport = AndroidXWebMessageTransport, | ||
| ) { | ||
| if (!ShopifyCheckoutKit.configuration.preloading.enabled) { | ||
| return | ||
| } | ||
|
|
||
| runOnUiThreadBlocking(activity) { | ||
| invalidate() | ||
| val view = CheckoutWebView(activity as Context).apply { | ||
| loadCheckout(url, isPreload = true) | ||
| log.d(LOG_TAG, "Pausing preloaded WebView.") | ||
| onPause() | ||
| try { | ||
| runOnUiThreadBlocking(activity) { | ||
| invalidate() | ||
| val view = CheckoutWebView(activity as Context, webMessageTransport).apply { | ||
| loadCheckout(url, isPreload = true) | ||
| log.d(LOG_TAG, "Pausing preloaded WebView.") | ||
| onPause() | ||
| } | ||
| preloadCache.store(PreloadKey.forUrl(url), view) | ||
| } | ||
| preloadCache.store(PreloadKey.forUrl(url), view) | ||
| } catch (_: UnsupportedWebViewException) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| fun checkoutViewFor(url: String, activity: ComponentActivity): CheckoutWebView { | ||
| fun checkoutViewFor( | ||
| url: String, | ||
| activity: ComponentActivity, | ||
| webMessageTransport: WebMessageTransport = AndroidXWebMessageTransport, | ||
| ): CheckoutWebView { | ||
| var checkoutWebView: CheckoutWebView? = null | ||
| runOnUiThreadBlocking(activity) { | ||
| val cachedView = if (ShopifyCheckoutKit.configuration.preloading.enabled) { | ||
|
|
@@ -180,7 +249,7 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
| } | ||
|
|
||
| checkoutWebView = cachedView ?: run { | ||
| CheckoutWebView(activity as Context).apply { | ||
| CheckoutWebView(activity as Context, webMessageTransport).apply { | ||
| loadCheckout(url) | ||
| } | ||
| } | ||
|
|
@@ -206,12 +275,14 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n | |
| return | ||
| } | ||
|
|
||
| var result: Result<Unit>? = null | ||
| val countDownLatch = CountDownLatch(1) | ||
| activity.runOnUiThread { | ||
| action() | ||
| result = runCatching { action() } | ||
| countDownLatch.countDown() | ||
| } | ||
| countDownLatch.await() | ||
| result?.getOrThrow() | ||
| } | ||
|
|
||
| private fun runOnMainThread(action: () -> Unit) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a new check. Some potential for issues if we're relying on any sub-frames to emit message
I don't see why we would be though