Skip to content

Commit

Permalink
Remove call to dequeue when selected tab change, put it back in viewD…
Browse files Browse the repository at this point in the history
…idLayoutSubviews

With recursion
Change where we call logs with result
  • Loading branch information
lmarceau committed Dec 20, 2024
1 parent 9f972e7 commit fe12c41
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
27 changes: 25 additions & 2 deletions firefox-ios/Client/Frontend/Browser/BrowserPrompts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private func titleForJavaScriptPanelInitiatedByFrame(_ frame: WKFrameInfo) -> St

@objc
protocol NewJSPromptAlertControllerDelegate: AnyObject {
func promptAlertControllerDidDismiss(_ alertController: NewJSPromptAlertController, withResult result: Any?)
func newPromptAlertControllerDidDismiss(_ alertController: NewJSPromptAlertController)
}

/// A simple version of UIAlertController that attaches a delegate to the viewDidDisappear method
Expand Down Expand Up @@ -161,14 +161,17 @@ class NewJSPromptAlertController: UIAlertController {
}

// Notify the delegate about dismissal and pass the result
delegate?.promptAlertControllerDidDismiss(self, withResult: dismissalResult)
delegate?.newPromptAlertControllerDidDismiss(self)

alertInfo?.handleAlertDismissal(dismissalResult)
}
}
}

protocol NewJSAlertInfo {
func alertController() -> NewJSPromptAlertController
func cancel()
func handleAlertDismissal(_ result: Any?)
}

struct NewMessageAlert: NewJSAlertInfo {
Expand Down Expand Up @@ -197,6 +200,10 @@ struct NewMessageAlert: NewJSAlertInfo {
logger.log("Message alert completion handler called through cancel", level: .info, category: .webview)
completionHandler()
}

func handleAlertDismissal(_ result: Any?) {
logger.log("Message alert dismissed with no result.", level: .info, category: .webview)
}
}

struct NewConfirmPanelAlert: NewJSAlertInfo {
Expand Down Expand Up @@ -227,6 +234,14 @@ struct NewConfirmPanelAlert: NewJSAlertInfo {
logger.log("Confirm panel alert completion handler called through cancel", level: .info, category: .webview)
completionHandler(false)
}

func handleAlertDismissal(_ result: Any?) {
if (result as? Bool) != nil {
logger.log("Confirm alert dismissed with result.", level: .info, category: .webview)
} else {
logger.log("Confirm alert dismissed with no result.", level: .info, category: .webview)
}
}
}

struct NewTextInputAlert: NewJSAlertInfo {
Expand Down Expand Up @@ -266,4 +281,12 @@ struct NewTextInputAlert: NewJSAlertInfo {
logger.log("Text input alert completion handler called through cancel", level: .info, category: .webview)
completionHandler(nil)
}

func handleAlertDismissal(_ result: Any?) {
if (result as? String) != nil {
logger.log("Text input alert dismissed with input.", level: .info, category: .webview)
} else {
logger.log("Text input alert dismissed with no input.", level: .info, category: .webview)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,33 @@ class BrowserViewController: UIViewController,
statusBarOverlay.heightAnchor.constraint(equalToConstant: view.safeAreaInsets.top)
])

if !isJSAlertRefactorEnabled {
if isJSAlertRefactorEnabled {
checkForJSAlerts()
} else {
showQueuedAlertIfAvailable()
}
switchToolbarIfNeeded()
adjustURLBarHeightBasedOnLocationViewHeight()
}

func checkForJSAlerts() {
guard tabManager.selectedTab?.hasJavascriptAlertPrompt() ?? false else { return }

if presentedViewController == nil {
// We can show the alert, let's show it
guard let nextAlert = tabManager.selectedTab?.newDequeueJavascriptAlertPrompt() else { return }
let alertController = nextAlert.alertController()
alertController.delegate = self
present(alertController, animated: true)
} else {
// We cannot show the alert right now but there is one queued on the selected tab
// check after a delay if we can show it
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { [weak self] in
self?.checkForJSAlerts()
}
}
}

private func adjustURLBarHeightBasedOnLocationViewHeight() {
guard isToolbarRefactorEnabled else {
adjustLegacyURLBarHeightBasedOnLocationViewHeight()
Expand Down Expand Up @@ -3914,10 +3934,6 @@ extension BrowserViewController: TabManagerDelegate {
if needsReload {
selectedTab.reloadPage()
}

if isJSAlertRefactorEnabled {
newShowQueuedAlertIfAvailable()
}
}

func tabManager(_ tabManager: TabManager, didAddTab tab: Tab, placeNextToParentTab: Bool, isRestoring: Bool) {
Expand Down Expand Up @@ -4134,40 +4150,15 @@ extension BrowserViewController: JSPromptAlertControllerDelegate {
}
}

extension BrowserViewController: NewJSPromptAlertControllerDelegate {
func promptAlertControllerDidDismiss(_ alertController: NewJSPromptAlertController, withResult result: Any?) {
newShowQueuedAlertIfAvailable()
// MARK: NewJSPromptAlertControllerDelegate

if let alertInfo = alertController.alertInfo {
handleAlertDismissalResult(result, for: alertInfo)
}
}

/// For debugging purposes of JS alerts, can be cleaned up later on
private func handleAlertDismissalResult(_ result: Any?, for alertInfo: NewJSAlertInfo) {
if alertInfo is NewMessageAlert {
logger.log("Message alert dismissed with no result.", level: .info, category: .webview)
} else if alertInfo is NewConfirmPanelAlert {
if (result as? Bool) != nil {
logger.log("Confirm alert dismissed with result.", level: .info, category: .webview)
} else {
logger.log("Confirm alert dismissed with no result.", level: .info, category: .webview)
}
} else if alertInfo is NewTextInputAlert {
if (result as? String) != nil {
logger.log("Text input alert dismissed with input.", level: .info, category: .webview)
} else {
logger.log("Text input alert dismissed with no input.", level: .info, category: .webview)
}
}
}
extension BrowserViewController: NewJSPromptAlertControllerDelegate {
func newPromptAlertControllerDidDismiss(_ alertController: NewJSPromptAlertController) {
logger.log("JS prompt was dismissed. Will dequeue next alert.",
level: .info,
category: .webview)

func newShowQueuedAlertIfAvailable() {
if let nextAlert = tabManager.selectedTab?.newDequeueJavascriptAlertPrompt() {
let alertController = nextAlert.alertController()
alertController.delegate = self
presentWithModalDismissIfNeeded(alertController, animated: true)
}
checkForJSAlerts()
}
}

Expand Down
4 changes: 4 additions & 0 deletions firefox-ios/Client/TabManagement/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,10 @@ class Tab: NSObject, ThemeApplicable, FeatureFlaggable, ShareTab {
return newAlertQueue.removeFirst()
}

func hasJavascriptAlertPrompt() -> Bool {
return !newAlertQueue.isEmpty
}

override func observeValue(
forKeyPath keyPath: String?,
of object: Any?,
Expand Down

0 comments on commit fe12c41

Please sign in to comment.