diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-05-31 14:16:55 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-06-08 12:31:16 +0200 |
| commit | a003d0c2a63042e339456f2566c7d0e2b9ae95b1 (patch) | |
| tree | 8467e9fb3e4b9844d7cd109773b52ebe2e86fe45 | |
| parent | 8a3bc95854654a1c3758ca9dd1d8f85ea4ba3775 (diff) | |
| download | mullvadvpn-a003d0c2a63042e339456f2566c7d0e2b9ae95b1.tar.xz mullvadvpn-a003d0c2a63042e339456f2566c7d0e2b9ae95b1.zip | |
Add background observer
| -rw-r--r-- | ios/MullvadVPN.xcodeproj/project.pbxproj | 4 | ||||
| -rw-r--r-- | ios/MullvadVPN/AddressCache/AddressCacheTracker.swift | 10 | ||||
| -rw-r--r-- | ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift | 10 | ||||
| -rw-r--r-- | ios/MullvadVPN/AppStoreReceipt.swift | 10 | ||||
| -rw-r--r-- | ios/MullvadVPN/Operations/BackgroundObserver.swift | 54 | ||||
| -rw-r--r-- | ios/MullvadVPN/RelayCache/RelayCacheTracker.swift | 10 | ||||
| -rw-r--r-- | ios/MullvadVPN/TunnelManager/TunnelManager.swift | 89 |
7 files changed, 92 insertions, 95 deletions
diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 813d27349b..3f4b6b5e80 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -197,6 +197,7 @@ 589D287A2846250500F9A7B3 /* OperationCondition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589D28772846250500F9A7B3 /* OperationCondition.swift */; }; 589D287B2846250500F9A7B3 /* AsyncOperationQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589D28782846250500F9A7B3 /* AsyncOperationQueue.swift */; }; 589D287C2846250500F9A7B3 /* OperationObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589D28792846250500F9A7B3 /* OperationObserver.swift */; }; + 589D288028462CB000F9A7B3 /* BackgroundObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589D287F28462CB000F9A7B3 /* BackgroundObserver.swift */; }; 589D28822846306C00F9A7B3 /* GroupOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 589D28812846306C00F9A7B3 /* GroupOperation.swift */; }; 58A1AA8C23F5584C009F7EA6 /* ConnectionPanelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58A1AA8B23F5584B009F7EA6 /* ConnectionPanelView.swift */; }; 58A8055E2716EA6700681642 /* AnyIPAddress.swift in Sources */ = {isa = PBXBuildFile; fileRef = 584D26BE270C550B004EA533 /* AnyIPAddress.swift */; }; @@ -470,6 +471,7 @@ 589D28772846250500F9A7B3 /* OperationCondition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperationCondition.swift; sourceTree = "<group>"; }; 589D28782846250500F9A7B3 /* AsyncOperationQueue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AsyncOperationQueue.swift; sourceTree = "<group>"; }; 589D28792846250500F9A7B3 /* OperationObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperationObserver.swift; sourceTree = "<group>"; }; + 589D287F28462CB000F9A7B3 /* BackgroundObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackgroundObserver.swift; sourceTree = "<group>"; }; 589D28812846306C00F9A7B3 /* GroupOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupOperation.swift; sourceTree = "<group>"; }; 58A1AA8623F43901009F7EA6 /* Location.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Location.swift; sourceTree = "<group>"; }; 58A1AA8B23F5584B009F7EA6 /* ConnectionPanelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionPanelView.swift; sourceTree = "<group>"; }; @@ -616,6 +618,7 @@ 580EE22324B3243100F9D8A1 /* AsyncBlockOperation.swift */, 58E973DD24850EB600096F90 /* AsyncOperation.swift */, 589D28782846250500F9A7B3 /* AsyncOperationQueue.swift */, + 589D287F28462CB000F9A7B3 /* BackgroundObserver.swift */, 589D28812846306C00F9A7B3 /* GroupOperation.swift */, 5840BE34279EDB16002836BA /* OperationCompletion.swift */, 589D28772846250500F9A7B3 /* OperationCondition.swift */, @@ -1287,6 +1290,7 @@ 5850367F25A481D800A43E93 /* IPAddressRange+Codable.swift in Sources */, 58F2E14C276A61C000A79513 /* RotateKeyOperation.swift in Sources */, 589D287A2846250500F9A7B3 /* OperationCondition.swift in Sources */, + 589D288028462CB000F9A7B3 /* BackgroundObserver.swift in Sources */, 5871FB96254ADE4E0051A0A4 /* ConsolidatedApplicationLog.swift in Sources */, 58FEEB58260B662E00A621A8 /* AutomaticKeyboardResponder.swift in Sources */, 5846227326E22A160035F7C2 /* AppStorePaymentObserver.swift in Sources */, diff --git a/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift b/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift index e42c4ef576..bbfaeb08c8 100644 --- a/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift +++ b/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift @@ -96,13 +96,9 @@ extension AddressCache { } ) - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "AddressCache.Tracker.updateEndpoints") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Update endpoints", cancelUponExpiration: true) + ) operationQueue.addOperation(operation) diff --git a/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift b/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift index a8b56a8921..6fb85bd1dd 100644 --- a/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift +++ b/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift @@ -189,13 +189,9 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { completionHandler: completionHandler ) - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Send AppStore receipt") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Send AppStore receipt", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.sendAppStoreReceipt) diff --git a/ios/MullvadVPN/AppStoreReceipt.swift b/ios/MullvadVPN/AppStoreReceipt.swift index 55c53b80af..a565bd4012 100644 --- a/ios/MullvadVPN/AppStoreReceipt.swift +++ b/ios/MullvadVPN/AppStoreReceipt.swift @@ -49,13 +49,9 @@ enum AppStoreReceipt { completionHandler: completionHandler ) - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Fetch AppStore receipt") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Fetch AppStore receipt", cancelUponExpiration: true) + ) operationQueue.addOperation(operation) diff --git a/ios/MullvadVPN/Operations/BackgroundObserver.swift b/ios/MullvadVPN/Operations/BackgroundObserver.swift new file mode 100644 index 0000000000..e5d83c6235 --- /dev/null +++ b/ios/MullvadVPN/Operations/BackgroundObserver.swift @@ -0,0 +1,54 @@ +// +// BackgroundObserver.swift +// MullvadVPN +// +// Created by pronebird on 31/05/2022. +// + +#if canImport(UIKit) + +import UIKit + +class BackgroundObserver: OperationObserver { + let name: String + let application: UIApplication + let cancelUponExpiration: Bool + + private var taskIdentifier: UIBackgroundTaskIdentifier? + + init( + application: UIApplication = .shared, + name: String, + cancelUponExpiration: Bool + ) + { + self.application = application + self.name = name + self.cancelUponExpiration = cancelUponExpiration + } + + func didAttach(to operation: Operation) { + let expirationHandler = cancelUponExpiration ? { operation.cancel() } : nil + + taskIdentifier = application.beginBackgroundTask( + withName: name, + expirationHandler: expirationHandler + ) + } + + func operationDidStart(_ operation: Operation) { + // no-op + } + + func operationDidCancel(_ operation: Operation) { + // no-op + } + + func operationDidFinish(_ operation: Operation) { + if let taskIdentifier = taskIdentifier { + application.endBackgroundTask(taskIdentifier) + } + } +} + +#endif diff --git a/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift b/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift index f164ce3030..218c55f69c 100644 --- a/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift +++ b/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift @@ -125,13 +125,9 @@ extension RelayCache { completionHandler: completionHandler ) - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Update relays") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Update relays", cancelUponExpiration: true) + ) operationQueue.addOperation(operation) diff --git a/ios/MullvadVPN/TunnelManager/TunnelManager.swift b/ios/MullvadVPN/TunnelManager/TunnelManager.swift index f97c8da036..19438ad893 100644 --- a/ios/MullvadVPN/TunnelManager/TunnelManager.swift +++ b/ios/MullvadVPN/TunnelManager/TunnelManager.swift @@ -228,6 +228,9 @@ final class TunnelManager: TunnelManagerStateDelegate { migrateSettingsOperation, loadTunnelOperation ]) + groupOperation.addObserver( + BackgroundObserver(name: "Load tunnel configuration", cancelUponExpiration: false) + ) groupOperation.addCondition( MutuallyExclusive(category: OperationCategory.manageTunnelProvider) @@ -260,14 +263,7 @@ final class TunnelManager: TunnelManagerStateDelegate { } }) - - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Start tunnel") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver(BackgroundObserver(name: "Start tunnel", cancelUponExpiration: true)) operation.addCondition(MutuallyExclusive(category: OperationCategory.manageTunnelProvider)) operationQueue.addOperation(operation) @@ -292,14 +288,7 @@ final class TunnelManager: TunnelManagerStateDelegate { } } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Stop tunnel") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } - + operation.addObserver(BackgroundObserver(name: "Stop tunnel", cancelUponExpiration: true)) operation.addCondition(MutuallyExclusive(category: OperationCategory.manageTunnelProvider)) operationQueue.addOperation(operation) @@ -332,14 +321,9 @@ final class TunnelManager: TunnelManagerStateDelegate { } } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Reconnect tunnel") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } - + operation.addObserver( + BackgroundObserver(name: "Reconnect tunnel", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.manageTunnelProvider) ) @@ -378,13 +362,7 @@ final class TunnelManager: TunnelManagerStateDelegate { } }) - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: action.taskName) { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver(BackgroundObserver(name: action.taskName, cancelUponExpiration: true)) operation.addCondition( MutuallyExclusive(category: OperationCategory.manageTunnelProvider) @@ -414,13 +392,9 @@ final class TunnelManager: TunnelManagerStateDelegate { completionHandler?(completion.error) } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Update account data") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Update account data", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.changeTunnelSettings) @@ -439,13 +413,9 @@ final class TunnelManager: TunnelManagerStateDelegate { operation.completionQueue = .main operation.completionHandler = completionHandler - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Update device data") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Update device data", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.changeTunnelSettings) @@ -484,13 +454,9 @@ final class TunnelManager: TunnelManagerStateDelegate { } } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Regenerate private key") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Regenerate private key", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.changeTunnelSettings) @@ -530,13 +496,9 @@ final class TunnelManager: TunnelManagerStateDelegate { } } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "Rotate private key") { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } + operation.addObserver( + BackgroundObserver(name: "Rotate private key", cancelUponExpiration: true) + ) operation.addCondition( MutuallyExclusive(category: OperationCategory.changeTunnelSettings) @@ -724,14 +686,7 @@ final class TunnelManager: TunnelManagerStateDelegate { completionHandler(completion.error) } - let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: taskName) { - operation.cancel() - } - - operation.completionBlock = { - UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier) - } - + operation.addObserver(BackgroundObserver(name: taskName, cancelUponExpiration: true)) operation.addCondition(MutuallyExclusive(category: OperationCategory.changeTunnelSettings)) operationQueue.addOperation(operation) |
