From f7c7da1aeb91f02ce806099126255bc4f4158bd5 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Wed, 14 Aug 2019 03:28:44 -0400 Subject: [PATCH] Closes #4098, #4110: Exposing new categories of content blocking. --- buildSrc/src/main/java/Gecko.kt | 2 +- .../browser/engine/gecko/GeckoEngine.kt | 14 +- .../engine/gecko/GeckoEngineSession.kt | 18 +- .../engine/gecko/GeckoEngineSessionTest.kt | 81 +++++--- .../browser/engine/gecko/GeckoEngineTest.kt | 54 ++---- .../browser/engine/gecko/GeckoEngine.kt | 11 +- .../engine/gecko/GeckoEngineSession.kt | 76 ++++++-- .../engine/gecko/GeckoEngineSessionTest.kt | 174 ++++++++++++++---- .../browser/engine/gecko/GeckoEngineTest.kt | 74 ++++---- .../browser/engine/gecko/GeckoEngine.kt | 14 +- .../engine/gecko/GeckoEngineSession.kt | 18 +- .../engine/gecko/GeckoEngineSessionTest.kt | 82 ++++++--- .../browser/engine/gecko/GeckoEngineTest.kt | 54 ++---- .../browser/engine/system/SystemEngineView.kt | 10 +- .../engine/system/SystemEngineSessionTest.kt | 16 +- .../browser/engine/system/SystemEngineTest.kt | 7 +- .../engine/system/SystemEngineViewTest.kt | 23 ++- .../components/browser/session/Session.kt | 12 ++ .../browser/session/engine/EngineObserver.kt | 5 + .../components/browser/session/SessionTest.kt | 61 ++++++ .../session/engine/EngineObserverTest.kt | 15 ++ .../concept/engine/EngineSession.kt | 128 +++++++++---- .../engine/content/blocking/Tracker.kt | 16 +- .../concept/engine/EngineSessionTest.kt | 99 ++++++---- .../components/concept/engine/SettingsTest.kt | 6 +- .../feature/session/SettingsUseCasesTest.kt | 8 +- docs/changelog.md | 7 + 27 files changed, 748 insertions(+), 337 deletions(-) diff --git a/buildSrc/src/main/java/Gecko.kt b/buildSrc/src/main/java/Gecko.kt index 536952e9d13..6d605a8412e 100644 --- a/buildSrc/src/main/java/Gecko.kt +++ b/buildSrc/src/main/java/Gecko.kt @@ -6,7 +6,7 @@ internal object GeckoVersions { /** * GeckoView Nightly Version. */ - const val nightly_version = "70.0.20190809095611" + const val nightly_version = "70.0.20190813095543" /** * GeckoView Beta Version. diff --git a/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt b/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt index 2394a1c93a8..f19f24f41fe 100644 --- a/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt +++ b/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt @@ -156,13 +156,17 @@ class GeckoEngine( defaultSettings?.automaticLanguageAdjustment = value } - override var trackingProtectionPolicy: TrackingProtectionPolicy? - get() = TrackingProtectionPolicy.select(runtime.settings.contentBlocking.categories) + override var trackingProtectionPolicy: TrackingProtectionPolicy? = null set(value) { - value?.let { - runtime.settings.contentBlocking.categories = it.categories - runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id + value?.let { policy -> + + val trackingCategories = policy.trackingCategories.sumBy { it.id } + + policy.safeBrowsingCategories.sumBy { it.id } + + runtime.settings.contentBlocking.categories = trackingCategories + runtime.settings.contentBlocking.cookieBehavior = policy.cookiePolicy.id defaultSettings?.trackingProtectionPolicy = value + field = value } } diff --git a/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt b/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt index 2cb0fdf94ec..978fed2c50c 100644 --- a/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt +++ b/components/browser/engine-gecko-beta/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt @@ -574,29 +574,33 @@ class GeckoEngineSession( } private fun ContentBlocking.BlockEvent.toTracker(): Tracker { - val blockedContentCategories = ArrayList() + val blockedContentCategories = mutableListOf() if (categories.contains(ContentBlocking.AT_AD)) { - blockedContentCategories.add(Tracker.Category.Ad) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.AD) } if (categories.contains(ContentBlocking.AT_ANALYTIC)) { - blockedContentCategories.add(Tracker.Category.Analytic) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.ANALYTICS) } if (categories.contains(ContentBlocking.AT_SOCIAL)) { - blockedContentCategories.add(Tracker.Category.Social) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.SOCIAL) } if (categories.contains(ContentBlocking.AT_FINGERPRINTING)) { - blockedContentCategories.add(Tracker.Category.Fingerprinting) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING) } if (categories.contains(ContentBlocking.AT_CRYPTOMINING)) { - blockedContentCategories.add(Tracker.Category.Cryptomining) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING) } if (categories.contains(ContentBlocking.AT_CONTENT)) { - blockedContentCategories.add(Tracker.Category.Content) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CONTENT) + } + + if (categories.contains(ContentBlocking.AT_TEST)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.TEST) } return Tracker(uri, blockedContentCategories) } diff --git a/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt b/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt index 1136891499e..0da1eb69507 100644 --- a/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt +++ b/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt @@ -14,6 +14,8 @@ import mozilla.components.concept.engine.DefaultSettings import mozilla.components.concept.engine.EngineSession import mozilla.components.concept.engine.EngineSession.LoadUrlFlags import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.SafeBrowsingCategory import mozilla.components.concept.engine.HitResult import mozilla.components.concept.engine.UnsupportedSettingException import mozilla.components.concept.engine.content.blocking.Tracker @@ -801,12 +803,24 @@ class GeckoEngineSessionTest { geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CRYPTOMINING) geckoCatgories = geckoCatgories.or(ContentBlocking.AT_FINGERPRINTING) geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CONTENT) + geckoCatgories = geckoCatgories.or(ContentBlocking.AT_TEST) contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", geckoCatgories) ) assertEquals("tracker1", trackerBlocked!!.url) - assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList())) + + val expectedBlockedCategories = listOf( + TrackingCategory.AD, + TrackingCategory.ANALYTICS, + TrackingCategory.SOCIAL, + TrackingCategory.CRYPTOMINING, + TrackingCategory.FINGERPRINTING, + TrackingCategory.CONTENT, + TrackingCategory.TEST + ) + + assertTrue(trackerBlocked!!.trackingCategories.containsAll(expectedBlockedCategories)) } @Test @@ -829,9 +843,14 @@ class GeckoEngineSessionTest { } }) - val allPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD) - val regularOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forRegularSessionsOnly() - val privateOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forPrivateSessionsOnly() + val allPolicy = TrackingProtectionPolicy.select( + arrayOf(TrackingProtectionPolicy.TrackingCategory.AD)) + val regularOnlyPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + ).forRegularSessionsOnly() + val privateOnlyPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + ).forPrivateSessionsOnly() session.enableTrackingProtection(allPolicy) assertTrue(trackerBlockingObserved) @@ -871,21 +890,40 @@ class GeckoEngineSessionTest { @Test fun trackingProtectionCategoriesAreAligned() { - assertEquals(TrackingProtectionPolicy.AD, ContentBlocking.AT_AD) - assertEquals(TrackingProtectionPolicy.ANALYTICS, ContentBlocking.AT_ANALYTIC) - assertEquals(TrackingProtectionPolicy.CONTENT, ContentBlocking.AT_CONTENT) - assertEquals(TrackingProtectionPolicy.SOCIAL, ContentBlocking.AT_SOCIAL) - assertEquals(TrackingProtectionPolicy.TEST, ContentBlocking.AT_TEST) - assertEquals(TrackingProtectionPolicy.CRYPTOMINING, ContentBlocking.AT_CRYPTOMINING) - assertEquals(TrackingProtectionPolicy.FINGERPRINTING, ContentBlocking.AT_FINGERPRINTING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_ALL, ContentBlocking.SB_ALL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, ContentBlocking.SB_HARMFUL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, ContentBlocking.SB_MALWARE) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_PHISHING, ContentBlocking.SB_PHISHING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, ContentBlocking.SB_UNWANTED) - - assertEquals(TrackingProtectionPolicy.all().categories, ContentBlocking.CB_STRICT) - assertEquals(TrackingProtectionPolicy.recommended().categories, ContentBlocking.CB_DEFAULT) + assertEquals(TrackingCategory.AD.id, ContentBlocking.AT_AD) + assertEquals(TrackingCategory.ANALYTICS.id, ContentBlocking.AT_ANALYTIC) + assertEquals(TrackingCategory.CONTENT.id, ContentBlocking.AT_CONTENT) + assertEquals(TrackingCategory.SOCIAL.id, ContentBlocking.AT_SOCIAL) + assertEquals(TrackingCategory.TEST.id, ContentBlocking.AT_TEST) + assertEquals(TrackingCategory.CRYPTOMINING.id, ContentBlocking.AT_CRYPTOMINING) + assertEquals(TrackingCategory.FINGERPRINTING.id, ContentBlocking.AT_FINGERPRINTING) + + assertEquals(SafeBrowsingCategory.RECOMMENDED.id, ContentBlocking.SB_ALL) + assertEquals(SafeBrowsingCategory.HARMFUL.id, ContentBlocking.SB_HARMFUL) + assertEquals(SafeBrowsingCategory.MALWARE.id, ContentBlocking.SB_MALWARE) + assertEquals(SafeBrowsingCategory.PHISHING.id, ContentBlocking.SB_PHISHING) + assertEquals(SafeBrowsingCategory.UNWANTED.id, ContentBlocking.SB_UNWANTED) + + assertEquals( + TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id }, + ContentBlocking.AT_STRICT + ) + + assertEquals( + TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id }, + ContentBlocking.SB_ALL + ) + + assertEquals( + TrackingProtectionPolicy.recommended().trackingCategories.sumBy { it.id }, + ContentBlocking.AT_DEFAULT + ) + + assertEquals( + TrackingProtectionPolicy.recommended().safeBrowsingCategories.sumBy { it.id }, + ContentBlocking.SB_ALL + ) + assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, ContentBlocking.COOKIE_ACCEPT_ALL) assertEquals( TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id, @@ -990,7 +1028,7 @@ class GeckoEngineSessionTest { } expectException(UnsupportedSettingException::class) { - settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() + settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() } } @@ -1286,7 +1324,8 @@ class GeckoEngineSessionTest { val runtime = mock() whenever(runtime.settings).thenReturn(mock()) - val defaultSettings = DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.all()) + val defaultSettings = + DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.strict()) GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider, privateMode = false, defaultSettings = defaultSettings) diff --git a/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt b/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt index ddaa3914d3b..81f67d90b88 100644 --- a/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt +++ b/components/browser/engine-gecko-beta/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt @@ -68,8 +68,7 @@ class GeckoEngineTest { @Test fun settings() { val defaultSettings = DefaultSettings() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() val runtime = mock() val runtimeSettings = mock() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) @@ -133,22 +132,15 @@ class GeckoEngineTest { engine.settings.userAgentString = engine.settings.userAgentString + "-test" assertEquals(GeckoSession.getDefaultUserAgent() + "-test", engine.settings.userAgentString) - assertEquals(TrackingProtectionPolicy.none(), engine.settings.trackingProtectionPolicy) - engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) - assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all()) + assertEquals(null, engine.settings.trackingProtectionPolicy) + + engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() + + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, ContentBlocking.AT_STRICT) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, ContentBlocking.SB_ALL) assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id) try { @@ -166,8 +158,7 @@ class GeckoEngineTest { fun defaultSettings() { val runtime = mock() val runtimeSettings = mock() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) whenever(runtime.settings).thenReturn(runtimeSettings) whenever(runtimeSettings.contentBlocking).thenReturn(contentBlockingSettings) @@ -176,7 +167,7 @@ class GeckoEngineTest { val engine = GeckoEngine(context, DefaultSettings( - trackingProtectionPolicy = TrackingProtectionPolicy.all(), + trackingProtectionPolicy = TrackingProtectionPolicy.strict(), javascriptEnabled = false, webFontsEnabled = false, automaticFontSizeAdjustment = false, @@ -198,19 +189,12 @@ class GeckoEngineTest { verify(runtimeSettings).remoteDebuggingEnabled = true verify(runtimeSettings).autoplayDefault = GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, ContentBlocking.AT_STRICT) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, ContentBlocking.SB_ALL) + assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id) assertTrue(engine.settings.testingModeEnabled) assertEquals("test-ua", engine.settings.userAgentString) @@ -220,7 +204,7 @@ class GeckoEngineTest { engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, + trackingCategories = arrayOf(TrackingProtectionPolicy.TrackingCategory.AD), cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY ) diff --git a/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt b/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt index 2394a1c93a8..58dd07984ff 100644 --- a/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt +++ b/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt @@ -156,13 +156,14 @@ class GeckoEngine( defaultSettings?.automaticLanguageAdjustment = value } - override var trackingProtectionPolicy: TrackingProtectionPolicy? - get() = TrackingProtectionPolicy.select(runtime.settings.contentBlocking.categories) + override var trackingProtectionPolicy: TrackingProtectionPolicy? = null set(value) { - value?.let { - runtime.settings.contentBlocking.categories = it.categories - runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id + value?.let { policy -> + runtime.settings.contentBlocking.setAntiTracking(policy.trackingCategories.sumBy { it.id }) + runtime.settings.contentBlocking.setSafeBrowsing(policy.safeBrowsingCategories.sumBy { it.id }) + runtime.settings.contentBlocking.cookieBehavior = policy.cookiePolicy.id defaultSettings?.trackingProtectionPolicy = value + field = value } } diff --git a/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt b/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt index 41105c91400..0368bfd6778 100644 --- a/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt +++ b/components/browser/engine-gecko-nightly/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt @@ -33,12 +33,6 @@ import mozilla.components.support.ktx.kotlin.isPhone import org.json.JSONObject import org.mozilla.geckoview.AllowOrDeny import org.mozilla.geckoview.ContentBlocking -import org.mozilla.geckoview.ContentBlocking.AT_AD -import org.mozilla.geckoview.ContentBlocking.AT_ANALYTIC -import org.mozilla.geckoview.ContentBlocking.AT_CONTENT -import org.mozilla.geckoview.ContentBlocking.AT_CRYPTOMINING -import org.mozilla.geckoview.ContentBlocking.AT_FINGERPRINTING -import org.mozilla.geckoview.ContentBlocking.AT_SOCIAL import org.mozilla.geckoview.GeckoResult import org.mozilla.geckoview.GeckoRuntime import org.mozilla.geckoview.GeckoSession @@ -583,34 +577,76 @@ class GeckoEngineSession( onTrackerBlocked(event.toTracker()) } } + + override fun onContentLoaded(session: GeckoSession, event: ContentBlocking.BlockEvent) { + notifyObservers { + onTrackerLoaded(event.toTracker()) + } + } } private fun ContentBlocking.BlockEvent.toTracker(): Tracker { - val blockedContentCategories = ArrayList() + val blockedContentCategories = mutableListOf() - if (categories.contains(AT_AD)) { - blockedContentCategories.add(Tracker.Category.Ad) + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.AD)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.AD) } - if (categories.contains(AT_ANALYTIC)) { - blockedContentCategories.add(Tracker.Category.Analytic) + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.ANALYTIC)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.ANALYTICS) } - if (categories.contains(AT_SOCIAL)) { - blockedContentCategories.add(Tracker.Category.Social) + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.SOCIAL)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.SOCIAL) } - if (categories.contains(AT_FINGERPRINTING)) { - blockedContentCategories.add(Tracker.Category.Fingerprinting) + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.FINGERPRINTING)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING) } - if (categories.contains(AT_CRYPTOMINING)) { - blockedContentCategories.add(Tracker.Category.Cryptomining) + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.CRYPTOMINING)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING) } - if (categories.contains(AT_CONTENT)) { - blockedContentCategories.add(Tracker.Category.Content) + + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.CONTENT)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CONTENT) } - return Tracker(uri, blockedContentCategories) + + if (antiTrackingCategory.contains(ContentBlocking.AntiTracking.TEST)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.TEST) + } + + return Tracker( + url = uri, + trackingCategories = blockedContentCategories, + cookiePolicies = getCookiePolicies() + ) + } + + private fun ContentBlocking.BlockEvent.getCookiePolicies(): List { + val cookiesPolicies = mutableListOf() + + if (cookieBehaviorCategory == ContentBlocking.CookieBehavior.ACCEPT_ALL) { + cookiesPolicies.add(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL) + } + + if (cookieBehaviorCategory.contains(ContentBlocking.CookieBehavior.ACCEPT_FIRST_PARTY)) { + cookiesPolicies.add(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY) + } + + if (cookieBehaviorCategory.contains(ContentBlocking.CookieBehavior.ACCEPT_NONE)) { + cookiesPolicies.add(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NONE) + } + + if (cookieBehaviorCategory.contains(ContentBlocking.CookieBehavior.ACCEPT_NON_TRACKERS)) { + cookiesPolicies.add(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS) + } + + if (cookieBehaviorCategory.contains(ContentBlocking.CookieBehavior.ACCEPT_VISITED)) { + cookiesPolicies.add(TrackingProtectionPolicy.CookiePolicy.ACCEPT_VISITED) + } + + return cookiesPolicies } private operator fun Int.contains(mask: Int): Boolean { diff --git a/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt b/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt index aa2bd3d352d..ff8710cc1b3 100644 --- a/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt +++ b/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt @@ -14,6 +14,9 @@ import mozilla.components.concept.engine.DefaultSettings import mozilla.components.concept.engine.EngineSession import mozilla.components.concept.engine.EngineSession.LoadUrlFlags import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.SafeBrowsingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy import mozilla.components.concept.engine.HitResult import mozilla.components.concept.engine.UnsupportedSettingException import mozilla.components.concept.engine.content.blocking.Tracker @@ -65,6 +68,9 @@ import org.mozilla.geckoview.WebRequestError import org.mozilla.geckoview.WebRequestError.ERROR_CATEGORY_UNKNOWN import org.mozilla.geckoview.WebRequestError.ERROR_MALFORMED_URI import org.mozilla.geckoview.WebRequestError.ERROR_UNKNOWN +typealias GeckoAntiTracking = ContentBlocking.AntiTracking +typealias GeckoSafeBrowsing = ContentBlocking.SafeBrowsing +typealias GeckoCookieBehavior = ContentBlocking.CookieBehavior @ExperimentalCoroutinesApi @RunWith(AndroidJUnit4::class) @@ -783,8 +789,10 @@ class GeckoEngineSessionTest { @Test fun trackingProtectionDelegateNotifiesObservers() { - val engineSession = GeckoEngineSession(mock(), - geckoSessionProvider = geckoSessionProvider) + val engineSession = GeckoEngineSession( + mock(), + geckoSessionProvider = geckoSessionProvider + ) var trackerBlocked: Tracker? = null engineSession.register(object : EngineSession.Observer { @@ -794,17 +802,75 @@ class GeckoEngineSessionTest { }) captureDelegates() - var geckoCatgories = 0 - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_AD) - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_ANALYTIC) - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_SOCIAL) - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CRYPTOMINING) - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_FINGERPRINTING) - geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CONTENT) - - contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", geckoCatgories)) + var geckoCategories = 0 + geckoCategories = geckoCategories.or(GeckoAntiTracking.AD) + geckoCategories = geckoCategories.or(GeckoAntiTracking.ANALYTIC) + geckoCategories = geckoCategories.or(GeckoAntiTracking.SOCIAL) + geckoCategories = geckoCategories.or(GeckoAntiTracking.CRYPTOMINING) + geckoCategories = geckoCategories.or(GeckoAntiTracking.FINGERPRINTING) + geckoCategories = geckoCategories.or(GeckoAntiTracking.CONTENT) + geckoCategories = geckoCategories.or(GeckoAntiTracking.TEST) + + contentBlockingDelegate.value.onContentBlocked( + geckoSession, + ContentBlocking.BlockEvent("tracker1", geckoCategories, 0, 0, false) + ) + assertEquals("tracker1", trackerBlocked!!.url) - assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList())) + + val expectedBlockedCategories = listOf( + TrackingCategory.AD, + TrackingCategory.ANALYTICS, + TrackingCategory.SOCIAL, + TrackingCategory.CRYPTOMINING, + TrackingCategory.FINGERPRINTING, + TrackingCategory.CONTENT, + TrackingCategory.TEST + ) + + assertTrue(trackerBlocked!!.trackingCategories.containsAll(expectedBlockedCategories)) + + var trackerLoaded: Tracker? = null + engineSession.register(object : EngineSession.Observer { + override fun onTrackerLoaded(tracker: Tracker) { + trackerLoaded = tracker + } + }) + + var geckoCookieCategories = 0 + geckoCookieCategories = geckoCookieCategories.or(GeckoCookieBehavior.ACCEPT_ALL) + geckoCookieCategories = geckoCookieCategories.or(GeckoCookieBehavior.ACCEPT_VISITED) + geckoCookieCategories = geckoCookieCategories.or(GeckoCookieBehavior.ACCEPT_NON_TRACKERS) + geckoCookieCategories = geckoCookieCategories.or(GeckoCookieBehavior.ACCEPT_NONE) + geckoCookieCategories = geckoCookieCategories.or(GeckoCookieBehavior.ACCEPT_FIRST_PARTY) + + contentBlockingDelegate.value.onContentLoaded( + geckoSession, + ContentBlocking.BlockEvent("tracker1", 0, 0, geckoCookieCategories, false) + ) + + val expectedCookieCategories = listOf( + CookiePolicy.ACCEPT_ONLY_FIRST_PARTY, + CookiePolicy.ACCEPT_NONE, + CookiePolicy.ACCEPT_VISITED, + CookiePolicy.ACCEPT_NON_TRACKERS + ) + + assertEquals("tracker1", trackerLoaded!!.url) + assertTrue(trackerLoaded!!.cookiePolicies.containsAll(expectedCookieCategories)) + + contentBlockingDelegate.value.onContentLoaded( + geckoSession, + ContentBlocking.BlockEvent("tracker1", 0, 0, GeckoCookieBehavior.ACCEPT_ALL, false) + ) + + assertTrue( + trackerLoaded!!.cookiePolicies.containsAll( + listOf( + CookiePolicy.ACCEPT_ALL + ) + ) + ) } @Test @@ -812,7 +878,11 @@ class GeckoEngineSessionTest { val runtime = mock() whenever(runtime.settings).thenReturn(mock()) val session = GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider) - val privSession = GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider, privateMode = true) + val privSession = GeckoEngineSession( + runtime, + geckoSessionProvider = geckoSessionProvider, + privateMode = true + ) var trackerBlockingObserved = false session.register(object : EngineSession.Observer { @@ -827,9 +897,15 @@ class GeckoEngineSessionTest { } }) - val allPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD) - val regularOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forRegularSessionsOnly() - val privateOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forPrivateSessionsOnly() + val allPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + ) + val regularOnlyPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + ).forRegularSessionsOnly() + val privateOnlyPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + ).forPrivateSessionsOnly() session.enableTrackingProtection(allPolicy) assertTrue(trackerBlockingObserved) @@ -869,32 +945,49 @@ class GeckoEngineSessionTest { @Test fun trackingProtectionCategoriesAreAligned() { - assertEquals(TrackingProtectionPolicy.AD, ContentBlocking.AT_AD) - assertEquals(TrackingProtectionPolicy.ANALYTICS, ContentBlocking.AT_ANALYTIC) - assertEquals(TrackingProtectionPolicy.CONTENT, ContentBlocking.AT_CONTENT) - assertEquals(TrackingProtectionPolicy.SOCIAL, ContentBlocking.AT_SOCIAL) - assertEquals(TrackingProtectionPolicy.TEST, ContentBlocking.AT_TEST) - assertEquals(TrackingProtectionPolicy.CRYPTOMINING, ContentBlocking.AT_CRYPTOMINING) - assertEquals(TrackingProtectionPolicy.FINGERPRINTING, ContentBlocking.AT_FINGERPRINTING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_ALL, ContentBlocking.SB_ALL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, ContentBlocking.SB_HARMFUL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, ContentBlocking.SB_MALWARE) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_PHISHING, ContentBlocking.SB_PHISHING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, ContentBlocking.SB_UNWANTED) - - 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(GeckoAntiTracking.NONE, TrackingCategory.NONE.id) + assertEquals(GeckoAntiTracking.AD, TrackingCategory.AD.id) + assertEquals(GeckoAntiTracking.CONTENT, TrackingCategory.CONTENT.id) + assertEquals(GeckoAntiTracking.SOCIAL, TrackingCategory.SOCIAL.id) + assertEquals(GeckoAntiTracking.TEST, TrackingCategory.TEST.id) + assertEquals(GeckoAntiTracking.CRYPTOMINING, TrackingCategory.CRYPTOMINING.id) + assertEquals(GeckoAntiTracking.FINGERPRINTING, TrackingCategory.FINGERPRINTING.id) + assertEquals(GeckoAntiTracking.DEFAULT, TrackingCategory.RECOMMENDED.id) + assertEquals(GeckoAntiTracking.STRICT, TrackingCategory.STRICT.id) + + assertEquals(GeckoSafeBrowsing.NONE, SafeBrowsingCategory.NONE.id) + assertEquals(GeckoSafeBrowsing.MALWARE, SafeBrowsingCategory.MALWARE.id) + assertEquals(GeckoSafeBrowsing.UNWANTED, SafeBrowsingCategory.UNWANTED.id) + assertEquals(GeckoSafeBrowsing.HARMFUL, SafeBrowsingCategory.HARMFUL.id) + assertEquals(GeckoSafeBrowsing.PHISHING, SafeBrowsingCategory.PHISHING.id) + assertEquals(GeckoSafeBrowsing.DEFAULT, SafeBrowsingCategory.RECOMMENDED.id) + + val recommendedPolicy = TrackingProtectionPolicy.recommended() + val strictPolicy = TrackingProtectionPolicy.strict() + var antiTrackingCategories = strictPolicy.trackingCategories.sumBy { it.id } + var antiSafeBrowsing = strictPolicy.safeBrowsingCategories.sumBy { it.id } + + assertEquals(GeckoAntiTracking.STRICT, antiTrackingCategories) + assertEquals(GeckoSafeBrowsing.DEFAULT, antiSafeBrowsing) + + antiTrackingCategories = recommendedPolicy.trackingCategories.sumBy { it.id } + antiSafeBrowsing = recommendedPolicy.safeBrowsingCategories.sumBy { it.id } + + assertEquals(GeckoAntiTracking.DEFAULT, antiTrackingCategories) + assertEquals(GeckoSafeBrowsing.DEFAULT, antiSafeBrowsing) + + assertEquals(GeckoCookieBehavior.ACCEPT_ALL, CookiePolicy.ACCEPT_ALL.id) assertEquals( - TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id, - ContentBlocking.COOKIE_ACCEPT_NON_TRACKERS + GeckoCookieBehavior.ACCEPT_NON_TRACKERS, + CookiePolicy.ACCEPT_NON_TRACKERS.id ) - assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_NONE.id, ContentBlocking.COOKIE_ACCEPT_NONE) + assertEquals(GeckoCookieBehavior.ACCEPT_NONE, CookiePolicy.ACCEPT_NONE.id) assertEquals( - TrackingProtectionPolicy.CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id, - ContentBlocking.COOKIE_ACCEPT_FIRST_PARTY + GeckoCookieBehavior.ACCEPT_FIRST_PARTY, CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id + ) - assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_VISITED.id, ContentBlocking.COOKIE_ACCEPT_VISITED) + assertEquals(GeckoCookieBehavior.ACCEPT_VISITED, CookiePolicy.ACCEPT_VISITED.id) } @Test @@ -988,7 +1081,7 @@ class GeckoEngineSessionTest { } expectException(UnsupportedSettingException::class) { - settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() + settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() } } @@ -1284,7 +1377,8 @@ class GeckoEngineSessionTest { val runtime = mock() whenever(runtime.settings).thenReturn(mock()) - val defaultSettings = DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.all()) + val defaultSettings = + DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.strict()) GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider, privateMode = false, defaultSettings = defaultSettings) diff --git a/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt b/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt index 4c7c547f19b..24aef09ac05 100644 --- a/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt +++ b/components/browser/engine-gecko-nightly/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt @@ -68,8 +68,7 @@ class GeckoEngineTest { @Test fun settings() { val defaultSettings = DefaultSettings() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() val runtime = mock() val runtimeSettings = mock() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) @@ -133,22 +132,17 @@ class GeckoEngineTest { engine.settings.userAgentString = engine.settings.userAgentString + "-test" assertEquals(GeckoSession.getDefaultUserAgent() + "-test", engine.settings.userAgentString) - assertEquals(TrackingProtectionPolicy.none(), engine.settings.trackingProtectionPolicy) - engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) - assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all()) + assertEquals(null, engine.settings.trackingProtectionPolicy) + + engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() + + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, contentBlockingSettings.antiTrackingCategories) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, contentBlockingSettings.safeBrowsingCategories) + + assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.strict()) assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id) try { @@ -166,8 +160,7 @@ class GeckoEngineTest { fun defaultSettings() { val runtime = mock() val runtimeSettings = mock() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) whenever(runtime.settings).thenReturn(runtimeSettings) whenever(runtimeSettings.contentBlocking).thenReturn(contentBlockingSettings) @@ -176,7 +169,7 @@ class GeckoEngineTest { val engine = GeckoEngine(context, DefaultSettings( - trackingProtectionPolicy = TrackingProtectionPolicy.all(), + trackingProtectionPolicy = TrackingProtectionPolicy.strict(), javascriptEnabled = false, webFontsEnabled = false, automaticFontSizeAdjustment = false, @@ -198,19 +191,11 @@ class GeckoEngineTest { verify(runtimeSettings).remoteDebuggingEnabled = true verify(runtimeSettings).autoplayDefault = GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, contentBlockingSettings.antiTrackingCategories) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, contentBlockingSettings.safeBrowsingCategories) assertEquals(CookiePolicy.ACCEPT_NON_TRACKERS.id, contentBlockingSettings.cookieBehavior) assertTrue(engine.settings.testingModeEnabled) @@ -220,9 +205,26 @@ class GeckoEngineTest { assertTrue(engine.settings.suspendMediaWhenInactive) engine.settings.trackingProtectionPolicy = - TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD, cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY) + TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingProtectionPolicy.TrackingCategory.AD), + safeBrowsingCategories = arrayOf(TrackingProtectionPolicy.SafeBrowsingCategory.PHISHING), + cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY + ) + + assertEquals( + TrackingProtectionPolicy.TrackingCategory.AD.id, + contentBlockingSettings.antiTrackingCategories + ) + + assertEquals( + TrackingProtectionPolicy.SafeBrowsingCategory.PHISHING.id, + contentBlockingSettings.safeBrowsingCategories + ) - assertEquals(CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id, contentBlockingSettings.cookieBehavior) + assertEquals( + CookiePolicy.ACCEPT_ONLY_FIRST_PARTY.id, + contentBlockingSettings.cookieBehavior + ) engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.none() diff --git a/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt b/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt index 2394a1c93a8..f19f24f41fe 100644 --- a/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt +++ b/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngine.kt @@ -156,13 +156,17 @@ class GeckoEngine( defaultSettings?.automaticLanguageAdjustment = value } - override var trackingProtectionPolicy: TrackingProtectionPolicy? - get() = TrackingProtectionPolicy.select(runtime.settings.contentBlocking.categories) + override var trackingProtectionPolicy: TrackingProtectionPolicy? = null set(value) { - value?.let { - runtime.settings.contentBlocking.categories = it.categories - runtime.settings.contentBlocking.cookieBehavior = it.cookiePolicy.id + value?.let { policy -> + + val trackingCategories = policy.trackingCategories.sumBy { it.id } + + policy.safeBrowsingCategories.sumBy { it.id } + + runtime.settings.contentBlocking.categories = trackingCategories + runtime.settings.contentBlocking.cookieBehavior = policy.cookiePolicy.id defaultSettings?.trackingProtectionPolicy = value + field = value } } diff --git a/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt b/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt index 72b91c12cd8..763e09503a9 100644 --- a/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt +++ b/components/browser/engine-gecko/src/main/java/mozilla/components/browser/engine/gecko/GeckoEngineSession.kt @@ -543,29 +543,33 @@ class GeckoEngineSession( } private fun ContentBlocking.BlockEvent.toTracker(): Tracker { - val blockedContentCategories = ArrayList() + val blockedContentCategories = mutableListOf() if (categories.contains(ContentBlocking.AT_AD)) { - blockedContentCategories.add(Tracker.Category.Ad) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.AD) } if (categories.contains(ContentBlocking.AT_ANALYTIC)) { - blockedContentCategories.add(Tracker.Category.Analytic) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.ANALYTICS) } if (categories.contains(ContentBlocking.AT_SOCIAL)) { - blockedContentCategories.add(Tracker.Category.Social) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.SOCIAL) } if (categories.contains(ContentBlocking.AT_FINGERPRINTING)) { - blockedContentCategories.add(Tracker.Category.Fingerprinting) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING) } if (categories.contains(ContentBlocking.AT_CRYPTOMINING)) { - blockedContentCategories.add(Tracker.Category.Cryptomining) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING) } if (categories.contains(ContentBlocking.AT_CONTENT)) { - blockedContentCategories.add(Tracker.Category.Content) + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.CONTENT) + } + + if (categories.contains(ContentBlocking.AT_TEST)) { + blockedContentCategories.add(TrackingProtectionPolicy.TrackingCategory.TEST) } return Tracker(uri, blockedContentCategories) } diff --git a/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt b/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt index 2b383a3664a..e1ac3cc68cd 100644 --- a/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt +++ b/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineSessionTest.kt @@ -14,6 +14,8 @@ import mozilla.components.concept.engine.DefaultSettings import mozilla.components.concept.engine.EngineSession import mozilla.components.concept.engine.EngineSession.LoadUrlFlags import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.SafeBrowsingCategory import mozilla.components.concept.engine.HitResult import mozilla.components.concept.engine.UnsupportedSettingException import mozilla.components.concept.engine.content.blocking.Tracker @@ -776,10 +778,22 @@ class GeckoEngineSessionTest { geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CRYPTOMINING) geckoCatgories = geckoCatgories.or(ContentBlocking.AT_FINGERPRINTING) geckoCatgories = geckoCatgories.or(ContentBlocking.AT_CONTENT) + geckoCatgories = geckoCatgories.or(ContentBlocking.AT_TEST) contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", geckoCatgories)) assertEquals("tracker1", trackerBlocked!!.url) - assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList())) + + val expectedBlockedCategories = listOf( + TrackingCategory.AD, + TrackingCategory.ANALYTICS, + TrackingCategory.SOCIAL, + TrackingCategory.CRYPTOMINING, + TrackingCategory.FINGERPRINTING, + TrackingCategory.CONTENT, + TrackingCategory.TEST + ) + + assertTrue(trackerBlocked!!.trackingCategories.containsAll(expectedBlockedCategories)) } @Test @@ -802,9 +816,15 @@ class GeckoEngineSessionTest { } }) - val allPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD) - val regularOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forRegularSessionsOnly() - val privateOnlyPolicy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forPrivateSessionsOnly() + val allPolicy = TrackingProtectionPolicy.select( + arrayOf(TrackingCategory.AD) + ) + val regularOnlyPolicy = TrackingProtectionPolicy.select( + arrayOf(TrackingCategory.AD) + ).forRegularSessionsOnly() + val privateOnlyPolicy = TrackingProtectionPolicy.select( + arrayOf(TrackingCategory.AD) + ).forPrivateSessionsOnly() session.enableTrackingProtection(allPolicy) assertTrue(trackerBlockingObserved) @@ -844,21 +864,40 @@ class GeckoEngineSessionTest { @Test fun trackingProtectionCategoriesAreAligned() { - assertEquals(TrackingProtectionPolicy.AD, ContentBlocking.AT_AD) - assertEquals(TrackingProtectionPolicy.ANALYTICS, ContentBlocking.AT_ANALYTIC) - assertEquals(TrackingProtectionPolicy.CONTENT, ContentBlocking.AT_CONTENT) - assertEquals(TrackingProtectionPolicy.SOCIAL, ContentBlocking.AT_SOCIAL) - assertEquals(TrackingProtectionPolicy.TEST, ContentBlocking.AT_TEST) - assertEquals(TrackingProtectionPolicy.CRYPTOMINING, ContentBlocking.AT_CRYPTOMINING) - assertEquals(TrackingProtectionPolicy.FINGERPRINTING, ContentBlocking.AT_FINGERPRINTING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_ALL, ContentBlocking.SB_ALL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, ContentBlocking.SB_HARMFUL) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, ContentBlocking.SB_MALWARE) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_PHISHING, ContentBlocking.SB_PHISHING) - assertEquals(TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, ContentBlocking.SB_UNWANTED) - - assertEquals(TrackingProtectionPolicy.all().categories, ContentBlocking.CB_STRICT) - assertEquals(TrackingProtectionPolicy.recommended().categories, ContentBlocking.CB_DEFAULT) + assertEquals(TrackingCategory.AD.id, ContentBlocking.AT_AD) + assertEquals(TrackingCategory.ANALYTICS.id, ContentBlocking.AT_ANALYTIC) + assertEquals(TrackingCategory.CONTENT.id, ContentBlocking.AT_CONTENT) + assertEquals(TrackingCategory.SOCIAL.id, ContentBlocking.AT_SOCIAL) + assertEquals(TrackingCategory.TEST.id, ContentBlocking.AT_TEST) + assertEquals(TrackingCategory.CRYPTOMINING.id, ContentBlocking.AT_CRYPTOMINING) + assertEquals(TrackingCategory.FINGERPRINTING.id, ContentBlocking.AT_FINGERPRINTING) + + assertEquals(SafeBrowsingCategory.RECOMMENDED.id, ContentBlocking.SB_ALL) + assertEquals(SafeBrowsingCategory.HARMFUL.id, ContentBlocking.SB_HARMFUL) + assertEquals(SafeBrowsingCategory.MALWARE.id, ContentBlocking.SB_MALWARE) + assertEquals(SafeBrowsingCategory.PHISHING.id, ContentBlocking.SB_PHISHING) + assertEquals(SafeBrowsingCategory.UNWANTED.id, ContentBlocking.SB_UNWANTED) + + assertEquals( + TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id }, + ContentBlocking.AT_STRICT + ) + + assertEquals( + TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id }, + ContentBlocking.SB_ALL + ) + + assertEquals( + TrackingProtectionPolicy.recommended().trackingCategories.sumBy { it.id }, + ContentBlocking.AT_DEFAULT + ) + + assertEquals( + TrackingProtectionPolicy.recommended().safeBrowsingCategories.sumBy { it.id }, + ContentBlocking.SB_ALL + ) + assertEquals(TrackingProtectionPolicy.CookiePolicy.ACCEPT_ALL.id, ContentBlocking.COOKIE_ACCEPT_ALL) assertEquals( TrackingProtectionPolicy.CookiePolicy.ACCEPT_NON_TRACKERS.id, @@ -963,7 +1002,7 @@ class GeckoEngineSessionTest { } expectException(UnsupportedSettingException::class) { - settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() + settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() } } @@ -1259,7 +1298,8 @@ class GeckoEngineSessionTest { val runtime = mock() whenever(runtime.settings).thenReturn(mock()) - val defaultSettings = DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.all()) + val defaultSettings = + DefaultSettings(trackingProtectionPolicy = TrackingProtectionPolicy.strict()) GeckoEngineSession(runtime, geckoSessionProvider = geckoSessionProvider, privateMode = false, defaultSettings = defaultSettings) diff --git a/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt b/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt index 57ce72dec0f..d76e02c3902 100644 --- a/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt +++ b/components/browser/engine-gecko/src/test/java/mozilla/components/browser/engine/gecko/GeckoEngineTest.kt @@ -68,8 +68,7 @@ class GeckoEngineTest { @Test fun settings() { val defaultSettings = DefaultSettings() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() val runtime = mock() val runtimeSettings = mock() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) @@ -133,22 +132,15 @@ class GeckoEngineTest { engine.settings.userAgentString = engine.settings.userAgentString + "-test" assertEquals(GeckoSession.getDefaultUserAgent() + "-test", engine.settings.userAgentString) - assertEquals(TrackingProtectionPolicy.none(), engine.settings.trackingProtectionPolicy) - engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) - assertEquals(defaultSettings.trackingProtectionPolicy, TrackingProtectionPolicy.all()) + assertEquals(null, engine.settings.trackingProtectionPolicy) + + engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() + + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, ContentBlocking.AT_STRICT) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, ContentBlocking.SB_ALL) assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id) try { @@ -166,8 +158,7 @@ class GeckoEngineTest { fun defaultSettings() { val runtime = mock() val runtimeSettings = mock() - val contentBlockingSettings = - ContentBlocking.Settings.Builder().categories(TrackingProtectionPolicy.none().categories).build() + val contentBlockingSettings = ContentBlocking.Settings.Builder().build() whenever(runtimeSettings.javaScriptEnabled).thenReturn(true) whenever(runtime.settings).thenReturn(runtimeSettings) whenever(runtimeSettings.contentBlocking).thenReturn(contentBlockingSettings) @@ -176,7 +167,7 @@ class GeckoEngineTest { val engine = GeckoEngine(context, DefaultSettings( - trackingProtectionPolicy = TrackingProtectionPolicy.all(), + trackingProtectionPolicy = TrackingProtectionPolicy.strict(), javascriptEnabled = false, webFontsEnabled = false, automaticFontSizeAdjustment = false, @@ -198,19 +189,12 @@ class GeckoEngineTest { verify(runtimeSettings).remoteDebuggingEnabled = true verify(runtimeSettings).autoplayDefault = GeckoRuntimeSettings.AUTOPLAY_DEFAULT_BLOCKED - assertEquals(TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, - TrackingProtectionPolicy.SOCIAL, - TrackingProtectionPolicy.ANALYTICS, - TrackingProtectionPolicy.CONTENT, - TrackingProtectionPolicy.TEST, - TrackingProtectionPolicy.CRYPTOMINING, - TrackingProtectionPolicy.FINGERPRINTING, - TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL, - TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED, - TrackingProtectionPolicy.SAFE_BROWSING_MALWARE, - TrackingProtectionPolicy.SAFE_BROWSING_PHISHING - ).categories, contentBlockingSettings.categories) + val trackingStrictCategories = TrackingProtectionPolicy.strict().trackingCategories.sumBy { it.id } + assertEquals(trackingStrictCategories, ContentBlocking.AT_STRICT) + + val safeStrictBrowsingCategories = TrackingProtectionPolicy.strict().safeBrowsingCategories.sumBy { it.id } + assertEquals(safeStrictBrowsingCategories, ContentBlocking.SB_ALL) + assertEquals(contentBlockingSettings.cookieBehavior, CookiePolicy.ACCEPT_NON_TRACKERS.id) assertTrue(engine.settings.testingModeEnabled) assertEquals("test-ua", engine.settings.userAgentString) @@ -220,7 +204,7 @@ class GeckoEngineTest { engine.settings.trackingProtectionPolicy = TrackingProtectionPolicy.select( - TrackingProtectionPolicy.AD, + trackingCategories = arrayOf(TrackingProtectionPolicy.TrackingCategory.AD), cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY ) diff --git a/components/browser/engine-system/src/main/java/mozilla/components/browser/engine/system/SystemEngineView.kt b/components/browser/engine-system/src/main/java/mozilla/components/browser/engine/system/SystemEngineView.kt index a499402fb81..def6c0f1cbb 100644 --- a/components/browser/engine-system/src/main/java/mozilla/components/browser/engine/system/SystemEngineView.kt +++ b/components/browser/engine-system/src/main/java/mozilla/components/browser/engine/system/SystemEngineView.kt @@ -723,15 +723,15 @@ class SystemEngineView @JvmOverloads constructor( internal var URL_MATCHER: UrlMatcher? = null private val urlMatcherCategoryMap = mapOf( - UrlMatcher.ADVERTISING to TrackingProtectionPolicy.AD, - UrlMatcher.ANALYTICS to TrackingProtectionPolicy.ANALYTICS, - UrlMatcher.CONTENT to TrackingProtectionPolicy.CONTENT, - UrlMatcher.SOCIAL to TrackingProtectionPolicy.SOCIAL + UrlMatcher.ADVERTISING to TrackingProtectionPolicy.TrackingCategory.AD, + UrlMatcher.ANALYTICS to TrackingProtectionPolicy.TrackingCategory.ANALYTICS, + UrlMatcher.CONTENT to TrackingProtectionPolicy.TrackingCategory.CONTENT, + UrlMatcher.SOCIAL to TrackingProtectionPolicy.TrackingCategory.SOCIAL ) @Synchronized internal fun getOrCreateUrlMatcher(resources: Resources, policy: TrackingProtectionPolicy): UrlMatcher { - val categories = urlMatcherCategoryMap.filterValues { policy.contains(it) }.keys + val categories = urlMatcherCategoryMap.filterValues { policy.trackingCategories.contains(it) }.keys URL_MATCHER?.setCategoriesEnabled(categories) ?: run { URL_MATCHER = UrlMatcher.createMatcher( diff --git a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineSessionTest.kt b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineSessionTest.kt index dfb2ed8cff0..6f5429958b5 100644 --- a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineSessionTest.kt +++ b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineSessionTest.kt @@ -235,7 +235,10 @@ class SystemEngineSessionTest { assertNull(engineSession.trackingProtectionPolicy) runBlocking { engineSession.enableTrackingProtection() } - assertEquals(EngineSession.TrackingProtectionPolicy.all(), engineSession.trackingProtectionPolicy) + assertEquals( + EngineSession.TrackingProtectionPolicy.strict(), + engineSession.trackingProtectionPolicy + ) assertNotNull(enabledObserved) assertTrue(enabledObserved as Boolean) } @@ -250,7 +253,7 @@ class SystemEngineSessionTest { } }) - engineSession.trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.all() + engineSession.trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.strict() engineSession.disableTrackingProtection() assertNull(engineSession.trackingProtectionPolicy) @@ -345,8 +348,9 @@ class SystemEngineSessionTest { assertFalse(engineSession.settings.webFontsEnabled) assertNull(engineSession.settings.trackingProtectionPolicy) - engineSession.settings.trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.all() - verify(engineSession).enableTrackingProtection(EngineSession.TrackingProtectionPolicy.all()) + engineSession.settings.trackingProtectionPolicy = + EngineSession.TrackingProtectionPolicy.strict() + verify(engineSession).enableTrackingProtection(EngineSession.TrackingProtectionPolicy.strict()) engineSession.settings.trackingProtectionPolicy = null verify(engineSession).disableTrackingProtection() @@ -366,7 +370,7 @@ class SystemEngineSessionTest { javascriptEnabled = false, domStorageEnabled = false, webFontsEnabled = false, - trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.all(), + trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.strict(), userAgentString = "userAgent", mediaPlaybackRequiresUserGesture = false, javaScriptCanOpenWindowsAutomatically = true, @@ -393,7 +397,7 @@ class SystemEngineSessionTest { verify(webViewSettings).loadWithOverviewMode = true verify(webViewSettings).useWideViewPort = true verify(webViewSettings).setSupportMultipleWindows(true) - verify(engineSession).enableTrackingProtection(EngineSession.TrackingProtectionPolicy.all()) + verify(engineSession).enableTrackingProtection(EngineSession.TrackingProtectionPolicy.strict()) assertFalse(engineSession.webFontsEnabled) } diff --git a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineTest.kt b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineTest.kt index 3d83b37ad93..8831898526d 100644 --- a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineTest.kt +++ b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineTest.kt @@ -54,14 +54,17 @@ class SystemEngineTest { fun settings() { val engine = SystemEngine(testContext, DefaultSettings( remoteDebuggingEnabled = true, - trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.all() + trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.strict() )) assertTrue(engine.settings.remoteDebuggingEnabled) engine.settings.remoteDebuggingEnabled = false assertFalse(engine.settings.remoteDebuggingEnabled) - assertEquals(engine.settings.trackingProtectionPolicy, EngineSession.TrackingProtectionPolicy.all()) + assertEquals( + engine.settings.trackingProtectionPolicy, + EngineSession.TrackingProtectionPolicy.strict() + ) engine.settings.trackingProtectionPolicy = EngineSession.TrackingProtectionPolicy.none() assertEquals(engine.settings.trackingProtectionPolicy, EngineSession.TrackingProtectionPolicy.none()) diff --git a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineViewTest.kt b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineViewTest.kt index 8835d9642d5..b016db22a29 100644 --- a/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineViewTest.kt +++ b/components/browser/engine-system/src/test/java/mozilla/components/browser/engine/system/SystemEngineViewTest.kt @@ -68,6 +68,7 @@ import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config import java.util.Calendar import java.util.Date +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory @RunWith(AndroidJUnit4::class) class SystemEngineViewTest { @@ -477,7 +478,7 @@ class SystemEngineViewTest { var response = webViewClient.shouldInterceptRequest(engineSession.webView, invalidRequest) assertNull(response) - engineSession.trackingProtectionPolicy = TrackingProtectionPolicy.all() + engineSession.trackingProtectionPolicy = TrackingProtectionPolicy.strict() response = webViewClient.shouldInterceptRequest(engineSession.webView, invalidRequest) assertNotNull(response) assertNull(response!!.data) @@ -899,13 +900,25 @@ class SystemEngineViewTest { SystemEngineView.URL_MATCHER = null val resources = testContext.resources - var urlMatcher = SystemEngineView.getOrCreateUrlMatcher(resources, - TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD, TrackingProtectionPolicy.ANALYTICS) + var urlMatcher = SystemEngineView.getOrCreateUrlMatcher( + resources, + TrackingProtectionPolicy.select( + arrayOf( + TrackingCategory.AD, + TrackingCategory.ANALYTICS + ) + ) ) assertEquals(setOf(UrlMatcher.ADVERTISING, UrlMatcher.ANALYTICS), urlMatcher.enabledCategories) - urlMatcher = SystemEngineView.getOrCreateUrlMatcher(resources, - TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD, TrackingProtectionPolicy.SOCIAL) + urlMatcher = SystemEngineView.getOrCreateUrlMatcher( + resources, + TrackingProtectionPolicy.select( + arrayOf( + TrackingCategory.AD, + TrackingCategory.SOCIAL + ) + ) ) assertEquals(setOf(UrlMatcher.ADVERTISING, UrlMatcher.SOCIAL), urlMatcher.enabledCategories) } diff --git a/components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt b/components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt index 0896903301b..1e7b2ac58ff 100644 --- a/components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt +++ b/components/browser/session/src/main/java/mozilla/components/browser/session/Session.kt @@ -85,6 +85,7 @@ class Session( fun onDownload(session: Session, download: Download): Boolean = false fun onTrackerBlockingEnabledChanged(session: Session, blockingEnabled: Boolean) = Unit fun onTrackerBlocked(session: Session, tracker: Tracker, all: List) = Unit + fun onTrackerLoaded(session: Session, tracker: Tracker, all: List) = Unit fun onLongPress(session: Session, hitResult: HitResult): Boolean = false fun onFindResult(session: Session, result: FindResult) = Unit fun onDesktopModeChanged(session: Session, enabled: Boolean) = Unit @@ -313,6 +314,17 @@ class Session( } } + /** + * List of [Tracker]s that could be blocked but have been loaded in this session. + */ + var trackersLoaded: List by Delegates.observable(emptyList()) { _, old, new -> + notifyObservers(old, new) { + if (new.isNotEmpty()) { + onTrackerLoaded(this@Session, new.last(), new) + } + } + } + /** * List of results of that latest "find in page" operation. */ diff --git a/components/browser/session/src/main/java/mozilla/components/browser/session/engine/EngineObserver.kt b/components/browser/session/src/main/java/mozilla/components/browser/session/engine/EngineObserver.kt index 5736be16009..8eff19b872c 100644 --- a/components/browser/session/src/main/java/mozilla/components/browser/session/engine/EngineObserver.kt +++ b/components/browser/session/src/main/java/mozilla/components/browser/session/engine/EngineObserver.kt @@ -76,6 +76,7 @@ internal class EngineObserver( if (loading) { session.findResults = emptyList() session.trackersBlocked = emptyList() + session.trackersLoaded = emptyList() } } @@ -93,6 +94,10 @@ internal class EngineObserver( session.trackersBlocked += tracker } + override fun onTrackerLoaded(tracker: Tracker) { + session.trackersLoaded += tracker + } + override fun onTrackerBlockingEnabledChange(enabled: Boolean) { session.trackerBlockingEnabled = enabled } diff --git a/components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt b/components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt index 8425be03f40..dfa890a7f2b 100644 --- a/components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt +++ b/components/browser/session/src/test/java/mozilla/components/browser/session/SessionTest.kt @@ -597,6 +597,40 @@ class SessionTest { verifyNoMoreInteractions(observer) } + @Test + fun `observer is notified when a tracker is loaded`() { + val observer = mock(Session.Observer::class.java) + + val session = Session("https://www.mozilla.org") + session.register(observer) + + val tracker1 = Tracker("trackerUrl1") + val tracker2 = Tracker("trackerUrl2") + + session.trackersLoaded += tracker1 + + verify(observer).onTrackerLoaded( + eq(session), + eq(tracker1), + eq(listOf(tracker1)) + ) + + assertEquals(listOf(tracker1), session.trackersLoaded) + + session.trackersLoaded += tracker2 + + verify(observer).onTrackerLoaded( + eq(session), + eq(tracker2), + eq(listOf(tracker1, tracker2)) + ) + + assertEquals(listOf(tracker1, tracker2), session.trackersLoaded) + + session.trackersLoaded = emptyList() + verifyNoMoreInteractions(observer) + } + @Test fun `observer is notified when tracker blocking is enabled`() { val observer = mock(Session.Observer::class.java) @@ -917,6 +951,33 @@ class SessionTest { } } + @Test + fun `handle empty loaded trackers list race conditions`() { + val observer = mock(Session.Observer::class.java) + val observer2 = mock(Session.Observer::class.java) + + val session = Session("about:blank") + session.register(observer) + session.register(observer2) + + runBlocking { + (1..3).map { + val def = GlobalScope.async(IO) { + session.trackersLoaded = emptyList() + session.trackersLoaded += Tracker("test") + session.trackersLoaded = emptyList() + } + val def2 = GlobalScope.async(IO) { + session.trackersLoaded = emptyList() + session.trackersLoaded += Tracker("test") + session.trackersLoaded = emptyList() + } + def.await() + def2.await() + } + } + } + @Test fun `toString returns string containing id and url`() { val session = Session(id = "my-session-id", initialUrl = "https://www.mozilla.org") diff --git a/components/browser/session/src/test/java/mozilla/components/browser/session/engine/EngineObserverTest.kt b/components/browser/session/src/test/java/mozilla/components/browser/session/engine/EngineObserverTest.kt index 346ecac834b..a7b760436cd 100644 --- a/components/browser/session/src/test/java/mozilla/components/browser/session/engine/EngineObserverTest.kt +++ b/components/browser/session/src/test/java/mozilla/components/browser/session/engine/EngineObserverTest.kt @@ -214,6 +214,21 @@ class EngineObserverTest { assertEquals(emptyList(), session.trackersBlocked) } + @Test + fun engineObserverClearsLoadedTrackersIfNewPageStartsLoading() { + val session = Session("https://www.mozilla.org") + val observer = EngineObserver(session) + + val tracker1 = Tracker("tracker1") + val tracker2 = Tracker("tracker2") + observer.onTrackerLoaded(tracker1) + observer.onTrackerLoaded(tracker2) + assertEquals(listOf(tracker1, tracker2), session.trackersLoaded) + + observer.onLoadingStateChange(true) + assertEquals(emptyList(), session.trackersLoaded) + } + @Test fun engineObserverPassingHitResult() { val session = Session("https://www.mozilla.org") diff --git a/components/concept/engine/src/main/java/mozilla/components/concept/engine/EngineSession.kt b/components/concept/engine/src/main/java/mozilla/components/concept/engine/EngineSession.kt index c31bbf829a5..2ae80290ce2 100644 --- a/components/concept/engine/src/main/java/mozilla/components/concept/engine/EngineSession.kt +++ b/components/concept/engine/src/main/java/mozilla/components/concept/engine/EngineSession.kt @@ -6,8 +6,10 @@ package mozilla.components.concept.engine import android.graphics.Bitmap import androidx.annotation.CallSuper +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.Companion.RECOMMENDED 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.EngineSession.TrackingProtectionPolicy.TrackingCategory.RECOMMENDED import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.manifest.WebAppManifest import mozilla.components.concept.engine.media.Media @@ -39,6 +41,7 @@ abstract class EngineSession( fun onSecurityChange(secure: Boolean, host: String? = null, issuer: String? = null) = Unit fun onTrackerBlockingEnabledChange(enabled: Boolean) = Unit fun onTrackerBlocked(tracker: Tracker) = Unit + fun onTrackerLoaded(tracker: Tracker) = Unit fun onLongPress(hitResult: HitResult) = Unit fun onDesktopModeChange(enabled: Boolean) = Unit fun onFind(text: String) = Unit @@ -91,7 +94,8 @@ abstract class EngineSession( * [TrackingProtectionPolicyForSessionTypes]). */ open class TrackingProtectionPolicy internal constructor( - val categories: Int, + val trackingCategories: Array = arrayOf(TrackingCategory.RECOMMENDED), + val safeBrowsingCategories: Array = arrayOf(SafeBrowsingCategory.RECOMMENDED), val useForPrivateSessions: Boolean = true, val useForRegularSessions: Boolean = true, val cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS @@ -133,95 +137,140 @@ abstract class EngineSession( ACCEPT_NON_TRACKERS(4) } - companion object { - internal const val NONE: Int = 0 + @Suppress("MagicNumber") + enum class TrackingCategory(val id: Int) { + + NONE(0), + /** * Blocks advertisement trackers. */ - const val AD: Int = 1 shl 1 + AD(1 shl 1), + /** * Blocks analytics trackers. */ - const val ANALYTICS: Int = 1 shl 2 + ANALYTICS(1 shl 2), + /** * Blocks social trackers. */ - const val SOCIAL: Int = 1 shl 3 + SOCIAL(1 shl 3), + /** * Blocks content trackers. * May cause issues with some web sites. */ - const val CONTENT: Int = 1 shl 4 - // This policy is just to align categories with GeckoView (which has AT_TEST = 1 << 5) - const val TEST: Int = 1 shl 5 + CONTENT(1 shl 4), + + // This policy is just to align categories with GeckoView + TEST(1 shl 5), + /** * Blocks cryptocurrency miners. */ - const val CRYPTOMINING = 1 shl 6 + CRYPTOMINING(1 shl 6), + /** * Blocks fingerprinting trackers. */ - const val FINGERPRINTING = 1 shl 7 + FINGERPRINTING(1 shl 7), + + RECOMMENDED(AD.id + ANALYTICS.id + SOCIAL.id + TEST.id), + + /** + * Combining the [RECOMMENDED] categories plus [CRYPTOMINING], + * [FINGERPRINTING] and [CONTENT]. + */ + STRICT(RECOMMENDED.id + CRYPTOMINING.id + FINGERPRINTING.id + CONTENT.id) + } + + @Suppress("MagicNumber") + enum class SafeBrowsingCategory(val id: Int) { + NONE(0), + /** * Blocks malware sites. */ - const val SAFE_BROWSING_MALWARE = 1 shl 10 + MALWARE(1 shl 10), + /** * Blocks unwanted sites. */ - const val SAFE_BROWSING_UNWANTED = 1 shl 11 + UNWANTED(1 shl 11), + /** * Blocks harmful sites. */ - const val SAFE_BROWSING_HARMFUL = 1 shl 12 + HARMFUL(1 shl 12), + /** * Blocks phishing sites. */ - const val SAFE_BROWSING_PHISHING = 1 shl 13 + PHISHING(1 shl 13), + /** * Blocks all unsafe sites. */ - const val SAFE_BROWSING_ALL = - SAFE_BROWSING_MALWARE + SAFE_BROWSING_UNWANTED + SAFE_BROWSING_HARMFUL + SAFE_BROWSING_PHISHING + RECOMMENDED(MALWARE.id + UNWANTED.id + HARMFUL.id + PHISHING.id) + } - const val RECOMMENDED = AD + ANALYTICS + SOCIAL + TEST + SAFE_BROWSING_ALL + companion object { - const val ALL = RECOMMENDED + CRYPTOMINING + FINGERPRINTING + CONTENT + internal val RECOMMENDED = TrackingProtectionPolicy() - fun none() = TrackingProtectionPolicy(NONE, cookiePolicy = ACCEPT_ALL) + fun none() = TrackingProtectionPolicy( + trackingCategories = arrayOf(TrackingCategory.NONE), + safeBrowsingCategories = arrayOf(SafeBrowsingCategory.NONE), + cookiePolicy = ACCEPT_ALL + ) /** * Strict policy. - * Combining the [recommended] categories plus [CRYPTOMINING], [FINGERPRINTING] and [CONTENT]. + * Combining the [TrackingCategory.STRICT] plus [SafeBrowsingCategory.RECOMMENDED] * With a cookiePolicy of [ACCEPT_NON_TRACKERS]. * This is the strictest setting and may cause issues on some web sites. */ - fun all() = TrackingProtectionPolicyForSessionTypes(ALL) + fun strict() = TrackingProtectionPolicyForSessionTypes( + trackingCategory = arrayOf(TrackingCategory.STRICT), + safeBrowsingCategory = arrayOf(SafeBrowsingCategory.RECOMMENDED), + cookiePolicy = ACCEPT_NON_TRACKERS + ) /** * Recommended policy. - * Combining the [AD], [ANALYTICS], [SOCIAL], [TEST] categories plus [SAFE_BROWSING_ALL]. + * Combining the [TrackingCategory.RECOMMENDED] plus [SafeBrowsingCategory.RECOMMENDED]. * With a [CookiePolicy] of [ACCEPT_NON_TRACKERS]. * This is the recommended setting. */ - fun recommended() = TrackingProtectionPolicyForSessionTypes(RECOMMENDED) - - fun select(vararg categories: Int, cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS) = - TrackingProtectionPolicyForSessionTypes(categories.sum(), cookiePolicy) + fun recommended() = TrackingProtectionPolicyForSessionTypes( + trackingCategory = arrayOf(TrackingCategory.RECOMMENDED), + safeBrowsingCategory = arrayOf(SafeBrowsingCategory.RECOMMENDED), + cookiePolicy = ACCEPT_NON_TRACKERS + ) + + fun select( + trackingCategories: Array = arrayOf(TrackingCategory.RECOMMENDED), + safeBrowsingCategories: Array = arrayOf(SafeBrowsingCategory.RECOMMENDED), + cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS + ) = TrackingProtectionPolicyForSessionTypes( + trackingCategories, + safeBrowsingCategories, + cookiePolicy + ) } - fun contains(category: Int) = (categories and category) != 0 - override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is TrackingProtectionPolicy) return false - if (categories != other.categories) return false + if (hashCode() != other.hashCode()) return false if (useForPrivateSessions != other.useForPrivateSessions) return false if (useForRegularSessions != other.useForRegularSessions) return false return true } - override fun hashCode() = categories + override fun hashCode() = + trackingCategories.sumBy { it.id } + safeBrowsingCategories.sumBy { it.id } + cookiePolicy.id } /** @@ -229,14 +278,20 @@ abstract class EngineSession( * should be applied to. By default, a policy will be applied to all sessions. */ class TrackingProtectionPolicyForSessionTypes internal constructor( - categories: Int, + trackingCategory: Array = arrayOf(TrackingCategory.RECOMMENDED), + safeBrowsingCategory: Array = arrayOf(SafeBrowsingCategory.RECOMMENDED), cookiePolicy: CookiePolicy = ACCEPT_NON_TRACKERS - ) : TrackingProtectionPolicy(categories, cookiePolicy = cookiePolicy) { + ) : TrackingProtectionPolicy( + trackingCategories = trackingCategory, + safeBrowsingCategories = safeBrowsingCategory, + cookiePolicy = cookiePolicy + ) { /** * Marks this policy to be used for private sessions only. */ fun forPrivateSessionsOnly() = TrackingProtectionPolicy( - categories, + trackingCategories = trackingCategories, + safeBrowsingCategories = safeBrowsingCategories, useForPrivateSessions = true, useForRegularSessions = false, cookiePolicy = cookiePolicy @@ -246,7 +301,8 @@ abstract class EngineSession( * Marks this policy to be used for regular (non-private) sessions only. */ fun forRegularSessionsOnly() = TrackingProtectionPolicy( - categories, + trackingCategories = trackingCategories, + safeBrowsingCategories = safeBrowsingCategories, useForPrivateSessions = false, useForRegularSessions = true, cookiePolicy = cookiePolicy @@ -353,7 +409,7 @@ abstract class EngineSession( * * @param policy the tracking protection policy to use, defaults to blocking all trackers. */ - abstract fun enableTrackingProtection(policy: TrackingProtectionPolicy = TrackingProtectionPolicy.all()) + abstract fun enableTrackingProtection(policy: TrackingProtectionPolicy = TrackingProtectionPolicy.strict()) /** * Disables tracking protection for this engine session. diff --git a/components/concept/engine/src/main/java/mozilla/components/concept/engine/content/blocking/Tracker.kt b/components/concept/engine/src/main/java/mozilla/components/concept/engine/content/blocking/Tracker.kt index 20811de53f6..6dcbe79ccb3 100644 --- a/components/concept/engine/src/main/java/mozilla/components/concept/engine/content/blocking/Tracker.kt +++ b/components/concept/engine/src/main/java/mozilla/components/concept/engine/content/blocking/Tracker.kt @@ -4,13 +4,17 @@ package mozilla.components.concept.engine.content.blocking +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy + /** * Represents a blocked content tracker. * @property url The URL of the tracker. - * @property categories A list of categories that this [Tracker] belongs. + * @property trackingCategories The anti-tracking category types of the blocked resource. + * @property cookiePolicies The cookie types of the blocked resource. */ -class Tracker(val url: String, val categories: List = emptyList()) { - enum class Category { - Ad, Analytic, Social, Cryptomining, Fingerprinting, Content - } -} +class Tracker( + val url: String, + val trackingCategories: List = emptyList(), + val cookiePolicies: List = emptyList() +) diff --git a/components/concept/engine/src/test/java/mozilla/components/concept/engine/EngineSessionTest.kt b/components/concept/engine/src/test/java/mozilla/components/concept/engine/EngineSessionTest.kt index 945648e531f..b67ab2385db 100644 --- a/components/concept/engine/src/test/java/mozilla/components/concept/engine/EngineSessionTest.kt +++ b/components/concept/engine/src/test/java/mozilla/components/concept/engine/EngineSessionTest.kt @@ -23,6 +23,9 @@ import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.Mockito.verifyNoMoreInteractions import org.mockito.Mockito.verifyZeroInteractions +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.SafeBrowsingCategory +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.CookiePolicy class EngineSessionTest { private val unknownHitResult = HitResult.UNKNOWN("file://foobar") @@ -543,40 +546,69 @@ class EngineSessionTest { @Test fun `tracking protection policies have correct categories`() { - assertEquals(TrackingProtectionPolicy.RECOMMENDED, TrackingProtectionPolicy.recommended().categories) - assertEquals(TrackingProtectionPolicy.ALL, TrackingProtectionPolicy.all().categories) - assertEquals(TrackingProtectionPolicy.NONE, TrackingProtectionPolicy.none().categories) - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).categories)) - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.ANALYTICS).categories)) - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.CONTENT).categories)) - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.TEST).categories)) - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.SOCIAL).categories)) - - assertTrue(TrackingProtectionPolicy.all().contains( - TrackingProtectionPolicy.select(TrackingProtectionPolicy.SAFE_BROWSING_ALL).categories)) - - val policy = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD, TrackingProtectionPolicy.ANALYTICS) - assertTrue(policy.contains(TrackingProtectionPolicy.AD)) - assertTrue(policy.contains(TrackingProtectionPolicy.ANALYTICS)) - assertFalse(policy.contains(TrackingProtectionPolicy.SOCIAL)) - assertFalse(policy.contains(TrackingProtectionPolicy.CONTENT)) - assertFalse(policy.contains(TrackingProtectionPolicy.TEST)) - assertFalse(policy.contains(TrackingProtectionPolicy.SAFE_BROWSING_ALL)) - assertFalse(policy.contains(TrackingProtectionPolicy.SAFE_BROWSING_HARMFUL)) - assertFalse(policy.contains(TrackingProtectionPolicy.SAFE_BROWSING_MALWARE)) - assertFalse(policy.contains(TrackingProtectionPolicy.SAFE_BROWSING_PHISHING)) - assertFalse(policy.contains(TrackingProtectionPolicy.SAFE_BROWSING_UNWANTED)) + + assertEquals(TrackingProtectionPolicy.RECOMMENDED, TrackingProtectionPolicy.recommended()) + + val strictPolicy = TrackingProtectionPolicy.strict() + + assertEquals( + strictPolicy.trackingCategories.sumBy { it.id }, + TrackingCategory.STRICT.id + ) + + assertEquals( + strictPolicy.safeBrowsingCategories.sumBy { it.id }, + SafeBrowsingCategory.RECOMMENDED.id + ) + + assertEquals(strictPolicy.cookiePolicy.id, CookiePolicy.ACCEPT_NON_TRACKERS.id) + + val nonePolicy = TrackingProtectionPolicy.none() + + assertEquals( + nonePolicy.trackingCategories.sumBy { it.id }, + TrackingCategory.NONE.id + ) + + assertEquals( + nonePolicy.safeBrowsingCategories.sumBy { it.id }, + SafeBrowsingCategory.NONE.id + ) + + assertEquals(nonePolicy.cookiePolicy.id, CookiePolicy.ACCEPT_ALL.id) + + val newPolicy = TrackingProtectionPolicy.select( + trackingCategories = arrayOf( + TrackingCategory.AD, + TrackingCategory.SOCIAL, + TrackingCategory.ANALYTICS, + TrackingCategory.CONTENT, + TrackingCategory.CRYPTOMINING, + TrackingCategory.FINGERPRINTING, + TrackingCategory.TEST + ) + ) + + assertEquals( + newPolicy.trackingCategories.sumBy { it.id }, + arrayOf( + TrackingCategory.AD, + TrackingCategory.SOCIAL, + TrackingCategory.ANALYTICS, + TrackingCategory.CONTENT, + TrackingCategory.CRYPTOMINING, + TrackingCategory.FINGERPRINTING, + TrackingCategory.TEST + ).sumBy { it.id }) } @Test fun `tracking protection policies can be specified for session type`() { - val all = TrackingProtectionPolicy.all() - val selected = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD) + val all = TrackingProtectionPolicy.strict() + val selected = TrackingProtectionPolicy.select( + trackingCategories = arrayOf(TrackingCategory.AD) + + ) // Tracking protection policies should be applied to all sessions by default assertTrue(all.useForPrivateSessions) @@ -584,11 +616,14 @@ class EngineSessionTest { assertTrue(selected.useForPrivateSessions) assertTrue(selected.useForRegularSessions) - val allForPrivate = TrackingProtectionPolicy.all().forPrivateSessionsOnly() + val allForPrivate = TrackingProtectionPolicy.strict().forPrivateSessionsOnly() assertTrue(allForPrivate.useForPrivateSessions) assertFalse(allForPrivate.useForRegularSessions) - val selectedForRegular = TrackingProtectionPolicy.select(TrackingProtectionPolicy.AD).forRegularSessionsOnly() + val selectedForRegular = + TrackingProtectionPolicy.select(trackingCategories = arrayOf(TrackingCategory.AD)) + .forRegularSessionsOnly() + assertTrue(selectedForRegular.useForRegularSessions) assertFalse(selectedForRegular.useForPrivateSessions) } diff --git a/components/concept/engine/src/test/java/mozilla/components/concept/engine/SettingsTest.kt b/components/concept/engine/src/test/java/mozilla/components/concept/engine/SettingsTest.kt index ca2f0a235c4..a885afd513a 100644 --- a/components/concept/engine/src/test/java/mozilla/components/concept/engine/SettingsTest.kt +++ b/components/concept/engine/src/test/java/mozilla/components/concept/engine/SettingsTest.kt @@ -34,7 +34,7 @@ class SettingsTest { { settings.automaticLanguageAdjustment }, { settings.automaticLanguageAdjustment = false }, { settings.trackingProtectionPolicy }, - { settings.trackingProtectionPolicy = TrackingProtectionPolicy.all() }, + { settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict() }, { settings.historyTrackingDelegate }, { settings.historyTrackingDelegate = null }, { settings.requestInterceptor }, @@ -128,7 +128,7 @@ class SettingsTest { webFontsEnabled = false, automaticFontSizeAdjustment = false, automaticLanguageAdjustment = false, - trackingProtectionPolicy = TrackingProtectionPolicy.all(), + trackingProtectionPolicy = TrackingProtectionPolicy.strict(), historyTrackingDelegate = historyTrackingDelegate, requestInterceptor = interceptor, userAgentString = "userAgent", @@ -157,7 +157,7 @@ class SettingsTest { assertFalse(defaultSettings.webFontsEnabled) assertFalse(defaultSettings.automaticFontSizeAdjustment) assertFalse(defaultSettings.automaticLanguageAdjustment) - assertEquals(TrackingProtectionPolicy.all(), defaultSettings.trackingProtectionPolicy) + assertEquals(TrackingProtectionPolicy.strict(), defaultSettings.trackingProtectionPolicy) assertEquals(historyTrackingDelegate, defaultSettings.historyTrackingDelegate) assertEquals(interceptor, defaultSettings.requestInterceptor) assertEquals("userAgent", defaultSettings.userAgentString) diff --git a/components/feature/session/src/test/java/mozilla/components/feature/session/SettingsUseCasesTest.kt b/components/feature/session/src/test/java/mozilla/components/feature/session/SettingsUseCasesTest.kt index d09efd91b69..fcdf2af6da9 100644 --- a/components/feature/session/src/test/java/mozilla/components/feature/session/SettingsUseCasesTest.kt +++ b/components/feature/session/src/test/java/mozilla/components/feature/session/SettingsUseCasesTest.kt @@ -56,9 +56,9 @@ class SettingsUseCasesTest { verify(engineSessionA).enableTrackingProtection(TrackingProtectionPolicy.none()) verify(engineSessionB).enableTrackingProtection(TrackingProtectionPolicy.none()) - useCases.updateTrackingProtection(TrackingProtectionPolicy.all()) - verify(settings).trackingProtectionPolicy = TrackingProtectionPolicy.all() - verify(engineSessionA).enableTrackingProtection(TrackingProtectionPolicy.all()) - verify(engineSessionB).enableTrackingProtection(TrackingProtectionPolicy.all()) + useCases.updateTrackingProtection(TrackingProtectionPolicy.strict()) + verify(settings).trackingProtectionPolicy = TrackingProtectionPolicy.strict() + verify(engineSessionA).enableTrackingProtection(TrackingProtectionPolicy.strict()) + verify(engineSessionB).enableTrackingProtection(TrackingProtectionPolicy.strict()) } } diff --git a/docs/changelog.md b/docs/changelog.md index f06f660dd9e..ce943384c38 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -18,6 +18,13 @@ permalink: /changelog/ * **feature-media** * Do not display title/url/icon of website in media notification if website is opened in private mode. +* **concept-engine** and **browser-session** + * ⚠️ **This is a breaking change**: `TrackingProtectionPolicy` removes the `categories` property to expose two new ones `trackingCategories: Array` and `safeBrowsingCategories: Array` to separate the tracking protection categories from the safe browsing ones. + * ⚠️ **This is a breaking change**: `TrackingProtectionPolicy.all()` has been replaced by `TrackingProtectionPolicy.strict()` to have similar naming conventions with GeckoView api. + * ⚠️ **This is a breaking change**: `Tracker#categories` has been replaced by `Tracker#trackingCategories` and `Tracker#cookiePolicies` to better report blocked content see [#4098](https://github.com/mozilla-mobile/android-components/issues/4098). + * Added: `Session#trackersLoaded` A list of `Tracker`s that could be blocked but has been loaded in this session. + * Added: `Session#Observer#onTrackerLoaded` Notifies that a tracker that could be blocked has been loaded. + # 8.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v7.0.0...v8.0.0)