Skip to content

Commit 8bf30a2

Browse files
committed
Merge branch 'hotfix/5.216.1'
2 parents 43b85c9 + 9ca1df9 commit 8bf30a2

File tree

12 files changed

+139
-20
lines changed

12 files changed

+139
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild3 {
6262
private var nestedScrollHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
6363
private val helper = CoordinatorLayoutHelper()
6464

65-
private var isDestroyed: Boolean = false
65+
var isDestroyed: Boolean = false
6666
var isSafeWebViewEnabled: Boolean = false
6767

6868
constructor(context: Context) : this(context, null)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import com.duckduckgo.app.browser.api.WebViewCapabilityChecker.WebViewCapability
2424
import com.duckduckgo.browser.api.WebViewVersionProvider
2525
import com.duckduckgo.common.utils.DispatcherProvider
2626
import com.duckduckgo.common.utils.extensions.compareSemanticVersion
27-
import com.duckduckgo.di.scopes.FragmentScope
27+
import com.duckduckgo.di.scopes.AppScope
2828
import com.squareup.anvil.annotations.ContributesBinding
2929
import javax.inject.Inject
3030
import kotlinx.coroutines.withContext
3131

32-
@ContributesBinding(FragmentScope::class)
32+
@ContributesBinding(AppScope::class)
3333
class RealWebViewCapabilityChecker @Inject constructor(
3434
private val dispatchers: DispatcherProvider,
3535
private val webViewVersionProvider: WebViewVersionProvider,
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (c) 2024 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 android.annotation.SuppressLint
20+
import android.webkit.WebView
21+
import androidx.webkit.WebViewCompat
22+
import androidx.webkit.WebViewCompat.WebMessageListener
23+
import com.duckduckgo.app.browser.api.SafeWebMessageHandler
24+
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker
25+
import com.duckduckgo.app.browser.api.WebViewCapabilityChecker.WebViewCapability
26+
import com.duckduckgo.di.scopes.AppScope
27+
import com.squareup.anvil.annotations.ContributesBinding
28+
import javax.inject.Inject
29+
import timber.log.Timber
30+
31+
@SuppressLint("RequiresFeature", "AddWebMessageListenerUsage", "RemoveWebMessageListenerUsage")
32+
@ContributesBinding(AppScope::class)
33+
class SafeWebMessageHandlerImpl @Inject constructor(
34+
private val webViewCapabilityChecker: WebViewCapabilityChecker,
35+
) : SafeWebMessageHandler {
36+
37+
override suspend fun addWebMessageListener(
38+
webView: WebView,
39+
jsObjectName: String,
40+
allowedOriginRules: Set<String>,
41+
listener: WebMessageListener,
42+
): Boolean = runCatching {
43+
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener) && !isDestroyed(webView)) {
44+
WebViewCompat.addWebMessageListener(webView, jsObjectName, allowedOriginRules, listener)
45+
true
46+
} else {
47+
false
48+
}
49+
}.getOrElse { exception ->
50+
Timber.e(exception, "Error adding WebMessageListener: $jsObjectName")
51+
false
52+
}
53+
54+
override suspend fun removeWebMessageListener(
55+
webView: WebView,
56+
jsObjectName: String,
57+
): Boolean = runCatching {
58+
if (webViewCapabilityChecker.isSupported(WebViewCapability.WebMessageListener) && !isDestroyed(webView)) {
59+
WebViewCompat.removeWebMessageListener(webView, jsObjectName)
60+
true
61+
} else {
62+
false
63+
}
64+
}.getOrElse { exception ->
65+
Timber.e(exception, "Error removing WebMessageListener: $jsObjectName")
66+
false
67+
}
68+
69+
/**
70+
* Can only check destroyed flag for DuckDuckGoWebView for now. If a normal WebView, assume not destroyed.
71+
*/
72+
private fun isDestroyed(webView: WebView): Boolean {
73+
return if (webView is DuckDuckGoWebView) {
74+
webView.isDestroyed
75+
} else {
76+
false
77+
}
78+
}
79+
}

app/version/version.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION=5.216.0
1+
VERSION=5.216.1

