Skip to content

Commit

Permalink
first step in decoupling widgets and favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Nov 18, 2024
1 parent 52b2b79 commit d7cc5ae
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 120 deletions.
7 changes: 5 additions & 2 deletions Core/FaviconsHelper.swift → Core/CoreFaviconsHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@
import Foundation
import Kingfisher

struct FaviconsHelper {
struct CoreFaviconsHelper {

// this function is now static and outside of Favicons, otherwise there is a circular dependency between
// Favicons and NotFoundCachingDownloader
public static func defaultResource(forDomain domain: String?, sourcesProvider: FaviconSourcesProvider) -> KF.ImageResource? {
guard let domain = domain,
let source = sourcesProvider.mainSource(forDomain: domain) else { return nil }

let key = Favicons.createHash(ofDomain: domain)
let key = FaviconHasher.createHash(ofDomain: domain)
return KF.ImageResource(downloadURL: source, cacheKey: key)
}

}


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()
}

}
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
}
}
}
4 changes: 2 additions & 2 deletions Core/NotFoundCachingDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NotFoundCachingDownloader: ImageDownloader {
}

func noFaviconsFound(forDomain domain: String) {
guard let hashedKey = FaviconsHelper.defaultResource(forDomain: domain, sourcesProvider: sourcesProvider)?.cacheKey else { return }
guard let hashedKey = CoreFaviconsHelper.defaultResource(forDomain: domain, sourcesProvider: sourcesProvider)?.cacheKey else { return }
notFoundCache[hashedKey] = Date().timeIntervalSince1970
}

Expand All @@ -56,7 +56,7 @@ class NotFoundCachingDownloader: ImageDownloader {
}

