Skip to content

Commit

Permalink
Add no-op WebMessageListener (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshliebe authored May 20, 2024
1 parent b89e969 commit 5c1d919
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.webkit.WebViewCompat
import androidx.webkit.WebViewFeature
import com.duckduckgo.anvil.annotations.InjectWith
import com.duckduckgo.app.accessibility.data.AccessibilitySettingsDataStore
import com.duckduckgo.app.autocomplete.api.AutoComplete.AutoCompleteSuggestion
Expand Down Expand Up @@ -161,6 +163,7 @@ import com.duckduckgo.app.browser.viewstate.OmnibarViewState
import com.duckduckgo.app.browser.viewstate.PrivacyShieldViewState
import com.duckduckgo.app.browser.viewstate.SavedSiteChangedViewState
import com.duckduckgo.app.browser.webshare.WebShareChooser
import com.duckduckgo.app.browser.webview.DummyWebMessageListenerFeature
import com.duckduckgo.app.browser.webview.WebContentDebugging
import com.duckduckgo.app.cta.ui.*
import com.duckduckgo.app.cta.ui.DaxDialogCta.*
Expand Down Expand Up @@ -482,6 +485,9 @@ class BrowserTabFragment :
@Inject
lateinit var subscriptions: Subscriptions

@Inject
lateinit var dummyWebMessageListenerFeature: DummyWebMessageListenerFeature

/**
* We use this to monitor whether the user was seeing the in-context Email Protection signup prompt
* This is needed because the activity stack will be cleared if an external link is opened in our browser
Expand Down Expand Up @@ -2344,11 +2350,35 @@ class BrowserTabFragment :
}
},
)
addNoOpWebMessageListener(it)
}

WebView.setWebContentsDebuggingEnabled(webContentDebugging.isEnabled())
}

// See https://app.asana.com/0/1200204095367872/1207300292572452/f (WebMessageListener debugging)
private fun addNoOpWebMessageListener(webView: DuckDuckGoWebView) {
lifecycleScope.launch(dispatchers.main()) {
val isFeatureEnabled = withContext(dispatchers.io()) {
dummyWebMessageListenerFeature.self().isEnabled()
}

if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_MESSAGE_LISTENER) &&
isFeatureEnabled &&
!webView.isDestroyed
) {
Timber.d("Adding no-op WebMessageListener")
WebViewCompat.addWebMessageListener(
webView,
"testObj",
setOf("*"),
) { _, _, _, _, _ ->
// no-op
}
}
}
}

private fun screenLock(data: JsCallbackData) {
val returnData = jsOrientationHandler.updateOrientation(data, this)
contentScopeScripts.onResponse(returnData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild3 {
private var nestedScrollHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)
private val helper = CoordinatorLayoutHelper()

var isDestroyed: Boolean = false

constructor(context: Context) : this(context, null)
constructor(
context: Context,
Expand All @@ -62,6 +64,11 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild3 {
helper.onViewAttached(this)
}

override fun destroy() {
isDestroyed = true
super.destroy()
}

fun setBottomMatchingBehaviourEnabled(value: Boolean) {
helper.setBottomMatchingBehaviourEnabled(value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.browser.webview

import com.duckduckgo.anvil.annotations.ContributesRemoteFeature
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.Toggle

@ContributesRemoteFeature(
scope = AppScope::class,
featureName = "dummyWebMessageListener",
)

interface DummyWebMessageListenerFeature {

@Toggle.DefaultValue(false)
fun self(): Toggle
}

0 comments on commit 5c1d919

Please sign in to comment.