autofill/autofill-api/src/main/java/com/duckduckgo/autofill/api/AutofillFeature.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ interface AutofillFeature {
3333
/**
3434
* Kill switch for if we should inject Autofill javascript into the browser.
3535
*
36-
* @return `true` when the remote config has the global "canIntegrateAutofillInWebView" autofill sub-feature flag enabled
36+
* @return `true` when the remote config has the global "canIntegrateWebMessageBasedAutofillInWebView" autofill sub-feature flag enabled
3737
* If the remote feature is not present defaults to `true`
3838
*/
3939
@Toggle.DefaultValue(true)
40-
fun canIntegrateAutofillInWebView(): Toggle
40+
fun canIntegrateWebMessageBasedAutofillInWebView(): Toggle
4141

4242
/**
4343
* @return `true` when the remote config has the global "canInjectCredentials" autofill sub-feature flag enabled

autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/AutofillGlobalCapabilityChecker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AutofillGlobalCapabilityCheckerImpl @Inject constructor(
5555
override suspend fun isAutofillEnabledByConfiguration(url: String): Boolean {
5656
return withContext(dispatcherProvider.io()) {
5757
val enabledAtTopLevel = isInternalTester() || isGlobalFeatureEnabled()
58-
val canIntegrateAutofill = autofillFeature.canIntegrateAutofillInWebView().isEnabled()
58+
val canIntegrateAutofill = autofillFeature.canIntegrateWebMessageBasedAutofillInWebView().isEnabled()
5959
enabledAtTopLevel && canIntegrateAutofill && !isAnException(url)
6060
}
6161
}

autofill/autofill-impl/src/main/java/com/duckduckgo/autofill/impl/InlineBrowserAutofill.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package com.duckduckgo.autofill.impl
1818

1919
import android.annotation.SuppressLint
2020
import android.webkit.WebView
21-
import androidx.webkit.WebViewCompat
21+
import com.duckduckgo.app.browser.api.SafeWebMessageHandler
2222
import com.duckduckgo.autofill.api.AutofillFeature
2323
import com.duckduckgo.autofill.api.BrowserAutofill
2424
import com.duckduckgo.autofill.api.Callback
@@ -90,7 +90,7 @@ class InlineBrowserAutofill @Inject constructor(
9090
}
9191
}
9292

93-
private fun WebView.addWebMessageListener(
93+
private suspend fun WebView.addWebMessageListener(
9494
messageListener: AutofillWebMessageListener,
9595
autofillCallback: Callback,
9696
tabId: String,
@@ -106,22 +106,22 @@ class InlineBrowserAutofill @Inject constructor(
106106
}
107107

108108
interface AutofillWebMessageAttacher {
109-
fun addListener(
109+
suspend fun addListener(
110110
webView: WebView,
111111
listener: AutofillWebMessageListener,
112112
)
113113
}
114114

115115
@SuppressLint("RequiresFeature")
116116
@ContributesBinding(FragmentScope::class)
117-
class AutofillWebMessageAttacherImpl @Inject constructor() : AutofillWebMessageAttacher {
117+
class AutofillWebMessageAttacherImpl @Inject constructor(
118+
private val safeWebMessageHandler: SafeWebMessageHandler,
119+
) : AutofillWebMessageAttacher {
118120

119-
@SuppressLint("AddWebMessageListenerUsage")
120-
// suppress AddWebMessageListenerUsage, we don't have access to DuckDuckGoWebView here.
121-
override fun addListener(
121+
override suspend fun addListener(
122122
webView: WebView,
123123
listener: AutofillWebMessageListener,
124124
) {
125-
WebViewCompat.addWebMessageListener(webView, listener.key, listener.origins, listener)
125+
safeWebMessageHandler.addWebMessageListener(webView, listener.key, listener.origins, listener)
126126
}
127127
}

autofill/autofill-impl/src/test/java/com/duckduckgo/autofill/impl/AutofillGlobalCapabilityCheckerImplGlobalFeatureTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class AutofillGlobalCapabilityCheckerImplGlobalFeatureTest(
230230
private fun configureCanIntegrateAutofillSubfeature(isEnabled: Boolean) {
231231
val toggle: Toggle = mock()
232232
whenever(toggle.isEnabled()).thenReturn(isEnabled)
233-
whenever(autofillFeature.canIntegrateAutofillInWebView()).thenReturn(toggle)
233+
whenever(autofillFeature.canIntegrateWebMessageBasedAutofillInWebView()).thenReturn(toggle)
234234
}
235235

236236
private fun configureIfUrlIsException(isException: Boolean) {

autofill/autofill-impl/src/test/java/com/duckduckgo/autofill/impl/InlineBrowserAutofillTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class InlineBrowserAutofillTest {
9595
canSaveCredentials: Boolean = true,
9696
canGeneratePassword: Boolean = true,
9797
canAccessCredentialManagement: Boolean = true,
98-
canIntegrateAutofillInWebView: Boolean = true,
98+
canIntegrateWebMessageBasedAutofillInWebView: Boolean = true,
9999
deviceWebViewSupportsAutofill: Boolean = true,
100100
): InlineBrowserAutofill {
101101
val autofillFeature = FakeFeatureToggleFactory.create(AutofillFeature::class.java)
@@ -104,10 +104,10 @@ class InlineBrowserAutofillTest {
104104
autofillFeature.canSaveCredentials().setRawStoredState(State(enable = canSaveCredentials))
105105
autofillFeature.canGeneratePasswords().setRawStoredState(State(enable = canGeneratePassword))
106106
autofillFeature.canAccessCredentialManagement().setRawStoredState(State(enable = canAccessCredentialManagement))
107-
autofillFeature.canIntegrateAutofillInWebView().setRawStoredState(State(enable = canIntegrateAutofillInWebView))
107+
autofillFeature.canIntegrateWebMessageBasedAutofillInWebView().setRawStoredState(State(enable = canIntegrateWebMessageBasedAutofillInWebView))
108108

109109
whenever(capabilityChecker.webViewSupportsAutofill()).thenReturn(deviceWebViewSupportsAutofill)
110-
whenever(capabilityChecker.canInjectCredentialsToWebView(any())).thenReturn(canIntegrateAutofillInWebView)
110+
whenever(capabilityChecker.canInjectCredentialsToWebView(any())).thenReturn(canInjectCredentials)
111111

112112
return InlineBrowserAutofill(
113113
autofillCapabilityChecker = capabilityChecker,

autofill/autofill-internal/src/main/java/com/duckduckgo/autofill/internal/AutofillInternalSettingsActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class AutofillInternalSettingsActivity : DuckDuckGoActivity() {
117117
val autofillEnabled = autofillFeature.self()
118118
val onByDefault = autofillFeature.onByDefault()
119119
val onForExistingUsers = autofillFeature.onForExistingUsers()
120-
val canIntegrateAutofill = autofillFeature.canIntegrateAutofillInWebView()
120+
val canIntegrateAutofill = autofillFeature.canIntegrateWebMessageBasedAutofillInWebView()
121121
val canSaveCredentials = autofillFeature.canSaveCredentials()
122122
val canInjectCredentials = autofillFeature.canInjectCredentials()
123123
val canGeneratePasswords = autofillFeature.canGeneratePasswords()

0 commit comments

Comments
 (0)