Skip to content

Commit

Permalink
For mozilla-mobile#26910: Enable TCP for all modes
Browse files Browse the repository at this point in the history
Unify the TCP feature with the TCP setting allowing both to be controlled
through the same Nimbus experiment.
Allow changing the default cookie policy to TCP based on the Nimbus experiment.
  • Loading branch information
Mugurell committed Sep 12, 2022
1 parent aef6c0a commit 49bccbd
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package org.mozilla.fenix.components

import android.content.res.Resources
import androidx.annotation.VisibleForTesting
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicyForSessionTypes
import org.mozilla.fenix.R
import org.mozilla.fenix.utils.Settings

Expand Down Expand Up @@ -40,9 +42,9 @@ class TrackingProtectionPolicyFactory(
}

return when {
normalMode && privateMode -> trackingProtectionPolicy
normalMode && !privateMode -> trackingProtectionPolicy.forRegularSessionsOnly()
!normalMode && privateMode -> trackingProtectionPolicy.forPrivateSessionsOnly()
normalMode && privateMode -> trackingProtectionPolicy.applyTCPIfNeeded(settings)
normalMode && !privateMode -> trackingProtectionPolicy.applyTCPIfNeeded(settings).forRegularSessionsOnly()
!normalMode && privateMode -> trackingProtectionPolicy.applyTCPIfNeeded(settings).forPrivateSessionsOnly()
else -> TrackingProtectionPolicy.none()
}
}
Expand Down Expand Up @@ -103,3 +105,20 @@ class TrackingProtectionPolicyFactory(
return settings.blockRedirectTrackersInCustomTrackingProtection
}
}

