|
| 1 | +// |
| 2 | +// DiaBrowser.swift |
| 3 | +// SimplyTrack |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import os.log |
| 8 | + |
| 9 | +/// Dia-specific implementation of browser interface. |
| 10 | +/// Handles URL detection for the Dia browser by The Browser Company. |
| 11 | +/// Dia exposes a custom AppleScript interface with tab and URL support. |
| 12 | +class DiaBrowser: BaseBrowser { |
| 13 | + private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "DiaBrowser") |
| 14 | + |
| 15 | + init() { |
| 16 | + super.init(bundleId: "company.thebrowser.dia", displayName: "Dia") |
| 17 | + } |
| 18 | + |
| 19 | + /// Dia-specific AppleScript for URL retrieval |
| 20 | + override var currentURLScript: String { |
| 21 | + return """ |
| 22 | + tell application "Dia" |
| 23 | + if (count of windows) > 0 then |
| 24 | + return URL of active tab of window 1 |
| 25 | + end if |
| 26 | + end tell |
| 27 | + """ |
| 28 | + } |
| 29 | + |
| 30 | + /// Checks if Dia is currently in incognito mode. |
| 31 | + /// Dia doesn't expose a private browsing property in its AppleScript dictionary, |
| 32 | + /// so we use the accessibility API to check the window's AXIdentifier which |
| 33 | + /// contains "bigIncognitoBrowserWindow" for private windows. |
| 34 | + override func isInPrivateBrowsingMode() -> Bool { |
| 35 | + let script = """ |
| 36 | + tell application "System Events" |
| 37 | + tell process "Dia" |
| 38 | + if (count of windows) > 0 then |
| 39 | + return value of attribute "AXIdentifier" of front window |
| 40 | + end if |
| 41 | + end tell |
| 42 | + end tell |
| 43 | + """ |
| 44 | + |
| 45 | + let scriptResult = executeAppleScript(script) |
| 46 | + |
| 47 | + if let error = scriptResult.error { |
| 48 | + if scriptResult.errorCode == -1719 { |
| 49 | + logger.debug("Dia System Events transient error (invalid index): \(error.description)") |
| 50 | + } else if scriptResult.errorCode == -1743 || scriptResult.errorCode == -1744 { |
| 51 | + PermissionManager.shared.handleSystemEventsPermissionResult(success: false) |
| 52 | + } else if scriptResult.errorCode == -25211 { |
| 53 | + PermissionManager.shared.handleAccessibilityPermissionResult(success: false) |
| 54 | + } else { |
| 55 | + logger.error("Dia System Events AppleScript error: \(error.description)") |
| 56 | + } |
| 57 | + return false |
| 58 | + } |
| 59 | + |
| 60 | + if scriptResult.result != nil { |
| 61 | + PermissionManager.shared.handleSystemEventsPermissionResult(success: true) |
| 62 | + PermissionManager.shared.handleAccessibilityPermissionResult(success: true) |
| 63 | + } |
| 64 | + |
| 65 | + guard let identifier = scriptResult.result else { |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + return identifier.contains("Incognito") |
| 70 | + } |
| 71 | +} |
0 commit comments