Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ATB -> PixelDataStore #356

Merged
merged 7 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

36 changes: 20 additions & 16 deletions DuckDuckGo/Common/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,34 @@ final class Database {
static let databaseName = "Database"
}

static let shared = Database()
static let shared: Database = {
#if DEBUG
if AppDelegate.isRunningTests {
let keyStoreMockClass = (NSClassFromString("EncryptionKeyStoreMock") as? NSObject.Type)!
let keyStoreMock = (keyStoreMockClass.init() as? EncryptionKeyStoring)!
return Database(keyStore: keyStoreMock)
}
#endif
return Database()
}()

private let container: NSPersistentContainer
private let storeLoadedCondition = RunLoop.ResumeCondition()

var model: NSManagedObjectModel {
return container.managedObjectModel
}

convenience init() {
let mainBundle = Bundle.main

guard let managedObjectModel = NSManagedObjectModel.mergedModel(from: [mainBundle]) else { fatalError("No DB scheme found") }

self.init(name: Constants.databaseName, model: managedObjectModel)
}

init(name: String, model: NSManagedObjectModel) {
init(name: String = Constants.databaseName,
model: NSManagedObjectModel = NSManagedObjectModel.mergedModel(from: [.main])!,
keyStore: EncryptionKeyStoring = EncryptionKeyStore(generator: EncryptionKeyGenerator())) {
do {
try EncryptedValueTransformer<NSImage>.registerTransformer()
try EncryptedValueTransformer<NSString>.registerTransformer()
try EncryptedValueTransformer<NSURL>.registerTransformer()
try EncryptedValueTransformer<NSNumber>.registerTransformer()
try EncryptedValueTransformer<NSError>.registerTransformer()
try EncryptedValueTransformer<NSImage>.registerTransformer(keyStore: keyStore)
try EncryptedValueTransformer<NSString>.registerTransformer(keyStore: keyStore)
try EncryptedValueTransformer<NSURL>.registerTransformer(keyStore: keyStore)
try EncryptedValueTransformer<NSNumber>.registerTransformer(keyStore: keyStore)
try EncryptedValueTransformer<NSError>.registerTransformer(keyStore: keyStore)
try EncryptedValueTransformer<NSData>.registerTransformer(keyStore: keyStore)
} catch {
fatalError("Failed to register encryption value transformers")
}
Expand All @@ -59,7 +63,7 @@ final class Database {
func loadStore(migrationHandler: @escaping (NSManagedObjectContext) -> Void = { _ in }) {
container.loadPersistentStores { _, error in
if let error = error {
Pixel.fire(.debug(event: .dbInitializationError, error: error, countedBy: .counter))
Pixel.fire(.debug(event: .dbInitializationError, error: error))
// Give Pixel a chance to be sent, but not too long
Thread.sleep(forTimeInterval: 1)
fatalError("Could not load DB: \(error.localizedDescription)")
Expand Down
36 changes: 19 additions & 17 deletions DuckDuckGo/Common/FileSystem/EncryptedValueTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ final class EncryptedValueTransformer<T: NSSecureCoding & NSObject>: ValueTransf
return nil
bwaresiak marked this conversation as resolved.
Show resolved Hide resolved
}
let archivedData: Data
do {
archivedData = try NSKeyedArchiver.archivedData(withRootObject: castValue, requiringSecureCoding: true)
} catch {
assertionFailure("Could not archive value \(castValue): \(error)")
return nil
// if T is Data
if let data = castValue as? Data {
archivedData = data
} else {
do {
archivedData = try NSKeyedArchiver.archivedData(withRootObject: castValue, requiringSecureCoding: true)
} catch {
assertionFailure("Could not archive value \(castValue): \(error)")
return nil
}
}

return try? DataEncryption.encrypt(data: archivedData, key: encryptionKey)
Expand All @@ -55,6 +60,11 @@ final class EncryptedValueTransformer<T: NSSecureCoding & NSObject>: ValueTransf
guard let data = value as? Data,
let decryptedData = try? DataEncryption.decrypt(data: data, key: encryptionKey) else { return nil }

// if T is Data
if let data = decryptedData as? T {
return data
}

return try? NSKeyedUnarchiver.unarchivedObject(ofClass: T.self, from: decryptedData as Data)
}

Expand All @@ -65,19 +75,11 @@ final class EncryptedValueTransformer<T: NSSecureCoding & NSObject>: ValueTransf
return NSValueTransformerName("\(className)Transformer")
}

static func registerTransformer() throws {
#if CI
// Don't register transformers when running on the CI, they'll fail to read the Keychain due to the build machines not being provisioned.
return
#else
let generator = EncryptionKeyGenerator()
let keyStore = EncryptionKeyStore(generator: generator)
let key = try keyStore.readKey()
let transformer = EncryptedValueTransformer<T>(encryptionKey: key)

ValueTransformer.setValueTransformer(transformer, forName: transformerName)
#endif
static func registerTransformer(keyStore: EncryptionKeyStoring) throws {
let key = try keyStore.readKey()
let transformer = EncryptedValueTransformer<T>(encryptionKey: key)

ValueTransformer.setValueTransformer(transformer, forName: transformerName)
}

}
1 change: 0 additions & 1 deletion DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public struct UserDefaultsWrapper<T> {
case atb = "statistics.atb.key"
case searchRetentionAtb = "statistics.retentionatb.key"
case appRetentionAtb = "statistics.appretentionatb.key"
case variant = "statistics.variant.key"
case lastAppRetentionRequestDate = "statistics.appretentionatb.last.request.key"

}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Configuration/ConfigurationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ final class ConfigurationManager {

if case .failure(let error) = completion {
os_log("Failed to complete configuration update %s", log: .config, type: .error, error.localizedDescription)
Pixel.fire(.debug(event: .configurationFetchError, error: error, countedBy: .counter))
Pixel.fire(.debug(event: .configurationFetchError, error: error))

tryAgainSoon()
} else {
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/Smarter Encryption/Store/HTTPSUpgradeStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ final class HTTPSUpgradePersistence: HTTPSUpgradeStore {
do {
try context.save()
} catch {
Pixel.fire(.debug(event: .dbSaveBloomFilterError, error: error, countedBy: .counter))
Pixel.fire(.debug(event: .dbSaveBloomFilterError, error: error))
}
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ final class HTTPSUpgradePersistence: HTTPSUpgradeStore {
do {
try context.save()
} catch {
Pixel.fire(.debug(event: .dbSaveExcludedHTTPSDomainsError, error: error, countedBy: .counter))
Pixel.fire(.debug(event: .dbSaveExcludedHTTPSDomainsError, error: error))
result = false
}
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Statistics/ATB/AtbAndVariantCleanup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Foundation

public class AtbAndVariantCleanup {

static func cleanup(statisticsStorage: StatisticsStore = StatisticsUserDefaults(),
static func cleanup(statisticsStorage: StatisticsStore = LocalStatisticsStore(),
variantManager: VariantManager = DefaultVariantManager()) {

guard let variant = statisticsStorage.variant else { return }
Expand Down
161 changes: 161 additions & 0 deletions DuckDuckGo/Statistics/ATB/LocalStatisticsStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//
// LocalStatisticsStore.swift
// DuckDuckGo
//
// Copyright © 2017 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

final class LocalStatisticsStore: StatisticsStore {

private struct LegacyStatisticsStore {
@UserDefaultsWrapper(key: .atb, defaultValue: nil)
var atb: String?

@UserDefaultsWrapper(key: .installDate, defaultValue: nil)
var installDate: Date?

@UserDefaultsWrapper(key: .searchRetentionAtb, defaultValue: nil)
var searchRetentionAtb: String?

@UserDefaultsWrapper(key: .appRetentionAtb, defaultValue: nil)
var appRetentionAtb: String?

@UserDefaultsWrapper(key: .lastAppRetentionRequestDate, defaultValue: nil)
var lastAppRetentionRequestDate: Date?

mutating func clear() {
atb = nil
installDate = nil
searchRetentionAtb = nil
appRetentionAtb = nil
lastAppRetentionRequestDate = nil
}
}

private struct Keys {
static let installDate = "statistics.installdate.key"
static let atb = "statistics.atb.key"
static let searchRetentionAtb = "statistics.retentionatb.key"
static let appRetentionAtb = "statistics.appretentionatb.key"
static let variant = "statistics.variant.key"
static let lastAppRetentionRequestDate = "statistics.appretentionatb.last.request.key"
}

private let pixelDataStore: PixelDataStore

init(pixelDataStore: PixelDataStore = LocalPixelDataStore.shared) {
self.pixelDataStore = pixelDataStore

var legacyStatisticsStore = LegacyStatisticsStore()
if let atb = legacyStatisticsStore.atb {
self.atb = atb
self.installDate = legacyStatisticsStore.installDate
self.searchRetentionAtb = legacyStatisticsStore.searchRetentionAtb
self.appRetentionAtb = legacyStatisticsStore.appRetentionAtb
self.lastAppRetentionRequestDate = legacyStatisticsStore.lastAppRetentionRequestDate

legacyStatisticsStore.clear()
}
}

var hasInstallStatistics: Bool {
return atb != nil
}

var atb: String? {
get {
pixelDataStore.value(forKey: Keys.atb)
}
set {
if let value = newValue {
pixelDataStore.set(value, forKey: Keys.atb)
} else {
assertionFailure("Unexpected ATB removal")
pixelDataStore.removeValue(forKey: Keys.atb)
}
}
}

var installDate: Date? {
get {
guard let timeInterval: Double = pixelDataStore.value(forKey: Keys.installDate) else { return nil }
return Date(timeIntervalSinceReferenceDate: timeInterval)
}
set {
if let value = newValue {
pixelDataStore.set(value.timeIntervalSinceReferenceDate, forKey: Keys.installDate)
} else {
assertionFailure("Unexpected ATB installDate removal")
pixelDataStore.removeValue(forKey: Keys.installDate)
}
}
}

var searchRetentionAtb: String? {
get {
pixelDataStore.value(forKey: Keys.searchRetentionAtb)
}
set {
if let value = newValue {
pixelDataStore.set(value, forKey: Keys.searchRetentionAtb)
} else {
pixelDataStore.removeValue(forKey: Keys.searchRetentionAtb)
}
}
}

var appRetentionAtb: String? {
get {
pixelDataStore.value(forKey: Keys.appRetentionAtb)
}
set {
if let value = newValue {
pixelDataStore.set(value, forKey: Keys.appRetentionAtb)
} else {
pixelDataStore.removeValue(forKey: Keys.appRetentionAtb)
}
}
}

var variant: String? {
get {
pixelDataStore.value(forKey: Keys.variant)
}
set {
if let value = newValue {
pixelDataStore.set(value, forKey: Keys.variant)
} else {
pixelDataStore.removeValue(forKey: Keys.variant)
}
}
}

var lastAppRetentionRequestDate: Date? {
get {
guard let timeInterval: Double = pixelDataStore.value(forKey: Keys.lastAppRetentionRequestDate) else { return nil }
return Date(timeIntervalSinceReferenceDate: timeInterval)
}
set {
if let value = newValue {
pixelDataStore.set(value.timeIntervalSinceReferenceDate, forKey: Keys.lastAppRetentionRequestDate)
} else {
pixelDataStore.removeValue(forKey: Keys.lastAppRetentionRequestDate)
}
}
}

}
2 changes: 1 addition & 1 deletion DuckDuckGo/Statistics/ATB/StatisticsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class StatisticsLoader {
private let parser = AtbParser()
private var isAppRetentionRequestInProgress = false

init(statisticsStore: StatisticsStore = StatisticsUserDefaults()) {
init(statisticsStore: StatisticsStore = LocalStatisticsStore()) {
self.statisticsStore = statisticsStore
}

Expand Down
49 changes: 0 additions & 49 deletions DuckDuckGo/Statistics/ATB/StatisticsUserDefaults.swift

This file was deleted.

2 changes: 1 addition & 1 deletion DuckDuckGo/Statistics/ATB/VariantManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ final class DefaultVariantManager: VariantManager {
private let rng: VariantRNG

init(variants: [Variant] = Variant.defaultVariants,
storage: StatisticsStore = StatisticsUserDefaults(),
storage: StatisticsStore = LocalStatisticsStore(),
rng: VariantRNG = Arc4RandomUniformVariantRNG()) {
self.variants = variants
self.storage = storage
Expand Down
Loading