|
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.browser.api.webviewcompat.WebViewCompatWrapper |
24 |
| -import com.duckduckgo.common.utils.DispatcherProvider |
25 | 19 | import com.duckduckgo.contentscopescripts.api.contentscopeExperiments.ContentScopeExperiments
|
26 | 20 | import com.duckduckgo.di.scopes.FragmentScope
|
27 | 21 | import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptPlugin
|
| 22 | +import com.duckduckgo.js.messaging.api.AddDocumentStartJavaScriptScriptStrategy |
| 23 | +import com.duckduckgo.js.messaging.api.AddDocumentStartScriptDelegate |
28 | 24 | import com.squareup.anvil.annotations.ContributesMultibinding
|
29 | 25 | import dagger.SingleInstanceIn
|
30 |
| -import kotlinx.coroutines.withContext |
31 | 26 | import javax.inject.Inject
|
32 | 27 |
|
33 | 28 | @SingleInstanceIn(FragmentScope::class)
|
34 | 29 | @ContributesMultibinding(FragmentScope::class)
|
35 | 30 | class ContentScopeScriptsAddDocumentStartJavaScriptPlugin @Inject constructor(
|
36 |
| - private val webViewCompatContentScopeScripts: WebViewCompatContentScopeScripts, |
37 |
| - private val dispatcherProvider: DispatcherProvider, |
38 |
| - private val webViewCapabilityChecker: WebViewCapabilityChecker, |
39 |
| - private val webViewCompatWrapper: WebViewCompatWrapper, |
40 |
| - private val contentScopeExperiments: ContentScopeExperiments, |
41 |
| -) : AddDocumentStartJavaScriptPlugin { |
42 |
| - private var script: ScriptHandler? = null |
43 |
| - private var currentScriptString: String? = null |
44 |
| - |
45 |
| - @SuppressLint("RequiresFeature") |
46 |
| - override suspend fun addDocumentStartJavaScript(webView: WebView) { |
47 |
| - if (!webViewCompatContentScopeScripts.isEnabled() || |
48 |
| - !webViewCapabilityChecker.isSupported( |
49 |
| - WebViewCapabilityChecker.WebViewCapability.DocumentStartJavaScript, |
50 |
| - ) |
51 |
| - ) { |
52 |
| - return |
| 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() |
53 | 38 | }
|
54 | 39 |
|
55 |
| - val activeExperiments = contentScopeExperiments.getActiveExperiments() |
56 |
| - val scriptString = webViewCompatContentScopeScripts.getScript(activeExperiments) |
57 |
| - if (scriptString == currentScriptString) { |
58 |
| - return |
59 |
| - } |
60 |
| - script?.let { |
61 |
| - withContext(dispatcherProvider.main()) { |
62 |
| - it.remove() |
63 |
| - } |
64 |
| - script = null |
| 40 | + override suspend fun getScriptString(): String { |
| 41 | + val activeExperiments = contentScopeExperiments.getActiveExperiments() |
| 42 | + return webViewCompatContentScopeScripts.getScript(activeExperiments) |
65 | 43 | }
|
66 | 44 |
|
67 |
| - webViewCompatWrapper |
68 |
| - .addDocumentStartJavaScript( |
69 |
| - webView, |
70 |
| - scriptString, |
71 |
| - setOf("*"), |
72 |
| - )?.let { |
73 |
| - script = it |
74 |
| - currentScriptString = scriptString |
75 |
| - } |
76 |
| - } |
77 |
| -} |
| 45 | + override val allowedOriginRules: Set<String> = setOf("*") |
| 46 | + }, |
| 47 | +) |
0 commit comments