-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDuckDuckGoDBPBackgroundAgentAppDelegate.swift
133 lines (110 loc) · 4.88 KB
/
DuckDuckGoDBPBackgroundAgentAppDelegate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// DuckDuckGoDBPBackgroundAgentAppDelegate.swift
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Cocoa
import Combine
import Common
import ServiceManagement
import DataBrokerProtection
import BrowserServicesKit
import PixelKit
import Networking
import Subscription
import os.log
@objc(Application)
final class DuckDuckGoDBPBackgroundAgentApplication: NSApplication {
private let _delegate: DuckDuckGoDBPBackgroundAgentAppDelegate
private let subscriptionManager: SubscriptionManager
override init() {
Logger.dbpBackgroundAgent.log("🟢 Starting: \(NSRunningApplication.current.processIdentifier, privacy: .public)")
let dryRun: Bool
#if DEBUG
dryRun = true
#else
dryRun = false
#endif
PixelKit.setUp(dryRun: dryRun,
appVersion: AppVersion.shared.versionNumber,
source: "dbpBackgroundAgent",
defaultHeaders: [:],
defaults: .standard) { (pixelName: String, headers: [String: String], parameters: [String: String], _, _, onComplete: @escaping (Bool, Error?) -> Void) in
let url = URL.pixelUrl(forPixelNamed: pixelName)
let apiHeaders = APIRequest.Headers(additionalHeaders: headers) // workaround - Pixel class should really handle APIRequest.Headers by itself
let configuration = APIRequest.Configuration(url: url, method: .get, queryParameters: parameters, headers: apiHeaders)
let request = APIRequest(configuration: configuration)
request.fetch { _, error in
onComplete(true, error)
}
}
let pixelHandler = DataBrokerProtectionPixelsHandler()
pixelHandler.fire(.backgroundAgentStarted)
// prevent agent from running twice
if let anotherInstance = NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.bundleIdentifier!).first(where: { $0 != .current }) {
Logger.dbpBackgroundAgent.error("Stopping: another instance is running: \(anotherInstance.processIdentifier, privacy: .public).")
pixelHandler.fire(.backgroundAgentStartedStoppingDueToAnotherInstanceRunning)
exit(0)
}
// Configure Subscription
subscriptionManager = DefaultSubscriptionManager()
_delegate = DuckDuckGoDBPBackgroundAgentAppDelegate(subscriptionManager: subscriptionManager)
super.init()
self.delegate = _delegate
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@main
final class DuckDuckGoDBPBackgroundAgentAppDelegate: NSObject, NSApplicationDelegate {
private let settings = DataBrokerProtectionSettings()
private var cancellables = Set<AnyCancellable>()
private var statusBarMenu: StatusBarMenu?
private let subscriptionManager: SubscriptionManager
private var manager: DataBrokerProtectionAgentManager?
init(subscriptionManager: SubscriptionManager) {
self.subscriptionManager = subscriptionManager
}
@MainActor
func applicationDidFinishLaunching(_ aNotification: Notification) {
Logger.dbpBackgroundAgent.log("DuckDuckGoAgent started")
let authenticationManager = DataBrokerAuthenticationManagerBuilder.buildAuthenticationManager(subscriptionManager: subscriptionManager)
manager = DataBrokerProtectionAgentManagerProvider.agentManager(authenticationManager: authenticationManager,
accountManager: subscriptionManager.accountManager)
manager?.agentFinishedLaunching()
setupStatusBarMenu()
// Aligning the environment with the Subscription one
settings.alignTo(subscriptionEnvironment: subscriptionManager.currentEnvironment)
}
@MainActor
private func setupStatusBarMenu() {
statusBarMenu = StatusBarMenu()
if settings.showInMenuBar {
statusBarMenu?.show()
} else {
statusBarMenu?.hide()
}
settings.showInMenuBarPublisher.sink { [weak self] showInMenuBar in
Task { @MainActor in
if showInMenuBar {
self?.statusBarMenu?.show()
} else {
self?.statusBarMenu?.hide()
}
}
}.store(in: &cancellables)
}
}