@@ -20,64 +20,79 @@ import android.webkit.WebView
20
20
import androidx.webkit.ScriptHandler
21
21
import androidx.webkit.WebViewCompat
22
22
import androidx.webkit.WebViewFeature
23
+ import com.duckduckgo.app.di.AppCoroutineScope
23
24
import com.duckduckgo.browser.api.JsInjectorPlugin
25
+ import com.duckduckgo.common.utils.DispatcherProvider
26
+ import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
24
27
import com.duckduckgo.di.scopes.AppScope
25
28
import com.duckduckgo.feature.toggles.api.Toggle
26
29
import com.squareup.anvil.annotations.ContributesMultibinding
27
30
import javax.inject.Inject
31
+ import kotlinx.coroutines.CoroutineScope
32
+ import kotlinx.coroutines.launch
33
+ import kotlinx.coroutines.withContext
28
34
29
35
@ContributesMultibinding(AppScope ::class )
30
36
class ContentScopeScriptsJsInjectorPlugin @Inject constructor(
31
37
private val coreContentScopeScripts : CoreContentScopeScripts ,
32
38
private val adsJsContentScopeScripts : AdsJsContentScopeScripts ,
39
+ private val contentScopeExperiments : ContentScopeExperiments ,
40
+ @AppCoroutineScope private val appCoroutineScope : CoroutineScope ,
41
+ private val dispatcherProvider : DispatcherProvider ,
33
42
) : JsInjectorPlugin {
34
43
private var script: ScriptHandler ? = null
35
44
private var currentScriptString: String? = null
36
45
46
+ private var activeExperiments: List <Toggle > = emptyList()
47
+
37
48
private fun reloadJSIfNeeded (
38
49
webView : WebView ,
39
- activeExperiments : List <Toggle >,
40
50
) {
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
+ }
55
71
}
56
72
}
57
73
58
74
override fun onInit (
59
75
webView : WebView ,
60
- activeExperiments : List <Toggle >,
61
76
) {
62
- reloadJSIfNeeded(webView, activeExperiments )
77
+ reloadJSIfNeeded(webView)
63
78
}
64
79
65
80
override fun onPageStarted (
66
81
webView : WebView ,
67
82
url : String? ,
68
83
isDesktopMode : Boolean? ,
69
- activeExperiments : List <Toggle >,
70
- ) {
84
+ ): List <Toggle > {
71
85
if (coreContentScopeScripts.isEnabled()) {
72
86
webView.evaluateJavascript(" javascript:${coreContentScopeScripts.getScript(isDesktopMode, activeExperiments)} " , null )
87
+ return activeExperiments
73
88
}
89
+ return listOf ()
74
90
}
75
91
76
92
override fun onPageFinished (
77
93
webView : WebView ,
78
94
url : String? ,
79
- activeExperiments : List <Toggle >,
80
95
) {
81
- reloadJSIfNeeded(webView, activeExperiments )
96
+ reloadJSIfNeeded(webView)
82
97
}
83
98
}
0 commit comments