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] - Fix alert continuation leak when WebKit automatically cancels the request #27069

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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 @@ -171,6 +171,15 @@ extension BrowserViewController: WKNavigationDelegate {
guard var requestURL = navigationAction.request.url else {
return (.cancel, preferences)
}

let tab = tab(for: webView)
if tab?.isExternalAppAlertPresented == true {
tab?.externalAppPopup?.dismissWithType(dismissType: .noAnimation)
tab?.externalAppPopupContinuation?.resume(with: .success(false))
tab?.externalAppPopupContinuation = nil
tab?.externalAppPopup = nil
}

if InternalURL.isValid(url: requestURL) {
if navigationAction.navigationType != .backForward, navigationAction.isInternalUnprivileged,
navigationAction.sourceFrame != nil || navigationAction.targetFrame?.isMainFrame == false
Expand Down Expand Up @@ -204,7 +213,6 @@ extension BrowserViewController: WKNavigationDelegate {

// First special case are some schemes that are about Calling. We prompt the user to confirm this action. This
// gives us the exact same behaviour as Safari.
let tab = tab(for: webView)

if ["sms", "tel", "facetime", "facetime-audio"].contains(requestURL.scheme) {
let shouldOpen = await handleExternalURL(
Expand Down Expand Up @@ -1264,6 +1272,8 @@ extension BrowserViewController {
titleSize: 21
)

tab?.externalAppPopup = popup

if isSuppressActive {
popup.addButton(title: Strings.suppressAlertsActionTitle, type: .destructive) {
[weak tab] () -> PopupViewDismissType in
Expand Down Expand Up @@ -1322,10 +1332,17 @@ extension BrowserViewController {

tab?.externalAppAlertCounter += 1

return await withCheckedContinuation { continuation in
showExternalSchemeAlert(isSuppressActive: tab?.externalAppAlertCounter ?? 0 > 2) {
continuation.resume(with: .success($0))
return await withTaskCancellationHandler {
return await withCheckedContinuation { [weak tab] continuation in
tab?.externalAppPopupContinuation = continuation
showExternalSchemeAlert(isSuppressActive: tab?.externalAppAlertCounter ?? 0 > 2) {
tab?.externalAppPopupContinuation = nil
continuation.resume(with: .success($0))
}
}
} onCancel: { [weak tab] in
Copy link
Contributor

Choose a reason for hiding this comment

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

do you still have to nil externalAppPopupContinuation if you weak reference tab?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. The continuation needs to have its reference set to nil or it won't be freed until the tab dies.

tab?.externalAppPopupContinuation?.resume(with: .success(false))
tab?.externalAppPopupContinuation = nil
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions ios/brave-ios/Sources/Brave/Frontend/Browser/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import BraveCore
import BraveShields
import BraveUI
import BraveWallet
import CertificateUtilities
import Data
Expand Down Expand Up @@ -429,6 +430,8 @@ class Tab: NSObject {

/// Boolean tracking custom url-scheme alert presented
var isExternalAppAlertPresented = false
var externalAppPopup: AlertPopupView?
Copy link
Contributor

Choose a reason for hiding this comment

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

and why we store a externalAppPopup? it's not being used anywhere

Copy link
Contributor Author

@Brandon-T Brandon-T Dec 19, 2024

Choose a reason for hiding this comment

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

var externalAppPopupContinuation: CheckedContinuation<Bool, Never>?
var externalAppAlertCounter = 0
var isExternalAppAlertSuppressed = false
var externalAppURLDomain: String?
Expand Down
Loading