forked from getAlby/alby-installer-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.swift
102 lines (88 loc) · 3.38 KB
/
Browser.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import AppKit
let safariExtensionBundleIdentifier = "com.getalby.Alby.macOS"
struct Browser: Identifiable {
let id: String
let appSupportFolder: String?
let chrome: Bool?
var json: String? {
guard let bundle = Bundle.main.resourcePath, let chrome = chrome else {
return nil
}
// https://github.com/getAlby/alby-companion-rs/releases
return """
{
"name": "alby",
"description": "Alby native messaging to connect to nodes behind Tor",
"path": "\(bundle)/alby",
"type": "stdio",
"allowed_\(chrome ? "origins" : "extensions")": ["\(chrome ? "chrome-extension://iokeahhehimjnekafflcihljlcjccdbe/" : "extension@getalby.com")"]
}
"""
}
static var all: [Browser] {
[Browser(id: "org.mozilla.firefox", appSupportFolder: "Mozilla", chrome: false),
Browser(id: "com.google.Chrome", appSupportFolder: "Google/Chrome", chrome: true),
Browser(id: "org.chromium.Chromium", appSupportFolder: "Chromium", chrome: true),
Browser(id: "com.vivaldi.Vivaldi", appSupportFolder: "Vivaldi", chrome: true),
Browser(id: "com.brave.Browser", appSupportFolder: "BraveSoftware/Brave-Browser", chrome: true)]
}
static var installed: [Browser] {
all.filter { $0.path != nil }
}
var path: URL? {
NSWorkspace.shared.urlForApplication(withBundleIdentifier: id)
}
private var nativeMessagingURL: URL? {
guard let appSupportFolder = appSupportFolder else {
return nil
}
let appSupportURL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let url = appSupportURL.appendingPathComponent(appSupportFolder).appendingPathComponent("NativeMessagingHosts")
try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
return url
}
var albyJsonURL: URL? {
nativeMessagingURL?.appendingPathComponent("alby").appendingPathExtension("json")
}
var companionInstalled: Bool? {
guard let albyJsonURL = albyJsonURL else {
return nil
}
return FileManager.default.fileExists(atPath: albyJsonURL.path)
}
var exists: Bool {
guard let nativeMessagingURL = nativeMessagingURL else {
return false
}
return FileManager.default.fileExists(atPath: nativeMessagingURL.path)
}
func install() throws {
guard let albyJsonURL = albyJsonURL else {
return
}
try json?.write(to: albyJsonURL, atomically: true, encoding: .ascii)
}
func remove() throws {
guard let albyJsonURL = albyJsonURL else {
return
}
try FileManager.default.removeItem(at: albyJsonURL)
}
var application: URL? {
NSWorkspace.shared.urlForApplication(withBundleIdentifier: id)
}
var icon: NSImage? {
var icon: String {
guard let chrome = chrome else {
return "AppIcon" // Safari
}
return chrome ? "app" : "firefox"
}
if let url = application {
if let image = Bundle(url: url)?.resourceURL?.appendingPathComponent(icon).appendingPathExtension("icns") {
return NSImage(contentsOf: image)
}
}
return nil
}
}