diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2022-03-21 14:33:33 +0100 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2022-03-24 14:00:24 +0100 |
| commit | 8058f8fc38f0ce34c39bca2d6fc146ac085d5eca (patch) | |
| tree | 0b6f1e527e56c1675c096dfc11843986bde0f552 /ios | |
| parent | 02c80f6534067e247fffb64e0b448ed1da77fd4d (diff) | |
| download | mullvadvpn-8058f8fc38f0ce34c39bca2d6fc146ac085d5eca.tar.xz mullvadvpn-8058f8fc38f0ce34c39bca2d6fc146ac085d5eca.zip | |
Add conformance of Operation to Cancellable, drop AnyCancellable
Diffstat (limited to 'ios')
| -rw-r--r-- | ios/MullvadVPN/AddressCache/AddressCacheTracker.swift | 6 | ||||
| -rw-r--r-- | ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift | 18 | ||||
| -rw-r--r-- | ios/MullvadVPN/AppStoreReceipt.swift | 6 | ||||
| -rw-r--r-- | ios/MullvadVPN/Cancellable.swift | 18 | ||||
| -rw-r--r-- | ios/MullvadVPN/REST/RESTClient.swift | 26 | ||||
| -rw-r--r-- | ios/MullvadVPN/RelayCache/RelayCacheTracker.swift | 8 | ||||
| -rw-r--r-- | ios/MullvadVPN/TunnelIPC/TunnelIPCSession.swift | 8 | ||||
| -rw-r--r-- | ios/MullvadVPN/TunnelManager/TunnelManager.swift | 4 | ||||
| -rw-r--r-- | ios/MullvadVPN/WireguardKeysViewController.swift | 2 |
9 files changed, 31 insertions, 65 deletions
diff --git a/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift b/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift index bf313ddcb8..8502d12ffc 100644 --- a/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift +++ b/ios/MullvadVPN/AddressCache/AddressCacheTracker.swift @@ -83,7 +83,7 @@ extension AddressCache { } } - func updateEndpoints(completionHandler: ((_ result: CacheUpdateResult) -> Void)? = nil) -> AnyCancellable { + func updateEndpoints(completionHandler: ((_ result: CacheUpdateResult) -> Void)? = nil) -> Cancellable { let operation = UpdateAddressCacheOperation( queue: stateQueue, restClient: restClient, @@ -106,9 +106,7 @@ extension AddressCache { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } private func scheduleEndpointsUpdate(startTime: DispatchWallTime) { diff --git a/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift b/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift index 041105124f..709ebff131 100644 --- a/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift +++ b/ios/MullvadVPN/AppStorePaymentManager/AppStorePaymentManager.swift @@ -97,7 +97,7 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { // MARK: - Products and payments - func requestProducts(with productIdentifiers: Set<AppStoreSubscription>, completionHandler: @escaping (OperationCompletion<SKProductsResponse, Swift.Error>) -> Void) -> AnyCancellable { + func requestProducts(with productIdentifiers: Set<AppStoreSubscription>, completionHandler: @escaping (OperationCompletion<SKProductsResponse, Swift.Error>) -> Void) -> Cancellable { let productIdentifiers = productIdentifiers.productIdentifiersSet let operation = ProductsRequestOperation(productIdentifiers: productIdentifiers, completionHandler: completionHandler) @@ -105,9 +105,7 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } func addPayment(_ payment: SKPayment, for accountToken: String) { @@ -120,7 +118,7 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { } } - func restorePurchases(for accountToken: String, completionHandler: @escaping (OperationCompletion<REST.CreateApplePaymentResponse, AppStorePaymentManager.Error>) -> Void) -> AnyCancellable { + func restorePurchases(for accountToken: String, completionHandler: @escaping (OperationCompletion<REST.CreateApplePaymentResponse, AppStorePaymentManager.Error>) -> Void) -> Cancellable { return sendAppStoreReceipt(accountToken: accountToken, forceRefresh: true, completionHandler: completionHandler) } @@ -151,7 +149,7 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { paymentQueue.add(payment) } - private func sendAppStoreReceipt(accountToken: String, forceRefresh: Bool, completionHandler: @escaping (OperationCompletion<REST.CreateApplePaymentResponse, Error>) -> Void) -> AnyCancellable { + private func sendAppStoreReceipt(accountToken: String, forceRefresh: Bool, completionHandler: @escaping (OperationCompletion<REST.CreateApplePaymentResponse, Error>) -> Void) -> Cancellable { let operation = SendAppStoreReceiptOperation(restClient: REST.Client.shared, accountToken: accountToken, forceRefresh: forceRefresh, receiptProperties: nil) { completion in completionHandler(completion) } @@ -168,9 +166,7 @@ class AppStorePaymentManager: NSObject, SKPaymentTransactionObserver { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } private func handleTransactions(_ transactions: [SKPaymentTransaction]) { @@ -279,8 +275,8 @@ private class SendAppStoreReceiptOperation: AsyncOperation { private let forceRefresh: Bool private let receiptProperties: [String: Any]? private var completionHandler: CompletionHandler? - private var fetchReceiptCancellable: AnyCancellable? - private var submitReceiptCancellable: AnyCancellable? + private var fetchReceiptCancellable: Cancellable? + private var submitReceiptCancellable: Cancellable? private let logger = Logger(label: "AppStorePaymentManager.SendAppStoreReceiptOperation") diff --git a/ios/MullvadVPN/AppStoreReceipt.swift b/ios/MullvadVPN/AppStoreReceipt.swift index be8015f98b..ff61ffd281 100644 --- a/ios/MullvadVPN/AppStoreReceipt.swift +++ b/ios/MullvadVPN/AppStoreReceipt.swift @@ -45,7 +45,7 @@ enum AppStoreReceipt { /// Read AppStore receipt from disk or refresh it from AppStore if it's missing. /// This call may trigger a sign in with AppStore prompt to appear. - static func fetch(forceRefresh: Bool = false, receiptProperties: [String: Any]? = nil, completionHandler: @escaping (OperationCompletion<Data, Error>) -> Void) -> AnyCancellable { + static func fetch(forceRefresh: Bool = false, receiptProperties: [String: Any]? = nil, completionHandler: @escaping (OperationCompletion<Data, Error>) -> Void) -> Cancellable { let operation = FetchAppStoreReceiptOperation( dispatchQueue: dispatchQueue, forceRefresh: forceRefresh, @@ -63,9 +63,7 @@ enum AppStoreReceipt { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } } diff --git a/ios/MullvadVPN/Cancellable.swift b/ios/MullvadVPN/Cancellable.swift index 4d693c3bcd..508d5e3432 100644 --- a/ios/MullvadVPN/Cancellable.swift +++ b/ios/MullvadVPN/Cancellable.swift @@ -12,20 +12,4 @@ protocol Cancellable { func cancel() } -class AnyCancellable: Cancellable { - private var closure: (() -> Void)? - private let lock = NSLock() - - init(_ block: @escaping () -> Void) { - self.closure = block - } - - func cancel() { - lock.lock() - let block = closure - closure = nil - lock.unlock() - - block?() - } -} +extension Operation: Cancellable {} diff --git a/ios/MullvadVPN/REST/RESTClient.swift b/ios/MullvadVPN/REST/RESTClient.swift index 70f914ce89..0530f332e5 100644 --- a/ios/MullvadVPN/REST/RESTClient.swift +++ b/ios/MullvadVPN/REST/RESTClient.swift @@ -57,7 +57,7 @@ extension REST { // MARK: - Public - func createAccount(retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<AccountResponse, REST.Error>) -> Void) -> AnyCancellable { + func createAccount(retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<AccountResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "create-account", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in let request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .post, path: "accounts") @@ -79,7 +79,7 @@ extension REST { } } - func getAddressList(retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<[AnyIPEndpoint], REST.Error>) -> Void) -> AnyCancellable { + func getAddressList(retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<[AnyIPEndpoint], REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "get-api-addrs", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in let request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .get, path: "api-addrs") @@ -100,7 +100,7 @@ extension REST { } } - func getRelays(etag: String?, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<ServerRelaysCacheResponse, REST.Error>) -> Void) -> AnyCancellable { + func getRelays(etag: String?, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<ServerRelaysCacheResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "get-relays", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .get, path: "relays") if let etag = etag { @@ -130,7 +130,7 @@ extension REST { } } - func getAccountExpiry(token: String, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<AccountResponse, REST.Error>) -> Void) -> AnyCancellable { + func getAccountExpiry(token: String, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<AccountResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "get-account-expiry", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .get, path: "me") @@ -153,7 +153,7 @@ extension REST { } } - func getWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> AnyCancellable { + func getWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "get-wireguard-key", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in let urlEncodedPublicKey = publicKey.base64Key .addingPercentEncoding(withAllowedCharacters: .alphanumerics)! @@ -179,7 +179,7 @@ extension REST { } } - func pushWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> AnyCancellable { + func pushWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "push-wireguard-key", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .post, path: "wireguard-keys") let body = PushWireguardKeyRequest(pubkey: publicKey.rawValue) @@ -209,7 +209,7 @@ extension REST { } } - func replaceWireguardKey(token: String, oldPublicKey: PublicKey, newPublicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> AnyCancellable { + func replaceWireguardKey(token: String, oldPublicKey: PublicKey, newPublicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<WireguardAddressesResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "replace-wireguard-key", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .post, path: "replace-wireguard-key") let body = ReplaceWireguardKeyRequest(old: oldPublicKey.rawValue, new: newPublicKey.rawValue) @@ -239,7 +239,7 @@ extension REST { } } - func deleteWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<(), REST.Error>) -> Void) -> AnyCancellable { + func deleteWireguardKey(token: String, publicKey: PublicKey, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<(), REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "delete-wireguard-key", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in let urlEncodedPublicKey = publicKey.base64Key .addingPercentEncoding(withAllowedCharacters: .alphanumerics)! @@ -266,7 +266,7 @@ extension REST { } } - func createApplePayment(token: String, receiptString: Data, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<CreateApplePaymentResponse, REST.Error>) -> Void) -> AnyCancellable { + func createApplePayment(token: String, receiptString: Data, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<CreateApplePaymentResponse, REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "create-apple-payment", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .post, path: "create-apple-payment") let body = CreateApplePaymentRequest(receiptString: receiptString) @@ -302,7 +302,7 @@ extension REST { } } - func sendProblemReport(_ body: ProblemReportRequest, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<(), REST.Error>) -> Void) -> AnyCancellable { + func sendProblemReport(_ body: ProblemReportRequest, retryStrategy: REST.RetryStrategy, completionHandler: @escaping (Result<(), REST.Error>) -> Void) -> Cancellable { return scheduleOperation(name: "send-problem-report", retryStrategy: retryStrategy, completionHandler: completionHandler) { endpoint, finishOperation in var request = self.createURLRequestWithEndpoint(endpoint: endpoint, method: .post, path: "problem-report") @@ -330,7 +330,7 @@ extension REST { // MARK: - Private - private func scheduleOperation<Response>(name: String, retryStrategy: REST.RetryStrategy, completionHandler: @escaping NetworkOperation<Response>.CompletionHandler, taskGenerator: @escaping NetworkOperation<Response>.Generator) -> AnyCancellable { + private func scheduleOperation<Response>(name: String, retryStrategy: REST.RetryStrategy, completionHandler: @escaping NetworkOperation<Response>.CompletionHandler, taskGenerator: @escaping NetworkOperation<Response>.Generator) -> Cancellable { let operation = NetworkOperation( name: name, networkTaskGenerator: taskGenerator, @@ -341,9 +341,7 @@ extension REST { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } private func dataTask(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data), URLError>) -> Void) -> URLSessionDataTask { diff --git a/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift b/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift index c2efd6cb24..b5e9d87a44 100644 --- a/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift +++ b/ios/MullvadVPN/RelayCache/RelayCacheTracker.swift @@ -98,7 +98,7 @@ extension RelayCache { } } - func updateRelays(completionHandler: @escaping (OperationCompletion<RelayCache.FetchResult, RelayCache.Error>) -> Void) -> AnyCancellable { + func updateRelays(completionHandler: @escaping (OperationCompletion<RelayCache.FetchResult, RelayCache.Error>) -> Void) -> Cancellable { let operation = UpdateRelaysOperation( dispatchQueue: stateQueue, restClient: REST.Client.shared, @@ -126,9 +126,7 @@ extension RelayCache { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } func read(completionHandler: @escaping (Result<CachedRelays, RelayCache.Error>) -> Void) { @@ -318,7 +316,7 @@ fileprivate class UpdateRelaysOperation: AsyncOperation { private let updateHandler: UpdateHandler private var completionHandler: CompletionHandler? - private var downloadCancellable: AnyCancellable? + private var downloadCancellable: Cancellable? init(dispatchQueue: DispatchQueue, restClient: REST.Client, diff --git a/ios/MullvadVPN/TunnelIPC/TunnelIPCSession.swift b/ios/MullvadVPN/TunnelIPC/TunnelIPCSession.swift index ce430b5b81..7d76e28e35 100644 --- a/ios/MullvadVPN/TunnelIPC/TunnelIPCSession.swift +++ b/ios/MullvadVPN/TunnelIPC/TunnelIPCSession.swift @@ -32,9 +32,7 @@ extension TunnelIPC { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } func getTunnelStatus(completionHandler: @escaping (OperationCompletion<PacketTunnelStatus, TunnelIPC.Error>) -> Void) -> Cancellable { @@ -48,9 +46,7 @@ extension TunnelIPC { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } } } diff --git a/ios/MullvadVPN/TunnelManager/TunnelManager.swift b/ios/MullvadVPN/TunnelManager/TunnelManager.swift index d08e273475..10af385a93 100644 --- a/ios/MullvadVPN/TunnelManager/TunnelManager.swift +++ b/ios/MullvadVPN/TunnelManager/TunnelManager.swift @@ -426,9 +426,7 @@ final class TunnelManager: TunnelManagerStateDelegate { operationQueue.addOperation(operation) - return AnyCancellable { - operation.cancel() - } + return operation } func setRelayConstraints(_ newConstraints: RelayConstraints, completionHandler: @escaping (TunnelManager.Error?) -> Void) { diff --git a/ios/MullvadVPN/WireguardKeysViewController.swift b/ios/MullvadVPN/WireguardKeysViewController.swift index ac5738eb2a..d777c8c8d3 100644 --- a/ios/MullvadVPN/WireguardKeysViewController.swift +++ b/ios/MullvadVPN/WireguardKeysViewController.swift @@ -34,7 +34,7 @@ class WireguardKeysViewController: UIViewController, TunnelObserver { private var publicKeyPeriodicUpdateTimer: DispatchSourceTimer? private var copyToPasteboardWork: DispatchWorkItem? - private var verifyKeyCancellable: AnyCancellable? + private var verifyKeyCancellable: Cancellable? private let alertPresenter = AlertPresenter() private var state: WireguardKeysViewState = .default { |
