Repository Public
1import Foundation23/// Capabilities SP Browser can actually grant or refuse per site with public WebKit API.4/// Geolocation, web notifications, clipboard reads and per-site autoplay are deliberately absent: WKWebView on macOS5/// exposes no public per-site control for them (see docs/SECURITY.md).6enum SitePermission: String, Codable, CaseIterable, Sendable {7 case camera, microphone, popups, javascript89 var title: String {10 switch self {11 case .camera: return L("Camera")12 case .microphone: return L("Microphone")13 case .popups: return L("Pop-up windows")14 case .javascript: return L("JavaScript")15 }16 }1718 var symbol: String {19 switch self {20 case .camera: return "video"21 case .microphone: return "mic"22 case .popups: return "macwindow.on.rectangle"23 case .javascript: return "curlybraces"24 }25 }2627 /// What applies when the user has made no choice for a site.28 var defaultDecision: PermissionDecision {29 switch self {30 case .camera, .microphone: return .ask31 case .popups: return .deny32 case .javascript: return .allow33 }34 }3536 /// Whether "ask" is meaningful: only capabilities WebKit lets us prompt for at the moment of use.37 var supportsAsk: Bool { self == .camera || self == .microphone }38}3940enum PermissionDecision: String, Codable, CaseIterable, Sendable {41 case ask, allow, deny4243 var title: String {44 switch self {45 case .ask: return L("Ask")46 case .allow: return L("Allow")47 case .deny: return L("Deny")48 }49 }50}5152struct SiteSettings: Codable, Equatable, Sendable {53 var permissions: [String: PermissionDecision] = [:]54 var pageZoom: Double?5556 var isEmpty: Bool { permissions.isEmpty && pageZoom == nil }57}5859/// Per-site decisions, keyed by host. Stored per profile; in-memory for private windows.60final class SitePermissionStore: Sendable {61 private let store: JSONFileStore<[String: SiteSettings]>6263 init(url: URL?) { store = JSONFileStore(url: url, defaults: [:]) }6465 static func key(for host: String?) -> String? {66 guard let host = host?.lowercased(), !host.isEmpty else { return nil }67 return host.hasPrefix("www.") ? String(host.dropFirst(4)) : host68 }6970 /// The stored choice for `host`, or `nil` when the user never decided.71 func storedDecision(_ permission: SitePermission, host: String?) -> PermissionDecision? {72 guard let key = Self.key(for: host) else { return nil }73 return store.value[key]?.permissions[permission.rawValue]74 }7576 /// The decision to apply: the stored one, else the default. Pages without a host (local files) get the default.77 func decision(_ permission: SitePermission, host: String?) -> PermissionDecision {78 storedDecision(permission, host: host) ?? permission.defaultDecision79 }8081 func set(_ decision: PermissionDecision?, for permission: SitePermission, host: String?) {82 guard let key = Self.key(for: host) else { return }83 store.update { all in84 var site = all[key] ?? SiteSettings()85 site.permissions[permission.rawValue] = decision86 all[key] = site.isEmpty ? nil : site87 }88 }8990 func zoom(forHost host: String?) -> Double? {91 Self.key(for: host).flatMap { store.value[$0]?.pageZoom }92 }9394 func setZoom(_ zoom: Double?, host: String?) {95 guard let key = Self.key(for: host) else { return }96 store.update { all in97 var site = all[key] ?? SiteSettings()98 site.pageZoom = (zoom.map { abs($0 - 1.0) < 0.001 } ?? true) ? nil : zoom99 all[key] = site.isEmpty ? nil : site100 }101 }102103 func reset(host: String?) {104 guard let key = Self.key(for: host) else { return }105 store.update { $0[key] = nil }106 }107108 func resetAll() { store.value = [:] }109110 var allSites: [String: SiteSettings] { store.value }111}112