Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into graeme/fix-geoswitchi…
Browse files Browse the repository at this point in the history
…ng-bug
  • Loading branch information
graeme committed Nov 24, 2023
2 parents 770ca09 + 5b84e6d commit 654d533
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 182 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ let package = Package(
.package(url: "https://github.com/duckduckgo/TrackerRadarKit", exact: "1.2.1"),
.package(url: "https://github.com/duckduckgo/sync_crypto", exact: "0.2.0"),
.package(url: "https://github.com/gumob/PunycodeSwift.git", exact: "2.1.0"),
.package(url: "https://github.com/duckduckgo/privacy-dashboard", exact: "3.1.0" ),
.package(url: "https://github.com/duckduckgo/content-scope-scripts", exact: "4.52.0"),
.package(url: "https://github.com/duckduckgo/privacy-dashboard", exact: "2.0.0"),
.package(url: "https://github.com/httpswift/swifter.git", exact: "1.5.0"),
.package(url: "https://github.com/duckduckgo/bloom_cpp.git", exact: "3.0.0"),
.package(url: "https://github.com/duckduckgo/wireguard-apple", exact: "1.1.1")
Expand Down
32 changes: 0 additions & 32 deletions Sources/Bookmarks/BookmarkUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,6 @@ public struct BookmarkUtils {
}
}

public static func migrateToFormFactorSpecificFavorites(byCopyingExistingTo folderID: FavoritesFolderID, in context: NSManagedObjectContext) {
assert(folderID != .unified, "You must specify either desktop or mobile folder")

guard let favoritesFolder = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.unified.rawValue, in: context) else {
return
}

if BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.desktop.rawValue, in: context) == nil {
let desktopFavoritesFolder = insertRootFolder(uuid: FavoritesFolderID.desktop.rawValue, into: context)

if folderID == .desktop {
favoritesFolder.favoritesArray.forEach { bookmark in
bookmark.addToFavorites(favoritesRoot: desktopFavoritesFolder)
}
} else {
desktopFavoritesFolder.shouldManageModifiedAt = false
}
}

if BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.mobile.rawValue, in: context) == nil {
let mobileFavoritesFolder = insertRootFolder(uuid: FavoritesFolderID.mobile.rawValue, into: context)

if folderID == .mobile {
favoritesFolder.favoritesArray.forEach { bookmark in
bookmark.addToFavorites(favoritesRoot: mobileFavoritesFolder)
}
} else {
mobileFavoritesFolder.shouldManageModifiedAt = false
}
}
}

public static func copyFavorites(
from sourceFolderID: FavoritesFolderID,
to targetFolderID: FavoritesFolderID,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// 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 Foundation
import CoreData
import Persistence
import Common

public class BookmarkFormFactorFavoritesMigration {

public enum MigrationErrors {
case couldNotLoadDatabase
}

public static func getFavoritesOrderFromPreV4Model(dbContainerLocation: URL,
dbFileURL: URL,
errorEvents: EventMapping<MigrationErrors>? = nil) -> [String]? {
var oldFavoritesOrder: [String]?

let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType,
at: dbFileURL)
if let metadata, let version = metadata["NSStoreModelVersionHashesVersion"] as? Int, version < 4 {
// Before migrating to latest scheme version, read order of favorites from DB

let v3BookmarksModel = NSManagedObjectModel.mergedModel(from: [Bookmarks.bundle], forStoreMetadata: metadata)!

let oldDB = CoreDataDatabase(name: "Bookmarks",
containerLocation: dbContainerLocation,
model: v3BookmarksModel)
oldDB.loadStore { context, error in
guard let context = context else {
errorEvents?.fire(.couldNotLoadDatabase, error: error)
return
}

let favs = BookmarkUtils.fetchLegacyFavoritesFolder(context)
oldFavoritesOrder = favs?.favoritesArray.compactMap { $0.uuid }
}
}
return oldFavoritesOrder
}

public static func migrateToFormFactorSpecificFavorites(byCopyingExistingTo folderID: FavoritesFolderID,
preservingOrderOf orderedUUIDs: [String]?,
in context: NSManagedObjectContext) {
assert(folderID != .unified, "You must specify either desktop or mobile folder")

guard let favoritesFolder = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.unified.rawValue, in: context) else {
return
}

if let orderedUUIDs {
// Fix order of favorites
let favorites = favoritesFolder.favoritesArray

for fav in favorites {
fav.removeFromFavorites(favoritesRoot: favoritesFolder)
}

for uuid in orderedUUIDs {
if let fav = favorites.first(where: { $0.uuid == uuid}) {
fav.addToFavorites(favoritesRoot: favoritesFolder)
}
}
}

if BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.desktop.rawValue, in: context) == nil {
let desktopFavoritesFolder = BookmarkUtils.insertRootFolder(uuid: FavoritesFolderID.desktop.rawValue, into: context)

if folderID == .desktop {
favoritesFolder.favoritesArray.forEach { bookmark in
bookmark.addToFavorites(favoritesRoot: desktopFavoritesFolder)
}
} else {
desktopFavoritesFolder.shouldManageModifiedAt = false
}
}

if BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.mobile.rawValue, in: context) == nil {
let mobileFavoritesFolder = BookmarkUtils.insertRootFolder(uuid: FavoritesFolderID.mobile.rawValue, into: context)

if folderID == .mobile {
favoritesFolder.favoritesArray.forEach { bookmark in
bookmark.addToFavorites(favoritesRoot: mobileFavoritesFolder)
}
} else {
mobileFavoritesFolder.shouldManageModifiedAt = false
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/PrivacyDashboard/Model/CookieConsentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct CookieConsentInfo: Encodable {
let optoutFailed: Bool?
let selftestFailed: Bool?
let configurable = true

public init(consentManaged: Bool, cosmetic: Bool?, optoutFailed: Bool?, selftestFailed: Bool?) {
self.consentManaged = consentManaged
self.cosmetic = cosmetic
Expand Down
2 changes: 1 addition & 1 deletion Sources/PrivacyDashboard/Model/ProtectionStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import Foundation

public struct ProtectionStatus: Encodable {

let unprotectedTemporary: Bool
let enabledFeatures: [String]
let allowlisted: Bool
Expand Down
10 changes: 5 additions & 5 deletions Sources/PrivacyDashboard/Model/TrackerInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public struct TrackerInfo: Encodable {
case requests
case installedSurrogates
}

public private (set) var trackers = Set<DetectedRequest>()
private(set) var thirdPartyRequests = Set<DetectedRequest>()
public private(set) var installedSurrogates = Set<String>()

public init() { }

// MARK: - Collecting detected elements
Expand All @@ -43,12 +43,12 @@ public struct TrackerInfo: Encodable {
public mutating func add(detectedThirdPartyRequest request: DetectedRequest) {
thirdPartyRequests.insert(request)
}

public mutating func addInstalledSurrogateHost(_ host: String, for tracker: DetectedRequest, onPageWithURL url: URL) {
guard tracker.pageUrl == url.absoluteString else { return }
installedSurrogates.insert(host)
}

// MARK: - Helper accessors

public var trackersBlocked: [DetectedRequest] {
Expand All @@ -67,5 +67,5 @@ public struct TrackerInfo: Encodable {
try container.encode(allRequests, forKey: .requests)
try container.encode(installedSurrogates, forKey: .installedSurrogates)
}

}
Loading

0 comments on commit 654d533

Please sign in to comment.