func shouldDownload(forDomain domain: String, referenceDate: Date = Date()) -> Bool {
guard let hashedKey = FaviconsHelper.defaultResource(forDomain: domain, sourcesProvider: sourcesProvider)?.cacheKey else { return false }
guard let hashedKey = CoreFaviconsHelper.defaultResource(forDomain: domain, sourcesProvider: sourcesProvider)?.cacheKey else { return false }
if let cacheAddTime = notFoundCache[hashedKey],
referenceDate.timeIntervalSince1970 - cacheAddTime < Self.expiry {
return false
Expand Down
16 changes: 12 additions & 4 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@
8590CB67268A2E520089F6BF /* RootDebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8590CB66268A2E520089F6BF /* RootDebugViewController.swift */; };
8590CB69268A4E190089F6BF /* DebugEtagStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8590CB68268A4E190089F6BF /* DebugEtagStorage.swift */; };
8596C30D2B7EB1800058EF90 /* DataStoreWarmup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8596C30C2B7EB1800058EF90 /* DataStoreWarmup.swift */; };
8598D2DC2CEB93AD00C45685 /* FaviconsCacheType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8598D2DB2CEB93A600C45685 /* FaviconsCacheType.swift */; };
8598D2DE2CEB97BE00C45685 /* FaviconHasher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8598D2DD2CEB97BE00C45685 /* FaviconHasher.swift */; };
8598F67B2405EB8D00FBC70C /* KeyboardSettingsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8598F6792405EB8600FBC70C /* KeyboardSettingsTests.swift */; };
8599690F29D2F1C100DBF9FA /* DDGSync in Frameworks */ = {isa = PBXBuildFile; productRef = 8599690E29D2F1C100DBF9FA /* DDGSync */; };
859DB8132CE6263C001F7210 /* TextZoomStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 859DB80D2CE6263B001F7210 /* TextZoomStorage.swift */; };
Expand Down Expand Up @@ -925,7 +927,7 @@
C1BF0BA529B63D7200482B73 /* AutofillLoginPromptHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1BF0BA429B63D7200482B73 /* AutofillLoginPromptHelper.swift */; };
C1BF0BA929B63E2200482B73 /* AutofillLoginPromptViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1BF0BA729B63E1A00482B73 /* AutofillLoginPromptViewModelTests.swift */; };
C1BF26152C74D10F00F6405E /* SyncPromoManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1BF26142C74D10F00F6405E /* SyncPromoManager.swift */; };
C1CCCBA7283E101500CF3791 /* FaviconsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1CCCBA6283E101500CF3791 /* FaviconsHelper.swift */; };
C1CCCBA7283E101500CF3791 /* CoreFaviconsHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1CCCBA6283E101500CF3791 /* CoreFaviconsHelper.swift */; };
C1CDA3162AFB9C7F006D1476 /* AutofillNeverPromptWebsitesManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1CDA3152AFB9C7F006D1476 /* AutofillNeverPromptWebsitesManager.swift */; };
C1CDA31E2AFBF811006D1476 /* AutofillNeverPromptWebsitesManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1CDA31D2AFBF811006D1476 /* AutofillNeverPromptWebsitesManagerTests.swift */; };
C1D21E2D293A5965006E5A05 /* AutofillLoginSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1D21E2C293A5965006E5A05 /* AutofillLoginSession.swift */; };
Expand Down Expand Up @@ -1836,6 +1838,8 @@
8590CB66268A2E520089F6BF /* RootDebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootDebugViewController.swift; sourceTree = "<group>"; };
8590CB68268A4E190089F6BF /* DebugEtagStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugEtagStorage.swift; sourceTree = "<group>"; };
8596C30C2B7EB1800058EF90 /* DataStoreWarmup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreWarmup.swift; sourceTree = "<group>"; };
8598D2DB2CEB93A600C45685 /* FaviconsCacheType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FaviconsCacheType.swift; sourceTree = "<group>"; };
8598D2DD2CEB97BE00C45685 /* FaviconHasher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FaviconHasher.swift; sourceTree = "<group>"; };
8598F6792405EB8600FBC70C /* KeyboardSettingsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyboardSettingsTests.swift; sourceTree = "<group>"; };
859DB80D2CE6263B001F7210 /* TextZoomStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextZoomStorage.swift; sourceTree = "<group>"; };
859DB80E2CE6263B001F7210 /* TextZoomController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextZoomController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2735,7 +2739,7 @@
C1BF0BA429B63D7200482B73 /* AutofillLoginPromptHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutofillLoginPromptHelper.swift; sourceTree = "<group>"; };
C1BF0BA729B63E1A00482B73 /* AutofillLoginPromptViewModelTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutofillLoginPromptViewModelTests.swift; sourceTree = "<group>"; };
C1BF26142C74D10F00F6405E /* SyncPromoManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncPromoManager.swift; sourceTree = "<group>"; };
C1CCCBA6283E101500CF3791 /* FaviconsHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FaviconsHelper.swift; sourceTree = "<group>"; };
C1CCCBA6283E101500CF3791 /* CoreFaviconsHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreFaviconsHelper.swift; sourceTree = "<group>"; };
C1CDA3152AFB9C7F006D1476 /* AutofillNeverPromptWebsitesManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutofillNeverPromptWebsitesManager.swift; sourceTree = "<group>"; };
C1CDA31D2AFBF811006D1476 /* AutofillNeverPromptWebsitesManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutofillNeverPromptWebsitesManagerTests.swift; sourceTree = "<group>"; };
C1D21E2C293A5965006E5A05 /* AutofillLoginSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutofillLoginSession.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4575,7 +4579,9 @@
85CA53A724BB342B00A6288C /* Favicons */ = {
isa = PBXGroup;
children = (
C1CCCBA6283E101500CF3791 /* FaviconsHelper.swift */,
8598D2DB2CEB93A600C45685 /* FaviconsCacheType.swift */,
C1CCCBA6283E101500CF3791 /* CoreFaviconsHelper.swift */,
8598D2DD2CEB97BE00C45685 /* FaviconHasher.swift */,
85CA53A324B9F2BD00A6288C /* Favicons.swift */,
85CA53A924BB376800A6288C /* NotFoundCachingDownloader.swift */,
85CA53AB24BBD39300A6288C /* FaviconRequestModifier.swift */,
Expand Down Expand Up @@ -8338,7 +8344,7 @@
850559D023CF647C0055C0D5 /* PreserveLogins.swift in Sources */,
4B27FBAE2C924EC6007E21A7 /* PersistentPixelStoring.swift in Sources */,
6F7FB8E32C660BF300867DA7 /* DailyPixelFiring.swift in Sources */,
C1CCCBA7283E101500CF3791 /* FaviconsHelper.swift in Sources */,
C1CCCBA7283E101500CF3791 /* CoreFaviconsHelper.swift in Sources */,
9813F79822BA71AA00A80EDB /* StorageCache.swift in Sources */,
B603974929C19F6F00902A34 /* Assertions.swift in Sources */,
F1134EB51F40AEEA00B73467 /* StatisticsLoader.swift in Sources */,
Expand All @@ -8363,6 +8369,7 @@
C14882DA27F2011C00D59F0C /* BookmarksExporter.swift in Sources */,
854858E32937BC550063610B /* CollectionExtension.swift in Sources */,
85528AA72C7CA95D0017BCCA /* UsageSegmentationCalculator.swift in Sources */,
8598D2DC2CEB93AD00C45685 /* FaviconsCacheType.swift in Sources */,
1E6A4D692984208800A371D3 /* LocaleExtension.swift in Sources */,
98F6EA472863124100720957 /* ContentBlockerRulesLists.swift in Sources */,
566B73732BECE4F200FF1959 /* SyncErrorHandling.swift in Sources */,
Expand Down Expand Up @@ -8419,6 +8426,7 @@
9887DC252354D2AA005C85F5 /* Database.swift in Sources */,
F143C3171E4A99D200CFDE3A /* AppURLs.swift in Sources */,
C1963863283794A000298D4D /* BookmarksCachingSearch.swift in Sources */,
8598D2DE2CEB97BE00C45685 /* FaviconHasher.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/FaviconViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ final class FaviconViewModel {

private let domain: String
private let useFakeFavicon: Bool
private let cacheType: Favicons.CacheType
private let cacheType: FaviconsCacheType
private let preferredFaviconLetters: String?

internal init(domain: String,
useFakeFavicon: Bool = true,
cacheType: Favicons.CacheType = .tabs,
cacheType: FaviconsCacheType = .tabs,
preferredFakeFaviconLetters: String? = nil) {

self.domain = domain
Expand Down
Loading

0 comments on commit d7cc5ae

Please sign in to comment.