Skip to content

Commit

Permalink
Restarting WWDCAgent when a new version is detected (i.e. after app u…
Browse files Browse the repository at this point in the history
…pdates)
  • Loading branch information
insidegui committed Jun 7, 2021
1 parent 5ed6d35 commit 80354a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions WWDC/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ extension Notification.Name {

class AppDelegate: NSObject, NSApplicationDelegate {

private lazy var agentController = WWDCAgentController()

private let log = OSLog(subsystem: "io.wwdc.app", category: String(describing: AppDelegate.self))

private lazy var commandsReceiver = AppCommandsReceiver()
Expand Down Expand Up @@ -62,6 +64,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ notification: Notification) {
UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": false])

agentController.registerAgentVersion()

NSApp.registerForRemoteNotifications(matching: [])

Expand Down
50 changes: 48 additions & 2 deletions WWDC/WWDCAgentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import Cocoa
import ServiceManagement
import os.log

extension Notification.Name {
static let WWDCAgentEnabledPreferenceChanged = Notification.Name("io.wwdc.app.AgentEnabledPreferenceChanged")
}

final class WWDCAgentController: NSObject {

private let log = OSLog(subsystem: "io.wwdc.app", category: String(describing: WWDCAgentController.self))

static private(set) var isAgentEnabled: Bool {
get { UserDefaults.standard.bool(forKey: #function) }
set {
Expand All @@ -31,7 +34,12 @@ final class WWDCAgentController: NSObject {
}
}

private lazy var agentBundleIdentifier: String = {
private(set) var lastRunAgentBuild: String? {
get { UserDefaults.standard.string(forKey: #function) }
set { UserDefaults.standard.set(newValue, forKey: #function) }
}

private lazy var agentBundle: Bundle = {
let agentURL = Bundle.main.bundleURL
.appendingPathComponent("Contents/Library/LoginItems", isDirectory: true)
.appendingPathComponent("WWDCAgent.app")
Expand All @@ -40,13 +48,25 @@ final class WWDCAgentController: NSObject {
preconditionFailure("Couldn't instatiate agent bundle")
}

guard let identifier = bundle.bundleIdentifier else {
return bundle
}()

private lazy var agentBundleIdentifier: String = {
guard let identifier = agentBundle.bundleIdentifier else {
preconditionFailure("Failed to read identifier for agent bundle")
}

return identifier
}()

private lazy var currentAgentBundleBuild: String = {
guard let build = agentBundle.infoDictionary?["CFBundleVersion"] as? String else {
preconditionFailure("Failed to read CFBundleVersion for agent bundle")
}

return build
}()

func enableAgent() -> Bool {
Self.isAgentEnabled = SMLoginItemSetEnabled(agentBundleIdentifier as CFString, true)

Expand All @@ -58,5 +78,31 @@ final class WWDCAgentController: NSObject {

Self.isAgentEnabled = false
}

func registerAgentVersion() {
guard currentAgentBundleBuild != lastRunAgentBuild else {
os_log("Agent bundle build has not changed", log: self.log, type: .debug)
return
}

if let build = lastRunAgentBuild {
os_log("Registering new agent build: %{public}@", log: self.log, type: .debug, build)
} else {
os_log("Registering agent build for the first time: %{public}@", log: self.log, type: .debug, currentAgentBundleBuild)
}

lastRunAgentBuild = currentAgentBundleBuild

guard let agentApp = NSRunningApplication.runningApplications(withBundleIdentifier: agentBundleIdentifier).first else {
os_log("Couldn't find agent app running, ignoring", log: self.log, type: .debug)
return
}

os_log("Found old agent running with pid %{public}d, restarting", log: self.log, type: .debug, agentApp.processIdentifier)

if !agentApp.forceTerminate() {
os_log("Failed to terminate agent", log: self.log, type: .fault)
}
}

}

0 comments on commit 80354a2

Please sign in to comment.