Skip to content

Commit f2d001c

Browse files
committed
Add plugins at the browser level
1 parent 68bf4c4 commit f2d001c

File tree

10 files changed

+82
-29
lines changed

10 files changed

+82
-29
lines changed

app/src/androidTest/java/com/duckduckgo/app/browser/BrowserWebViewClientTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import com.duckduckgo.duckplayer.api.DuckPlayer.OpenDuckPlayerInNewTab.On
8484
import com.duckduckgo.duckplayer.api.DuckPlayer.OpenDuckPlayerInNewTab.Unavailable
8585
import com.duckduckgo.feature.toggles.api.Toggle
8686
import com.duckduckgo.history.api.NavigationHistory
87-
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
87+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
8888
import com.duckduckgo.js.messaging.api.PostMessageWrapperPlugin
8989
import com.duckduckgo.js.messaging.api.SubscriptionEventData
9090
import com.duckduckgo.js.messaging.api.WebMessagingPlugin

app/src/main/java/com/duckduckgo/app/browser/BrowserWebViewClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import com.duckduckgo.app.statistics.pixels.Pixel
6565
import com.duckduckgo.autoconsent.api.Autoconsent
6666
import com.duckduckgo.autofill.api.BrowserAutofill
6767
import com.duckduckgo.autofill.api.InternalTestUserChecker
68+
import com.duckduckgo.browser.api.AddDocumentStartJavaScriptPlugin
6869
import com.duckduckgo.browser.api.JsInjectorPlugin
6970
import com.duckduckgo.common.utils.AppUrl.ParamKey.QUERY
7071
import com.duckduckgo.common.utils.CurrentTimeProvider
@@ -79,7 +80,6 @@ import com.duckduckgo.duckplayer.api.DuckPlayer.DuckPlayerState.ENABLED
7980
import com.duckduckgo.duckplayer.api.DuckPlayer.OpenDuckPlayerInNewTab.On
8081
import com.duckduckgo.duckplayer.impl.DUCK_PLAYER_OPEN_IN_YOUTUBE_PATH
8182
import com.duckduckgo.history.api.NavigationHistory
82-
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
8383
import com.duckduckgo.js.messaging.api.PostMessageWrapperPlugin
8484
import com.duckduckgo.js.messaging.api.SubscriptionEventData
8585
import com.duckduckgo.js.messaging.api.WebMessagingPlugin
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.app.browser
18+
19+
import com.duckduckgo.browser.api.AddDocumentStartJavaScriptPlugin
20+
import com.duckduckgo.contentscopescripts.impl.ContentScopeScriptsAddDocumentStartJavaScript
21+
import com.duckduckgo.di.scopes.FragmentScope
22+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
23+
import com.squareup.anvil.annotations.ContributesMultibinding
24+
import javax.inject.Inject
25+
26+
@ContributesMultibinding(FragmentScope::class)
27+
class ContentScopeScriptsAddDocumentStartJavaScriptPlugin @Inject constructor(
28+
private val contentScopeScriptsAddDocumentStartJavaScript: ContentScopeScriptsAddDocumentStartJavaScript,
29+
) : AddDocumentStartJavaScriptPlugin {
30+
override fun addDocumentStartJavaScript(): AddDocumentStartJavaScript {
31+
return contentScopeScriptsAddDocumentStartJavaScript
32+
}
33+
}

app/src/main/java/com/duckduckgo/app/plugins/AddDocumentStartJavaScriptPluginPoint.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package com.duckduckgo.app.plugins
1818

1919
import com.duckduckgo.anvil.annotations.ContributesPluginPoint
20+
import com.duckduckgo.browser.api.AddDocumentStartJavaScriptPlugin
2021
import com.duckduckgo.di.scopes.AppScope
21-
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
2222

