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

remove widget favicon coupling and move favicon code from core to the app #3590

Merged
merged 4 commits into from
Nov 21, 2024
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
4 changes: 2 additions & 2 deletions Core/CookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class CookieStorage {

/// Update ALL cookies. The absence of cookie domains here indicateds they have been removed by the website, so be sure to call this with all cookies that might need to be persisted even if those websites have not been visited yet.
@discardableResult
func updateCookies(_ cookies: [HTTPCookie], keepingPreservedLogins preservedLogins: PreserveLogins) -> CookieDomainsOnUpdateDiagnostic {
func updateCookies(_ cookies: [HTTPCookie], preservingFireproofedDomains fireproofing: Fireproofing) -> CookieDomainsOnUpdateDiagnostic {
guard isConsumed else { return .notConsumed }

isConsumed = false
Expand Down Expand Up @@ -130,7 +130,7 @@ public class CookieStorage {
persistedCookiesByDomain.keys.forEach {
guard !URL.isDuckDuckGo(domain: $0) else { return } // DDG cookies are for SERP settings only

if !preservedLogins.isAllowed(cookieDomain: $0) {
if !fireproofing.isAllowed(cookieDomain: $0) {
persistedCookiesByDomain.removeValue(forKey: $0)
}
}
Expand Down
31 changes: 31 additions & 0 deletions Core/FaviconHasher.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// FaviconHasher.swift
// DuckDuckGo
//
// Copyright © 2024 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 Kingfisher

public struct FaviconHasher {

static let salt = "DDGSalt:"
public static func createHash(ofDomain domain: String) -> String {
return "\(Self.salt)\(domain)".sha256()
}

}
1 change: 1 addition & 0 deletions Core/FaviconRequestModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// limitations under the License.
//

import Core
import Kingfisher

class FaviconRequestModifier: ImageDownloadRequestModifier {
Expand Down
43 changes: 43 additions & 0 deletions Core/FaviconsCacheType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// FaviconsCacheType.swift
// DuckDuckGo
//
// Copyright © 2024 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

public enum FaviconsCacheType: String {

static let faviconsFolderName = "Favicons"

case tabs
case fireproof

public func cacheLocation() -> URL? {
return baseCacheURL()?.appendingPathComponent(Self.faviconsFolderName)
}

private func baseCacheURL() -> URL? {
switch self {
case .fireproof:
let groupName = BookmarksDatabase.Constants.bookmarksGroupID
return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupName)

case .tabs:
return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first
}
}
}
34 changes: 0 additions & 34 deletions Core/FaviconsHelper.swift

This file was deleted.

26 changes: 20 additions & 6 deletions Core/PreserveLogins.swift → Core/Fireproofing.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// PreserveLogins.swift
// Fireproofing.swift
// Core
//
// Copyright © 2020 DuckDuckGo. All rights reserved.
Expand All @@ -19,18 +19,32 @@

import Foundation

public class PreserveLogins {
public protocol Fireproofing {

var loginDetectionEnabled: Bool { get set }
var allowedDomains: [String] { get }

func isAllowed(cookieDomain: String) -> Bool
func isAllowed(fireproofDomain domain: String) -> Bool
func addToAllowed(domain: String)
func remove(domain: String)
func clearAll()

}

// This class is not final because we override allowed domains in WebCacheManagerTests
public class UserDefaultsFireproofing: Fireproofing {

public static let shared: Fireproofing = UserDefaultsFireproofing()

public struct Notifications {
public static let loginDetectionStateChanged = Foundation.Notification.Name("com.duckduckgo.ios.PreserveLogins.loginDetectionStateChanged")
}

public static let shared = PreserveLogins()

@UserDefaultsWrapper(key: .preserveLoginsAllowedDomains, defaultValue: [])
@UserDefaultsWrapper(key: .fireproofingAllowedDomains, defaultValue: [])
private(set) public var allowedDomains: [String]

@UserDefaultsWrapper(key: .preserveLoginsDetectionEnabled, defaultValue: false)
@UserDefaultsWrapper(key: .fireproofingDetectionEnabled, defaultValue: false)
public var loginDetectionEnabled: Bool {
didSet {
NotificationCenter.default.post(name: Notifications.loginDetectionStateChanged, object: nil)
Expand Down
1 change: 1 addition & 0 deletions Core/NotFoundCachingDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// limitations under the License.
//

import Core
import Kingfisher

class NotFoundCachingDownloader: ImageDownloader {
Expand Down
8 changes: 6 additions & 2 deletions Core/SyncBookmarksAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
public let databaseCleaner: BookmarkDatabaseCleaner
public let syncDidCompletePublisher: AnyPublisher<Void, Never>
let syncErrorHandler: SyncErrorHandling
private let faviconStoring: FaviconStoring

@UserDefaultsWrapper(key: .syncDidMigrateToImprovedListsHandling, defaultValue: false)
private var didMigrateToImprovedListsHandling: Bool
Expand All @@ -88,10 +89,13 @@

public init(database: CoreDataDatabase,
favoritesDisplayModeStorage: FavoritesDisplayModeStoring,
syncErrorHandler: SyncErrorHandling) {
syncErrorHandler: SyncErrorHandling,
faviconStoring: FaviconStoring) {
self.database = database
self.favoritesDisplayModeStorage = favoritesDisplayModeStorage
self.syncErrorHandler = syncErrorHandler
self.faviconStoring = faviconStoring

syncDidCompletePublisher = syncDidCompleteSubject.eraseToAnyPublisher()
databaseCleaner = BookmarkDatabaseCleaner(
bookmarkDatabase: database,
Expand Down Expand Up @@ -172,7 +176,7 @@
database: database,
stateStore: stateStore,
fetcher: FaviconFetcher(),
faviconStore: Favicons.shared,
faviconStore: faviconStoring,
errorEvents: BookmarksFaviconsFetcherErrorHandler()
)
}
Expand All @@ -192,7 +196,7 @@
Logger.sync.debug("Favicons Fetching is in progress. Starting background task to allow it to gracefully complete.")

var taskID: UIBackgroundTaskIdentifier!
taskID = application.beginBackgroundTask(withName: "Cancelled Favicons Fetching Completion Task") {

Check warning on line 199 in Core/SyncBookmarksAdapter.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

'taskID' mutated after capture by sendable closure

Check warning on line 199 in Core/SyncBookmarksAdapter.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

'taskID' mutated after capture by sendable closure
Logger.sync.debug("Forcing background task completion")
application.endBackgroundTask(taskID)
}
Expand Down
6 changes: 4 additions & 2 deletions Core/SyncDataProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ public class SyncDataProviders: DataProvidersSource {
secureVaultErrorReporter: SecureVaultReporting,
settingHandlers: [SettingSyncHandler],
favoritesDisplayModeStorage: FavoritesDisplayModeStoring,
syncErrorHandler: SyncErrorHandling
syncErrorHandler: SyncErrorHandling,
faviconStoring: FaviconStoring
) {
self.bookmarksDatabase = bookmarksDatabase
self.secureVaultFactory = secureVaultFactory
self.secureVaultErrorReporter = secureVaultErrorReporter
bookmarksAdapter = SyncBookmarksAdapter(database: bookmarksDatabase,
favoritesDisplayModeStorage: favoritesDisplayModeStorage,
syncErrorHandler: syncErrorHandler)
syncErrorHandler: syncErrorHandler,
faviconStoring: faviconStoring)
credentialsAdapter = SyncCredentialsAdapter(secureVaultFactory: secureVaultFactory,
secureVaultErrorReporter: secureVaultErrorReporter,
syncErrorHandler: syncErrorHandler)
Expand Down
6 changes: 3 additions & 3 deletions Core/UserDefaultsPropertyWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public struct UserDefaultsWrapper<T> {
case gridViewEnabled = "com.duckduckgo.ios.tabs.grid"
case gridViewSeen = "com.duckduckgo.ios.tabs.seen"

case preserveLoginsAllowedDomains = "com.duckduckgo.ios.PreserveLogins.userDecision.allowedDomains2"
case preserveLoginsDetectionEnabled = "com.duckduckgo.ios.PreserveLogins.detectionEnabled"
case preserveLoginsLegacyAllowedDomains = "com.duckduckgo.ios.PreserveLogins.userDecision.allowedDomains"
case fireproofingAllowedDomains = "com.duckduckgo.ios.PreserveLogins.userDecision.allowedDomains2"
case fireproofingDetectionEnabled = "com.duckduckgo.ios.PreserveLogins.detectionEnabled"
case fireproofingLegacyAllowedDomains = "com.duckduckgo.ios.PreserveLogins.userDecision.allowedDomains"

case daxIsDismissed = "com.duckduckgo.ios.daxOnboardingIsDismissed"
case daxHomeScreenMessagesSeen = "com.duckduckgo.ios.daxOnboardingHomeScreenMessagesSeen"
Expand Down
4 changes: 2 additions & 2 deletions Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class WebCacheManager {
}

public func clear(cookieStorage: CookieStorage = CookieStorage(),
logins: PreserveLogins = PreserveLogins.shared,
fireproofing: Fireproofing = UserDefaultsFireproofing.shared,
dataStoreIdManager: DataStoreIdManaging = DataStoreIdManager.shared) async {

var cookiesToUpdate = [HTTPCookie]()
Expand All @@ -92,7 +92,7 @@ public class WebCacheManager {
// Perform legacy clearing to migrate to new container
cookiesToUpdate += await legacyDataClearing() ?? []

cookieStorage.updateCookies(cookiesToUpdate, keepingPreservedLogins: logins)
cookieStorage.updateCookies(cookiesToUpdate, preservingFireproofedDomains: fireproofing)

// Attempt to clean up leftover stores again after a delay
// This should not be a problem as these containers are not supposed to be used anymore.
Expand Down
Loading
Loading