TunnelsManager: do not track currentTunnel

It's error prone and does not give us anything.
This commit is contained in:
Jason A. Donenfeld 2018-11-03 06:22:57 +01:00
parent 8bbc49015c
commit b81943aaac
1 changed files with 6 additions and 33 deletions

View File

@ -38,23 +38,8 @@ class TunnelsManager {
private var isModifyingTunnel: Bool = false
private var isDeletingTunnel: Bool = false
private var currentTunnel: TunnelContainer?
private var currentTunnelStatusObservationToken: AnyObject?
init(tunnelProviders: [NETunnelProviderManager]) {
var tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }
tunnels.sort { $0.name < $1.name }
var currentTunnel: TunnelContainer? = nil
for tunnel in tunnels {
if (tunnel.status != .inactive) {
currentTunnel = tunnel
break
}
}
self.tunnels = tunnels
if let currentTunnel = currentTunnel {
setCurrentTunnel(tunnel: currentTunnel)
}
self.tunnels = tunnelProviders.map { TunnelContainer(tunnel: $0) }.sorted { $0.name < $1.name }
}
static func create(completionHandler: @escaping (TunnelsManager?) -> Void) {
@ -203,11 +188,12 @@ class TunnelsManager {
completionHandler(TunnelActivationError.attemptingActivationWhenTunnelIsNotInactive)
return
}
guard (currentTunnel == nil) else {
completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: currentTunnel!.status))
return
for t in tunnels {
if t.status != .inactive {
completionHandler(TunnelActivationError.attemptingActivationWhenAnotherTunnelIsBusy(otherTunnelStatus: t.status))
return
}
}
setCurrentTunnel(tunnel: tunnel)
tunnel.startActivation(completionHandler: completionHandler)
}
@ -216,21 +202,8 @@ class TunnelsManager {
completionHandler(TunnelActivationError.attemptingDeactivationWhenTunnelIsInactive)
return
}
assert(tunnel == currentTunnel!)
tunnel.startDeactivation()
}
private func setCurrentTunnel(tunnel: TunnelContainer) {
currentTunnel = tunnel
currentTunnelStatusObservationToken = tunnel.observe(\.status) { [weak self] (tunnel, change) in
guard let s = self else { return }
if (tunnel.status == .inactive) {
s.currentTunnel = nil
s.currentTunnelStatusObservationToken = nil
}
}
}
}
extension NETunnelProviderProtocol {