Skip to content

Commit 676e4c1

Browse files
committed
Use safeWebMessageHandler code for autofill integration
1 parent 3b18036 commit 676e4c1

File tree

6 files changed

+130
-9
lines changed

6 files changed

+130
-9
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+
}

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

Lines changed: 8 additions & 6 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,24 @@ 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

119121
@SuppressLint("AddWebMessageListenerUsage")
120122
// suppress AddWebMessageListenerUsage, we don't have access to DuckDuckGoWebView here.
121-
override fun addListener(
123+
override suspend fun addListener(
122124
webView: WebView,
123125
listener: AutofillWebMessageListener,
124126
) {
125-
WebViewCompat.addWebMessageListener(webView, listener.key, listener.origins, listener)
127+
safeWebMessageHandler.addWebMessageListener(webView, listener.key, listener.origins, listener)
126128
}
127129
}

browser-api/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ dependencies {
3535

3636
// LiveData
3737
implementation AndroidX.lifecycle.liveDataKtx
38+
39+
implementation AndroidX.webkit
3840
}
3941
android {
4042
namespace 'com.duckduckgo.browser.api'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.api
18+
19+
import android.webkit.WebView
20+
import androidx.webkit.WebViewCompat.WebMessageListener
21+
22+
/**
23+
* Add and remove web message listeners to a WebView, guarded by extra checks to ensure WebView compatibility
24+
*/
25+
interface SafeWebMessageHandler {
26+
27+
suspend fun addWebMessageListener(
28+
webView: WebView,
29+
jsObjectName: String,
30+
allowedOriginRules: Set<String>,
31+
listener: WebMessageListener,
32+
): Boolean
33+
34+
suspend fun removeWebMessageListener(
35+
webView: WebView,
36+
jsObjectName: String,
37+
): Boolean
38+
}

0 commit comments

Comments
 (0)