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 pathFire.swift
634 lines (525 loc) · 22.3 KB
/
Fire.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//
// Fire.swift
//
// Copyright © 2020 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 Common
import Foundation
import BrowserServicesKit
import DDGSync
import PrivacyDashboard
import WebKit
import SecureStorage
import History
import PrivacyStats
import os.log
final class Fire {
let webCacheManager: WebCacheManager
let historyCoordinating: HistoryCoordinating
let permissionManager: PermissionManagerProtocol
let savedZoomLevelsCoordinating: SavedZoomLevelsCoordinating
let downloadListCoordinator: DownloadListCoordinator
let windowControllerManager: WindowControllersManager
let faviconManagement: FaviconManagement
let autoconsentManagement: AutoconsentManagement?
let stateRestorationManager: AppStateRestorationManager?
let recentlyClosedCoordinator: RecentlyClosedCoordinating?
let pinnedTabsManager: PinnedTabsManager
let bookmarkManager: BookmarkManager
let syncService: DDGSyncing?
let syncDataProviders: SyncDataProviders?
let tabCleanupPreparer = TabCleanupPreparer()
let secureVaultFactory: AutofillVaultFactory
let tld: TLD
let getVisitedLinkStore: () -> WKVisitedLinkStoreWrapper?
let getPrivacyStats: () async -> PrivacyStatsCollecting
private var dispatchGroup: DispatchGroup?
enum BurningData: Equatable {
case specificDomains(_ domains: Set<String>, shouldPlayFireAnimation: Bool)
case all
var shouldPlayFireAnimation: Bool {
switch self {
case .all, .specificDomains(_, shouldPlayFireAnimation: true):
return true
// We don't present the fire animation if user burns from the privacy feed
case .specificDomains(_, shouldPlayFireAnimation: false):
return false
}
}
}
enum BurningEntity {
case none(selectedDomains: Set<String>)
case tab(tabViewModel: TabViewModel,
selectedDomains: Set<String>,
parentTabCollectionViewModel: TabCollectionViewModel)
case window(tabCollectionViewModel: TabCollectionViewModel,
selectedDomains: Set<String>)
case allWindows(mainWindowControllers: [MainWindowController],
selectedDomains: Set<String>)
var shouldPlayFireAnimation: Bool {
switch self {
// We don't present the fire animation if user burns from the privacy feed
case .none:
return false
case .tab, .window, .allWindows:
return true
}
}
}
@Published private(set) var burningData: BurningData?
@MainActor
init(cacheManager: WebCacheManager = WebCacheManager.shared,
historyCoordinating: HistoryCoordinating = HistoryCoordinator.shared,
permissionManager: PermissionManagerProtocol = PermissionManager.shared,
savedZoomLevelsCoordinating: SavedZoomLevelsCoordinating = AccessibilityPreferences.shared,
downloadListCoordinator: DownloadListCoordinator = DownloadListCoordinator.shared,
windowControllerManager: WindowControllersManager? = nil,
faviconManagement: FaviconManagement = FaviconManager.shared,
autoconsentManagement: AutoconsentManagement? = nil,
stateRestorationManager: AppStateRestorationManager? = nil,
recentlyClosedCoordinator: RecentlyClosedCoordinating? = nil,
pinnedTabsManager: PinnedTabsManager? = nil,
tld: TLD,
bookmarkManager: BookmarkManager = LocalBookmarkManager.shared,
syncService: DDGSyncing? = nil,
syncDataProviders: SyncDataProviders? = nil,
secureVaultFactory: AutofillVaultFactory = AutofillSecureVaultFactory,
getPrivacyStats: (() async -> PrivacyStatsCollecting)? = nil,
getVisitedLinkStore: (() -> WKVisitedLinkStoreWrapper?)? = nil
) {
self.webCacheManager = cacheManager
self.historyCoordinating = historyCoordinating
self.permissionManager = permissionManager
self.savedZoomLevelsCoordinating = savedZoomLevelsCoordinating
self.downloadListCoordinator = downloadListCoordinator
self.windowControllerManager = windowControllerManager ?? WindowControllersManager.shared
self.faviconManagement = faviconManagement
self.recentlyClosedCoordinator = recentlyClosedCoordinator ?? RecentlyClosedCoordinator.shared
self.pinnedTabsManager = pinnedTabsManager ?? WindowControllersManager.shared.pinnedTabsManager
self.bookmarkManager = bookmarkManager
self.syncService = syncService ?? NSApp.delegateTyped.syncService
self.syncDataProviders = syncDataProviders ?? NSApp.delegateTyped.syncDataProviders
self.secureVaultFactory = secureVaultFactory
self.tld = tld
self.getPrivacyStats = getPrivacyStats ?? { NSApp.delegateTyped.privacyStats }
self.getVisitedLinkStore = getVisitedLinkStore ?? { WKWebViewConfiguration.sharedVisitedLinkStore }
self.autoconsentManagement = autoconsentManagement ?? AutoconsentManagement.shared
if let stateRestorationManager = stateRestorationManager {
self.stateRestorationManager = stateRestorationManager
} else {
self.stateRestorationManager = NSApp.delegateTyped.stateRestorationManager
}
}
@MainActor
func burnEntity(entity: BurningEntity,
includingHistory: Bool = true,
completion: (() -> Void)? = nil) {
Logger.fire.debug("Fire started")
let group = DispatchGroup()
dispatchGroup = group
let domains = domainsToBurn(from: entity)
assert(domains.areAllETLDPlus1(tld: tld))
burningData = .specificDomains(domains, shouldPlayFireAnimation: entity.shouldPlayFireAnimation)
burnLastSessionState()
burnDeletedBookmarks()
let tabViewModels = tabViewModels(of: entity)
tabCleanupPreparer.prepareTabsForCleanup(tabViewModels) {
group.enter()
self.burnTabs(burningEntity: entity) {
Task {
await self.burnWebCache(baseDomains: domains)
if includingHistory {
self.burnHistory(ofEntity: entity) {
self.burnFavicons(for: domains) {
group.leave()
}
}
} else {
group.leave()
}
}
}
group.enter()
self.burnPermissions(of: domains, completion: {
self.burnDownloads(of: domains)
group.leave()
})
self.burnRecentlyClosed(baseDomains: domains)
self.burnAutoconsentCache()
self.burnZoomLevels(of: domains)
group.notify(queue: .main) {
self.dispatchGroup = nil
self.closeWindows(entity: entity)
self.burningData = nil
completion?()
Logger.fire.debug("Fire finished")
}
}
}
@MainActor
func burnAll(completion: (() -> Void)? = nil) {
Logger.fire.debug("Fire started")
let group = DispatchGroup()
dispatchGroup = group
burningData = .all
let entity = BurningEntity.allWindows(mainWindowControllers: windowControllerManager.mainWindowControllers, selectedDomains: Set())
burnLastSessionState()
burnDeletedBookmarks()
let windowControllers = windowControllerManager.mainWindowControllers
let tabViewModels = tabViewModels(of: entity)
tabCleanupPreparer.prepareTabsForCleanup(tabViewModels) {
group.enter()
self.burnTabs(burningEntity: .allWindows(mainWindowControllers: windowControllers, selectedDomains: Set())) {
Task { @MainActor in
await self.burnWebCache()
await self.burnPrivacyStats()
self.burnAllVisitedLinks()
self.burnAllHistory {
self.burnPermissions {
self.burnFavicons {
self.burnDownloads()
group.leave()
}
}
}
}
}
self.burnRecentlyClosed()
self.burnAutoconsentCache()
self.burnZoomLevels()
group.notify(queue: .main) {
self.dispatchGroup = nil
self.closeWindows(entity: entity)
self.burningData = nil
completion?()
Logger.fire.debug("Fire finished")
}
}
}
// Burns visit passed to the method but preserves other visits of same domains
@MainActor
func burnVisits(of visits: [Visit],
except fireproofDomains: FireproofDomains,
isToday: Bool,
completion: (() -> Void)? = nil) {
// Get domains to burn
var domains = Set<String>()
visits.forEach { visit in
guard let historyEntry = visit.historyEntry else {
assertionFailure("No history entry")
return
}
if let domain = historyEntry.url.host,
!fireproofDomains.isFireproof(fireproofDomain: domain) {
domains.insert(domain)
}
}
// Convert to eTLD+1 domains
domains = domains.convertedToETLDPlus1(tld: tld)
burnVisitedLinks(visits)
historyCoordinating.burnVisits(visits) {
let entity: BurningEntity
// Burn all windows in case we are burning visits for today
if isToday {
entity = .allWindows(mainWindowControllers: self.windowControllerManager.mainWindowControllers, selectedDomains: domains)
} else {
entity = .none(selectedDomains: domains)
}
self.burnEntity(entity: entity,
includingHistory: false,
completion: completion)
}
}
// MARK: - Fire animation
func fireAnimationDidStart() {
assert(dispatchGroup != nil)
dispatchGroup?.enter()
}
func fireAnimationDidFinish() {
assert(dispatchGroup != nil)
dispatchGroup?.leave()
}
// MARK: - Closing windows
@MainActor
private func closeWindows(entity: BurningEntity) {
func closeWindow(of tabCollectionViewModel: TabCollectionViewModel) {
guard let windowController = windowControllerManager.windowController(for: tabCollectionViewModel) else {
return
}
windowController.close()
}
switch entity {
case .none:
return
case .tab(tabViewModel: _, selectedDomains: _, parentTabCollectionViewModel: let tabCollectionViewModel):
if tabCollectionViewModel.tabs.count == 0 {
closeWindow(of: tabCollectionViewModel)
}
case .window(tabCollectionViewModel: let tabCollectionViewModel, selectedDomains: _):
closeWindow(of: tabCollectionViewModel)
case .allWindows(mainWindowControllers: let mainWindowControllers, selectedDomains: _):
mainWindowControllers.forEach {
$0.close()
}
}
// If the app is not active, don't retake focus by opening a new window
guard NSApp.isActive else { return }
// Open a new window in case there is none
DispatchQueue.main.async { [weak self] in
guard let self else { return }
if self.windowControllerManager.mainWindowControllers.count == 0 {
NSApp.delegateTyped.newWindow(self)
}
}
}
// MARK: - Web cache
private func burnWebCache() async {
Logger.fire.debug("WebsiteDataStore began cookie deletion")
await webCacheManager.clear()
Logger.fire.debug("WebsiteDataStore completed cookie deletion")
}
private func burnWebCache(baseDomains: Set<String>? = nil) async {
Logger.fire.debug("WebsiteDataStore began cookie deletion")
await webCacheManager.clear(baseDomains: baseDomains)
Logger.fire.debug("WebsiteDataStore completed cookie deletion")
}
// MARK: - History
@MainActor
private func burnHistory(ofEntity entity: BurningEntity, completion: @escaping () -> Void) {
let visits: [Visit]
switch entity {
case .none(selectedDomains: let domains):
burnHistory(of: domains) { urls in
self.burnVisitedLinks(urls)
completion()
}
return
case .tab(tabViewModel: let tabViewModel, selectedDomains: _, parentTabCollectionViewModel: _):
visits = tabViewModel.tab.localHistory
case .window(tabCollectionViewModel: let tabCollectionViewModel, selectedDomains: _):
visits = tabCollectionViewModel.localHistory
case .allWindows:
burnAllVisitedLinks()
burnAllHistory(completion: completion)
return
}
burnVisitedLinks(visits)
historyCoordinating.burnVisits(visits, completion: completion)
}
private func burnHistory(of baseDomains: Set<String>, completion: @escaping (Set<URL>) -> Void) {
historyCoordinating.burnDomains(baseDomains, tld: ContentBlocking.shared.tld, completion: completion)
}
private func burnAllHistory(completion: @escaping () -> Void) {
historyCoordinating.burnAll(completion: completion)
}
// MARK: - Privacy Stats
private func burnPrivacyStats() async {
await getPrivacyStats().clearPrivacyStats()
}
// MARK: - Visited links
@MainActor
private func burnAllVisitedLinks() {
getVisitedLinkStore()?.removeAll()
}
@MainActor
private func burnVisitedLinks(_ visits: [Visit]) {
guard let visitedLinkStore = getVisitedLinkStore() else { return }
for visit in visits {
guard let url = visit.historyEntry?.url else { continue }
visitedLinkStore.removeVisitedLink(with: url)
}
}
@MainActor
private func burnVisitedLinks(_ urls: Set<URL>) {
guard let visitedLinkStore = getVisitedLinkStore() else { return }
for url in urls {
visitedLinkStore.removeVisitedLink(with: url)
}
}
// MARK: - Zoom levels
private func burnZoomLevels() {
savedZoomLevelsCoordinating.burnZoomLevels(except: FireproofDomains.shared)
}
private func burnZoomLevels(of baseDomains: Set<String>) {
savedZoomLevelsCoordinating.burnZoomLevel(of: baseDomains)
}
// MARK: - Permissions
private func burnPermissions(completion: @escaping () -> Void) {
self.permissionManager.burnPermissions(except: FireproofDomains.shared, completion: completion)
}
private func burnPermissions(of baseDomains: Set<String>, completion: @escaping () -> Void) {
self.permissionManager.burnPermissions(of: baseDomains, tld: tld, completion: completion)
}
// MARK: - Downloads
@MainActor
private func burnDownloads() {
self.downloadListCoordinator.cleanupInactiveDownloads(for: nil)
}
@MainActor
private func burnDownloads(of baseDomains: Set<String>) {
self.downloadListCoordinator.cleanupInactiveDownloads(for: baseDomains, tld: tld)
}
// MARK: - Favicons
private func autofillDomains() -> Set<String> {
guard let vault = try? secureVaultFactory.makeVault(reporter: SecureVaultReporter.shared),
let accounts = try? vault.accounts() else {
return []
}
return Set(accounts.compactMap { $0.domain })
}
@MainActor
private func burnFavicons(completion: @escaping () -> Void) {
let autofillDomains = autofillDomains()
self.faviconManagement.burnExcept(fireproofDomains: FireproofDomains.shared,
bookmarkManager: LocalBookmarkManager.shared,
savedLogins: autofillDomains,
completion: completion)
}
@MainActor
private func burnFavicons(for baseDomains: Set<String>, completion: @escaping () -> Void) {
let autofillDomains = autofillDomains()
self.faviconManagement.burnDomains(baseDomains,
exceptBookmarks: LocalBookmarkManager.shared,
exceptSavedLogins: autofillDomains,
exceptExistingHistory: historyCoordinating.history ?? [],
tld: tld,
completion: completion)
}
// MARK: - Tabs
@MainActor
private func burnTabs(burningEntity: BurningEntity,
completion: @escaping () -> Void) {
func replacementPinnedTab(from pinnedTab: Tab) -> Tab {
return Tab(content: pinnedTab.content.loadedFromCache(), shouldLoadInBackground: true)
}
func burnPinnedTabs() {
for (index, pinnedTab) in pinnedTabsManager.tabCollection.tabs.enumerated() {
let newTab = replacementPinnedTab(from: pinnedTab)
pinnedTabsManager.tabCollection.replaceTab(at: index, with: newTab)
}
}
// Close tabs
switch burningEntity {
case .none: break
case .tab(tabViewModel: let tabViewModel,
selectedDomains: _,
parentTabCollectionViewModel: let tabCollectionViewModel):
assert(tabViewModel === tabCollectionViewModel.selectedTabViewModel)
if pinnedTabsManager.isTabPinned(tabViewModel.tab) {
let tab = replacementPinnedTab(from: tabViewModel.tab)
if let index = tabCollectionViewModel.selectionIndex {
tabCollectionViewModel.replaceTab(at: index, with: tab, forceChange: true)
}
} else {
tabCollectionViewModel.removeSelected(forceChange: true)
}
case .window(tabCollectionViewModel: let tabCollectionViewModel,
selectedDomains: _):
tabCollectionViewModel.removeAllTabs(forceChange: true)
burnPinnedTabs()
case .allWindows(mainWindowControllers: let mainWindowControllers,
selectedDomains: _):
mainWindowControllers.forEach {
$0.mainViewController.tabCollectionViewModel.removeAllTabs(forceChange: true)
}
burnPinnedTabs()
}
completion()
}
private func domainsToBurn(from entity: BurningEntity) -> Set<String> {
switch entity {
case .none(let domains):
return domains
case .tab(tabViewModel: _, selectedDomains: let domains, parentTabCollectionViewModel: _):
return domains
case .window(tabCollectionViewModel: _, selectedDomains: let domains):
return domains
case .allWindows(mainWindowControllers: _, selectedDomains: let domains):
return domains
}
}
@MainActor
private func tabViewModels(of entity: BurningEntity) -> [TabViewModel] {
switch entity {
case .none:
return []
case .tab(tabViewModel: let tabViewModel, selectedDomains: _, parentTabCollectionViewModel: _):
return [tabViewModel]
case .window(tabCollectionViewModel: let tabCollectionViewModel, selectedDomains: _):
let pinnedTabViewModels = Array(pinnedTabsManager.tabViewModels.values)
let tabViewModels = Array(tabCollectionViewModel.tabViewModels.values)
return pinnedTabViewModels + tabViewModels
case .allWindows:
let pinnedTabViewModels = Array(pinnedTabsManager.tabViewModels.values)
let tabViewModels = windowControllerManager.allTabViewModels
return pinnedTabViewModels + tabViewModels
}
}
// MARK: - Autoconsent visit cache
private func burnAutoconsentCache() {
self.autoconsentManagement?.clearCache()
}
// MARK: - Last Session State
@MainActor
private func burnLastSessionState() {
stateRestorationManager?.clearLastSessionState()
}
// MARK: - Burn Recently Closed
@MainActor
private func burnRecentlyClosed(baseDomains: Set<String>? = nil) {
recentlyClosedCoordinator?.burnCache(baseDomains: baseDomains, tld: tld)
}
// MARK: - Bookmarks cleanup
private func burnDeletedBookmarks() {
if syncService?.authState == .inactive {
syncDataProviders?.bookmarksAdapter.databaseCleaner.cleanUpDatabaseNow()
}
}
}
extension TabCollection {
// Local history of TabCollection instance including history of already closed tabs
var localHistory: [Visit] {
tabs.flatMap { $0.localHistory }
}
var localHistoryDomains: Set<String> {
var domains = Set<String>()
for tab in tabs {
domains = domains.union(tab.localHistoryDomains)
}
return domains
}
var localHistoryDomainsOfRemovedTabs: Set<String> {
var domains = Set<String>()
for visit in localHistoryOfRemovedTabs {
if let host = visit.historyEntry?.url.host {
domains.insert(host)
}
}
return domains
}
}
extension Set where Element == String {
func areAllETLDPlus1(tld: TLD) -> Bool {
for domain in self {
guard let eTLDPlus1Host = tld.eTLDplus1(domain) else {
return false
}
if domain != eTLDPlus1Host {
return false
}
}
return true
}
}