Something in the way

This commit is contained in:
Hariharan Mudaliar 2026-01-13 14:35:23 +05:30
commit 9bd445986d
No known key found for this signature in database
GPG key ID: 025583EB0BE5640E
19 changed files with 2395 additions and 1 deletions

View file

@ -0,0 +1,159 @@
import AppKit
import Foundation
import Defaults
import AtollExtensionKit
@MainActor
final class ExtensionXPCService: NSObject, AtollXPCServiceProtocol {
private let bundleIdentifier: String
private weak var host: ExtensionXPCServiceHost?
private weak var connection: NSXPCConnection?
private let authorizationManager = ExtensionAuthorizationManager.shared
private let liveActivityManager = ExtensionLiveActivityManager.shared
private let widgetManager = ExtensionLockScreenWidgetManager.shared
private let decoder = JSONDecoder()
init(bundleIdentifier: String, host: ExtensionXPCServiceHost, connection: NSXPCConnection) {
self.bundleIdentifier = bundleIdentifier
self.host = host
self.connection = connection
super.init()
}
// MARK: Authorization
func requestAuthorization(bundleIdentifier providedBundleIdentifier: String, reply: @escaping (Bool, Error?) -> Void) {
guard validate(bundleIdentifier: providedBundleIdentifier, reply: reply) else { return }
guard authorizationManager.isExtensionsFeatureEnabled else {
reply(false, ExtensionValidationError.featureDisabled.asNSError)
return
}
let entry = authorizationManager.ensureEntryExists(bundleIdentifier: bundleIdentifier, appName: resolvedApplicationName())
if entry.status == .pending {
authorizationManager.authorize(bundleIdentifier: bundleIdentifier, appName: resolvedApplicationName())
host?.notifyAuthorizationChange(bundleIdentifier: bundleIdentifier, isAuthorized: true)
reply(true, nil)
return
}
reply(entry.isAuthorized, entry.isAuthorized ? nil : ExtensionValidationError.unauthorized.asNSError)
}
func checkAuthorization(bundleIdentifier providedBundleIdentifier: String, reply: @escaping (Bool) -> Void) {
guard providedBundleIdentifier == bundleIdentifier else {
reply(false)
return
}
let isAuthorized = authorizationManager.authorizationEntry(for: bundleIdentifier)?.isAuthorized ?? false
reply(isAuthorized)
}
// MARK: Live Activities
func presentLiveActivity(descriptorData: Data, reply: @escaping (Bool, Error?) -> Void) {
respond(reply: reply) {
let descriptor = try decoder.decode(AtollLiveActivityDescriptor.self, from: descriptorData)
try ExtensionDescriptorValidator.validate(descriptor)
try liveActivityManager.present(descriptor: descriptor, bundleIdentifier: bundleIdentifier)
}
}
func updateLiveActivity(descriptorData: Data, reply: @escaping (Bool, Error?) -> Void) {
respond(reply: reply) {
let descriptor = try decoder.decode(AtollLiveActivityDescriptor.self, from: descriptorData)
try ExtensionDescriptorValidator.validate(descriptor)
try liveActivityManager.update(descriptor: descriptor, bundleIdentifier: bundleIdentifier)
}
}
func dismissLiveActivity(activityID: String, bundleIdentifier providedBundleIdentifier: String, reply: @escaping (Bool, Error?) -> Void) {
guard validate(bundleIdentifier: providedBundleIdentifier, reply: reply) else { return }
liveActivityManager.dismiss(activityID: activityID, bundleIdentifier: bundleIdentifier)
reply(true, nil)
}
// MARK: Lock Screen Widgets
func presentLockScreenWidget(descriptorData: Data, reply: @escaping (Bool, Error?) -> Void) {
respond(reply: reply) {
let descriptor = try decoder.decode(AtollLockScreenWidgetDescriptor.self, from: descriptorData)
try ExtensionDescriptorValidator.validate(descriptor)
try widgetManager.present(descriptor: descriptor, bundleIdentifier: bundleIdentifier)
}
}
func updateLockScreenWidget(descriptorData: Data, reply: @escaping (Bool, Error?) -> Void) {
respond(reply: reply) {
let descriptor = try decoder.decode(AtollLockScreenWidgetDescriptor.self, from: descriptorData)
try ExtensionDescriptorValidator.validate(descriptor)
try widgetManager.update(descriptor: descriptor, bundleIdentifier: bundleIdentifier)
}
}
func dismissLockScreenWidget(widgetID: String, bundleIdentifier providedBundleIdentifier: String, reply: @escaping (Bool, Error?) -> Void) {
guard validate(bundleIdentifier: providedBundleIdentifier, reply: reply) else { return }
widgetManager.dismiss(widgetID: widgetID, bundleIdentifier: bundleIdentifier)
reply(true, nil)
}
// MARK: Diagnostics
func getVersion(reply: @escaping (String) -> Void) {
reply(appVersion)
}
// MARK: Helpers
private func respond(reply: @escaping (Bool, Error?) -> Void, operation: () throws -> Void) {
do {
try operation()
reply(true, nil)
} catch {
if Defaults[.extensionDiagnosticsLoggingEnabled] {
Logger.log("Extension XPC request failed: \(error)", category: .extensions)
}
reply(false, error.asNSError)
}
}
private func validate(bundleIdentifier providedBundleIdentifier: String, reply: @escaping (Bool, Error?) -> Void) -> Bool {
guard providedBundleIdentifier == bundleIdentifier else {
let error = ExtensionXPCServiceError.bundleMismatch(expected: bundleIdentifier, received: providedBundleIdentifier)
reply(false, error.asNSError)
return false
}
return true
}
private func resolvedApplicationName() -> String {
guard let processIdentifier = connection?.processIdentifier,
processIdentifier != 0,
let app = NSRunningApplication(processIdentifier: pid_t(processIdentifier)),
let name = app.localizedName else {
return bundleIdentifier
}
return name
}
}
private enum ExtensionXPCServiceError: LocalizedError {
case bundleMismatch(expected: String, received: String)
var errorDescription: String? {
switch self {
case let .bundleMismatch(expected, received):
return "Bundle identifier mismatch. Expected \(expected) but received \(received)."
}
}
}
private extension Error {
var asNSError: NSError { self as NSError }
}

