Skip to content

Commit

Permalink
Fix #5848: Remove add search engine button from keyboard (#5857)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnarcese authored Dec 5, 2019
1 parent 68c0cc5 commit adfafb2
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 170 deletions.
4 changes: 0 additions & 4 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@
3BC659491E5BA4AE006D560F /* TopSites in Resources */ = {isa = PBXBuildFile; fileRef = 3BC659481E5BA4AE006D560F /* TopSites */; };
3BC659591E5BA505006D560F /* top_sites.json in Resources */ = {isa = PBXBuildFile; fileRef = 3BC659581E5BA505006D560F /* top_sites.json */; };
3BCE6D3C1CEB9E4D0080928C /* ThirdPartySearchAlerts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BCE6D3B1CEB9E4D0080928C /* ThirdPartySearchAlerts.swift */; };
3BE7275D1CCFE8B60099189F /* CustomSearchHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BE7275C1CCFE8B60099189F /* CustomSearchHandler.swift */; };
3BF4B8E91D38497A00493393 /* BaseTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF4B8E81D38497A00493393 /* BaseTestCase.swift */; };
3BF56D271CDBBE1F00AC4D75 /* SimpleToast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BF56D261CDBBE1F00AC4D75 /* SimpleToast.swift */; };
3BFCBF201E04B1C50070C042 /* UIImageViewExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BFCBF1F1E04B1C50070C042 /* UIImageViewExtensionsTests.swift */; };
Expand Down Expand Up @@ -1375,7 +1374,6 @@
3BC659481E5BA4AE006D560F /* TopSites */ = {isa = PBXFileReference; lastKnownFileType = folder; path = TopSites; sourceTree = "<group>"; };
3BC659581E5BA505006D560F /* top_sites.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = top_sites.json; sourceTree = "<group>"; };
3BCE6D3B1CEB9E4D0080928C /* ThirdPartySearchAlerts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThirdPartySearchAlerts.swift; sourceTree = "<group>"; };
3BE7275C1CCFE8B60099189F /* CustomSearchHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomSearchHandler.swift; sourceTree = "<group>"; };
3BF4B8E81D38497A00493393 /* BaseTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestCase.swift; sourceTree = "<group>"; };
3BF56D261CDBBE1F00AC4D75 /* SimpleToast.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleToast.swift; sourceTree = "<group>"; };
3BFCBF1F1E04B1C50070C042 /* UIImageViewExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewExtensionsTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2892,7 +2890,6 @@
C82CDD45233E8996002E2743 /* Tab+ChangeUserAgent.swift */,
31ADB5D91E58CEC300E87909 /* ClipboardBarDisplayHandler.swift */,
D3BA7E0D1B0E934F00153782 /* ContextMenuHelper.swift */,
3BE7275C1CCFE8B60099189F /* CustomSearchHandler.swift */,
D01017F4219CB6BD009CBB5A /* DownloadContentScript.swift */,
D0625C97208E87F10081F3B2 /* DownloadQueue.swift */,
D04D1B852097859B0074B35F /* DownloadToast.swift */,
Expand Down Expand Up @@ -5163,7 +5160,6 @@
884CA7492344A301002E4711 /* TextContentDetector.swift in Sources */,
7B844E3D1BBDDB9D00E733A2 /* ChevronView.swift in Sources */,
E4C358551AF144BA00299F7E /* FSReadingList.m in Sources */,
3BE7275D1CCFE8B60099189F /* CustomSearchHandler.swift in Sources */,
E692E3291C46E62D009D1240 /* AuthenticationSettingsViewController.swift in Sources */,
E663D5781BB341C4001EF30E /* ToggleButton.swift in Sources */,
EBA3B2D22268F57E00728BDB /* BadgeIcon.swift in Sources */,
Expand Down
140 changes: 0 additions & 140 deletions Client/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ class BrowserViewController: UIViewController {

lazy var mailtoLinkHandler = MailtoLinkHandler()

lazy fileprivate var customSearchEngineButton: UIButton = {
let searchButton = UIButton()
searchButton.setImage(UIImage(named: "AddSearch")?.withRenderingMode(.alwaysTemplate), for: [])
searchButton.addTarget(self, action: #selector(addCustomSearchEngineForFocusedElement), for: .touchUpInside)
searchButton.accessibilityIdentifier = "BrowserViewController.customSearchEngineButton"
return searchButton
}()

fileprivate var customSearchBarButton: UIBarButtonItem?

// popover rotation handling
Expand Down Expand Up @@ -1535,9 +1527,6 @@ extension BrowserViewController: TabDelegate {
let printHelper = PrintHelper(tab: tab)
tab.addContentScript(printHelper, name: PrintHelper.name())

let customSearchHelper = CustomSearchHelper(tab: tab)
tab.addContentScript(customSearchHelper, name: CustomSearchHelper.name())

let nightModeHelper = NightModeHelper(tab: tab)
tab.addContentScript(nightModeHelper, name: NightModeHelper.name())

Expand Down Expand Up @@ -2205,116 +2194,6 @@ extension BrowserViewController {
}
}

/**
A third party search engine Browser extension
**/
extension BrowserViewController {

func addCustomSearchButtonToWebView(_ webView: WKWebView) {
//check if the search engine has already been added.
let domain = webView.url?.domainURL.host
let matches = self.profile.searchEngines.orderedEngines.filter {$0.shortName == domain}
if !matches.isEmpty {
self.customSearchEngineButton.tintColor = UIColor.Photon.Grey50
self.customSearchEngineButton.isUserInteractionEnabled = false
} else {
self.customSearchEngineButton.tintColor = UIConstants.SystemBlueColor
self.customSearchEngineButton.isUserInteractionEnabled = true
}

/*
This is how we access hidden views in the WKContentView
Using the public headers we can find the keyboard accessoryView which is not usually available.
Specific values here are from the WKContentView headers.
https://github.com/JaviSoto/iOS9-Runtime-Headers/blob/master/Frameworks/WebKit.framework/WKContentView.h
*/
guard let webContentView = UIView.findSubViewWithFirstResponder(webView) else {
/*
In some cases the URL bar can trigger the keyboard notification. In that case the webview isnt the first responder
and a search button should not be added.
*/
return
}

guard let input = webContentView.perform(#selector(getter: UIResponder.inputAccessoryView)),
let inputView = input.takeUnretainedValue() as? UIInputView,
let nextButton = inputView.value(forKey: "_nextItem") as? UIBarButtonItem,
let nextButtonView = nextButton.value(forKey: "view") as? UIView else {
//failed to find the inputView instead lets use the inputAssistant
addCustomSearchButtonToInputAssistant(webContentView)
return
}
inputView.addSubview(self.customSearchEngineButton)
self.customSearchEngineButton.snp.remakeConstraints { make in
make.leading.equalTo(nextButtonView.snp.trailing).offset(20)
make.width.equalTo(inputView.snp.height)
make.top.equalTo(nextButtonView.snp.top)
make.height.equalTo(inputView.snp.height)
}
}

/**
This adds the customSearchButton to the inputAssistant
for cases where the inputAccessoryView could not be found for example
on the iPad where it does not exist. However this only works on iOS9
**/
func addCustomSearchButtonToInputAssistant(_ webContentView: UIView) {
guard customSearchBarButton == nil else {
return //The searchButton is already on the keyboard
}
let inputAssistant = webContentView.inputAssistantItem
let item = UIBarButtonItem(customView: customSearchEngineButton)
customSearchBarButton = item
inputAssistant.trailingBarButtonGroups.last?.barButtonItems.append(item)
}

@objc func addCustomSearchEngineForFocusedElement() {
guard let webView = tabManager.selectedTab?.webView else {
return
}
webView.evaluateJavaScript("__firefox__.searchQueryForField && __firefox__.searchQueryForField()") { (result, _) in
guard let searchQuery = result as? String, let favicon = self.tabManager.selectedTab!.displayFavicon else {
//Javascript responded with an incorrectly formatted message. Show an error.
let alert = ThirdPartySearchAlerts.failedToAddThirdPartySearch()
self.present(alert, animated: true, completion: nil)
return
}
self.addSearchEngine(searchQuery, favicon: favicon)
self.customSearchEngineButton.tintColor = UIColor.Photon.Grey50
self.customSearchEngineButton.isUserInteractionEnabled = false
}
}

func addSearchEngine(_ searchQuery: String, favicon: Favicon) {
guard !searchQuery.isEmpty,
let iconURL = URL(string: favicon.url),
let url = URL(string: searchQuery.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlFragmentAllowed)!),
let shortName = url.domainURL.host else {
let alert = ThirdPartySearchAlerts.failedToAddThirdPartySearch()
self.present(alert, animated: true, completion: nil)
return
}

let alert = ThirdPartySearchAlerts.addThirdPartySearchEngine { alert in
self.customSearchEngineButton.tintColor = UIColor.Photon.Grey50
self.customSearchEngineButton.isUserInteractionEnabled = false
SDWebImageManager.shared.loadImage(with: iconURL, options: .continueInBackground, progress: nil) { (image, _, _, _, _, _) in
guard let image = image else {
let alert = ThirdPartySearchAlerts.failedToAddThirdPartySearch()
self.present(alert, animated: true, completion: nil)
return
}

self.profile.searchEngines.addSearchEngine(OpenSearchEngine(engineID: nil, shortName: shortName, image: image, searchTemplate: searchQuery, suggestTemplate: nil, isCustomEngine: true))
let Toast = SimpleToast()
Toast.showAlertWithText(Strings.ThirdPartySearchEngineAdded, bottomContainer: self.webViewContainer)
}
}

self.present(alert, animated: true, completion: {})
}
}

extension BrowserViewController: KeyboardHelperDelegate {
func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardWillShowWithState state: KeyboardState) {
keyboardState = state
Expand All @@ -2324,16 +2203,6 @@ extension BrowserViewController: KeyboardHelperDelegate {
UIView.setAnimationCurve(state.animationCurve)
self.alertStackView.layoutIfNeeded()
}

guard let webView = tabManager.selectedTab?.webView else {
return
}
webView.evaluateJavaScript("__firefox__.searchQueryForField && __firefox__.searchQueryForField()") { (result, _) in
guard let _ = result as? String else {
return
}
self.addCustomSearchButtonToWebView(webView)
}
}

func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardDidShowWithState state: KeyboardState) {
Expand All @@ -2343,15 +2212,6 @@ extension BrowserViewController: KeyboardHelperDelegate {
func keyboardHelper(_ keyboardHelper: KeyboardHelper, keyboardWillHideWithState state: KeyboardState) {
keyboardState = nil
updateViewConstraints()
//If the searchEngineButton exists remove it form the keyboard
if let buttonGroup = customSearchBarButton?.buttonGroup {
buttonGroup.barButtonItems = buttonGroup.barButtonItems.filter { $0 != customSearchBarButton }
customSearchBarButton = nil
}

if self.customSearchEngineButton.superview != nil {
self.customSearchEngineButton.removeFromSuperview()
}

UIView.animate(withDuration: state.animationDuration) {
UIView.setAnimationCurve(state.animationCurve)
Expand Down
26 changes: 0 additions & 26 deletions Client/Frontend/Browser/CustomSearchHandler.swift

This file was deleted.

0 comments on commit adfafb2

Please sign in to comment.