2019-04-17 08:05:43 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2023-02-14 15:10:32 +00:00
|
|
|
// Copyright © 2018-2023 WireGuard LLC. All Rights Reserved.
|
2019-04-17 08:05:43 +00:00
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class MacAppStoreUpdateDetector {
|
|
|
|
static func isUpdatingFromMacAppStore(quitAppleEvent: NSAppleEventDescriptor) -> Bool {
|
|
|
|
guard isQuitEvent(quitAppleEvent) else { return false }
|
|
|
|
guard let senderPIDDescriptor = quitAppleEvent.attributeDescriptor(forKeyword: keySenderPIDAttr) else { return false }
|
|
|
|
let pid = senderPIDDescriptor.int32Value
|
2021-07-22 07:06:26 +00:00
|
|
|
wg_log(.debug, message: "aevt/quit Apple event received from pid: \(pid)")
|
2019-04-17 08:05:43 +00:00
|
|
|
guard let executablePath = getExecutablePath(from: pid) else { return false }
|
2021-07-22 07:06:26 +00:00
|
|
|
wg_log(.debug, message: "aevt/quit Apple event received from executable: \(executablePath)")
|
2019-04-17 08:05:43 +00:00
|
|
|
if executablePath.hasPrefix("/System/Library/") {
|
|
|
|
let executableName = URL(fileURLWithPath: executablePath, isDirectory: false).lastPathComponent
|
2021-07-22 07:08:45 +00:00
|
|
|
return executableName.hasPrefix("com.apple.") && executableName.hasSuffix(".StoreAEService")
|
2019-04-17 08:05:43 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func isQuitEvent(_ event: NSAppleEventDescriptor) -> Bool {
|
2019-05-20 10:56:02 +00:00
|
|
|
return event.eventClass == kCoreEventClass && event.eventID == kAEQuitApplication
|
2019-04-17 08:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func getExecutablePath(from pid: pid_t) -> String? {
|
|
|
|
let bufferSize = Int(PATH_MAX)
|
|
|
|
var buffer = Data(capacity: bufferSize)
|
|
|
|
return buffer.withUnsafeMutableBytes { (ptr: UnsafeMutableRawBufferPointer) -> String? in
|
|
|
|
if let basePtr = ptr.baseAddress {
|
|
|
|
let byteCount = proc_pidpath(pid, basePtr, UInt32(bufferSize))
|
|
|
|
return byteCount > 0 ? String(cString: basePtr.bindMemory(to: CChar.self, capacity: bufferSize)) : nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|