diff --git a/ios/MullvadTransport/DeferredCancellable.swift b/ios/MullvadTransport/CancellableChain.swift similarity index 52% rename from ios/MullvadTransport/DeferredCancellable.swift rename to ios/MullvadTransport/CancellableChain.swift index d73104773d2b..510d76094a4c 100644 --- a/ios/MullvadTransport/DeferredCancellable.swift +++ b/ios/MullvadTransport/CancellableChain.swift @@ -1,5 +1,5 @@ // -// DeferredCancellable.swift +// CancellableChain.swift // MullvadTransport // // Created by pronebird on 23/10/2023. @@ -9,23 +9,23 @@ import Foundation import MullvadTypes -/// Cancellable object that defers cancellation until the other token is connected to it. -final class DeferredCancellable: Cancellable { +/// Cancellable object that cancels all cancellable objects linked to it. +final class CancellableChain: Cancellable { private let stateLock = NSLock() private var isCancelled = false - private var connectedTokens: [Cancellable] = [] + private var linkedTokens: [Cancellable] = [] init() {} - /// Connect deferred cancellation token with some other. + /// Link cancellation token with some other. /// - /// The token is cancelled immediately, if the deferred object is already cancelled. - func connect(_ token: Cancellable) { + /// The token is cancelled immediately, if the chain is already cancelled. + func link(_ token: Cancellable) { stateLock.withLock { if isCancelled { token.cancel() } else { - connectedTokens.append(token) + linkedTokens.append(token) } } } @@ -36,8 +36,8 @@ final class DeferredCancellable: Cancellable { func cancel() { stateLock.withLock { isCancelled = true - connectedTokens.forEach { $0.cancel() } - connectedTokens.removeAll() + linkedTokens.forEach { $0.cancel() } + linkedTokens.removeAll() } } } diff --git a/ios/MullvadTransport/URLSessionSocks5Transport.swift b/ios/MullvadTransport/URLSessionSocks5Transport.swift index 371562e0c8da..2b1b36eb9376 100644 --- a/ios/MullvadTransport/URLSessionSocks5Transport.swift +++ b/ios/MullvadTransport/URLSessionSocks5Transport.swift @@ -72,7 +72,7 @@ public class URLSessionSocks5Transport: RESTTransport { request: URLRequest, completion: @escaping (Data?, URLResponse?, Error?) -> Void ) -> Cancellable { - let deferred = DeferredCancellable() + let chain = CancellableChain() socksProxy.start { [weak self, weak socksProxy] error in if let error { @@ -80,14 +80,14 @@ public class URLSessionSocks5Transport: RESTTransport { } else if let self, let localPort = socksProxy?.listenPort { let token = self.startDataTask(request: request, localPort: localPort, completion: completion) - // Propagate cancellation from deferred to the data task cancellation token. - deferred.connect(token) + // Propagate cancellation from the chain to the data task cancellation token. + chain.link(token) } else { completion(nil, nil, URLError(.cancelled)) } } - return deferred + return chain } /// Execute data task, rewriting the original URLRequest to communicate over the socks proxy listening on the local TCP port. diff --git a/ios/MullvadVPN.xcodeproj/project.pbxproj b/ios/MullvadVPN.xcodeproj/project.pbxproj index 272e8160ac1b..86663c6b41f4 100644 --- a/ios/MullvadVPN.xcodeproj/project.pbxproj +++ b/ios/MullvadVPN.xcodeproj/project.pbxproj @@ -133,7 +133,7 @@ 585F5A752AE68688001D9DF7 /* Socks5ConnectNegotiation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A652AE68687001D9DF7 /* Socks5ConnectNegotiation.swift */; }; 585F5A762AE68688001D9DF7 /* NWConnection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A662AE68688001D9DF7 /* NWConnection+Extensions.swift */; }; 585F5A782AE6879A001D9DF7 /* URLSessionSocks5Transport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A772AE6879A001D9DF7 /* URLSessionSocks5Transport.swift */; }; - 585F5A7A2AE6A523001D9DF7 /* DeferredCancellable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */; }; + 585F5A7A2AE6A523001D9DF7 /* CancellableChain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A792AE6A523001D9DF7 /* CancellableChain.swift */; }; 585F5A7C2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */; }; 585F5A7E2AE6B62D001D9DF7 /* Socks5Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 585F5A7D2AE6B62D001D9DF7 /* Socks5Configuration.swift */; }; 58607A4D2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58607A4C2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift */; }; @@ -1315,7 +1315,7 @@ 585F5A652AE68687001D9DF7 /* Socks5ConnectNegotiation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Socks5ConnectNegotiation.swift; sourceTree = ""; }; 585F5A662AE68688001D9DF7 /* NWConnection+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NWConnection+Extensions.swift"; sourceTree = ""; }; 585F5A772AE6879A001D9DF7 /* URLSessionSocks5Transport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionSocks5Transport.swift; sourceTree = ""; }; - 585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeferredCancellable.swift; sourceTree = ""; }; + 585F5A792AE6A523001D9DF7 /* CancellableChain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CancellableChain.swift; sourceTree = ""; }; 585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AnyIPEndpoint+Socks5.swift"; sourceTree = ""; }; 585F5A7D2AE6B62D001D9DF7 /* Socks5Configuration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Socks5Configuration.swift; sourceTree = ""; }; 58607A4C2947287800BC467D /* AccountExpiryInAppNotificationProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountExpiryInAppNotificationProvider.swift; sourceTree = ""; }; @@ -3056,7 +3056,7 @@ isa = PBXGroup; children = ( 585F5A7B2AE6A537001D9DF7 /* AnyIPEndpoint+Socks5.swift */, - 585F5A792AE6A523001D9DF7 /* DeferredCancellable.swift */, + 585F5A792AE6A523001D9DF7 /* CancellableChain.swift */, A97F1F432A1F4E1A00ECEFDE /* MullvadTransport.h */, 586F2BE129F6916F009E6924 /* shadowsocks.h */, A9467E872A2DCD57000DC21F /* ShadowsocksConfiguration.swift */, @@ -4760,7 +4760,7 @@ 585F5A782AE6879A001D9DF7 /* URLSessionSocks5Transport.swift in Sources */, 585F5A6B2AE68688001D9DF7 /* Socks5Handshake.swift in Sources */, 585F5A682AE68688001D9DF7 /* Socks5ForwardingProxy.swift in Sources */, - 585F5A7A2AE6A523001D9DF7 /* DeferredCancellable.swift in Sources */, + 585F5A7A2AE6A523001D9DF7 /* CancellableChain.swift in Sources */, 585F5A692AE68688001D9DF7 /* Socks5ConnectCommand.swift in Sources */, A95F86B72A1F53BA00245DAC /* URLSessionTransport.swift in Sources */, 5822C0042A3724A800A3A5FB /* ShadowsocksConfigurationCache.swift in Sources */,