-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add core ad blocking capabilities #8622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CrisBarreiro
merged 5 commits into
develop
from
feature/cris/ad-blocking/core-ad-blocking
May 25, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43c2f79
Add core ad blocking capabilities
CrisBarreiro 77a52ea
Simplify data model
CrisBarreiro c90bd3b
Add explicit charset
CrisBarreiro 3c229a4
Encapsulate injection checks on a separate class
CrisBarreiro ed8ffe3
Adjust status checker to the new flags
CrisBarreiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...-impl/src/main/java/com/duckduckgo/adblocking/impl/AdBlockingExtensionJsInjectorPlugin.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| import android.webkit.WebView | ||
| import androidx.annotation.UiThread | ||
| import androidx.core.net.toUri | ||
| import com.duckduckgo.adblocking.impl.domain.AdBlockingStatusChecker | ||
| import com.duckduckgo.app.browser.Domain | ||
| import com.duckduckgo.app.browser.UriString | ||
| import com.duckduckgo.app.di.AppCoroutineScope | ||
| import com.duckduckgo.app.global.model.Site | ||
| import com.duckduckgo.browser.api.JsInjectorPlugin | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.duckduckgo.feature.toggles.api.Toggle | ||
| import com.squareup.anvil.annotations.ContributesMultibinding | ||
| import dagger.SingleInstanceIn | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.SharingStarted | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.stateIn | ||
| import logcat.logcat | ||
| import javax.inject.Inject | ||
|
|
||
| @SingleInstanceIn(AppScope::class) | ||
| @ContributesMultibinding(AppScope::class) | ||
| class AdBlockingExtensionJsInjectorPlugin @Inject constructor( | ||
| private val statusChecker: AdBlockingStatusChecker, | ||
| repository: AdBlockingExtensionRepository, | ||
| @AppCoroutineScope appScope: CoroutineScope, | ||
| ) : JsInjectorPlugin { | ||
|
|
||
| private val payload: StateFlow<String?> = repository | ||
| .scriptletsFlow() | ||
| .map(::buildScript) | ||
| .stateIn(appScope, SharingStarted.Eagerly, initialValue = null) | ||
|
|
||
| @UiThread | ||
| override fun onPageStarted( | ||
| webView: WebView, | ||
| url: String?, | ||
| isDesktopMode: Boolean?, | ||
| activeExperiments: List<Toggle>, | ||
| ) { | ||
| if (!statusChecker.canInject()) { | ||
| logcat { "Status checker rejected injection, skipping" } | ||
| return | ||
| } | ||
| val uri = url?.toUri() ?: return | ||
| if (domains.none { UriString.sameOrSubdomain(uri, it) }) { | ||
| logcat { "No domains matching, skipping" } | ||
| return | ||
| } | ||
| val script = payload.value ?: run { | ||
| logcat { "Empty payload, skipping" } | ||
| return | ||
| } | ||
|
|
||
| logcat { "Injecting script" } | ||
| webView.evaluateJavascript("javascript:$script", null) | ||
| } | ||
|
|
||
| override fun onPageFinished(webView: WebView, url: String?, site: Site?) = Unit | ||
|
|
||
| private fun buildScript(scriptlets: List<Scriptlet>): String? = | ||
| scriptlets | ||
| .takeUnless { it.isEmpty() } | ||
| ?.sortedBy { it.name } | ||
| ?.joinToString(separator = "\n") { it.content } | ||
|
|
||
| private val domains = listOf( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this expected to be local list or provided from the remote config?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, expected to be local. Discussed over zoom |
||
| Domain("youtube.com"), | ||
| Domain("youtube-nocookie.com"), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
ad-blocking/ad-blocking-impl/src/main/java/com/duckduckgo/adblocking/impl/Scriptlet.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| data class Scriptlet( | ||
| val name: String, | ||
| val content: String, | ||
| ) |
47 changes: 47 additions & 0 deletions
47
...cking-impl/src/main/java/com/duckduckgo/adblocking/impl/domain/AdBlockingStatusChecker.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl.domain | ||
|
|
||
| import com.duckduckgo.adblocking.impl.remoteconfig.AdBlockingExtensionFeature | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import dagger.SingleInstanceIn | ||
| import logcat.logcat | ||
| import javax.inject.Inject | ||
|
|
||
| interface AdBlockingStatusChecker { | ||
| fun canInject(): Boolean | ||
| } | ||
|
|
||
| @SingleInstanceIn(AppScope::class) | ||
| @ContributesBinding(AppScope::class) | ||
| class RealAdBlockingStatusChecker @Inject constructor( | ||
| private val feature: AdBlockingExtensionFeature, | ||
| ) : AdBlockingStatusChecker { | ||
|
|
||
| override fun canInject(): Boolean { | ||
| if (!feature.self().isEnabled()) { | ||
| logcat { "Kill-switch is off" } | ||
| return false | ||
| } | ||
| if (feature.enableContingencyMode().isEnabled()) { | ||
| logcat { "Contingency mode is on" } | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
| } |
204 changes: 204 additions & 0 deletions
204
...l/src/test/java/com/duckduckgo/adblocking/impl/AdBlockingExtensionJsInjectorPluginTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| import android.webkit.WebView | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.duckduckgo.adblocking.impl.domain.AdBlockingStatusChecker | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.cancel | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
| import org.junit.After | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.doAnswer | ||
| import org.mockito.kotlin.doReturn | ||
| import org.mockito.kotlin.eq | ||
| import org.mockito.kotlin.isNull | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.never | ||
| import org.mockito.kotlin.verify | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| class AdBlockingExtensionJsInjectorPluginTest { | ||
|
|
||
| private var canInject = true | ||
| private val scriptletsFlow = MutableStateFlow<List<Scriptlet>>(emptyList()) | ||
|
|
||
| private val statusChecker: AdBlockingStatusChecker = mock { | ||
| on { canInject() } doAnswer { canInject } | ||
| } | ||
| private val repository: AdBlockingExtensionRepository = mock { | ||
| on { scriptletsFlow() } doReturn scriptletsFlow | ||
| } | ||
| private val webView: WebView = mock() | ||
| private val testScope = CoroutineScope(UnconfinedTestDispatcher()) | ||
|
|
||
| private val singleScriptlet = listOf(Scriptlet(name = "a.js", content = "console.log('a')")) | ||
| private val twoScriptlets = listOf( | ||
| Scriptlet(name = "b.js", content = "console.log('b')"), | ||
| Scriptlet(name = "a.js", content = "console.log('a')"), | ||
| ) | ||
|
|
||
| private val plugin by lazy { | ||
| AdBlockingExtensionJsInjectorPlugin( | ||
| statusChecker = statusChecker, | ||
| repository = repository, | ||
| appScope = testScope, | ||
| ) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| testScope.cancel() | ||
| } | ||
|
|
||
| @Test | ||
| fun whenAllGatesAreOpenThenScriptIsInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript( | ||
| eq("javascript:console.log('a')"), | ||
|
CrisBarreiro marked this conversation as resolved.
|
||
| isNull(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenMultipleScriptletsThenAllAreInjectedInSortedOrder() { | ||
| scriptletsFlow.value = twoScriptlets | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript( | ||
| eq("javascript:console.log('a')\nconsole.log('b')"), | ||
| isNull(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlIsSubdomainOfConfiguredDomainThenScriptIsInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://m.youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlHasWwwPrefixThenScriptIsInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://www.youtube.com", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlMatchesSecondConfiguredDomainThenScriptIsInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://youtube-nocookie.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlHostNotInConfiguredDomainsThenScriptIsNotInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://example.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenStatusCheckerRejectsThenScriptIsNotInjected() { | ||
| canInject = false | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlIsNullThenScriptIsNotInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = null, isDesktopMode = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenScriptletsListIsEmptyThenScriptIsNotInjected() { | ||
| scriptletsFlow.value = emptyList() | ||
|
|
||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenUrlHasNoHostThenScriptIsNotInjected() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageStarted(webView, url = "data:text/html,<p>hi</p>", isDesktopMode = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenScriptletDataIsUpdatedThenNextInjectionUsesNewPayload() { | ||
| scriptletsFlow.value = singleScriptlet | ||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| scriptletsFlow.value = listOf(Scriptlet(name = "a.js", content = "console.log('updated')")) | ||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript( | ||
| eq("javascript:console.log('updated')"), | ||
| isNull(), | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenStatusCheckerStartsRejectingMidSessionThenNextInjectionNoOps() { | ||
| scriptletsFlow.value = singleScriptlet | ||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| canInject = false | ||
| plugin.onPageStarted(webView, url = "https://youtube.com/page", isDesktopMode = null) | ||
|
|
||
| verify(webView).evaluateJavascript(any(), isNull()) | ||
| } | ||
|
|
||
| @Test | ||
| fun whenOnPageFinishedCalledThenNoInjection() { | ||
| scriptletsFlow.value = singleScriptlet | ||
|
|
||
| plugin.onPageFinished(webView, url = "https://youtube.com/page", site = null) | ||
|
|
||
| verify(webView, never()).evaluateJavascript(any(), isNull()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.