View file

@ -0,0 +1,131 @@
import AppKit
import Foundation
import AtollExtensionKit
/// Shared constants for the Atoll extension XPC service.
enum ExtensionXPCServiceConstants {
static let machServiceName = "com.ebullioscopic.Atoll.xpc"
}
@MainActor
final class ExtensionXPCServiceHost: NSObject, NSXPCListenerDelegate {
static let shared = ExtensionXPCServiceHost()
private final class ClientContext {
weak var connection: NSXPCConnection?
let bundleIdentifier: String
init(connection: NSXPCConnection, bundleIdentifier: String) {
self.connection = connection
self.bundleIdentifier = bundleIdentifier
}
}
private var listener: NSXPCListener?
private var clientContexts: [ObjectIdentifier: ClientContext] = [:]
func start() {
guard listener == nil else { return }
let listener = NSXPCListener(machServiceName: ExtensionXPCServiceConstants.machServiceName)
listener.delegate = self
self.listener = listener
listener.resume()
Logger.log("Started Atoll XPC listener", category: .extensions)
}
func stop() {
listener?.invalidate()
listener = nil
clientContexts.removeAll()
Logger.log("Stopped Atoll XPC listener", category: .extensions)
}
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection connection: NSXPCConnection) -> Bool {
guard let bundleIdentifier = resolveBundleIdentifier(for: connection) else {
Logger.log("Rejected XPC connection without bundle identifier", category: .extensions)
return false
}
let service = ExtensionXPCService(bundleIdentifier: bundleIdentifier, host: self, connection: connection)
connection.exportedInterface = NSXPCInterface(with: AtollXPCServiceProtocol.self)
connection.exportedObject = service
connection.remoteObjectInterface = NSXPCInterface(with: AtollXPCClientProtocol.self)
connection.invalidationHandler = { [weak self, weak connection] in
guard let connection else { return }
Task { @MainActor [weak self] in
self?.removeConnection(connection)
}
}
connection.interruptionHandler = { [weak self, weak connection] in
guard let connection else { return }
Task { @MainActor [weak self] in
self?.removeConnection(connection)
}
}
connection.resume()
clientContexts[ObjectIdentifier(connection)] = ClientContext(connection: connection, bundleIdentifier: bundleIdentifier)
Logger.log("Accepted XPC connection from \(bundleIdentifier)", category: .extensions)
return true
}
func notifyAuthorizationChange(bundleIdentifier: String, isAuthorized: Bool) {
deliver(to: bundleIdentifier) { client in
client.authorizationDidChange(isAuthorized: isAuthorized)
}
}
func notifyActivityDismiss(bundleIdentifier: String, activityID: String) {
deliver(to: bundleIdentifier) { client in
client.activityDidDismiss(activityID: activityID)
}
}
func notifyWidgetDismiss(bundleIdentifier: String, widgetID: String) {
deliver(to: bundleIdentifier) { client in
client.widgetDidDismiss(widgetID: widgetID)
}
}
private func resolveBundleIdentifier(for connection: NSXPCConnection) -> String? {
let processIdentifier = connection.processIdentifier
guard processIdentifier != 0 else { return nil }
if let app = NSRunningApplication(processIdentifier: pid_t(processIdentifier)),
let bundleIdentifier = app.bundleIdentifier {
return bundleIdentifier
}
return nil
}
private func deliver(to bundleIdentifier: String, send block: (AtollXPCClientProtocol) -> Void) {
var staleIdentifiers: [ObjectIdentifier] = []
for (identifier, context) in clientContexts where context.bundleIdentifier == bundleIdentifier {
guard let connection = context.connection else {
staleIdentifiers.append(identifier)
continue
}
guard let client = connection.remoteObjectProxyWithErrorHandler({ error in
Logger.log("Failed to deliver XPC callback to \(bundleIdentifier): \(error)", category: .extensions)
}) as? AtollXPCClientProtocol else {
continue
}
block(client)
}
staleIdentifiers.forEach { clientContexts.removeValue(forKey: $0) }
}
private func removeConnection(_ connection: NSXPCConnection) {
clientContexts.removeValue(forKey: ObjectIdentifier(connection))
Logger.log("Removed XPC connection", category: .extensions)
}
}