web: honour the global third-party-cookie block in PWA windows
- Commit
c64cb2e27d1bbab8ba96f9996f084261e28ca3df- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 04:08 UTC (28 days ago)
- Parents
acf42b3
createPwaWebView hardcoded setAcceptThirdPartyCookies(true), ignoring the global block-third-party preference that both browser-tab WebViews apply. A user who blocks third-party cookies was still tracked by them inside installed-site windows. Feed the same web_prefs flag through a small shouldAcceptPwaThirdParty Cookies decision seam wired into createPwaWebView, mirroring how the browser tab gates both of its WebViews on the identical pref. User-visible effect: enabling block-third-party-cookies now also stops third-party cookies in installed-site (PWA) windows. Verified: new Robolectric test pwaThirdPartyCookieDecisionHonoursGlobalPref failed before the fix and passes after (Robolectric's cookie manager ignores the setter, so the decision seam itself is the unit under test); full :web:testDevUnitTest suite green (334 tests, 0 failures).
Diff
web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt
diff --git a/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt b/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.ktindex dda1627fd8..e1975550d9 100644--- a/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt+++ b/web/src/main/java/com/vayunmathur/web/platform/PwaBrowser.kt@@ -269,7 +269,7 @@ private fun createPwaWebView( ) val cookieManager = CookieManager.getInstance() cookieManager.setAcceptCookie(true)- cookieManager.setAcceptThirdPartyCookies(this, true)+ cookieManager.setAcceptThirdPartyCookies(this, shouldAcceptPwaThirdPartyCookies(ctx)) applyPwaWebSettings(this, ctx) @@ -298,6 +298,18 @@ private fun createPwaWebView( loadUrl(initialUrl) } +/**+ * Whether the PWA WebView accepts third-party cookies. Browser tabs gate this on the+ * global block-third-party setting; the PWA window must honour the same pref instead of+ * hardcoding acceptance.+ */+internal fun shouldAcceptPwaThirdPartyCookies(context: Context): Boolean {+ val blocked = context+ .getSharedPreferences("web_prefs", Context.MODE_PRIVATE)+ .getBoolean(P_BLOCK_THIRD_PARTY, false)+ return !blocked+}+ private fun applyPwaWebSettings(webView: WebView, ctx: Context) { val settings = webView.settings settings.javaScriptEnabled = true
web/src/test/java/com/vayunmathur/web/ui/ChromeGrantAndPwaDownloadTest.kt
diff --git a/web/src/test/java/com/vayunmathur/web/ui/ChromeGrantAndPwaDownloadTest.kt b/web/src/test/java/com/vayunmathur/web/ui/ChromeGrantAndPwaDownloadTest.ktindex efbc3dc970..cacebf2c6c 100644--- a/web/src/test/java/com/vayunmathur/web/ui/ChromeGrantAndPwaDownloadTest.kt+++ b/web/src/test/java/com/vayunmathur/web/ui/ChromeGrantAndPwaDownloadTest.kt@@ -220,6 +220,22 @@ class ChromeGrantAndPwaDownloadTest { assertTrue(req.granted == null, "granted=${req.granted}") } + @Test fun pwaThirdPartyCookieDecisionHonoursGlobalPref() {+ // Robolectric's cookie manager ignores setAcceptThirdPartyCookies, so the+ // decision itself is the unit under test; createPwaWebView feeds it through.+ val prefs = context.getSharedPreferences("web_prefs", android.content.Context.MODE_PRIVATE)+ prefs.edit().putBoolean("web_block_third_party", true).apply()+ try {+ assertEquals(+ false,+ com.vayunmathur.web.platform.shouldAcceptPwaThirdPartyCookies(context),+ )+ } finally {+ prefs.edit().remove("web_block_third_party").apply()+ }+ assertEquals(true, com.vayunmathur.web.platform.shouldAcceptPwaThirdPartyCookies(context))+ }+ @Test fun pwaBackPressFinishesWhenNoHistory() { rule.setContent { com.vayunmathur.web.platform.PwaBrowser(initialUrl = "https://pwa.example/app", title = "PWA")