Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #8235: Fix potential crash around changing VPN regions #8237

Merged
merged 1 commit into from
Oct 16, 2023
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
2 changes: 1 addition & 1 deletion App/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_ENTITLEMENTS = BrowserIntents/Entitlements/BrowserIntentsDebug.entitlements;
CODE_SIGN_ENTITLEMENTS = "BrowserIntents/Entitlements/BrowserIntentsRelease (AppStore).entitlements";
soner-yuksel marked this conversation as resolved.
Show resolved Hide resolved
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_TESTABILITY = YES;
Expand Down
65 changes: 37 additions & 28 deletions Sources/BraveVPN/BraveVPNRegionPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class BraveVPNRegionPickerViewController: BraveVPNPickerViewController {

/// This group monitors vpn connection status.
private var dispatchGroup: DispatchGroup?
private var timeoutTimer: Timer?
private var vpnRegionChangeSuccess = false

public override init() {
Expand All @@ -45,11 +46,16 @@ public class BraveVPNRegionPickerViewController: BraveVPNPickerViewController {
guard let connection = notification.object as? NEVPNConnection else { return }

if connection.status == .connected {
vpnRegionChangeSuccess = true
timeoutTimer?.invalidate()
dispatchGroup?.leave()
self.vpnRegionChangeSuccess = true
dispatchGroup = nil
}
}

deinit {
timeoutTimer?.invalidate()
}
}

// MARK: - UITableView Data Source & Delegate
Expand Down Expand Up @@ -116,40 +122,43 @@ extension BraveVPNRegionPickerViewController: UITableViewDelegate, UITableViewDa
let newRegion = indexPath.section == Section.automatic.rawValue ? nil : region

self.dispatchGroup = DispatchGroup()

// Changing vpn server settings takes lot of time,
// and nothing we can do about it as it relies on Apple apis.
// Here we observe vpn status and we show success alert if it connected,
// otherwise an error alert is show if it did not manage to connect in 60 seconds.
self.dispatchGroup?.enter()

BraveVPN.changeVPNRegion(to: newRegion) { [weak self] success in
guard let self = self else { return }

if !success {
self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle,
message: Strings.VPN.regionPickerErrorMessage)
}

// Changing vpn server settings takes lot of time,
// and nothing we can do about it as it relies on Apple apis.
// Here we observe vpn status and we show success alert if it connected,
// otherwise an error alert is show if it did not manage to connect in 60 seconds.
self.dispatchGroup?.enter()

DispatchQueue.main.asyncAfter(deadline: .now() + 60) {
self.vpnRegionChangeSuccess = false
self.dispatchGroup?.leave()
self.dispatchGroup = nil
self.markRegionChangeFailed()
} else {
// Start a timeout timer
self.timeoutTimer = Timer.scheduledTimer(withTimeInterval: 60, repeats: false, block: { [weak self] _ in
guard let self = self else { return }
self.markRegionChangeFailed()
})
}

self.dispatchGroup?.notify(queue: .main) { [weak self] in
guard let self = self else { return }
if self.vpnRegionChangeSuccess {

self.dismiss(animated: true) {
self.showSuccessAlert(text: Strings.VPN.regionSwitchSuccessPopupText)
BraveVPN.fetchLastUsedRegionDetail()
}
} else {
self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle,
message: Strings.VPN.regionPickerErrorMessage)
}

dispatchGroup?.notify(queue: .main) { [weak self] in
guard let self = self else { return }
if self.vpnRegionChangeSuccess {
self.dismiss(animated: true) {
self.showSuccessAlert(text: Strings.VPN.regionSwitchSuccessPopupText)
BraveVPN.fetchLastUsedRegionDetail()
}
} else {
self.showErrorAlert(title: Strings.VPN.regionPickerErrorTitle,
message: Strings.VPN.regionPickerErrorMessage)
}
}
}

private func markRegionChangeFailed() {
vpnRegionChangeSuccess = false
dispatchGroup?.leave()
dispatchGroup = nil
}
}