-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Note: This checklist is a reminder of our shared engineering expectations. --> Please review the release process for BrowserServicesKit [here](https://app.asana.com/0/1200194497630846/1200837094583426). **Required**: Task/Issue URL: https://app.asana.com/0/1205591970852438/1206773442486707/f iOS PR: duckduckgo/iOS#3264 macOS PR: duckduckgo/macos-browser#3131 What kind of version bump will this require?: Major/Minor/Patch **Optional**: Tech Design URL: CC: **Description**: This PR updates @graeme's geolocation PRs up to date with changes from BSK and address some prior review feedback. <!-- Tagging instructions If this PR isn't ready to be merged for whatever reason it should be marked with the `DO NOT MERGE` label (particularly if it's a draft) If it's pending Product Review/PFR, please add the `Pending Product Review` label. If at any point it isn't actively being worked on/ready for review/otherwise moving forward (besides the above PR/PFR exception) strongly consider closing it (or not opening it in the first place). If you decide not to close it, make sure it's labelled to make it clear the PRs state and comment with more information. --> **Steps to test this PR**: Check platform-specific PRs. <!-- Before submitting a PR, please ensure you have tested the combinations you expect the reviewer to test, then delete configurations you *know* do not need explicit testing. Using a simulator where a physical device is unavailable is acceptable. --> **OS Testing**: * [ ] iOS 14 * [ ] iOS 15 * [ ] iOS 16 * [ ] macOS 10.15 * [ ] macOS 11 * [ ] macOS 12 --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943) --------- Co-authored-by: Graeme Arthur <[email protected]>
- Loading branch information
1 parent
ce1b722
commit 1ee0979
Showing
9 changed files
with
372 additions
and
14 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
Sources/NetworkProtection/Models/VPNServerSelectionResolver.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,109 @@ | ||
// | ||
// VPNServerSelectionResolver.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 | ||
|
||
enum VPNServerSelectionResolverError: Error { | ||
case countryNotFound | ||
case fetchingLocationsFailed(Error) | ||
} | ||
|
||
protocol VPNServerSelectionResolving { | ||
func resolvedServerSelectionMethod() async -> NetworkProtectionServerSelectionMethod | ||
} | ||
|
||
final class VPNServerSelectionResolver: VPNServerSelectionResolving { | ||
private let locationListRepository: NetworkProtectionLocationListRepository | ||
private let vpnSettings: VPNSettings | ||
|
||
init(locationListRepository: NetworkProtectionLocationListRepository, vpnSettings: VPNSettings) { | ||
self.locationListRepository = locationListRepository | ||
self.vpnSettings = vpnSettings | ||
} | ||
|
||
/// Address the case where the prefered location becomes unavailable | ||
/// We fall back to the country, if a city isn't available, | ||
/// or nearest if the country isn't available | ||
public func resolvedServerSelectionMethod() async -> NetworkProtectionServerSelectionMethod { | ||
switch currentServerSelectionMethod { | ||
case .automatic, .preferredServer, .avoidServer, .failureRecovery: | ||
return currentServerSelectionMethod | ||
case .preferredLocation(let networkProtectionSelectedLocation): | ||
do { | ||
let location = try await resolveSelectionAgainstAvailableLocations(networkProtectionSelectedLocation) | ||
return .preferredLocation(location) | ||
} catch let error as VPNServerSelectionResolverError { | ||
switch error { | ||
case .countryNotFound: | ||
return .automatic | ||
case .fetchingLocationsFailed: | ||
return currentServerSelectionMethod | ||
} | ||
} catch { | ||
return currentServerSelectionMethod | ||
} | ||
} | ||
} | ||
|
||
private func resolveSelectionAgainstAvailableLocations(_ selection: NetworkProtectionSelectedLocation) async throws -> NetworkProtectionSelectedLocation { | ||
let availableLocations: [NetworkProtectionLocation] | ||
do { | ||
availableLocations = try await locationListRepository.fetchLocationList(cachePolicy: .ignoreCache) | ||
} catch { | ||
throw VPNServerSelectionResolverError.fetchingLocationsFailed(error) | ||
} | ||
|
||
let availableCitySelections = availableLocations.flatMap { location in | ||
location.cities.map { city in NetworkProtectionSelectedLocation(country: location.country, city: city.name) } | ||
} | ||
|
||
if availableCitySelections.contains(selection) { | ||
return selection | ||
} | ||
|
||
let selectedCountry = NetworkProtectionSelectedLocation(country: selection.country) | ||
let availableCountrySelections = availableLocations.map { NetworkProtectionSelectedLocation(country: $0.country) } | ||
guard availableCountrySelections.contains(selectedCountry) else { | ||
throw VPNServerSelectionResolverError.countryNotFound | ||
} | ||
|
||
return selectedCountry | ||
} | ||
|
||
private var currentServerSelectionMethod: NetworkProtectionServerSelectionMethod { | ||
var serverSelectionMethod: NetworkProtectionServerSelectionMethod | ||
|
||
switch vpnSettings.selectedLocation { | ||
case .nearest: | ||
serverSelectionMethod = .automatic | ||
case .location(let networkProtectionSelectedLocation): | ||
serverSelectionMethod = .preferredLocation(networkProtectionSelectedLocation) | ||
} | ||
|
||
switch vpnSettings.selectedServer { | ||
case .automatic: | ||
break | ||
case .endpoint(let string): | ||
// Selecting a specific server will override locations setting | ||
// Only available in debug | ||
serverSelectionMethod = .preferredServer(serverName: string) | ||
} | ||
|
||
return serverSelectionMethod | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
...NetworkProtectionTestUtils/Repositories/MockNetworkProtectionLocationListRepository.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,59 @@ | ||
// | ||
// MockNetworkProtectionLocationListRepository.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 | ||
@testable import NetworkProtection | ||
|
||
final class MockNetworkProtectionLocationListRepository: NetworkProtectionLocationListRepository { | ||
var stubFetchLocationList: [NetworkProtectionLocation] = [] | ||
var stubFetchLocationListError: Error? | ||
var spyIgnoreCache: Bool = false | ||
|
||
func fetchLocationList() async throws -> [NetworkProtectionLocation] { | ||
if let stubFetchLocationListError { | ||
throw stubFetchLocationListError | ||
} | ||
return stubFetchLocationList | ||
} | ||
|
||
func fetchLocationList(cachePolicy: NetworkProtectionLocationListCachePolicy) async throws -> [NetworkProtectionLocation] { | ||
switch cachePolicy { | ||
case .returnCacheElseLoad: | ||
return try await fetchLocationList() | ||
case .ignoreCache: | ||
return try await fetchLocationListIgnoringCache() | ||
} | ||
} | ||
|
||
func fetchLocationListIgnoringCache() async throws -> [NetworkProtection.NetworkProtectionLocation] { | ||
spyIgnoreCache = true | ||
return try await fetchLocationList() | ||
} | ||
} | ||
|
||
extension NetworkProtectionLocation { | ||
static func testData(country: String = "", cities: [City] = []) -> NetworkProtectionLocation { | ||
return Self(country: country, cities: cities) | ||
} | ||
} | ||
|
||
extension NetworkProtectionLocation.City { | ||
static func testData(name: String = "") -> NetworkProtectionLocation.City { | ||
Self(name: name) | ||
} | ||
} |
Oops, something went wrong.