This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDataBrokerProtectionDebugMenu.swift
343 lines (272 loc) · 14 KB
/
DataBrokerProtectionDebugMenu.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//
// DataBrokerProtectionDebugMenu.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 DataBrokerProtection
import Foundation
import AppKit
import Common
import LoginItems
import NetworkProtectionProxy
import os.log
final class DataBrokerProtectionDebugMenu: NSMenu {
enum EnvironmentTitle: String {
case staging = "Staging"
case production = "Production"
}
private let waitlistTokenItem = NSMenuItem(title: "Waitlist Token:")
private let waitlistTimestampItem = NSMenuItem(title: "Waitlist Timestamp:")
private let waitlistInviteCodeItem = NSMenuItem(title: "Waitlist Invite Code:")
private let waitlistTermsAndConditionsAcceptedItem = NSMenuItem(title: "T&C Accepted:")
private let productionURLMenuItem = NSMenuItem(title: "Use Production URL", action: #selector(DataBrokerProtectionDebugMenu.useWebUIProductionURL))
private let customURLMenuItem = NSMenuItem(title: "Use Custom URL", action: #selector(DataBrokerProtectionDebugMenu.useWebUICustomURL))
private var databaseBrowserWindowController: NSWindowController?
private var dataBrokerForceOptOutWindowController: NSWindowController?
private let customURLLabelMenuItem = NSMenuItem(title: "")
private let environmentMenu = NSMenu()
private let statusMenuIconMenu = NSMenuItem(title: "Show Status Menu Icon", action: #selector(DataBrokerProtectionDebugMenu.toggleShowStatusMenuItem))
private let webUISettings = DataBrokerProtectionWebUIURLSettings(.dbp)
private let settings = DataBrokerProtectionSettings(defaults: .dbp)
init() {
super.init(title: "Personal Information Removal")
buildItems {
NSMenuItem(title: "Environment")
.submenu(environmentMenu)
NSMenuItem(title: "Background Agent") {
NSMenuItem(title: "Enable", action: #selector(DataBrokerProtectionDebugMenu.backgroundAgentEnable))
.targetting(self)
NSMenuItem(title: "Disable", action: #selector(DataBrokerProtectionDebugMenu.backgroundAgentDisable))
.targetting(self)
NSMenuItem(title: "Restart", action: #selector(DataBrokerProtectionDebugMenu.backgroundAgentRestart))
.targetting(self)
NSMenuItem.separator()
NSMenuItem(title: "Show agent IP address", action: #selector(DataBrokerProtectionDebugMenu.showAgentIPAddress))
.targetting(self)
}
NSMenuItem(title: "Operations") {
NSMenuItem(title: "Hidden WebView") {
menuItem(withTitle: "Run queued operations",
action: #selector(DataBrokerProtectionDebugMenu.startScheduledOperations(_:)),
representedObject: false)
menuItem(withTitle: "Run scan operations",
action: #selector(DataBrokerProtectionDebugMenu.runScanOperations(_:)),
representedObject: false)
menuItem(withTitle: "Run opt-out operations",
action: #selector(DataBrokerProtectionDebugMenu.runOptoutOperations(_:)),
representedObject: false)
}
NSMenuItem(title: "Visible WebView") {
menuItem(withTitle: "Run queued operations",
action: #selector(DataBrokerProtectionDebugMenu.startScheduledOperations(_:)),
representedObject: true)
menuItem(withTitle: "Run scan operations",
action: #selector(DataBrokerProtectionDebugMenu.runScanOperations(_:)),
representedObject: true)
menuItem(withTitle: "Run opt-out operations",
action: #selector(DataBrokerProtectionDebugMenu.runOptoutOperations(_:)),
representedObject: true)
}
}
NSMenuItem(title: "Web UI") {
productionURLMenuItem.targetting(self)
customURLMenuItem.targetting(self)
NSMenuItem.separator()
NSMenuItem(title: "Set Custom URL", action: #selector(DataBrokerProtectionDebugMenu.setWebUICustomURL))
.targetting(self)
NSMenuItem(title: "Reset Custom URL", action: #selector(DataBrokerProtectionDebugMenu.resetCustomURL))
.targetting(self)
customURLLabelMenuItem
}
NSMenuItem.separator()
statusMenuIconMenu.targetting(self)
NSMenuItem(title: "Show DB Browser", action: #selector(DataBrokerProtectionDebugMenu.showDatabaseBrowser))
.targetting(self)
NSMenuItem(title: "Force Profile Removal", action: #selector(DataBrokerProtectionDebugMenu.showForceOptOutWindow))
.targetting(self)
NSMenuItem(title: "Force broker JSON files update", action: #selector(DataBrokerProtectionDebugMenu.forceBrokerJSONFilesUpdate))
.targetting(self)
NSMenuItem(title: "Run Personal Information Removal Debug Mode", action: #selector(DataBrokerProtectionDebugMenu.runCustomJSON))
.targetting(self)
NSMenuItem(title: "Reset All State and Delete All Data", action: #selector(DataBrokerProtectionDebugMenu.deleteAllDataAndStopAgent))
.targetting(self)
populateDataBrokerProtectionEnvironmentListMenuItems()
}
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Menu State Update
override func update() {
updateWebUIMenuItemsState()
updateEnvironmentMenu()
updateShowStatusMenuIconMenu()
}
// MARK: - Menu functions
@objc private func useWebUIProductionURL() {
webUISettings.setURLType(.production)
}
@objc private func useWebUICustomURL() {
webUISettings.setURLType(.custom)
webUISettings.setCustomURL(webUISettings.productionURL)
}
@objc private func resetCustomURL() {
webUISettings.setURLType(.production)
webUISettings.setCustomURL(webUISettings.productionURL)
}
@objc private func setWebUICustomURL() {
showCustomURLAlert { [weak self] value in
guard let value = value, let url = URL(string: value), url.isValid else { return false }
self?.webUISettings.setCustomURL(value)
return true
}
}
@objc private func startScheduledOperations(_ sender: NSMenuItem) {
Logger.dataBrokerProtection.log("Running queued operations...")
let showWebView = sender.representedObject as? Bool ?? false
DataBrokerProtectionManager.shared.loginItemInterface.startScheduledOperations(showWebView: showWebView)
}
@objc private func runScanOperations(_ sender: NSMenuItem) {
Logger.dataBrokerProtection.log("Running scan operations...")
let showWebView = sender.representedObject as? Bool ?? false
DataBrokerProtectionManager.shared.loginItemInterface.startImmediateOperations(showWebView: showWebView)
}
@objc private func runOptoutOperations(_ sender: NSMenuItem) {
Logger.dataBrokerProtection.log("Running Optout operations...")
let showWebView = sender.representedObject as? Bool ?? false
DataBrokerProtectionManager.shared.loginItemInterface.runAllOptOuts(showWebView: showWebView)
}
@objc private func backgroundAgentRestart() {
LoginItemsManager().restartLoginItems([LoginItem.dbpBackgroundAgent])
}
@objc private func backgroundAgentDisable() {
LoginItemsManager().disableLoginItems([LoginItem.dbpBackgroundAgent])
}
@objc private func backgroundAgentEnable() {
LoginItemsManager().enableLoginItems([LoginItem.dbpBackgroundAgent])
}
@objc private func deleteAllDataAndStopAgent() {
Task { @MainActor in
guard case .alertFirstButtonReturn = await NSAlert.removeAllDBPStateAndDataAlert().runModal() else { return }
DataBrokerProtectionFeatureDisabler().disableAndDelete()
}
}
@objc private func showDatabaseBrowser() {
let viewController = DataBrokerDatabaseBrowserViewController()
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 500, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false)
window.contentViewController = viewController
window.minSize = NSSize(width: 500, height: 400)
window.center()
databaseBrowserWindowController = NSWindowController(window: window)
databaseBrowserWindowController?.showWindow(nil)
window.delegate = self
}
@objc private func showAgentIPAddress() {
DataBrokerProtectionManager.shared.showAgentIPAddress()
}
@objc private func showForceOptOutWindow() {
let viewController = DataBrokerForceOptOutViewController()
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 500, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false)
window.contentViewController = viewController
window.minSize = NSSize(width: 500, height: 400)
window.center()
dataBrokerForceOptOutWindowController = NSWindowController(window: window)
dataBrokerForceOptOutWindowController?.showWindow(nil)
window.delegate = self
}
@objc private func runCustomJSON() {
let authenticationManager = DataBrokerProtectionAuthenticationManager(
subscriptionManager: Application.appDelegate.subscriptionManager)
let viewController = DataBrokerRunCustomJSONViewController(authenticationManager: authenticationManager)
let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 500, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false)
window.contentViewController = viewController
window.minSize = NSSize(width: 500, height: 400)
window.center()
databaseBrowserWindowController = NSWindowController(window: window)
databaseBrowserWindowController?.showWindow(nil)
window.delegate = self
}
@objc private func forceBrokerJSONFilesUpdate() {
if let updater = DefaultDataBrokerProtectionBrokerUpdater.provideForDebug() {
updater.updateBrokers()
}
}
@objc private func toggleShowStatusMenuItem() {
settings.showInMenuBar.toggle()
}
// MARK: - Utility Functions
private func populateDataBrokerProtectionEnvironmentListMenuItems() {
environmentMenu.items = [
NSMenuItem(title: "⚠️ The environment can be set in the Subscription > Environment menu", action: nil, target: nil),
NSMenuItem(title: EnvironmentTitle.production.rawValue, action: nil, target: nil, keyEquivalent: ""),
NSMenuItem(title: EnvironmentTitle.staging.rawValue, action: nil, target: nil, keyEquivalent: ""),
]
}
func showCustomURLAlert(callback: @escaping (String?) -> Bool) {
let alert = NSAlert()
alert.messageText = "Enter URL"
alert.addButton(withTitle: "Accept")
alert.addButton(withTitle: "Cancel")
let inputTextField = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 24))
alert.accessoryView = inputTextField
let response = alert.runModal()
if response == .alertFirstButtonReturn {
if !callback(inputTextField.stringValue) {
let invalidAlert = NSAlert()
invalidAlert.messageText = "Invalid URL"
invalidAlert.informativeText = "Please enter a valid URL."
invalidAlert.addButton(withTitle: "OK")
invalidAlert.runModal()
}
} else {
_ = callback(nil)
}
}
private func updateWebUIMenuItemsState() {
productionURLMenuItem.state = webUISettings.selectedURLType == .custom ? .off : .on
customURLMenuItem.state = webUISettings.selectedURLType == .custom ? .on : .off
customURLLabelMenuItem.title = "Custom URL: [\(webUISettings.customURL ?? "")]"
}
func menuItem(withTitle title: String, action: Selector, representedObject: Any?) -> NSMenuItem {
let menuItem = NSMenuItem(title: title, action: action, keyEquivalent: "")
menuItem.target = self
menuItem.representedObject = representedObject
return menuItem
}
private func updateEnvironmentMenu() {
let selectedEnvironment = settings.selectedEnvironment
guard environmentMenu.items.count == 3 else { return }
environmentMenu.items[1].state = selectedEnvironment == .production ? .on: .off
environmentMenu.items[2].state = selectedEnvironment == .staging ? .on: .off
}
private func updateShowStatusMenuIconMenu() {
statusMenuIconMenu.state = settings.showInMenuBar ? .on : .off
}
}
extension DataBrokerProtectionDebugMenu: NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
databaseBrowserWindowController = nil
dataBrokerForceOptOutWindowController = nil
}
}