aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/LaunchedAtLoginDetector.swift
blob: 0d8e3d86f58095a42c54618127f4cf56b734037a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// SPDX-License-Identifier: MIT
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

import Cocoa

class LaunchedAtLoginDetector {
    static let launchCode = "LaunchedByWireGuardLoginItemHelper"

    static func isLaunchedAtLogin(openAppleEvent: NSAppleEventDescriptor) -> Bool {
        guard isOpenEvent(openAppleEvent) else { return false }
        guard let propData = openAppleEvent.paramDescriptor(forKeyword: keyAEPropData) else { return false }
        return propData.stringValue == launchCode
    }

    static func isReopenedByLoginItemHelper(reopenAppleEvent: NSAppleEventDescriptor) -> Bool {
        guard isReopenEvent(reopenAppleEvent) else { return false }
        guard let propData = reopenAppleEvent.paramDescriptor(forKeyword: keyAEPropData) else { return false }
        return propData.stringValue == launchCode
    }
}

private func isOpenEvent(_ event: NSAppleEventDescriptor) -> Bool {
    return event.eventClass == kCoreEventClass && event.eventID == kAEOpenApplication
}

private func isReopenEvent(_ event: NSAppleEventDescriptor) -> Bool {
    return event.eventClass == kCoreEventClass && event.eventID == kAEReopenApplication
}