Skip to content

Commit e4c0262

Browse files
committed
Properly store and inject activeExperiments
1 parent 1c56c20 commit e4c0262

File tree

3 files changed

+43
-38
lines changed

3 files changed

+43
-38
lines changed

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import com.duckduckgo.common.utils.AppUrl.ParamKey.QUERY
7070
import com.duckduckgo.common.utils.CurrentTimeProvider
7171
import com.duckduckgo.common.utils.DispatcherProvider
7272
import com.duckduckgo.common.utils.plugins.PluginPoint
73-
import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
7473
import com.duckduckgo.cookies.api.CookieManagerProvider
7574
import com.duckduckgo.duckchat.api.DuckChat
7675
import com.duckduckgo.duckplayer.api.DuckPlayer
@@ -125,7 +124,6 @@ class BrowserWebViewClient @Inject constructor(
125124
private val uriLoadedManager: UriLoadedManager,
126125
private val androidFeaturesHeaderPlugin: AndroidFeaturesHeaderPlugin,
127126
private val duckChat: DuckChat,
128-
private val contentScopeExperiments: ContentScopeExperiments,
129127
) : WebViewClient() {
130128

131129
var webViewClientListener: WebViewClientListener? = null
@@ -441,10 +439,10 @@ class BrowserWebViewClient @Inject constructor(
441439
val navigationList = webView.safeCopyBackForwardList() ?: return
442440

443441
appCoroutineScope.launch(dispatcherProvider.main()) {
444-
val activeExperiments = contentScopeExperiments.getActiveExperiments()
445-
webViewClientListener?.pageStarted(WebViewNavigationState(navigationList), activeExperiments)
446-
jsPlugins.getPlugins().forEach {
447-
it.onPageStarted(webView, url, webViewClientListener?.getSite()?.isDesktopMode, activeExperiments)
442+
jsPlugins.getPlugins().map {
443+
it.onPageStarted(webView, url, webViewClientListener?.getSite()?.isDesktopMode)
444+
}.flatten().distinct().let { activeExperiments ->
445+
webViewClientListener?.pageStarted(WebViewNavigationState(navigationList), activeExperiments)
448446
}
449447
}
450448
if (url != null && url == lastPageStarted) {
@@ -464,11 +462,8 @@ class BrowserWebViewClient @Inject constructor(
464462
}
465463

466464
fun triggerJSInit(webView: WebView) {
467-
appCoroutineScope.launch(dispatcherProvider.main()) {
468-
val activeExperiments = contentScopeExperiments.getActiveExperiments()
469-
jsPlugins.getPlugins().forEach {
470-
it.onInit(webView, activeExperiments)
471-
}
465+
jsPlugins.getPlugins().forEach {
466+
it.onInit(webView)
472467
}
473468
}
474469

@@ -480,11 +475,9 @@ class BrowserWebViewClient @Inject constructor(
480475
// See https://app.asana.com/0/0/1206159443951489/f (WebView limitations)
481476
if (webView.progress == 100) {
482477
jsPlugins.getPlugins().forEach {
483-
val activeExperiments = webViewClientListener?.getSite()?.activeContentScopeExperiments ?: listOf()
484478
it.onPageFinished(
485479
webView,
486480
url,
487-
activeExperiments,
488481
)
489482
}
490483

browser-api/src/main/java/com/duckduckgo/browser/api/JsInjectorPlugin.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ interface JsInjectorPlugin {
2626
*/
2727
fun onInit(
2828
webView: WebView,
29-
activeExperiments: List<Toggle>,
3029
)
3130

3231
/**
@@ -36,15 +35,13 @@ interface JsInjectorPlugin {
3635
webView: WebView,
3736
url: String?,
3837
isDesktopMode: Boolean?,
39-
activeExperiments: List<Toggle> = listOf(),
40-
)
38+
): List<Toggle>
4139

4240
/**
4341
* This method is called during onPageFinished and receives a [webView] instance, the [url] of the website and the [site]
4442
*/
4543
fun onPageFinished(
4644
webView: WebView,
4745
url: String?,
48-
activeExperiments: List<Toggle>,
4946
)
5047
}

content-scope-scripts/content-scope-scripts-impl/src/main/java/com/duckduckgo/contentscopescripts/impl/ContentScopeScriptsJsInjectorPlugin.kt

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,79 @@ import android.webkit.WebView
2020
import androidx.webkit.ScriptHandler
2121
import androidx.webkit.WebViewCompat
2222
import androidx.webkit.WebViewFeature
23+
import com.duckduckgo.app.di.AppCoroutineScope
2324
import com.duckduckgo.browser.api.JsInjectorPlugin
25+
import com.duckduckgo.common.utils.DispatcherProvider
26+
import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
2427
import com.duckduckgo.di.scopes.AppScope
2528
import com.duckduckgo.feature.toggles.api.Toggle
2629
import com.squareup.anvil.annotations.ContributesMultibinding
2730
import javax.inject.Inject
31+
import kotlinx.coroutines.CoroutineScope
32+
import kotlinx.coroutines.launch
33+
import kotlinx.coroutines.withContext
2834

2935
@ContributesMultibinding(AppScope::class)
3036
class ContentScopeScriptsJsInjectorPlugin @Inject constructor(
3137
private val coreContentScopeScripts: CoreContentScopeScripts,
3238
private val adsJsContentScopeScripts: AdsJsContentScopeScripts,
39+
private val contentScopeExperiments: ContentScopeExperiments,
40+
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
41+
private val dispatcherProvider: DispatcherProvider,
3342
) : JsInjectorPlugin {
3443
private var script: ScriptHandler? = null
3544
private var currentScriptString: String? = null
3645

46+
private var activeExperiments: List<Toggle> = emptyList()
47+
3748
private fun reloadJSIfNeeded(
3849
webView: WebView,
39-
activeExperiments: List<Toggle>,
4050
) {
41-
if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
42-
return
43-
}
44-
val scriptString = adsJsContentScopeScripts.getScript(activeExperiments)
45-
if (scriptString == currentScriptString) {
46-
return
47-
}
48-
script?.let {
49-
it.remove()
50-
script = null
51-
}
52-
if (coreContentScopeScripts.isEnabled()) {
53-
currentScriptString = scriptString
54-
script = WebViewCompat.addDocumentStartJavaScript(webView, scriptString, setOf("*"))
51+
appCoroutineScope.launch(dispatcherProvider.io()) {
52+
activeExperiments = contentScopeExperiments.getActiveExperiments()
53+
54+
withContext(dispatcherProvider.main()) {
55+
if (!WebViewFeature.isFeatureSupported(WebViewFeature.DOCUMENT_START_SCRIPT)) {
56+
return@withContext
57+
}
58+
val scriptString = adsJsContentScopeScripts.getScript(activeExperiments)
59+
if (scriptString == currentScriptString) {
60+
return@withContext
61+
}
62+
script?.let {
63+
it.remove()
64+
script = null
65+
}
66+
if (coreContentScopeScripts.isEnabled()) {
67+
currentScriptString = scriptString
68+
script = WebViewCompat.addDocumentStartJavaScript(webView, scriptString, setOf("*"))
69+
}
70+
}
5571
}
5672
}
5773

5874
override fun onInit(
5975
webView: WebView,
60-
activeExperiments: List<Toggle>,
6176
) {
62-
reloadJSIfNeeded(webView, activeExperiments)
77+
reloadJSIfNeeded(webView)
6378
}
6479

6580
override fun onPageStarted(
6681
webView: WebView,
6782
url: String?,
6883
isDesktopMode: Boolean?,
69-
activeExperiments: List<Toggle>,
70-
) {
84+
): List<Toggle> {
7185
if (coreContentScopeScripts.isEnabled()) {
7286
webView.evaluateJavascript("javascript:${coreContentScopeScripts.getScript(isDesktopMode, activeExperiments)}", null)
87+
return activeExperiments
7388
}
89+
return listOf()
7490
}
7591

7692
override fun onPageFinished(
7793
webView: WebView,
7894
url: String?,
79-
activeExperiments: List<Toggle>,
8095
) {
81-
reloadJSIfNeeded(webView, activeExperiments)
96+
reloadJSIfNeeded(webView)
8297
}
8398
}

0 commit comments

Comments
 (0)