Skip to content

Commit

Permalink
Closes mozilla-mobile#3788: Expose to which category a blocked tracke…
Browse files Browse the repository at this point in the history
…r belongs.
  • Loading branch information
Amejia481 committed Jul 19, 2019
1 parent 4bb4757 commit 8fdc655
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.manifest.WebAppManifestParser
import mozilla.components.concept.engine.request.RequestInterceptor
Expand Down Expand Up @@ -543,10 +544,45 @@ class GeckoEngineSession(

private fun createContentBlockingDelegate() = object : ContentBlocking.Delegate {
override fun onContentBlocked(session: GeckoSession, event: ContentBlocking.BlockEvent) {
notifyObservers { onTrackerBlocked(event.uri) }
notifyObservers {
onTrackerBlocked(event.toTracker())
}
}
}

@Suppress("LongMethod")
private fun ContentBlocking.BlockEvent.toTracker(): Tracker {
val blockedContentCategories = ArrayList<Tracker.Category>()

if (categories.contains(ContentBlocking.AT_AD)) {
blockedContentCategories.add(Tracker.Category.Ad)
}

if (categories.contains(ContentBlocking.AT_ANALYTIC)) {
blockedContentCategories.add(Tracker.Category.Analytic)
}

if (categories.contains(ContentBlocking.AT_SOCIAL)) {
blockedContentCategories.add(Tracker.Category.Social)
}

if (categories.contains(ContentBlocking.AT_FINGERPRINTING)) {
blockedContentCategories.add(Tracker.Category.Fingerprinting)
}

if (categories.contains(ContentBlocking.AT_CRYPTOMINING)) {
blockedContentCategories.add(Tracker.Category.Cryptomining)
}
if (categories.contains(ContentBlocking.AT_CONTENT)) {
blockedContentCategories.add(Tracker.Category.Content)
}
return Tracker(uri, blockedContentCategories)
}

private operator fun Int.contains(mask: Int): Boolean {
return (this and mask) != 0
}

private fun createPermissionDelegate() = object : GeckoSession.PermissionDelegate {
override fun onContentPermissionRequest(
session: GeckoSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.concept.engine.permission.PermissionRequest
Expand Down Expand Up @@ -784,17 +785,28 @@ class GeckoEngineSessionTest {
val engineSession = GeckoEngineSession(mock(),
geckoSessionProvider = geckoSessionProvider)

var trackerBlocked = ""
var trackerBlocked: Tracker? = null
engineSession.register(object : EngineSession.Observer {
override fun onTrackerBlocked(url: String) {
trackerBlocked = url
override fun onTrackerBlocked(tracker: Tracker) {
trackerBlocked = tracker
}
})

captureDelegates()

contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", 0))
assertEquals("tracker1", trackerBlocked)
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)
)
assertEquals("tracker1", trackerBlocked!!.url)
assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList()))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSessionState
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.manifest.WebAppManifestParser
import mozilla.components.concept.engine.request.RequestInterceptor
Expand All @@ -30,6 +31,12 @@ import mozilla.components.support.utils.DownloadUtils
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
Expand Down Expand Up @@ -543,10 +550,45 @@ class GeckoEngineSession(

private fun createContentBlockingDelegate() = object : ContentBlocking.Delegate {
override fun onContentBlocked(session: GeckoSession, event: ContentBlocking.BlockEvent) {
notifyObservers { onTrackerBlocked(event.uri) }
notifyObservers {
onTrackerBlocked(event.toTracker())
}
}
}

@Suppress("LongMethod")
private fun ContentBlocking.BlockEvent.toTracker(): Tracker {
val blockedContentCategories = ArrayList<Tracker.Category>()

if (categories.contains(AT_AD)) {
blockedContentCategories.add(Tracker.Category.Ad)
}

if (categories.contains(AT_ANALYTIC)) {
blockedContentCategories.add(Tracker.Category.Analytic)
}

if (categories.contains(AT_SOCIAL)) {
blockedContentCategories.add(Tracker.Category.Social)
}

if (categories.contains(AT_FINGERPRINTING)) {
blockedContentCategories.add(Tracker.Category.Fingerprinting)
}

if (categories.contains(AT_CRYPTOMINING)) {
blockedContentCategories.add(Tracker.Category.Cryptomining)
}
if (categories.contains(AT_CONTENT)) {
blockedContentCategories.add(Tracker.Category.Content)
}
return Tracker(uri, blockedContentCategories)
}

private operator fun Int.contains(mask: Int): Boolean {
return (this and mask) != 0
}

private fun createPermissionDelegate() = object : GeckoSession.PermissionDelegate {
override fun onContentPermissionRequest(
session: GeckoSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.concept.engine.permission.PermissionRequest
Expand Down Expand Up @@ -784,17 +785,25 @@ class GeckoEngineSessionTest {
val engineSession = GeckoEngineSession(mock(),
geckoSessionProvider = geckoSessionProvider)

var trackerBlocked = ""
var trackerBlocked: Tracker? = null
engineSession.register(object : EngineSession.Observer {
override fun onTrackerBlocked(url: String) {
trackerBlocked = url
override fun onTrackerBlocked(tracker: Tracker) {
trackerBlocked = tracker
}
})

captureDelegates()

contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", 0))
assertEquals("tracker1", trackerBlocked)
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))
assertEquals("tracker1", trackerBlocked!!.url)
assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList()))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.Settings
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.request.RequestInterceptor
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.request.RequestInterceptor.InterceptionResponse
import mozilla.components.concept.storage.VisitType
import mozilla.components.support.ktx.android.util.Base64
Expand Down Expand Up @@ -538,10 +539,45 @@ class GeckoEngineSession(

private fun createContentBlockingDelegate() = object : ContentBlocking.Delegate {
override fun onContentBlocked(session: GeckoSession, event: ContentBlocking.BlockEvent) {
notifyObservers { onTrackerBlocked(event.uri) }
notifyObservers {
onTrackerBlocked(event.toTracker())
}
}
}

@Suppress("LongMethod")
private fun ContentBlocking.BlockEvent.toTracker(): Tracker {
val blockedContentCategories = ArrayList<Tracker.Category>()

if (categories.contains(ContentBlocking.AT_AD)) {
blockedContentCategories.add(Tracker.Category.Ad)
}

if (categories.contains(ContentBlocking.AT_ANALYTIC)) {
blockedContentCategories.add(Tracker.Category.Analytic)
}

if (categories.contains(ContentBlocking.AT_SOCIAL)) {
blockedContentCategories.add(Tracker.Category.Social)
}

if (categories.contains(ContentBlocking.AT_FINGERPRINTING)) {
blockedContentCategories.add(Tracker.Category.Fingerprinting)
}

if (categories.contains(ContentBlocking.AT_CRYPTOMINING)) {
blockedContentCategories.add(Tracker.Category.Cryptomining)
}
if (categories.contains(ContentBlocking.AT_CONTENT)) {
blockedContentCategories.add(Tracker.Category.Content)
}
return Tracker(uri, blockedContentCategories)
}

private operator fun Int.contains(mask: Int): Boolean {
return (this and mask) != 0
}

private fun createPermissionDelegate() = object : GeckoSession.PermissionDelegate {
override fun onContentPermissionRequest(
session: GeckoSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import mozilla.components.concept.engine.EngineSession.LoadUrlFlags
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.UnsupportedSettingException
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.history.HistoryTrackingDelegate
import mozilla.components.concept.engine.permission.PermissionRequest
import mozilla.components.concept.engine.request.RequestInterceptor
Expand Down Expand Up @@ -759,17 +760,26 @@ class GeckoEngineSessionTest {
val engineSession = GeckoEngineSession(mock(),
geckoSessionProvider = geckoSessionProvider)

var trackerBlocked = ""
var trackerBlocked: Tracker? = null
engineSession.register(object : EngineSession.Observer {
override fun onTrackerBlocked(url: String) {
trackerBlocked = url
override fun onTrackerBlocked(tracker: Tracker) {
trackerBlocked = tracker
}
})

captureDelegates()

contentBlockingDelegate.value.onContentBlocked(geckoSession, ContentBlocking.BlockEvent("tracker1", 0))
assertEquals("tracker1", trackerBlocked)
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))
assertEquals("tracker1", trackerBlocked!!.url)
assertTrue(trackerBlocked!!.categories.containsAll(Tracker.Category.values().toList()))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy
import mozilla.components.concept.engine.EngineView
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.concept.engine.request.RequestInterceptor.InterceptionResponse
import mozilla.components.concept.storage.VisitType
Expand Down Expand Up @@ -214,7 +215,14 @@ class SystemEngineView @JvmOverloads constructor(

if (!request.isForMainFrame &&
getOrCreateUrlMatcher(resources, it).matches(resourceUri, Uri.parse(session?.currentUrl))) {
session?.internalNotifyObservers { onTrackerBlocked(resourceUri.toString()) }
session?.internalNotifyObservers {
onTrackerBlocked(
Tracker(
resourceUri.toString(),
emptyList()
)
)
}
return WebResourceResponse(null, null, null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import mozilla.components.browser.state.action.ContentAction.UpdateTitleAction
import mozilla.components.browser.state.action.ContentAction.UpdateUrlAction
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.engine.media.RecordingDevice
Expand Down Expand Up @@ -81,7 +82,7 @@ class Session(
fun onWebAppManifestChanged(session: Session, manifest: WebAppManifest?) = Unit
fun onDownload(session: Session, download: Download): Boolean = false
fun onTrackerBlockingEnabledChanged(session: Session, blockingEnabled: Boolean) = Unit
fun onTrackerBlocked(session: Session, blocked: String, all: List<String>) = Unit
fun onTrackerBlocked(session: Session, tracker: Tracker, all: List<Tracker>) = Unit
fun onLongPress(session: Session, hitResult: HitResult): Boolean = false
fun onFindResult(session: Session, result: FindResult) = Unit
fun onDesktopModeChanged(session: Session, enabled: Boolean) = Unit
Expand Down Expand Up @@ -300,9 +301,9 @@ class Session(
}

/**
* List of URIs that have been blocked in this session.
* List of [Tracker]s that have been blocked in this session.
*/
var trackersBlocked: List<String> by Delegates.observable(emptyList()) { _, old, new ->
var trackersBlocked: List<Tracker> by Delegates.observable(emptyList()) { _, old, new ->
notifyObservers(old, new) {
if (new.isNotEmpty()) {
onTrackerBlocked(this@Session, new.last(), new)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import mozilla.components.browser.session.engine.request.LoadRequestMetadata
import mozilla.components.browser.session.engine.request.LoadRequestOption
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.manifest.WebAppManifest
import mozilla.components.concept.engine.media.Media
import mozilla.components.concept.engine.media.RecordingDevice
Expand Down Expand Up @@ -88,8 +89,8 @@ internal class EngineObserver(
?: "", issuer ?: "")
}

override fun onTrackerBlocked(url: String) {
session.trackersBlocked += url
override fun onTrackerBlocked(tracker: Tracker) {
session.trackersBlocked += tracker
}

override fun onTrackerBlockingEnabledChange(enabled: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mozilla.components.browser.session

import android.graphics.Bitmap
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.support.test.mock
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
Expand Down Expand Up @@ -147,7 +148,7 @@ class SelectionAwareSessionObserverTest {
observer.onCustomTabConfigChanged(session, null)
observer.onDownload(session, Mockito.mock(Download::class.java))
observer.onTrackerBlockingEnabledChanged(session, true)
observer.onTrackerBlocked(session, "", emptyList())
observer.onTrackerBlocked(session, Tracker(""), emptyList())
observer.onLongPress(session, Mockito.mock(HitResult::class.java))
observer.onFindResult(session, Mockito.mock(Session.FindResult::class.java))
observer.onDesktopModeChanged(session, true)
Expand Down
Loading

0 comments on commit 8fdc655

Please sign in to comment.