From 0275ca0693c0d86539ce92e71cb76c4fedbeb2e6 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Tue, 12 Nov 2019 09:39:59 -0500 Subject: [PATCH] No issue: Do not prevent trackers duplication in the tracking protection panel. --- .../trackingprotection/TrackerBuckets.kt | 101 +++++++++++------- .../trackingprotection/TrackerBucketsTest.kt | 43 +++++++- 2 files changed, 101 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt b/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt index b2e0d4343e..ed95b0c681 100644 --- a/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt +++ b/app/src/main/java/org/mozilla/fenix/trackingprotection/TrackerBuckets.kt @@ -69,50 +69,69 @@ class TrackerBuckets { val loadedMap = EnumMap>(TrackingProtectionCategory::class.java) for (item in list) { - when { - // Blocked categories - item.cookiesHasBeenBlocked -> { - blockedMap[CROSS_SITE_TRACKING_COOKIES] = - blockedMap[CROSS_SITE_TRACKING_COOKIES].orEmpty() + item.url.tryGetHostFromUrl() - } - CRYPTOMINING in item.blockedCategories -> { - blockedMap[CRYPTOMINERS] = blockedMap[CRYPTOMINERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - FINGERPRINTING in item.blockedCategories -> { - blockedMap[FINGERPRINTERS] = blockedMap[FINGERPRINTERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - MOZILLA_SOCIAL in item.blockedCategories -> { - blockedMap[SOCIAL_MEDIA_TRACKERS] = - blockedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - TrackingCategory.SCRIPTS_AND_SUB_RESOURCES in item.blockedCategories -> { - blockedMap[TRACKING_CONTENT] = blockedMap[TRACKING_CONTENT].orEmpty() + - item.url.tryGetHostFromUrl() - } - // Loaded categories - CRYPTOMINING in item.loadedCategories -> { - loadedMap[CRYPTOMINERS] = loadedMap[CRYPTOMINERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - FINGERPRINTING in item.loadedCategories -> { - loadedMap[FINGERPRINTERS] = loadedMap[FINGERPRINTERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - MOZILLA_SOCIAL in item.loadedCategories -> { - loadedMap[SOCIAL_MEDIA_TRACKERS] = - loadedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() + - item.url.tryGetHostFromUrl() - } - TrackingCategory.SCRIPTS_AND_SUB_RESOURCES in item.loadedCategories -> { - loadedMap[TRACKING_CONTENT] = loadedMap[TRACKING_CONTENT].orEmpty() + - item.url.tryGetHostFromUrl() - } + + if (item.cookiesHasBeenBlocked) { + blockedMap[CROSS_SITE_TRACKING_COOKIES] = + blockedMap[CROSS_SITE_TRACKING_COOKIES].orEmpty() + item.url.tryGetHostFromUrl() } + + // Blocked categories + bucketBlockedCategories(item, blockedMap) + + // Loaded categories + bucketLoadedCategories(item, loadedMap) } return BucketedTrackerLog(blockedMap, loadedMap) } + + private fun bucketLoadedCategories( + item: TrackerLog, + loadedMap: EnumMap> + ) { + item.loadedCategories.forEach { category -> + if (CRYPTOMINING == category) { + loadedMap[CRYPTOMINERS] = loadedMap[CRYPTOMINERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (FINGERPRINTING == category) { + loadedMap[FINGERPRINTERS] = loadedMap[FINGERPRINTERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (MOZILLA_SOCIAL == category) { + loadedMap[SOCIAL_MEDIA_TRACKERS] = + loadedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (TrackingCategory.SCRIPTS_AND_SUB_RESOURCES == category) { + loadedMap[TRACKING_CONTENT] = loadedMap[TRACKING_CONTENT].orEmpty() + + item.url.tryGetHostFromUrl() + } + } + } + + private fun bucketBlockedCategories( + item: TrackerLog, + blockedMap: EnumMap> + ) { + item.blockedCategories.forEach { category -> + if (CRYPTOMINING == category) { + blockedMap[CRYPTOMINERS] = blockedMap[CRYPTOMINERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (FINGERPRINTING == category) { + blockedMap[FINGERPRINTERS] = blockedMap[FINGERPRINTERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (MOZILLA_SOCIAL == category) { + blockedMap[SOCIAL_MEDIA_TRACKERS] = + blockedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() + + item.url.tryGetHostFromUrl() + } + if (TrackingCategory.SCRIPTS_AND_SUB_RESOURCES == category) { + blockedMap[TRACKING_CONTENT] = blockedMap[TRACKING_CONTENT].orEmpty() + + item.url.tryGetHostFromUrl() + } + } + } } } diff --git a/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackerBucketsTest.kt b/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackerBucketsTest.kt index 415933263f..bbef778ddf 100644 --- a/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackerBucketsTest.kt +++ b/app/src/test/java/org/mozilla/fenix/trackingprotection/TrackerBucketsTest.kt @@ -4,6 +4,8 @@ package org.mozilla.fenix.trackingprotection +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.SCRIPTS_AND_SUB_RESOURCES +import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.MOZILLA_SOCIAL import mozilla.components.concept.engine.content.blocking.TrackerLog @@ -13,6 +15,7 @@ import org.junit.Test import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS +private typealias FenixTrackingProtectionCategory = TrackingProtectionCategory class TrackerBucketsTest { @Test @@ -38,7 +41,7 @@ class TrackerBucketsTest { assertEquals(listOf("google.com"), buckets.buckets.blockedBucketMap[FINGERPRINTERS]) assertEquals( listOf("facebook.com"), - buckets.buckets.loadedBucketMap[TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS] + buckets.buckets.loadedBucketMap[FenixTrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS] ) assertTrue(buckets.buckets.blockedBucketMap[CRYPTOMINERS].isNullOrEmpty()) assertTrue(buckets.buckets.loadedBucketMap[CRYPTOMINERS].isNullOrEmpty()) @@ -60,7 +63,7 @@ class TrackerBucketsTest { assertEquals( mapOf( - TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS to listOf("facebook.com") + FenixTrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS to listOf("facebook.com") ), buckets.buckets.loadedBucketMap ) @@ -70,4 +73,40 @@ class TrackerBucketsTest { ), buckets.buckets.blockedBucketMap ) } + + @Test + fun `trackers in the same site but with different categories`() { + val buckets = TrackerBuckets() + val acCategories = listOf( + CRYPTOMINING, + MOZILLA_SOCIAL, + FINGERPRINTING, + SCRIPTS_AND_SUB_RESOURCES + ) + + buckets.updateIfNeeded( + listOf( + TrackerLog( + url = "http://facebook.com", + cookiesHasBeenBlocked = true, + blockedCategories = acCategories, + loadedCategories = acCategories + ) + ) + ) + + val expectedBlockedMap = + mapOf( + FenixTrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS to listOf("facebook.com"), + FenixTrackingProtectionCategory.TRACKING_CONTENT to listOf("facebook.com"), + FenixTrackingProtectionCategory.FINGERPRINTERS to listOf("facebook.com"), + FenixTrackingProtectionCategory.CRYPTOMINERS to listOf("facebook.com"), + FenixTrackingProtectionCategory.CROSS_SITE_TRACKING_COOKIES to listOf("facebook.com") + ) + val expectedLoadedMap = + expectedBlockedMap - FenixTrackingProtectionCategory.CROSS_SITE_TRACKING_COOKIES + + assertEquals(expectedBlockedMap, buckets.buckets.blockedBucketMap) + assertEquals(expectedLoadedMap, buckets.buckets.loadedBucketMap) + } }