Make ignoredTypes a static property to avoid redundant lookups

This commit is contained in:
Nate Weaver 2020-01-31 16:09:01 -06:00
parent f65bf63bb1
commit 101e140233
2 changed files with 23 additions and 18 deletions

View File

@ -60,6 +60,8 @@ final class FaviconDownloader {
loadHomePageToFaviconURLCache()
loadHomePageURLsWithNoFaviconURLCache()
FaviconURLFinder.ignoredTypes = [kUTTypeScalableVectorGraphics as String]
NotificationCenter.default.addObserver(self, selector: #selector(didLoadFavicon(_:)), name: .DidLoadFavicon, object: nil)
}
@ -208,9 +210,7 @@ private extension FaviconDownloader {
return
}
let ignoredTypes = [kUTTypeScalableVectorGraphics as String]
FaviconURLFinder.findFaviconURLs(with: homePageURL, ignoredTypes: ignoredTypes) { (faviconURLs) in
FaviconURLFinder.findFaviconURLs(with: homePageURL) { (faviconURLs) in
var defaultFaviconURL: String? = nil
if let scheme = url.scheme, let host = url.host {

View File

@ -13,23 +13,15 @@ import RSParser
struct FaviconURLFinder {
/// Finds favicon URLs in a web page.
/// - Parameters:
/// - homePageURL: The page to search.
/// - ignoredTypes: An array of uniform type identifiers to ignore.
/// - completion: A closure called when the links have been found.
/// - urls: An array of favicon URLs as strings.
static func findFaviconURLs(with homePageURL: String, ignoredTypes ignoredTypes: [String]? = nil, _ completion: @escaping (_ urls:[String]?) -> Void) {
private static var ignoredMimeTypes = [String]()
private static var ignoredExtensions = [String]()
guard let _ = URL(string: homePageURL) else {
completion(nil)
return
}
static var ignoredTypes: [String]? {
didSet {
guard let ignoredTypes = ignoredTypes else {
return
}
var ignoredMimeTypes = [String]()
var ignoredExtensions = [String]()
if let ignoredTypes = ignoredTypes {
for type in ignoredTypes {
if let mimeTypes = UTTypeCopyAllTagsWithClass(type as CFString, kUTTagClassMIMEType)?.takeRetainedValue() {
ignoredMimeTypes.append(contentsOf: mimeTypes as! [String])
@ -39,6 +31,19 @@ struct FaviconURLFinder {
}
}
}
}
/// Finds favicon URLs in a web page.
/// - Parameters:
/// - homePageURL: The page to search.
/// - completion: A closure called when the links have been found.
/// - urls: An array of favicon URLs as strings.
static func findFaviconURLs(with homePageURL: String, _ completion: @escaping (_ urls:[String]?) -> Void) {
guard let _ = URL(string: homePageURL) else {
completion(nil)
return
}
// If the favicon has an explicit type, check that for an ignored type; otherwise, check the file extension.
HTMLMetadataDownloader.downloadMetadata(for: homePageURL) { (htmlMetadata) in