2323
@ContributesPluginPoint(
2424
scope = AppScope::class,

browser-api/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
implementation project(path: ':design-system')
2727
implementation project(path: ':common-utils')
2828
implementation project(':feature-toggles-api')
29+
implementation project(':js-messaging-api')
2930
implementation AndroidX.core.ktx
3031
implementation AndroidX.webkit
3132
implementation KotlinX.coroutines.core
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 DuckDuckGo
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.duckduckgo.browser.api
18+
19+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
20+
21+
interface AddDocumentStartJavaScriptPlugin {
22+
fun addDocumentStartJavaScript(): AddDocumentStartJavaScript
23+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ package com.duckduckgo.contentscopescripts.impl
1818

1919
import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
2020
import com.duckduckgo.di.scopes.FragmentScope
21-
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
21+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
2222
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy
2323
import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate
24-
import com.squareup.anvil.annotations.ContributesMultibinding
2524
import dagger.SingleInstanceIn
2625
import javax.inject.Inject
2726

2827
@SingleInstanceIn(FragmentScope::class)
29-
@ContributesMultibinding(FragmentScope::class)
30-
class ContentScopeScriptsAddDocumentStartJavaScriptPlugin @Inject constructor(
28+
class ContentScopeScriptsAddDocumentStartJavaScript @Inject constructor(
3129
webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts,
3230
contentScopeExperiments: ContentScopeExperiments,
3331
scriptInjectorDelegate: AddDocumentStartScriptDelegate,
34-
) : AddDocumentStartJavaScriptPlugin by scriptInjectorDelegate.createPlugin(
32+
) : AddDocumentStartJavaScript by scriptInjectorDelegate.createPlugin(
3533
object : AddDocumentStartJavaScriptScriptStrategy {
3634
override suspend fun canInject(): Boolean {
3735
return webViewCompatContentScopeScripts.isEnabled()
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.mockito.kotlin.never
1515
import org.mockito.kotlin.verify
1616
import org.mockito.kotlin.whenever
1717

18-
class ContentScopeScriptsAddDocumentStartJavaScriptPluginTest {
18+
class ContentScopeScriptsAddDocumentStartJavaScriptTest {
1919
@get:Rule
2020
var coroutineRule = CoroutineTestRule()
2121

@@ -25,21 +25,16 @@ class ContentScopeScriptsAddDocumentStartJavaScriptPluginTest {
2525
private val mockWebView: WebView = mock()
2626
private val mockActiveContentScopeExperiments: ContentScopeExperiments = mock()
2727

28-
private lateinit var testee: ContentScopeScriptsAddDocumentStartJavaScriptPlugin
28+
private lateinit var testee: ContentScopeScriptsAddDocumentStartJavaScript
2929

3030
@Before
31-
fun setUp() =
32-
runTest {
33-
whenever(mockActiveContentScopeExperiments.getActiveExperiments()).thenReturn(listOf())
34-
testee =
35-
ContentScopeScriptsAddDocumentStartJavaScriptPlugin(
36-
mockWebViewCompatContentScopeScripts,
37-
coroutineRule.testDispatcherProvider,
38-
mockWebViewCapabilityChecker,
39-
mockWebViewCompatWrapper,
40-
mockActiveContentScopeExperiments,
41-
)
42-
}
31+
fun setUp() = runTest {
32+
whenever(mockActiveContentScopeExperiments.getActiveExperiments()).thenReturn(listOf())
33+
testee = ContentScopeScriptsAddDocumentStartJavaScript(
34+
mockWebViewCompatContentScopeScripts,
35+
mockActiveContentScopeExperiments,
36+
)
37+
}
4338

4439
@Test
4540
fun whenFeatureIsEnabledAndCapabilitySupportedThenCallScriptInjectionWithCorrectParams() =
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import android.webkit.WebView
2323
* * Allows plugins to inject JavaScript that will be executed before any other scripts on the page.
2424
* Useful for privacy protections and that need to run as early as possible and/or on iframes.
2525
*/
26-
interface AddDocumentStartJavaScriptPlugin {
27-
suspend fun addDocumentStartJavaScript(webView: WebView)
26+
interface AddDocumentStartJavaScript {
27+
28+
suspend fun addDocumentStartJavaScript(
29+
webView: WebView,
30+
)
2831
}
2932

3033
/**
@@ -55,7 +58,7 @@ interface AddDocumentStartScriptDelegate {
5558
/**
5659
* Creates an AddDocumentStartJavaScriptPlugin implementation with the given [AddDocumentStartJavaScriptScriptStrategy].
5760
* @param strategy the strategy to use for determining injection behavior
58-
* @return [AddDocumentStartJavaScriptPlugin] implementation
61+
* @return [AddDocumentStartJavaScript] implementation
5962
*/
60-
fun createPlugin(strategy: AddDocumentStartJavaScriptScriptStrategy): AddDocumentStartJavaScriptPlugin
63+
fun createPlugin(strategy: AddDocumentStartJavaScriptScriptStrategy): AddDocumentStartJavaScript
6164
}

js-messaging/js-messaging-impl/src/main/java/com/duckduckgo/js/messaging/impl/RealAddDocumentStartScriptDelegate.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import com.duckduckgo.app.di.AppCoroutineScope
2424
import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper
2525
import com.duckduckgo.common.utils.DispatcherProvider
2626
import com.duckduckgo.di.scopes.AppScope
27-
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
27+
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScript
2828
import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy
2929
import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate
3030
import com.squareup.anvil.annotations.ContributesBinding
@@ -45,8 +45,8 @@ class RealAddDocumentStartScriptDelegate @Inject constructor(
4545
private val webViewCompatWrapper: WebViewCompatWrapper,
4646
) : AddDocumentStartScriptDelegate {
4747

48-
override fun createPlugin(strategy: AddDocumentStartJavaScriptScriptStrategy): AddDocumentStartJavaScriptPlugin {
49-
return object : AddDocumentStartJavaScriptPlugin {
48+
override fun createPlugin(strategy: AddDocumentStartJavaScriptScriptStrategy): AddDocumentStartJavaScript {
49+
return object : AddDocumentStartJavaScript {
5050

5151
private var script: ScriptHandler? = null
5252
private var currentScriptString: String? = null

0 commit comments

Comments
 (0)