From e1df3912783d5e5eaac26ce6417320171238f5d5 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 22 Nov 2024 21:12:53 -0800 Subject: [PATCH] Fix deprecation warnings. --- Shared/Favicons/FaviconDownloader.swift | 2 - Shared/Favicons/FaviconURLFinder.swift | 65 ++++++++++++------------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index 3f8b85d30..bc0712e5f 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -61,8 +61,6 @@ final class FaviconDownloader { loadHomePageToFaviconURLCache() loadHomePageURLsWithNoFaviconURLCache() - FaviconURLFinder.ignoredTypes = [kUTTypeScalableVectorGraphics as String] - NotificationCenter.default.addObserver(self, selector: #selector(didLoadFavicon(_:)), name: .DidLoadFavicon, object: nil) } diff --git a/Shared/Favicons/FaviconURLFinder.swift b/Shared/Favicons/FaviconURLFinder.swift index dd51da4a5..c90b3ad4a 100644 --- a/Shared/Favicons/FaviconURLFinder.swift +++ b/Shared/Favicons/FaviconURLFinder.swift @@ -9,32 +9,12 @@ import Foundation import CoreServices import RSParser +import UniformTypeIdentifiers // The favicon URLs may be specified in the head section of the home page. struct FaviconURLFinder { - private static var ignoredMimeTypes = [String]() - private static var ignoredExtensions = [String]() - - /// Uniform types to ignore when finding favicon URLs. - static var ignoredTypes: [String]? { - didSet { - guard let ignoredTypes = ignoredTypes else { - return - } - - for type in ignoredTypes { - if let mimeTypes = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassMIMEType)?.takeRetainedValue() { - ignoredMimeTypes.append(contentsOf: mimeTypes as! [String]) - } - if let extensions = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassFilenameExtension)?.takeRetainedValue() { - ignoredExtensions.append(contentsOf: extensions as! [String]) - } - } - } - } - /// Finds favicon URLs in a web page. /// - Parameters: /// - homePageURL: The page to search. @@ -49,23 +29,38 @@ struct FaviconURLFinder { // If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension. HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (htmlMetadata) in - let faviconURLs = htmlMetadata?.favicons.compactMap({ (favicon) -> String? in - if let type = favicon.type { - if ignoredMimeTypes.contains(type) { - return nil - } - } - else { - if let urlString = favicon.urlString, let url = URL(string: urlString), ignoredExtensions.contains(url.pathExtension) { - return nil - } - } - return favicon.urlString - }) + guard let favicons = htmlMetadata?.favicons else { + completion(nil) + return + } + + let faviconURLs = favicons.compactMap { + shouldAllowFavicon($0) ? $0.urlString : nil + } completion(faviconURLs) } } -} + private static let ignoredTypes = [UTType.svg] + + private static func shouldAllowFavicon(_ favicon: RSHTMLMetadataFavicon) -> Bool { + + // Check mime type. + if let mimeType = favicon.type, let utType = UTType(mimeType: mimeType) { + if Self.ignoredTypes.contains(utType) { + return false + } + } + + // Check file extension. + if let urlString = favicon.urlString, let url = URL(string: urlString), let utType = UTType(filenameExtension: url.pathExtension) { + if Self.ignoredTypes.contains(utType) { + return false + } + } + + return true + } +}