Fix deprecation warnings.

This commit is contained in:
Brent Simmons
2024-11-22 21:12:53 -08:00
parent 2936922269
commit e1df391278
2 changed files with 30 additions and 37 deletions

View File

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

View File

@@ -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
}
}