Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Don't read pasteboard for text when evaluating search suggestions #27212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,14 @@ extension BrowserViewController: TopToolbarDelegate {
} else {
showSearchController()

let locationLastReplacement = topToolbar.locationLastReplacement
let isPasting = topToolbar.isPastingInURLBar
Task {
await searchController?.setSearchQuery(
query: text,
showSearchSuggestions: URLBarHelper.shared.shouldShowSearchSuggestions(
using: topToolbar.locationLastReplacement
using: locationLastReplacement,
isPasting: isPasting
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class TopToolbarView: UIView, ToolbarProtocol {
locationTextField?.lastReplacement ?? ""
}

var isPastingInURLBar: Bool {
locationTextField?.isPasting ?? false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: isPasting == true

}

// MARK: Views

private var locationTextField: AutocompleteTextField?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public class AutocompleteTextField: UITextField, UITextFieldDelegate {
self,
didEnterText: self.text?.preferredSearchSuggestionText ?? ""
)
if self.isPasting {
self.isPasting = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You can do self.isPasting = false without the if-check I think. If it's true, it gets set to false. If it's already false, it's set to false. No branches :)

}
}
}
)
Expand Down Expand Up @@ -217,6 +220,13 @@ public class AutocompleteTextField: UITextField, UITextFieldDelegate {
return hasActiveCompletion
}

private(set) var isPasting: Bool = false

public override func paste(_ sender: Any?) {
isPasting = true
super.paste(sender)
}

// `shouldChangeCharactersInRange` is called before the text changes, and textDidChange is called after.
// Since the text has changed, remove the completion here, and textDidChange will fire the callback to
// get the new autocompletion.
Expand Down
22 changes: 3 additions & 19 deletions ios/brave-ios/Sources/Brave/Helpers/URLBarHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,11 @@ class URLBarHelper {

static let shared = URLBarHelper()

func shouldShowSearchSuggestions(using lastReplacement: String) async -> Bool {
// Check if last entry to url textfield needs to be checked as suspicious.
// The reason of checking count is bigger than 1 is the single character
// entries will always be safe and only way to achieve multi character entry is
// using paste board.
// This check also allow us to handle paste permission case
guard lastReplacement.count > 1 else {
return true
}

// Check if paste board has any text to guarantee the case
guard UIPasteboard.general.hasStrings || UIPasteboard.general.hasURLs else {
return true
}

// Perform check on pasted text
if let pasteboardContents = UIPasteboard.general.string {
let isSuspicious = await isSuspiciousQuery(pasteboardContents)
func shouldShowSearchSuggestions(using lastReplacement: String, isPasting: Bool) async -> Bool {
if isPasting {
let isSuspicious = await isSuspiciousQuery(lastReplacement)
return !isSuspicious
}

return true
}

Expand Down
Loading