Ariver
2026-07-13 14a1efc86d0295be3a0d3fe1ebe7b8080266da2d
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import Foundation
 
public struct SystemCriticalWindowDescriptor: Equatable, Sendable {
    public let ownerName: String
    public let title: String
    public let bundleIdentifier: String?
    public let processIdentifier: Int32?
    public let layer: Int?
 
    public init(
        ownerName: String = "",
        title: String = "",
        bundleIdentifier: String? = nil,
        processIdentifier: Int32? = nil,
        layer: Int? = nil
    ) {
        self.ownerName = ownerName
        self.title = title
        self.bundleIdentifier = bundleIdentifier
        self.processIdentifier = processIdentifier
        self.layer = layer
    }
}
 
public enum SystemCriticalWindowClassifier {
    private static let criticalBundleIdentifiers: Set<String> = [
        "com.apple.SecurityAgent",
        "com.apple.CoreServicesUIAgent"
    ]
 
    private static let criticalOwnerNames: Set<String> = [
        "SecurityAgent",
        "CoreServicesUIAgent"
    ]
 
    private static let rescueWindowBundleIdentifiers: Set<String> = [
        "com.apple.loginwindow"
    ]
 
    private static let rescueWindowOwnerNames: Set<String> = [
        "loginwindow"
    ]
 
    public static func isSystemCritical(app: AlignerApp) -> Bool {
        criticalBundleIdentifiers.contains(app.bundleIdentifier)
    }
 
    public static func isSystemCritical(_ descriptor: SystemCriticalWindowDescriptor) -> Bool {
        if let bundleIdentifier = descriptor.bundleIdentifier,
           criticalBundleIdentifiers.contains(bundleIdentifier)
        {
            return true
        }
 
        if criticalOwnerNames.contains(descriptor.ownerName) {
            return true
        }
 
        if let bundleIdentifier = descriptor.bundleIdentifier,
           rescueWindowBundleIdentifiers.contains(bundleIdentifier)
        {
            return isRescueWindowTitle(descriptor.title)
        }
 
        if rescueWindowOwnerNames.contains(descriptor.ownerName) {
            return isRescueWindowTitle(descriptor.title)
        }
 
        return isRescueWindowTitle(descriptor.title)
    }
 
    public static func isSystemCriticalTitle(_ title: String) -> Bool {
        isRescueWindowTitle(title) || isPermissionPromptTitle(title)
    }
 
    private static func isRescueWindowTitle(_ title: String) -> Bool {
        let normalized = title.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        guard !normalized.isEmpty else { return false }
 
        return normalized == "force quit applications"
            || normalized == "force quit"
            || normalized == "强制退出应用程序"
            || normalized == "强制退出"
    }
 
    private static func isPermissionPromptTitle(_ title: String) -> Bool {
        let normalized = title.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
        guard !normalized.isEmpty else { return false }
 
        return normalized == "accessibility permissions"
            || normalized == "screen recording permissions"
            || normalized == "安全性与隐私权限"
            || normalized == "辅助功能权限"
            || normalized == "屏幕录制权限"
    }
}