This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Cleans up some code #1517
Merged
diegoreymendez
merged 4 commits into
develop
from
diego/prevent-netp-activation-without-authcode-step1
Aug 23, 2023
Merged
Cleans up some code #1517
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d56ef3
Cleans up some code moving it to a class of its own
diegoreymendez f29ca42
Adds a missing file
diegoreymendez ad23c66
Merge branch 'develop' into diego/prevent-netp-activation-without-aut…
diegoreymendez a5f2270
Renames a method to better follow our standard naming
diegoreymendez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
2 changes: 1 addition & 1 deletion
2
DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -211,7 +211,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel | |
| UserDefaultsWrapper<Any>.clearRemovedKeys() | ||
|
|
||
| #if NETWORK_PROTECTION | ||
| startupNetworkProtection() | ||
| if #available(macOS 11.4, *) { | ||
| NetworkProtectionAppEvents().applicationDidFinishLaunching() | ||
| } | ||
| #endif | ||
| } | ||
|
|
||
|
|
@@ -311,82 +313,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, FileDownloadManagerDel | |
| } | ||
| } | ||
|
|
||
| // MARK: - Network Protection | ||
|
|
||
| #if NETWORK_PROTECTION | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just wanted to remove all this logic from the app delegate, because it looks awful here. I'm purposedly avoiding any changes to make the comparison of the old code and new code easy. We can do follow up PRs to make this better if we want. |
||
|
|
||
| private func startupNetworkProtection() { | ||
| guard #available(macOS 11.4, *) else { return } | ||
|
|
||
| let loginItemsManager = NetworkProtectionLoginItemsManager() | ||
| let networkProtectionFeatureVisibility = NetworkProtectionKeychainTokenStore() | ||
|
|
||
| guard networkProtectionFeatureVisibility.isFeatureActivated else { | ||
| loginItemsManager.disableLoginItems() | ||
| LocalPinningManager.shared.unpin(.networkProtection) | ||
| return | ||
| } | ||
|
|
||
| restartNetworkProtectionIfVersionChanged(using: loginItemsManager) | ||
| refreshNetworkProtectionServers() | ||
| } | ||
|
|
||
| @available(macOS 11.4, *) | ||
| private func restartNetworkProtectionIfVersionChanged(using loginItemsManager: NetworkProtectionLoginItemsManager) { | ||
| let currentVersion = AppVersion.shared.versionNumber | ||
| let versionStore = NetworkProtectionLastVersionRunStore() | ||
| defer { | ||
| versionStore.lastVersionRun = currentVersion | ||
| } | ||
|
|
||
| // should‘ve been run at least once with NetP enabled | ||
| guard let lastVersionRun = versionStore.lastVersionRun else { | ||
| os_log(.info, log: .networkProtection, "No last version found for the NetP login items, skipping update") | ||
| return | ||
| } | ||
|
|
||
| if lastVersionRun != currentVersion { | ||
| os_log(.info, log: .networkProtection, "App updated from %{public}s to %{public}s: updating login items", lastVersionRun, currentVersion) | ||
| restartNetworkProtectionTunnelAndMenu(using: loginItemsManager) | ||
| } else { | ||
| // If login items failed to launch (e.g. because of the App bundle rename), launch using NSWorkspace | ||
| loginItemsManager.ensureLoginItemsAreRunning(.ifLoginItemsAreEnabled, after: 1) | ||
| } | ||
| } | ||
|
|
||
| @available(macOS 11.4, *) | ||
| private func restartNetworkProtectionTunnelAndMenu(using loginItemsManager: NetworkProtectionLoginItemsManager) { | ||
| loginItemsManager.restartLoginItems() | ||
|
|
||
| Task { | ||
| let provider = NetworkProtectionTunnelController() | ||
|
|
||
| // Restart NetP SysEx on app update | ||
| if await provider.isConnected { | ||
| await provider.stop() | ||
| await provider.start() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Fetches a new list of Network Protection servers, and updates the existing set. | ||
| @available(macOS 11.4, *) | ||
| private func refreshNetworkProtectionServers() { | ||
| Task { | ||
| let serverCount: Int | ||
| do { | ||
| serverCount = try await NetworkProtectionDeviceManager.create().refreshServerList().count | ||
| } catch { | ||
| os_log("Failed to update Network Protection servers", log: .networkProtection, type: .error) | ||
| return | ||
| } | ||
|
|
||
| os_log("Successfully updated Network Protection servers; total server count = %{public}d", log: .networkProtection, serverCount) | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| private func subscribeToEmailProtectionStatusNotifications() { | ||
| NotificationCenter.default.addObserver(self, | ||
| selector: #selector(emailDidSignInNotification(_:)), | ||
|
|
||
99 changes: 99 additions & 0 deletions
99
DuckDuckGo/NetworkProtection/AppTargets/BothAppTargets/NetworkProtectionAppEvents.swift
This file contains hidden or 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,99 @@ | ||
| // | ||
| // NetworkProtectionAppEvents.swift | ||
| // | ||
| // Copyright © 2023 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 Common | ||
| import Foundation | ||
|
|
||
| #if NETWORK_PROTECTION | ||
| import NetworkProtection | ||
|
|
||
| /// Implements the sequence of steps that Network Protection needs to execute when the App starts up. | ||
| /// | ||
| @available(macOS 11.4, *) | ||
| final class NetworkProtectionAppEvents { | ||
mallexxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Call this method when the app finishes launching, to run the startup logic for NetP. | ||
| /// | ||
| func applicationDidFinishLaunching() { | ||
| let loginItemsManager = NetworkProtectionLoginItemsManager() | ||
| let keychainStore = NetworkProtectionKeychainTokenStore() | ||
|
Comment on lines
+33
to
+34
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we‘re doing this, could you move these dependencies to the instance init with default values provided? |
||
|
|
||
| guard keychainStore.isFeatureActivated else { | ||
| loginItemsManager.disableLoginItems() | ||
| LocalPinningManager.shared.unpin(.networkProtection) | ||
| return | ||
| } | ||
|
|
||
| restartNetworkProtectionIfVersionChanged(using: loginItemsManager) | ||
| refreshNetworkProtectionServers() | ||
| } | ||
|
|
||
| private func restartNetworkProtectionIfVersionChanged(using loginItemsManager: NetworkProtectionLoginItemsManager) { | ||
| let currentVersion = AppVersion.shared.versionNumber | ||
| let versionStore = NetworkProtectionLastVersionRunStore() | ||
mallexxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| defer { | ||
| versionStore.lastVersionRun = currentVersion | ||
| } | ||
|
|
||
| // should‘ve been run at least once with NetP enabled | ||
| guard let lastVersionRun = versionStore.lastVersionRun else { | ||
| os_log(.info, log: .networkProtection, "No last version found for the NetP login items, skipping update") | ||
| return | ||
| } | ||
|
|
||
| if lastVersionRun != currentVersion { | ||
| os_log(.info, log: .networkProtection, "App updated from %{public}s to %{public}s: updating login items", lastVersionRun, currentVersion) | ||
| restartNetworkProtectionTunnelAndMenu(using: loginItemsManager) | ||
| } else { | ||
| // If login items failed to launch (e.g. because of the App bundle rename), launch using NSWorkspace | ||
| loginItemsManager.ensureLoginItemsAreRunning(.ifLoginItemsAreEnabled, after: 1) | ||
| } | ||
| } | ||
|
|
||
| private func restartNetworkProtectionTunnelAndMenu(using loginItemsManager: NetworkProtectionLoginItemsManager) { | ||
| loginItemsManager.restartLoginItems() | ||
|
|
||
| Task { | ||
| let provider = NetworkProtectionTunnelController() | ||
mallexxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Restart NetP SysEx on app update | ||
| if await provider.isConnected { | ||
| await provider.stop() | ||
| await provider.start() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Fetches a new list of Network Protection servers, and updates the existing set. | ||
| /// | ||
| private func refreshNetworkProtectionServers() { | ||
| Task { | ||
| let serverCount: Int | ||
| do { | ||
| serverCount = try await NetworkProtectionDeviceManager.create().refreshServerList().count | ||
mallexxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| os_log("Failed to update Network Protection servers", log: .networkProtection, type: .error) | ||
| return | ||
| } | ||
|
|
||
| os_log("Successfully updated Network Protection servers; total server count = %{public}d", log: .networkProtection, serverCount) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.