2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-17 15:26:23 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/23697: Avoid NPEs while pasting URLs from the clipboard

It's possible for clipboard contents to have changed in the time between we've constructed
the onClick listeners and when they're actually invoked. Code before assumed that 'text'
will be not-null, and would crash otherwise. With this change, we're capturing contents of
the clipboard before constructing onClick handlers (while also asserting correct behaviour
of 'ClipboardHandler').

With this change, we'll log an error if we were told that clipboard contains a URL, but immediately
after that check 'text' was actually null. This would indicate one of two things:
- issues within the ClipboardHandler
- changing contents of the ClipboardHandler within a tiny time window between our check and querying
This commit is contained in:
Grigory Kruglov 2022-03-09 14:31:24 -08:00 committed by mergify[bot]
parent 86ab03ac93
commit f31000f48d

View File

@ -22,6 +22,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import java.lang.ref.WeakReference
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.databinding.BrowserToolbarPopupWindowBinding
object ToolbarPopupWindow {
@ -34,7 +35,8 @@ object ToolbarPopupWindow {
) {
val context = view.get()?.context ?: return
val clipboard = context.components.clipboardHandler
if (!copyVisible && !clipboard.containsURL()) return
val clipboardUrl = clipboard.getUrl()
if (!copyVisible && clipboardUrl == null) return
val isCustomTabSession = customTabId != null
@ -54,20 +56,21 @@ object ToolbarPopupWindow {
binding.copy.isVisible = copyVisible
binding.paste.isVisible = clipboard.containsURL() && !isCustomTabSession
binding.pasteAndGo.isVisible =
clipboard.containsURL() && !isCustomTabSession
val showPaste = clipboardUrl != null && !isCustomTabSession
binding.paste.isVisible = showPaste
binding.pasteAndGo.isVisible = showPaste
binding.copy.setOnClickListener {
if (copyVisible) {
binding.copy.setOnClickListener { copyView ->
popupWindow.dismiss()
clipboard.text = getUrlForClipboard(
it.context.components.core.store,
copyView.context.components.core.store,
customTabId
)
view.get()?.let {
view.get()?.let { toolbarView ->
FenixSnackbar.make(
view = it,
view = toolbarView,
duration = Snackbar.LENGTH_SHORT,
isDisplayedWithBrowserToolbar = true
)
@ -76,15 +79,18 @@ object ToolbarPopupWindow {
}
context.components.analytics.metrics.track(Event.CopyUrlUsed)
}
}
clipboardUrl?.let { url ->
binding.paste.setOnClickListener {
popupWindow.dismiss()
handlePaste(clipboard.text!!)
handlePaste(url)
}
binding.pasteAndGo.setOnClickListener {
popupWindow.dismiss()
handlePasteAndGo(clipboard.text!!)
handlePasteAndGo(url)
}
}
view.get()?.let {
@ -110,4 +116,12 @@ object ToolbarPopupWindow {
selectedTab?.readerState?.activeUrl ?: selectedTab?.content?.url
}
}
private fun ClipboardHandler.getUrl(): String? {
if (containsURL()) {
text?.let { return it }
Logger("ToolbarPopupWindow").error("Clipboard contains URL but unable to read text")
}
return null
}
}