From 71e3e3e35cb97479b85ad75dade5828a63f5d564 Mon Sep 17 00:00:00 2001 From: amsam0 <44983869+amsam0@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:56:29 -0700 Subject: [PATCH 1/3] fix: avoid unwrapping values that are likely to be null and cause a crash --- aw_watcher_window/macos.swift | 37 +++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/aw_watcher_window/macos.swift b/aw_watcher_window/macos.swift index fceb90d1..921ef56f 100644 --- a/aw_watcher_window/macos.swift +++ b/aw_watcher_window/macos.swift @@ -324,15 +324,23 @@ class MainThing { var windowTitle: AnyObject? AXUIElementCopyAttributeValue(axElement, kAXTitleAttribute as CFString, &windowTitle) - var data = NetworkMessage(app: frontmost.localizedName!, title: windowTitle as? String ?? "") + let applicationName = frontmost.localizedName ?? bundleIdentifier + var data = NetworkMessage(app: applicationName, title: windowTitle as? String ?? "Unknown Title") - if CHROME_BROWSERS.contains(frontmost.localizedName!) { + if CHROME_BROWSERS.contains(applicationName) { debug("Chrome browser detected, extracting URL and title") let chromeObject: ChromeProtocol = SBApplication.init(bundleIdentifier: bundleIdentifier)! - let frontWindow = chromeObject.windows!()[0] - let activeTab = frontWindow.activeTab! + guard let windows = chromeObject.windows, + let frontWindow = windows().first else { + log("Failed to get chrome front window") + return + } + guard let activeTab = frontWindow.activeTab else { + log("Failed to get chrome active tab") + return + } if frontWindow.mode == "incognito" { data = NetworkMessage(app: "", title: "") @@ -345,7 +353,7 @@ class MainThing { if let tabTitle = activeTab.title { if(tabTitle != "" && data.title != tabTitle) { - error("tab title diff: \(tabTitle), window title: \(data.title ?? "")") + error("tab title diff: \(tabTitle), window title: \(data.title)") data.title = tabTitle } } @@ -355,8 +363,15 @@ class MainThing { let safariObject: SafariApplication = SBApplication.init(bundleIdentifier: bundleIdentifier)! - let frontWindow = safariObject.windows!()[0] - let activeTab = frontWindow.currentTab! + guard let windows = safariObject.windows, + let frontWindow = windows().first else { + log("Failed to get safari front window") + return + } + guard let activeTab = frontWindow.currentTab else { + log("Failed to get safari active tab") + return + } // Safari doesn't allow incognito mode to be inspected, so we do not know if we should hide the url data.url = activeTab.URL @@ -364,7 +379,7 @@ class MainThing { // comment above applies here as well if let tabTitle = activeTab.name { if tabTitle != "" && data.title != tabTitle { - error("tab title diff: \(tabTitle), window title: \(data.title ?? "")") + error("tab title diff: \(tabTitle), window title: \(data.title)") data.title = tabTitle } } @@ -378,8 +393,7 @@ class MainThing { debug("Focused window changed") if oldWindow != nil { - AXObserverRemoveNotification( - observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString) + AXObserverRemoveNotification(observer, oldWindow!, kAXFocusedWindowChangedNotification as CFString) } let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()) @@ -436,8 +450,7 @@ class MainThing { }, &observer) let selfPtr = UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()) - AXObserverAddNotification( - observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr) + AXObserverAddNotification(observer!, focusedApp, kAXFocusedWindowChangedNotification as CFString, selfPtr) CFRunLoopAddSource( RunLoop.current.getCFRunLoop(), From 58db5a18017923ce3505f22513ca59164acf0617 Mon Sep 17 00:00:00 2001 From: amsam0 <44983869+amsam0@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:05:21 -0700 Subject: [PATCH 2/3] Don't fail if there is no bundle identifier --- aw_watcher_window/macos.swift | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/aw_watcher_window/macos.swift b/aw_watcher_window/macos.swift index 921ef56f..6367ee4f 100644 --- a/aw_watcher_window/macos.swift +++ b/aw_watcher_window/macos.swift @@ -313,23 +313,22 @@ class MainThing { return } - guard let bundleIdentifier = frontmost.bundleIdentifier else { - log("Failed to get bundle identifier from frontmost application") - return - } - // calculate now before executing any scripting since that can take some time let nowTime = Date.now var windowTitle: AnyObject? AXUIElementCopyAttributeValue(axElement, kAXTitleAttribute as CFString, &windowTitle) - let applicationName = frontmost.localizedName ?? bundleIdentifier - var data = NetworkMessage(app: applicationName, title: windowTitle as? String ?? "Unknown Title") + let applicationName = frontmost.localizedName ?? frontmost.bundleIdentifier ?? "" + var data = NetworkMessage(app: applicationName, title: windowTitle as? String ?? "") if CHROME_BROWSERS.contains(applicationName) { debug("Chrome browser detected, extracting URL and title") + guard let bundleIdentifier = frontmost.bundleIdentifier else { + log("Failed to get bundle identifier from frontmost application") + return + } let chromeObject: ChromeProtocol = SBApplication.init(bundleIdentifier: bundleIdentifier)! guard let windows = chromeObject.windows, @@ -361,6 +360,10 @@ class MainThing { } else if frontmost.localizedName == "Safari" { debug("Safari browser detected, extracting URL and title") + guard let bundleIdentifier = frontmost.bundleIdentifier else { + log("Failed to get bundle identifier from frontmost application") + return + } let safariObject: SafariApplication = SBApplication.init(bundleIdentifier: bundleIdentifier)! guard let windows = safariObject.windows, From 58dcb87a14b8d955c533fcde19bf80868263c35a Mon Sep 17 00:00:00 2001 From: amsam0 <44983869+amsam0@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:15:55 -0700 Subject: [PATCH 3/3] Improve logging --- aw_watcher_window/macos.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aw_watcher_window/macos.swift b/aw_watcher_window/macos.swift index 6367ee4f..88d10470 100644 --- a/aw_watcher_window/macos.swift +++ b/aw_watcher_window/macos.swift @@ -101,7 +101,7 @@ func logPrefix(_ level: String) -> String { let logLevel = ProcessInfo.processInfo.environment["LOG_LEVEL"]?.uppercased() ?? "INFO" func debug(_ msg: String) { - if(logLevel == "DEBUG") { + if (logLevel == "DEBUG") { print("\(logPrefix("DEBUG")) \(msg)") fflush(stdout) } @@ -326,7 +326,7 @@ class MainThing { debug("Chrome browser detected, extracting URL and title") guard let bundleIdentifier = frontmost.bundleIdentifier else { - log("Failed to get bundle identifier from frontmost application") + log("Failed to get bundle identifier from frontmost application, which was recognized to be Chrome") return } let chromeObject: ChromeProtocol = SBApplication.init(bundleIdentifier: bundleIdentifier)! @@ -361,7 +361,7 @@ class MainThing { debug("Safari browser detected, extracting URL and title") guard let bundleIdentifier = frontmost.bundleIdentifier else { - log("Failed to get bundle identifier from frontmost application") + log("Failed to get bundle identifier from frontmost application, which was recognized to be Safari") return } let safariObject: SafariApplication = SBApplication.init(bundleIdentifier: bundleIdentifier)!