From adfafb2bffe0bc247da6ccf78f042d576cacdd35 Mon Sep 17 00:00:00 2001 From: Daniela Arcese Date: Thu, 5 Dec 2019 14:38:26 -0500 Subject: [PATCH] Fix #5848: Remove add search engine button from keyboard (#5857) --- Client.xcodeproj/project.pbxproj | 4 - .../Browser/BrowserViewController.swift | 140 ------------------ .../Browser/CustomSearchHandler.swift | 26 ---- 3 files changed, 170 deletions(-) delete mode 100644 Client/Frontend/Browser/CustomSearchHandler.swift diff --git a/Client.xcodeproj/project.pbxproj b/Client.xcodeproj/project.pbxproj index 4c28f210f70f..6fce435c9b72 100644 --- a/Client.xcodeproj/project.pbxproj +++ b/Client.xcodeproj/project.pbxproj @@ -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 */; }; @@ -1375,7 +1374,6 @@ 3BC659481E5BA4AE006D560F /* TopSites */ = {isa = PBXFileReference; lastKnownFileType = folder; path = TopSites; sourceTree = ""; }; 3BC659581E5BA505006D560F /* top_sites.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = top_sites.json; sourceTree = ""; }; 3BCE6D3B1CEB9E4D0080928C /* ThirdPartySearchAlerts.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ThirdPartySearchAlerts.swift; sourceTree = ""; }; - 3BE7275C1CCFE8B60099189F /* CustomSearchHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomSearchHandler.swift; sourceTree = ""; }; 3BF4B8E81D38497A00493393 /* BaseTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BaseTestCase.swift; sourceTree = ""; }; 3BF56D261CDBBE1F00AC4D75 /* SimpleToast.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleToast.swift; sourceTree = ""; }; 3BFCBF1F1E04B1C50070C042 /* UIImageViewExtensionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageViewExtensionsTests.swift; sourceTree = ""; }; @@ -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 */, @@ -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 */, diff --git a/Client/Frontend/Browser/BrowserViewController.swift b/Client/Frontend/Browser/BrowserViewController.swift index 567ee8d861cd..d81865d547ea 100644 --- a/Client/Frontend/Browser/BrowserViewController.swift +++ b/Client/Frontend/Browser/BrowserViewController.swift @@ -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 @@ -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()) @@ -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 @@ -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) { @@ -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) diff --git a/Client/Frontend/Browser/CustomSearchHandler.swift b/Client/Frontend/Browser/CustomSearchHandler.swift deleted file mode 100644 index 3186c80b7c50..000000000000 --- a/Client/Frontend/Browser/CustomSearchHandler.swift +++ /dev/null @@ -1,26 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -import Foundation -import WebKit - -class CustomSearchHelper: TabContentScript { - fileprivate weak var tab: Tab? - - required init(tab: Tab) { - self.tab = tab - } - - func scriptMessageHandlerName() -> String? { - return "customSearchHelper" - } - - func userContentController(_ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { - //We don't listen to messages because the BVC calls the searchHelper script by itself. - } - - class func name() -> String { - return "CustomSearchHelper" - } -}