|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2023 DuckDuckGo |
| 2 | + * Copyright (c) 2025 DuckDuckGo |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package com.duckduckgo.contentscopescripts.impl
|
18 | 18 |
|
19 |
| -import android.annotation.SuppressLint |
20 |
| -import android.webkit.WebView |
21 |
| -import androidx.webkit.ScriptHandler |
22 |
| -import com.duckduckgo.app.browser.api.WebViewCapabilityChecker |
23 |
| -import com.duckduckgo.app.di.AppCoroutineScope |
24 |
| -import com.duckduckgo.browser.api.webviewcompat.WebViewCompatWrapper |
25 |
| -import com.duckduckgo.common.utils.DispatcherProvider |
26 | 19 | import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
|
27 | 20 | import com.duckduckgo.di.scopes.FragmentScope
|
28 | 21 | import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
|
| 22 | +import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy |
| 23 | +import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate |
29 | 24 | import com.squareup.anvil.annotations.ContributesMultibinding
|
30 | 25 | import dagger.SingleInstanceIn
|
31 | 26 | import javax.inject.Inject
|
32 |
| -import kotlinx.coroutines.CoroutineScope |
33 |
| -import kotlinx.coroutines.launch |
34 |
| -import kotlinx.coroutines.withContext |
35 | 27 |
|
36 | 28 | @SingleInstanceIn(FragmentScope::class)
|
37 | 29 | @ContributesMultibinding(FragmentScope::class)
|
38 | 30 | class ContentScopeScriptsAddDocumentStartJavaScriptPlugin @Inject constructor(
|
39 |
| - private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
40 |
| - private val dispatcherProvider: DispatcherProvider, |
41 |
| - private val webViewCapabilityChecker: WebViewCapabilityChecker, |
42 |
| - private val webViewCompatWrapper: WebViewCompatWrapper, |
43 |
| - private val contentScopeExperiments: ContentScopeExperiments, |
44 |
| - @AppCoroutineScope private val appCoroutineScope: CoroutineScope, |
45 |
| -) : AddDocumentStartJavaScriptPlugin { |
46 |
| - private var script: ScriptHandler? = null |
47 |
| - private var currentScriptString: String? = null |
48 |
| - |
49 |
| - @SuppressLint("RequiresFeature") |
50 |
| - override fun addDocumentStartJavaScript( |
51 |
| - webView: WebView, |
52 |
| - ) { |
53 |
| - appCoroutineScope.launch { |
54 |
| - if (!webViewCompatContentScopeScripts.isEnabled() || !webViewCapabilityChecker.isSupported( |
55 |
| - WebViewCapabilityChecker.WebViewCapability.DocumentStartJavaScript, |
56 |
| - ) |
57 |
| - ) { |
58 |
| - return@launch |
59 |
| - } |
| 31 | + webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
| 32 | + contentScopeExperiments: ContentScopeExperiments, |
| 33 | + scriptInjectorDelegate: AddDocumentStartScriptDelegate, |
| 34 | +) : AddDocumentStartJavaScriptPlugin by scriptInjectorDelegate.createPlugin( |
| 35 | + object : AddDocumentStartJavaScriptScriptStrategy { |
| 36 | + override suspend fun canInject(): Boolean { |
| 37 | + return webViewCompatContentScopeScripts.isEnabled() |
| 38 | + } |
60 | 39 |
|
| 40 | + override suspend fun getScriptString(): String { |
61 | 41 | val activeExperiments = contentScopeExperiments.getActiveExperiments()
|
62 |
| - val scriptString = webViewCompatContentScopeScripts.getScript(activeExperiments) |
63 |
| - if (scriptString == currentScriptString) { |
64 |
| - return@launch |
65 |
| - } |
66 |
| - script?.let { |
67 |
| - withContext(dispatcherProvider.main()) { |
68 |
| - it.remove() |
69 |
| - } |
70 |
| - script = null |
71 |
| - } |
72 |
| - |
73 |
| - webViewCompatWrapper.addDocumentStartJavaScript( |
74 |
| - webView, |
75 |
| - scriptString, |
76 |
| - setOf("*"), |
77 |
| - )?.let { |
78 |
| - script = it |
79 |
| - currentScriptString = scriptString |
80 |
| - } |
| 42 | + return webViewCompatContentScopeScripts.getScript(activeExperiments) |
81 | 43 | }
|
82 |
| - } |
83 |
| -} |
| 44 | + |
| 45 | + override val allowedOriginRules: Set<String> = setOf("*") |
| 46 | + }, |
| 47 | +) |
0 commit comments