@VisibleForTesting
internal fun TrackingProtectionPolicyForSessionTypes.applyTCPIfNeeded(settings: Settings):
TrackingProtectionPolicyForSessionTypes {
val updatedCookiePolicy = if (settings.enabledTotalCookieProtection) {
CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS
} else {
cookiePolicy
}

return TrackingProtectionPolicy.select(
trackingCategories = trackingCategories,
cookiePolicy = updatedCookiePolicy,
strictSocialTrackingProtection = strictSocialTrackingProtection,
cookiePurging = cookiePurging,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CustomEtpCookiesOptionsDropDownListPreference @JvmOverloads constructor(
)

@Suppress("UNCHECKED_CAST")
if (context.settings().enabledTotalCookieProtectionSetting) {
if (context.settings().enabledTotalCookieProtection) {
// If the new "Total cookie protection" should be shown it must be first item.
entries = arrayOf(getString(R.string.preference_enhanced_tracking_protection_custom_cookies_5)) +
entries as Array<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class TrackingProtectionBlockingFragment :
binding = FragmentTrackingProtectionBlockingBinding.bind(view)

// Text for the updated "Total cookie protection" option should be updated as part of a staged rollout
if (requireContext().settings().enabledTotalCookieProtectionSetting) {
if (requireContext().settings().enabledTotalCookieProtection) {
binding.categoryCookies.apply {
trackingProtectionCategoryTitle.text = getText(R.string.etp_cookies_title_2)
trackingProtectionCategoryItemDescription.text = getText(R.string.etp_cookies_description_2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class TrackingProtectionPanelView(
binding.notBlockingHeader.isGone = bucketedTrackers.loadedIsEmpty()
binding.blockingHeader.isGone = bucketedTrackers.blockedIsEmpty()

if (containerView.context.settings().enabledTotalCookieProtectionSetting) {
if (containerView.context.settings().enabledTotalCookieProtection) {
binding.crossSiteTracking.text = containerView.context.getString(R.string.etp_cookies_title_2)
binding.crossSiteTrackingLoaded.text = containerView.context.getString(R.string.etp_cookies_title_2)
}
Expand All @@ -147,7 +147,7 @@ class TrackingProtectionPanelView(
binding.detailsMode.visibility = View.VISIBLE

if (category == CROSS_SITE_TRACKING_COOKIES &&
containerView.context.settings().enabledTotalCookieProtectionSetting
containerView.context.settings().enabledTotalCookieProtection
) {
binding.categoryTitle.setText(R.string.etp_cookies_title_2)
binding.categoryDescription.setText(R.string.etp_cookies_description_2)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/mozilla/fenix/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class Settings(private val appContext: Context) : PreferencesHolder {
true,
)

val enabledTotalCookieProtectionSetting: Boolean
val enabledTotalCookieProtection: Boolean
get() = mr2022Sections[Mr2022Section.TCP_FEATURE] == true

/**
Expand All @@ -599,7 +599,7 @@ class Settings(private val appContext: Context) : PreferencesHolder {

val blockCookiesSelectionInCustomTrackingProtection by stringPreference(
key = appContext.getPreferenceKey(R.string.pref_key_tracking_protection_custom_cookies_select),
default = if (enabledTotalCookieProtectionSetting) {
default = if (enabledTotalCookieProtection) {
appContext.getString(R.string.total_protection)
} else {
appContext.getString(R.string.social)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,46 @@ class TrackingProtectionPolicyFactoryTest {
expected.assertPolicyEquals(always, checkPrivacy = false)
}

@Test
fun `GIVEN TCP is enabled by nimbus WHEN applyTCPIfNeeded THEN cookie policy should be TCP`() {
val settings: Settings = mockk(relaxed = true)
every { settings.enabledTotalCookieProtection } returns true

val policies = arrayOf(
TrackingProtectionPolicy.strict(),
TrackingProtectionPolicy.recommended(),
TrackingProtectionPolicy.select(),
)

for (policy in policies) {
val adaptedPolicy = policy.applyTCPIfNeeded(settings)
assertEquals(
CookiePolicy.ACCEPT_FIRST_PARTY_AND_ISOLATE_OTHERS,
adaptedPolicy.cookiePolicy,
)
}
}

fun `GIVEN TCP is NOT enabled by nimbus WHEN applyTCPIfNeeded THEN reuse cookie policy`() {
val settings: Settings = mockk(relaxed = true)

every { settings.enabledTotalCookieProtection } returns false

val policies = arrayOf(
TrackingProtectionPolicy.strict(),
TrackingProtectionPolicy.recommended(),
TrackingProtectionPolicy.select(),
)

for (policy in policies) {
val adaptedPolicy = policy.applyTCPIfNeeded(settings)
assertEquals(
policy.cookiePolicy,
adaptedPolicy.cookiePolicy,
)
}
}

@Test
fun `GIVEN custom policy WHEN cookie policy social THEN tracking policy should have cookie policy allow non-trackers`() {
val expected = TrackingProtectionPolicy.select(
Expand Down Expand Up @@ -586,6 +626,7 @@ class TrackingProtectionPolicyFactoryTest {
useCustom: Boolean = false,
useTrackingProtection: Boolean = false,
): Settings = mockk {
every { enabledTotalCookieProtection } returns false
every { useStrictTrackingProtection } returns useStrict
every { useCustomTrackingProtection } returns useCustom
every { shouldUseTrackingProtection } returns useTrackingProtection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CustomEtpCookiesOptionsDropDownListPreferenceTest {

mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns true
every { enabledTotalCookieProtection } returns true
}

val preference = CustomEtpCookiesOptionsDropDownListPreference(testContext)
Expand All @@ -44,7 +44,7 @@ class CustomEtpCookiesOptionsDropDownListPreferenceTest {
fun `GIVEN total cookie protection is disabled WHEN using this preference THEN don't show the total cookie protection option`() {
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns false
every { enabledTotalCookieProtection } returns false
}

val preference = CustomEtpCookiesOptionsDropDownListPreference(testContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TrackingProtectionBlockingFragmentTest {

mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk(relaxed = true) {
every { enabledTotalCookieProtectionSetting } returns true
every { enabledTotalCookieProtection } returns true
}

val fragment = createFragment()
Expand All @@ -46,7 +46,7 @@ class TrackingProtectionBlockingFragmentTest {

mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk(relaxed = true) {
every { enabledTotalCookieProtectionSetting } returns false
every { enabledTotalCookieProtection } returns false
}

val fragment = createFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class TrackingProtectionPanelViewTest {
fun testNormalModeUiCookiesWithTotalCookieProtectionEnabled() {
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns true
every { enabledTotalCookieProtection } returns true
}
val expectedTitle = testContext.getString(R.string.etp_cookies_title_2)

Expand All @@ -89,7 +89,7 @@ class TrackingProtectionPanelViewTest {
fun testNormalModeUiCookiesWithTotalCookieProtectionDisabled() {
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns false
every { enabledTotalCookieProtection } returns false
}
val expectedTitle = testContext.getString(R.string.etp_cookies_title)

Expand Down Expand Up @@ -130,7 +130,7 @@ class TrackingProtectionPanelViewTest {
fun testPrivateModeUiCookiesWithTotalCookieProtectionEnabled() {
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns true
every { enabledTotalCookieProtection } returns true
}
val expectedTitle = testContext.getString(R.string.etp_cookies_title_2)
val expectedDescription = testContext.getString(R.string.etp_cookies_description_2)
Expand All @@ -153,7 +153,7 @@ class TrackingProtectionPanelViewTest {
fun testPrivateModeUiCookiesWithTotalCookieProtectionDisabled() {
mockkStatic("org.mozilla.fenix.ext.ContextKt") {
every { any<Context>().settings() } returns mockk {
every { enabledTotalCookieProtectionSetting } returns false
every { enabledTotalCookieProtection } returns false
}
val expectedTitle = testContext.getString(R.string.etp_cookies_title)
val expectedDescription = testContext.getString(R.string.etp_cookies_description)
Expand Down

0 comments on commit 49bccbd

Please sign in to comment.