-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9906b94
commit 1b43462
Showing
9 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// KnownFailure.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc | ||
final public class KnownFailure: NSObject, Codable { | ||
public let domain: String | ||
public let code: Int | ||
public let localizedDescription: String | ||
|
||
public init?(_ error: Error?) { | ||
guard let nsError = error as? NSError else { return nil } | ||
|
||
domain = nsError.domain | ||
code = nsError.code | ||
localizedDescription = nsError.localizedDescription | ||
} | ||
|
||
public override var description: String { | ||
"Error domain=\(domain) code=\(code)\n\(localizedDescription)" | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/NetworkProtection/Status/KnownFailureObserver/KnownFailureObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// KnownFailureObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
|
||
public protocol KnownFailureObserver { | ||
var publisher: AnyPublisher<KnownFailure?, Never> { get } | ||
var recentValue: KnownFailure? { get } | ||
} |
56 changes: 56 additions & 0 deletions
56
...ion/Status/KnownFailureObserver/KnownFailureObserverThroughDistributedNotifications.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// KnownFailureObserver.swift | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import NetworkExtension | ||
import NotificationCenter | ||
|
||
public class KnownFailureObserverThroughDistributedNotifications: KnownFailureObserver { | ||
public lazy var publisher = subject.eraseToAnyPublisher() | ||
public var recentValue: KnownFailure? { | ||
subject.value | ||
} | ||
|
||
private let subject = CurrentValueSubject<KnownFailure?, Never>(nil) | ||
|
||
private let distributedNotificationCenter: DistributedNotificationCenter | ||
private var cancellable: AnyCancellable? | ||
|
||
public init(distributedNotificationCenter: DistributedNotificationCenter = .default()) { | ||
self.distributedNotificationCenter = distributedNotificationCenter | ||
|
||
start() | ||
} | ||
|
||
private func start() { | ||
cancellable = distributedNotificationCenter.publisher(for: .knownFailureUpdated).sink { [weak self] notification in | ||
self?.handleKnownFailureUpdated(notification) | ||
} | ||
} | ||
|
||
private func handleKnownFailureUpdated(_ notification: Notification) { | ||
if let object = notification.object as? String, | ||
let data = object.data(using: .utf8), | ||
let failure = try? JSONDecoder().decode(KnownFailure.self, from: data) { | ||
subject.send(failure) | ||
} else { | ||
subject.send(nil) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters