Skip to content

Commit

Permalink
Closes mozilla-mobile#4098, mozilla-mobile#4110: Exposing new categor…
Browse files Browse the repository at this point in the history
…ies of content blocking.
  • Loading branch information
Amejia481 committed Aug 14, 2019
1 parent 1c6ddd3 commit 8ed5804
Show file tree
Hide file tree
Showing 27 changed files with 759 additions and 337 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Gecko.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,29 +574,33 @@ class GeckoEngineSession(
}

private fun ContentBlocking.BlockEvent.toTracker(): Tracker {
val blockedContentCategories = ArrayList<Tracker.Category>()
val blockedContentCategories = mutableListOf<TrackingProtectionPolicy.TrackingCategory>()

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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ import org.mozilla.geckoview.WebRequestError.ERROR_CATEGORY_UNKNOWN
import org.mozilla.geckoview.WebRequestError.ERROR_MALFORMED_URI
import org.mozilla.geckoview.WebRequestError.ERROR_UNKNOWN

typealias ACAntiTracking = TrackingProtectionPolicy.TrackingCategory
typealias ACSafeBrowsing = TrackingProtectionPolicy.SafeBrowsingCategory
typealias ACCookiePolicy = TrackingProtectionPolicy.CookiePolicy

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class GeckoEngineSessionTest {
Expand Down Expand Up @@ -801,12 +805,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(
ACAntiTracking.AD,
ACAntiTracking.ANALYTICS,
ACAntiTracking.SOCIAL,
ACAntiTracking.CRYPTOMINING,
ACAntiTracking.FINGERPRINTING,
ACAntiTracking.CONTENT,
ACAntiTracking.TEST
)

assertTrue(trackerBlocked!!.trackingCategories.containsAll(expectedBlockedCategories))
}

@Test
Expand All @@ -829,9 +845,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(ACAntiTracking.AD)
).forRegularSessionsOnly()
val privateOnlyPolicy = TrackingProtectionPolicy.select(
trackingCategories = arrayOf(ACAntiTracking.AD)
).forPrivateSessionsOnly()

session.enableTrackingProtection(allPolicy)
assertTrue(trackerBlockingObserved)
Expand Down Expand Up @@ -871,21 +892,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(ACAntiTracking.AD.id, ContentBlocking.AT_AD)
assertEquals(ACAntiTracking.ANALYTICS.id, ContentBlocking.AT_ANALYTIC)
assertEquals(ACAntiTracking.CONTENT.id, ContentBlocking.AT_CONTENT)
assertEquals(ACAntiTracking.SOCIAL.id, ContentBlocking.AT_SOCIAL)
assertEquals(ACAntiTracking.TEST.id, ContentBlocking.AT_TEST)
assertEquals(ACAntiTracking.CRYPTOMINING.id, ContentBlocking.AT_CRYPTOMINING)
assertEquals(ACAntiTracking.FINGERPRINTING.id, ContentBlocking.AT_FINGERPRINTING)

assertEquals(ACSafeBrowsing.RECOMMENDED.id, ContentBlocking.SB_ALL)
assertEquals(ACSafeBrowsing.HARMFUL.id, ContentBlocking.SB_HARMFUL)
assertEquals(ACSafeBrowsing.MALWARE.id, ContentBlocking.SB_MALWARE)
assertEquals(ACSafeBrowsing.PHISHING.id, ContentBlocking.SB_PHISHING)
assertEquals(ACSafeBrowsing.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,
Expand Down Expand Up @@ -990,7 +1030,7 @@ class GeckoEngineSessionTest {
}

expectException(UnsupportedSettingException::class) {
settings.trackingProtectionPolicy = TrackingProtectionPolicy.all()
settings.trackingProtectionPolicy = TrackingProtectionPolicy.strict()
}
}

Expand Down Expand Up @@ -1286,7 +1326,8 @@ class GeckoEngineSessionTest {
val runtime = mock<GeckoRuntime>()
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeckoRuntime>()
val runtimeSettings = mock<GeckoRuntimeSettings>()
whenever(runtimeSettings.javaScriptEnabled).thenReturn(true)
Expand Down Expand Up @@ -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 {
Expand All @@ -166,8 +158,7 @@ class GeckoEngineTest {
fun defaultSettings() {
val runtime = mock<GeckoRuntime>()
val runtimeSettings = mock<GeckoRuntimeSettings>()
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)
Expand All @@ -176,7 +167,7 @@ class GeckoEngineTest {

val engine = GeckoEngine(context,
DefaultSettings(
trackingProtectionPolicy = TrackingProtectionPolicy.all(),
trackingProtectionPolicy = TrackingProtectionPolicy.strict(),
javascriptEnabled = false,
webFontsEnabled = false,
automaticFontSizeAdjustment = false,
Expand All @@ -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)
Expand All @@ -220,7 +204,7 @@ class GeckoEngineTest {

engine.settings.trackingProtectionPolicy =
TrackingProtectionPolicy.select(
TrackingProtectionPolicy.AD,
trackingCategories = arrayOf(TrackingProtectionPolicy.TrackingCategory.AD),
cookiePolicy = CookiePolicy.ACCEPT_ONLY_FIRST_PARTY
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Loading

0 comments on commit 8ed5804

Please sign in to comment.