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 pathConfigurationManager.swift
234 lines (199 loc) · 9.37 KB
/
ConfigurationManager.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
//
// ConfigurationManager.swift
//
// Copyright © 2021 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 Foundation
import os.log
import Combine
import BrowserServicesKit
import Persistence
import Configuration
import Common
import Networking
import PixelKit
final class ConfigurationManager: DefaultConfigurationManager {
private let trackerDataManager: TrackerDataManager
private let privacyConfigurationManager: PrivacyConfigurationManaging
private var contentBlockingManager: ContentBlockerRulesManagerProtocol
private enum Constants {
static let lastConfigurationInstallDateKey = "config.last.installed"
}
private var defaults: KeyValueStoring
private(set) var lastConfigurationInstallDate: Date? {
get {
defaults.object(forKey: Constants.lastConfigurationInstallDateKey) as? Date
}
set {
defaults.set(newValue, forKey: Constants.lastConfigurationInstallDateKey)
}
}
static let configurationDebugEvents = EventMapping<ConfigurationDebugEvents> { event, error, _, _ in
let domainEvent: GeneralPixel
switch event {
case .invalidPayload(let configuration):
domainEvent = .invalidPayload(configuration)
}
PixelKit.fire(DebugEvent(domainEvent, error: error))
}
init(fetcher: ConfigurationFetching = ConfigurationFetcher(store: ConfigurationStore(), eventMapping: configurationDebugEvents),
store: ConfigurationStoring = ConfigurationStore(),
defaults: KeyValueStoring = UserDefaults.appConfiguration,
trackerDataManager: TrackerDataManager = ContentBlocking.shared.trackerDataManager,
privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager,
contentBlockingManager: ContentBlockerRulesManagerProtocol = ContentBlocking.shared.contentBlockingManager) {
self.trackerDataManager = trackerDataManager
self.privacyConfigurationManager = privacyConfigurationManager
self.contentBlockingManager = contentBlockingManager
self.defaults = defaults
super.init(fetcher: fetcher, store: store, defaults: defaults)
}
func log() {
Logger.config.log("last update \(String(describing: self.lastUpdateTime), privacy: .public)")
Logger.config.log("last refresh check \(String(describing: self.lastRefreshCheckTime), privacy: .public)")
}
override public func refreshNow(isDebug: Bool = false) async {
let updateTrackerBlockingDependenciesTask = Task {
let didFetchAnyTrackerBlockingDependencies = await fetchTrackerBlockingDependencies(isDebug: isDebug)
if didFetchAnyTrackerBlockingDependencies {
updateTrackerBlockingDependencies()
tryAgainLater()
}
}
let updateBloomFilterTask = Task {
do {
try await fetcher.fetch(all: [.bloomFilterBinary, .bloomFilterSpec])
try await updateBloomFilter()
tryAgainLater()
} catch {
handleRefreshError(error)
}
}
let updateBloomFilterExclusionsTask = Task {
do {
try await fetcher.fetch(.bloomFilterExcludedDomains, isDebug: isDebug)
try await updateBloomFilterExclusions()
tryAgainLater()
} catch {
handleRefreshError(error)
}
}
await updateTrackerBlockingDependenciesTask.value
await updateBloomFilterTask.value
await updateBloomFilterExclusionsTask.value
(store as? ConfigurationStore)?.log()
Logger.config.info("last update \(String(describing: self.lastUpdateTime), privacy: .public)")
Logger.config.info("last refresh check \(String(describing: self.lastRefreshCheckTime), privacy: .public)")
}
private func fetchTrackerBlockingDependencies(isDebug: Bool) async -> Bool {
var didFetchAnyTrackerBlockingDependencies = false
// Start surrogates fetch task
let surrogatesTask = Task { try await fetcher.fetch(.surrogates, isDebug: isDebug) }
// Perform privacyConfiguration fetch and update
do {
try await fetcher.fetch(.privacyConfiguration, isDebug: isDebug)
didFetchAnyTrackerBlockingDependencies = true
privacyConfigurationManager.reload(etag: store.loadEtag(for: .privacyConfiguration),
data: store.loadData(for: .privacyConfiguration))
} catch {
Logger.config.error(
"Failed to complete configuration update to \(Configuration.privacyConfiguration.rawValue, privacy: .public): \(error.localizedDescription, privacy: .public)"
)
tryAgainSoon()
}
// Start trackerDataSet fetch task after privacyConfiguration completes
let trackerDataSetTask = Task { try await fetcher.fetch(.trackerDataSet, isDebug: isDebug) }
// Wait for surrogates and trackerDataSet tasks
let tasks: [(Configuration, Task<(), Swift.Error>)] = [
(.surrogates, surrogatesTask),
(.trackerDataSet, trackerDataSetTask)
]
for (configuration, task) in tasks {
do {
try await task.value
didFetchAnyTrackerBlockingDependencies = true
} catch {
Logger.config.error(
"Failed to complete configuration update to \(configuration.rawValue, privacy: .public): \(error.localizedDescription, privacy: .public)"
)
tryAgainSoon()
}
}
return didFetchAnyTrackerBlockingDependencies
}
private func handleRefreshError(_ error: Swift.Error) {
// Avoid firing a configuration fetch error pixel when we received a 304 status code.
// A 304 status code is expected when we request the config with an ETag that matches the current remote version.
if case APIRequest.Error.invalidStatusCode(304) = error {
return
}
Logger.config.error("Failed to complete configuration update \(error.localizedDescription, privacy: .public)")
PixelKit.fire(DebugEvent(GeneralPixel.configurationFetchError(error: error)))
tryAgainSoon()
}
private func updateTrackerBlockingDependencies() {
lastConfigurationInstallDate = Date()
trackerDataManager.reload(etag: store.loadEtag(for: .trackerDataSet),
data: store.loadData(for: .trackerDataSet))
privacyConfigurationManager.reload(etag: store.loadEtag(for: .privacyConfiguration),
data: store.loadData(for: .privacyConfiguration))
contentBlockingManager.scheduleCompilation()
}
private func updateBloomFilter() async throws {
guard let specData = store.loadData(for: .bloomFilterSpec) else {
throw Error.bloomFilterSpecNotFound
}
guard let bloomFilterData = store.loadData(for: .bloomFilterBinary) else {
throw Error.bloomFilterBinaryNotFound
}
try await Task.detached {
let spec = try JSONDecoder().decode(HTTPSBloomFilterSpecification.self, from: specData)
do {
try await PrivacyFeatures.httpsUpgrade.persistBloomFilter(specification: spec, data: bloomFilterData)
} catch {
assertionFailure("persistBloomFilter failed: \(error)")
throw Error.bloomFilterPersistenceFailed.withUnderlyingError(error)
}
await PrivacyFeatures.httpsUpgrade.loadData()
}.value
}
private func updateBloomFilterExclusions() async throws {
guard let bloomFilterExclusions = store.loadData(for: .bloomFilterExcludedDomains) else {
throw Error.bloomFilterExclusionsNotFound
}
try await Task.detached {
let excludedDomains = try JSONDecoder().decode(HTTPSExcludedDomains.self, from: bloomFilterExclusions).data
do {
try await PrivacyFeatures.httpsUpgrade.persistExcludedDomains(excludedDomains)
} catch {
throw Error.bloomFilterExclusionsPersistenceFailed.withUnderlyingError(error)
}
await PrivacyFeatures.httpsUpgrade.loadData()
}.value
}
}
extension ConfigurationManager {
override var presentedItemURL: URL? {
store.fileUrl(for: .privacyConfiguration).deletingLastPathComponent()
}
override func presentedSubitemDidAppear(at url: URL) {
guard url == store.fileUrl(for: .privacyConfiguration) else { return }
updateTrackerBlockingDependencies()
}
override func presentedSubitemDidChange(at url: URL) {
guard url == store.fileUrl(for: .privacyConfiguration) else { return }
updateTrackerBlockingDependencies()
}
}