From d39689d21bf570a178aae5113127dc758eafabd4 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Mon, 12 Feb 2024 13:08:45 +0100 Subject: [PATCH] Simplifies some code (#2180) Task/Issue URL: https://app.asana.com/0/1199230911884351/1206556986192396/f ## Description This PR brings balance back to the force. No I'm kidding - it fixes a very strange issue in macOS 11.7 (x86) where some code was just being swallowed up by the system (not crashing, and not proceeding through any of the available flows). This was probably "swallowed" due to `async / await`, but that's just a guess. --- DuckDuckGo/LoginItems/LoginItemsManager.swift | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/DuckDuckGo/LoginItems/LoginItemsManager.swift b/DuckDuckGo/LoginItems/LoginItemsManager.swift index 23c8f293cc..324bbb26f9 100644 --- a/DuckDuckGo/LoginItems/LoginItemsManager.swift +++ b/DuckDuckGo/LoginItems/LoginItemsManager.swift @@ -23,14 +23,34 @@ import LoginItems /// Class to manage the login items for Network Protection and DBP /// final class LoginItemsManager { + private enum Action: String { + case enable + case disable + case restart + } + // MARK: - Main Interactions func enableLoginItems(_ items: Set, log: OSLog) { - updateLoginItems(items, whatAreWeDoing: "enable", using: LoginItem.enable) + for item in items { + do { + try item.enable() + os_log("🟢 Enabled successfully %{public}@", log: log, String(describing: item)) + } catch let error as NSError { + handleError(for: item, action: .enable, error: error) + } + } } func restartLoginItems(_ items: Set, log: OSLog) { - updateLoginItems(items, whatAreWeDoing: "restart", using: LoginItem.restart) + for item in items { + do { + try item.restart() + os_log("🟢 Restarted successfully %{public}@", log: log, String(describing: item)) + } catch let error as NSError { + handleError(for: item, action: .restart, error: error) + } + } } func disableLoginItems(_ items: Set) { @@ -39,31 +59,23 @@ final class LoginItemsManager { } } - // MARK: - Debug Interactions + private func handleError(for item: LoginItem, action: Action, error: NSError) { + let event = Pixel.Event.Debug.loginItemUpdateError( + loginItemBundleID: item.agentBundleID, + action: "enable", + buildType: AppVersion.shared.buildType, + osVersion: AppVersion.shared.osVersion + ) + DailyPixel.fire(pixel: .debug(event: event, error: error), frequency: .dailyAndCount, includeAppVersionParameter: true) - func resetLoginItems(_ items: Set) async throws { - for item in items { - try? item.disable() - } + logOrAssertionFailure("🔴 Could not enable \(item): \(error.debugDescription)") } - // MARK: - Misc Utility + // MARK: - Debug Interactions - private func updateLoginItems(_ items: Set, whatAreWeDoing: String, using action: (LoginItem) -> () throws -> Void) { + func resetLoginItems(_ items: Set) async throws { for item in items { - do { - try action(item)() - } catch let error as NSError { - let event = Pixel.Event.Debug.loginItemUpdateError( - loginItemBundleID: item.agentBundleID, - action: whatAreWeDoing, - buildType: AppVersion.shared.buildType, - osVersion: AppVersion.shared.osVersion - ) - - DailyPixel.fire(pixel: .debug(event: event, error: error), frequency: .dailyAndCount, includeAppVersionParameter: true) - logOrAssertionFailure("🔴 Could not \(whatAreWeDoing) \(item): \(error.debugDescription)") - } + try? item.disable() } }