[Android] Replace JavascriptInterface with WebMessageListener - #424
Conversation
d97376b to
4f9359a
Compare
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Package Size
Android file breakdown
Measured from the PR base SHA and PR head SHA. The file breakdown shows uncompressed sizes within each package artifact, so individual files do not sum to the compressed artifact total. This comment reports package artifact sizes only; it is not a final app binary-size report. |
f0c8de2 to
0b38f90
Compare
| } | ||
|
|
||
| internal fun receiveWebMessage(message: String?, isMainFrame: Boolean = true) { | ||
| if (!isMainFrame) { |
There was a problem hiding this comment.
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
0b38f90 to
d63a691
Compare
f14f9d6 to
1f74d2c
Compare
83fa948 to
8437018
Compare
| fun detach(webView: WebView, jsObjectName: String) | ||
| } | ||
|
|
||
| internal object WebMessageListenerTransport : WebMessageTransport { |
There was a problem hiding this comment.
I like this - was mulling over something to just wrap up the combination of the isfeaturesupported + addWebMessageListener would be a nice compromise to the mocking
Decoupling the feature supported checks from CheckoutWebView looks great
0f18bd2 to
08fdf1d
Compare
d45dd70 to
ad74a4d
Compare
ad74a4d to
a7833d1
Compare

What changes are you making?
Replaces the
addJavascriptInterfacetransport for the Embedded Checkout Protocol (ECP) bridge withWebViewCompat.addWebMessageListener, a more secure and modern WebView API.See
https://developer.android.com/privacy-and-security/risks/insecure-webview-native-bridges
Recommended here. And addJavaScriptInterface is referred to as legacy further down on the same page.
The bridge is now registered via
WebViewCompat.addWebMessageListenerwhenWebViewFeature.WEB_MESSAGE_LISTENERis supported, and torn down viaWebViewCompat.removeWebMessageListeneron detach.EmbeddedCheckoutProtocolBridge.postMessagenow delegates to a new internalreceiveMessagemethod, andCheckoutWebViewexposes areceiveWebMessageentry point that filters out child-frame messages before forwarding to the bridge.A new
CheckoutWebViewFeatureSupportobject centralises feature detection, exposes theweb_view_not_supportederror code and exception, and provides test override/reset helpers. WhenWEB_MESSAGE_LISTENERis not available,presentimmediately callsonCheckoutFailedwith aCheckoutKitExceptioncarrying theweb_view_not_supportederror code and returnsnull, andpreloadexits early without creating a cached view.The Android README documents the new
web_view_not_supportederror code in the exceptions table and adds a troubleshooting entry advising callers to open the checkout URL in Chrome, Chrome Custom Tabs, or another full browser, or to prompt the buyer to update Android System WebView or Chrome. The minimum supported WebView version is Android System WebView / Chrome 83 (released May 2020).WebMessageListener Support
Components
ShopifyCheckoutKit— entry point; supplies the production transport publicly while allowing transport injection in tests.CheckoutBottomSheet— binds presentation concerns: listener, protocol client, and the retained or newly created WebView.CheckoutWebView— owns the bridge for the WebView’s lifetime, including preloaded WebViews.WebMessageTransport— internal messaging boundary for attaching/detaching listeners and sending responses.WebMessageListenerTransport— production AndroidX WebKit implementation of the above interface, including feature detection, listener registration, JavaScript evaluation, escaping, and main-thread dispatch.EmbeddedCheckoutProtocolBridge— owns transport lifecycle, filters inbound messages, decodes/routes protocol messages on a serial executor, composes built-in and merchant handlers, and sends responses.How to test
WebMessageListenertransport path and unsupported-WebView behaviour.web message is received by protocol bridgetest passes, confirming that messages delivered viareceiveWebMessageare correctly handled by the protocol bridge.web message from child frame is ignoredtest passes, confirming that messages from child frames are filtered out.present emits unsupported WebView error and returns nullandpreload does nothing when WebView is unsupportedpass, confirming early-exit behaviour when the feature is unavailable.WEB_MESSAGE_LISTENER, load a checkout and confirm that ECP messages are received and processed correctly.WEB_MESSAGE_LISTENERis not supported, confirm thatpresentcallsonCheckoutFailedwithweb_view_not_supportedand does not display the bottom sheet.Before you merge
Important
platforms/swift/README.mdand/orplatforms/android/README.md)Releasing a new Swift version?
ShopifyCheckoutKit.podspecplatforms/swift/Sources/ShopifyCheckoutKit/ShopifyCheckoutKit.swiftplatforms/swift/README.md(major version only)Releasing a new Embedded Checkout Protocol version?
embeddedCheckoutProtocolAndroidinplatforms/android/gradle/libs.versions.tomlprotocol/languages/kotlin/embedded-checkout-protocol/api/embedded-checkout-protocol.apiif the public API changedReleasing a new Android version?
checkoutKitAndroidinplatforms/android/gradle/libs.versions.tomlplatforms/android/README.mdTip
See the Contributing documentation for the full release process per platform.