Commit 6b585972 authored by Arturo Mejia's avatar Arturo Mejia Committed by Christian Sadilek
Browse files

Closes #3743: Adding TrackingProtectionPolicy.CookiePolicy

parent b97aef4b
......@@ -161,6 +161,7 @@ class GeckoEngine(
set(value) {
value?.let {
runtime.settings.contentBlocking.categories = it.categories
runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id
defaultSettings?.trackingProtectionPolicy = value
}
}
......
......@@ -874,6 +874,17 @@ class GeckoEngineSessionTest {
assertEquals(TrackingProtectionPolicy.all().categories, ContentBlocking.CB_STRICT)
assertEquals(TrackingProtectionPolicy.recommended().categories, ContentBlocking.CB_DEFAULT)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, ContentBlocking.COOKIE_ACCEPT_ALL)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id,
ContentBlocking.COOKIE_ACCEPT_NON_TRACKERS
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NONE.id, ContentBlocking.COOKIE_ACCEPT_NONE)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id,
ContentBlocking.COOKIE_ACCEPT_FIRST_PARTY
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_VISITED.id, ContentBlocking.COOKIE_ACCEPT_VISITED)
}
@Test
......
......@@ -11,6 +11,7 @@ import mozilla.components.browser.engine.gecko.mediaquery.toGeckoValue
import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
import mozilla.components.support.test.any
......@@ -148,6 +149,7 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all())
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id)
try {
engine.settings.domStorageEnabled
......@@ -209,11 +211,24 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_MALWARE,
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id)
assertTrue(engine.settings.testingModeEnabled)
assertEquals("test-ua", engine.settings.userAgentString)
assertEquals(PreferredColorScheme.Light, engine.settings.preferredColorScheme)
assertFalse(engine.settings.allowAutoplayMedia)
assertTrue(engine.settings.suspendMediaWhenInactive)
engine.settings.trackingProtectionPolicy =
TrackingProtectionPolicy.select(
TrackingProtectionPolicy.AD,
cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY
)
assertEquals(CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id, contentBlockingSettings.cookieBehavior)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
assertEquals(CookiePolicy.ACCEPT_ALL.id, contentBlockingSettings.cookieBehavior)
}
@Test
......
......@@ -161,6 +161,7 @@ class GeckoEngine(
set(value) {
value?.let {
runtime.settings.contentBlocking.categories = it.categories
runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id
defaultSettings?.trackingProtectionPolicy = value
}
}
......
......@@ -874,6 +874,17 @@ class GeckoEngineSessionTest {
assertEquals(TrackingProtectionPolicy.all().categories, ContentBlocking.CB_STRICT)
assertEquals(TrackingProtectionPolicy.recommended().categories, ContentBlocking.CB_DEFAULT)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, ContentBlocking.COOKIE_ACCEPT_ALL)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id,
ContentBlocking.COOKIE_ACCEPT_NON_TRACKERS
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NONE.id, ContentBlocking.COOKIE_ACCEPT_NONE)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id,
ContentBlocking.COOKIE_ACCEPT_FIRST_PARTY
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_VISITED.id, ContentBlocking.COOKIE_ACCEPT_VISITED)
}
@Test
......
......@@ -11,6 +11,7 @@ import mozilla.components.browser.engine.gecko.mediaquery.toGeckoValue
import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
import mozilla.components.support.test.any
......@@ -148,6 +149,7 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all())
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id)
try {
engine.settings.domStorageEnabled
......@@ -209,11 +211,22 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_MALWARE,
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(CookiePolicy.ACCEPT_NON_TRACKERS.id, contentBlockingSettings.cookieBehavior)
assertTrue(engine.settings.testingModeEnabled)
assertEquals("test-ua", engine.settings.userAgentString)
assertEquals(PreferredColorScheme.Light, engine.settings.preferredColorScheme)
assertFalse(engine.settings.allowAutoplayMedia)
assertTrue(engine.settings.suspendMediaWhenInactive)
engine.settings.trackingProtectionPolicy =
TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD, cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY)
assertEquals(CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id, contentBlockingSettings.cookieBehavior)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
assertEquals(CookiePolicy.ACCEPT_ALL.id, contentBlockingSettings.cookieBehavior)
}
@Test
......
......@@ -161,6 +161,7 @@ class GeckoEngine(
set(value) {
value?.let {
runtime.settings.contentBlocking.categories = it.categories
runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id
defaultSettings?.trackingProtectionPolicy = value
}
}
......
......@@ -849,6 +849,17 @@ class GeckoEngineSessionTest {
assertEquals(TrackingProtectionPolicy.all().categories, ContentBlocking.CB_STRICT)
assertEquals(TrackingProtectionPolicy.recommended().categories, ContentBlocking.CB_DEFAULT)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, ContentBlocking.COOKIE_ACCEPT_ALL)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id,
ContentBlocking.COOKIE_ACCEPT_NON_TRACKERS
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NONE.id, ContentBlocking.COOKIE_ACCEPT_NONE)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id,
ContentBlocking.COOKIE_ACCEPT_FIRST_PARTY
)
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_VISITED.id, ContentBlocking.COOKIE_ACCEPT_VISITED)
}
@Test
......
......@@ -11,6 +11,7 @@ import mozilla.components.browser.engine.gecko.mediaquery.toGeckoValue
import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.mediaquery.PreferredColorScheme
import mozilla.components.support.test.any
......@@ -148,6 +149,7 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all())
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id)
try {
engine.settings.domStorageEnabled
......@@ -209,11 +211,27 @@ class GeckoEngineTest {
TrackingProtectionPolicy.SAFE_BROWSING_MALWARE,
TrackingProtectionPolicy.SAFE_BROWSING_PHISHING
).categories, contentBlockingSettings.categories)
assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id)
assertTrue(engine.settings.testingModeEnabled)
assertEquals("test-ua", engine.settings.userAgentString)
assertEquals(PreferredColorScheme.Light, engine.settings.preferredColorScheme)
assertFalse(engine.settings.allowAutoplayMedia)
assertTrue(engine.settings.suspendMediaWhenInactive)
engine.settings.trackingProtectionPolicy =
TrackingProtectionPolicy.select(
TrackingProtectionPolicy.AD,
cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY
)
assertEquals(
TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id,
contentBlockingSettings.cookieBehavior
)
engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none()
assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, contentBlockingSettings.cookieBehavior)
}
@Test
......
......@@ -6,6 +6,8 @@ package mozilla.components.concept.engine
import android.graphics.Bitmap
import androidx.annotation.CallSuper
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.engine.media.RecordingDevice
......@@ -90,8 +92,46 @@ abstract class EngineSession(
open class TrackingProtectionPolicy internal constructor(
val categories: Int,
val useForPrivateSessions: Boolean = true,
val useForRegularSessions: Boolean = true
val useForRegularSessions: Boolean = true,
val cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS
) {
/**
* Indicates how cookies should behave for a given [TrackingProtectionPolicy].
* The ids of each cookiePolicy is aligned with the GeckoView @CookieBehavior constants.
*/
@Suppress("MagicNumber")
enum class CookiePolicy(val id: Int) {
/**
* Accept first-party and third-party cookies and site data.
*/
ACCEPT_ALL(0),
/**
* Accept only first-party cookies and site data to block cookies which are
* not associated with the domain of the visited site.
*/
ACCEPT_ONLY_FIRST_PARTY(1),
/**
* Do not store any cookies and site data.
*/
ACCEPT_NONE(2),
/**
* Accept first-party and third-party cookies and site data only from
* sites previously visited in a first-party context.
*/
ACCEPT_VISITED(3),
/**
* Accept only first-party and non-tracking third-party cookies and site data
* to block cookies which are not associated with the domain of the visited
* site set by known trackers.
*/
ACCEPT_NON_TRACKERS(4)
}
companion object {
internal const val NONE: Int = 0
/**
......@@ -143,15 +183,16 @@ abstract class EngineSession(
const val SAFE_BROWSING_ALL =
SAFE_BROWSING_MALWARE + SAFE_BROWSING_UNWANTED + SAFE_BROWSING_HARMFUL + SAFE_BROWSING_PHISHING
internal const val RECOMMENDED = AD + ANALYTICS + SOCIAL + TEST + SAFE_BROWSING_ALL
const val RECOMMENDED = AD + ANALYTICS + SOCIAL + TEST + SAFE_BROWSING_ALL
internal const val ALL = RECOMMENDED + CRYPTOMINING + FINGERPRINTING + CONTENT
const val ALL = RECOMMENDED + CRYPTOMINING + FINGERPRINTING + CONTENT
fun none() = TrackingProtectionPolicy(NONE)
fun none() = TrackingProtectionPolicy(NONE, cookiePolicy = ACCEPT_ALL)
/**
* Strict policy.
* Combining the [recommended] categories plus [CRYPTOMINING], [FINGERPRINTING] and [CONTENT].
* With a cookiePolicy of [ACCEPT_NON_TRACKERS].
* This is the strictest setting and may cause issues on some web sites.
*/
fun all() = TrackingProtectionPolicyForSessionTypes(ALL)
......@@ -159,11 +200,13 @@ abstract class EngineSession(
/**
* Recommended policy.
* Combining the [AD], [ANALYTICS], [SOCIAL], [TEST] categories plus [SAFE_BROWSING_ALL].
* With a [CookiePolicy] of [ACCEPT_NON_TRACKERS].
* This is the recommended setting.
*/
fun recommended() = TrackingProtectionPolicyForSessionTypes(RECOMMENDED)
fun select(vararg categories: Int) = TrackingProtectionPolicyForSessionTypes(categories.sum())
fun select(vararg categories: Int, cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS) =
TrackingProtectionPolicyForSessionTypes(categories.sum(), cookiePolicy)
}
fun contains(category: Int) = (categories and category) != 0
......@@ -185,15 +228,17 @@ abstract class EngineSession(
* should be applied to. By default, a policy will be applied to all sessions.
*/
class TrackingProtectionPolicyForSessionTypes internal constructor(
categories: Int
) : TrackingProtectionPolicy(categories) {
categories: Int,
cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS
) : TrackingProtectionPolicy(categories, cookiePolicy = cookiePolicy) {
/**
* Marks this policy to be used for private sessions only.
*/
fun forPrivateSessionsOnly() = TrackingProtectionPolicy(
categories,
useForPrivateSessions = true,
useForRegularSessions = false
useForRegularSessions = false,
cookiePolicy = cookiePolicy
)
/**
......@@ -202,7 +247,8 @@ abstract class EngineSession(
fun forRegularSessionsOnly() = TrackingProtectionPolicy(
categories,
useForPrivateSessions = false,
useForRegularSessions = true
useForRegularSessions = true,
cookiePolicy = cookiePolicy
)
}
......
......@@ -34,6 +34,11 @@ permalink: /changelog/
* See component's [README](https://github.com/mozilla-mobile/android-components/blob/master/components/service/firefox-accounts/README.md) for detailed description of the new API.
* As part of these changes, token caching issue has been fixed. See [#3579](https://github.com/mozilla-mobile/android-components/pull/3579) for details.
* **concept-engine**, **browser-engine-gecko(-beta/nightly)**.
* Added `TrackingProtectionPolicy.CookiePolicy` to indicate how cookies should behave for a given `TrackingProtectionPolicy`.
* Now `TrackingProtectionPolicy.select` allows you to specify a `TrackingProtectionPolicy.CookiePolicy`, if not specified, `TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS` will be used.
* Behavior change: Now `TrackingProtectionPolicy.none()` will get assigned a `TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL`, and both `TrackingProtectionPolicy.all()` and `TrackingProtectionPolicy.recommended()` will have a `TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS`.
# 3.0.0
* [Commits](https://github.com/mozilla-mobile/android-components/compare/v2.0.0...v3.0.0)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment