Skip to content

Commit

Permalink
Simplifies some code (#2180)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diegoreymendez authored Feb 12, 2024
1 parent c295cc6 commit d39689d
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions DuckDuckGo/LoginItems/LoginItemsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<LoginItem>, 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<LoginItem>, 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<LoginItem>) {
Expand All @@ -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<LoginItem>) 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<LoginItem>, whatAreWeDoing: String, using action: (LoginItem) -> () throws -> Void) {
func resetLoginItems(_ items: Set<LoginItem>) 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()
}
}

Expand Down

0 comments on commit d39689d

Please sign in to comment.