2017-11-20 08:59:04 +01:00
|
|
|
|
//
|
|
|
|
|
// FaviconDownloader.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2017-11-20 08:59:04 +01:00
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 11/19/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2019-04-12 00:53:03 +02:00
|
|
|
|
import Foundation
|
2020-02-01 08:09:36 +01:00
|
|
|
|
import CoreServices
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2018-07-28 21:16:14 +02:00
|
|
|
|
import Account
|
2017-11-20 08:59:04 +01:00
|
|
|
|
import RSCore
|
|
|
|
|
|
|
|
|
|
extension Notification.Name {
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
static let FaviconDidBecomeAvailable = Notification.Name("FaviconDidBecomeAvailableNotification") // userInfo key: FaviconDownloader.UserInfoKey.faviconURL
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final class FaviconDownloader {
|
|
|
|
|
|
2019-10-31 20:04:34 +01:00
|
|
|
|
private static let saveQueue = CoalescingQueue(name: "Cache Save Queue", interval: 1.0)
|
|
|
|
|
|
2017-11-20 08:59:04 +01:00
|
|
|
|
private let folder: String
|
2017-11-24 22:12:18 +01:00
|
|
|
|
private let diskCache: BinaryDiskCache
|
2017-12-03 06:27:25 +01:00
|
|
|
|
private var singleFaviconDownloaderCache = [String: SingleFaviconDownloader]() // faviconURL: SingleFaviconDownloader
|
2019-11-26 02:54:09 +01:00
|
|
|
|
private var remainingFaviconURLs = [String: ArraySlice<String>]() // homePageURL: array of faviconURLs that haven't been checked yet
|
2020-01-30 23:02:20 +01:00
|
|
|
|
private var currentHomePageHasOnlyFaviconICO = false
|
2019-11-26 02:54:09 +01:00
|
|
|
|
|
2017-12-03 06:27:25 +01:00
|
|
|
|
private var homePageToFaviconURLCache = [String: String]() //homePageURL: faviconURL
|
2019-10-31 20:04:34 +01:00
|
|
|
|
private var homePageToFaviconURLCachePath: String
|
|
|
|
|
private var homePageToFaviconURLCacheDirty = false {
|
|
|
|
|
didSet {
|
|
|
|
|
queueSaveHomePageToFaviconURLCacheIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var homePageURLsWithNoFaviconURLCache = Set<String>()
|
|
|
|
|
private var homePageURLsWithNoFaviconURLCachePath: String
|
|
|
|
|
private var homePageURLsWithNoFaviconURLCacheDirty = false {
|
|
|
|
|
didSet {
|
|
|
|
|
queueSaveHomePageURLsWithNoFaviconURLCacheIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 08:59:04 +01:00
|
|
|
|
private let queue: DispatchQueue
|
2024-11-02 05:34:08 +01:00
|
|
|
|
private var cache = [Feed: IconImage]() // faviconURL: RSImage
|
2017-11-20 08:59:04 +01:00
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
struct UserInfoKey {
|
2017-11-20 08:59:04 +01:00
|
|
|
|
static let faviconURL = "faviconURL"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init(folder: String) {
|
|
|
|
|
|
|
|
|
|
self.folder = folder
|
2017-11-24 22:12:18 +01:00
|
|
|
|
self.diskCache = BinaryDiskCache(folder: folder)
|
2017-11-23 23:15:28 +01:00
|
|
|
|
self.queue = DispatchQueue(label: "FaviconDownloader serial queue - \(folder)")
|
2017-11-24 22:12:18 +01:00
|
|
|
|
|
2019-10-31 20:04:34 +01:00
|
|
|
|
self.homePageToFaviconURLCachePath = (folder as NSString).appendingPathComponent("HomePageToFaviconURLCache.plist")
|
|
|
|
|
self.homePageURLsWithNoFaviconURLCachePath = (folder as NSString).appendingPathComponent("HomePageURLsWithNoFaviconURLCache.plist")
|
|
|
|
|
loadHomePageToFaviconURLCache()
|
|
|
|
|
loadHomePageURLsWithNoFaviconURLCache()
|
|
|
|
|
|
2020-01-31 23:09:01 +01:00
|
|
|
|
FaviconURLFinder.ignoredTypes = [kUTTypeScalableVectorGraphics as String]
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(didLoadFavicon(_:)), name: .DidLoadFavicon, object: nil)
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - API
|
|
|
|
|
|
2019-06-14 22:33:13 +02:00
|
|
|
|
func resetCache() {
|
2024-11-02 05:34:08 +01:00
|
|
|
|
cache = [Feed: IconImage]()
|
2019-06-14 22:33:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 19:08:58 +01:00
|
|
|
|
func favicon(for feed: Feed) -> IconImage? {
|
2017-11-20 08:59:04 +01:00
|
|
|
|
|
|
|
|
|
assert(Thread.isMainThread)
|
2017-11-24 19:45:22 +01:00
|
|
|
|
|
2024-11-02 19:08:58 +01:00
|
|
|
|
var homePageURL = feed.homePageURL
|
|
|
|
|
if let faviconURL = feed.faviconURL {
|
2019-11-26 02:54:09 +01:00
|
|
|
|
return favicon(with: faviconURL, homePageURL: homePageURL)
|
2017-11-24 19:45:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 04:45:12 +01:00
|
|
|
|
if homePageURL == nil {
|
|
|
|
|
// Base homePageURL off feedURL if needed. Won’t always be accurate, but is good enough.
|
2024-11-02 19:08:58 +01:00
|
|
|
|
if let feedURL = URL(string: feed.url), let scheme = feedURL.scheme, let host = feedURL.host {
|
2017-12-14 04:45:12 +01:00
|
|
|
|
homePageURL = scheme + "://" + host + "/"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let homePageURL = homePageURL {
|
|
|
|
|
return favicon(withHomePageURL: homePageURL)
|
2017-11-20 22:29:20 +01:00
|
|
|
|
}
|
2017-12-14 04:45:12 +01:00
|
|
|
|
|
|
|
|
|
return nil
|
2017-11-24 19:45:22 +01:00
|
|
|
|
}
|
2019-06-14 22:33:13 +02:00
|
|
|
|
|
2024-11-02 19:08:58 +01:00
|
|
|
|
func faviconAsIcon(for feed: Feed) -> IconImage? {
|
2019-06-14 22:33:13 +02:00
|
|
|
|
|
2024-11-02 19:08:58 +01:00
|
|
|
|
if let image = cache[feed] {
|
2019-06-14 22:33:13 +02:00
|
|
|
|
return image
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 19:08:58 +01:00
|
|
|
|
if let iconImage = favicon(for: feed), let imageData = iconImage.image.dataRepresentation() {
|
2019-11-06 01:05:57 +01:00
|
|
|
|
if let scaledImage = RSImage.scaledForIcon(imageData) {
|
|
|
|
|
let scaledIconImage = IconImage(scaledImage)
|
2024-11-02 19:08:58 +01:00
|
|
|
|
cache[feed] = scaledIconImage
|
2019-11-06 01:05:57 +01:00
|
|
|
|
return scaledIconImage
|
2019-06-14 22:33:13 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-11-24 19:45:22 +01:00
|
|
|
|
|
2019-11-27 21:08:52 +01:00
|
|
|
|
func favicon(with faviconURL: String, homePageURL: String?) -> IconImage? {
|
2019-11-26 02:54:09 +01:00
|
|
|
|
let downloader = faviconDownloader(withURL: faviconURL, homePageURL: homePageURL)
|
2019-11-06 01:05:57 +01:00
|
|
|
|
return downloader.iconImage
|
2017-11-24 19:45:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
|
func favicon(withHomePageURL homePageURL: String) -> IconImage? {
|
2017-11-20 22:29:20 +01:00
|
|
|
|
|
2020-01-17 03:09:18 +01:00
|
|
|
|
let url = homePageURL.normalizedURL
|
2019-12-07 23:08:57 +01:00
|
|
|
|
|
|
|
|
|
if let url = URL(string: homePageURL) {
|
2023-06-14 06:48:11 +02:00
|
|
|
|
if url.host == "nnw.ranchero.com" || url.host == "netnewswire.blog" {
|
2019-12-07 23:08:57 +01:00
|
|
|
|
return IconImage.appIcon
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 20:04:34 +01:00
|
|
|
|
if homePageURLsWithNoFaviconURLCache.contains(url) {
|
2017-11-24 22:12:18 +01:00
|
|
|
|
return nil
|
2017-11-24 19:45:22 +01:00
|
|
|
|
}
|
2019-11-26 02:54:09 +01:00
|
|
|
|
|
2017-12-03 06:27:25 +01:00
|
|
|
|
if let faviconURL = homePageToFaviconURLCache[url] {
|
2019-11-26 02:54:09 +01:00
|
|
|
|
return favicon(with: faviconURL, homePageURL: url)
|
2017-12-03 06:27:25 +01:00
|
|
|
|
}
|
2017-11-24 19:45:22 +01:00
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
findFaviconURLs(with: url) { (faviconURLs) in
|
|
|
|
|
if let faviconURLs = faviconURLs {
|
2020-02-15 15:22:59 +01:00
|
|
|
|
// If the site explicitly specifies favicon.ico, it will appear twice.
|
2020-01-30 23:02:20 +01:00
|
|
|
|
self.currentHomePageHasOnlyFaviconICO = faviconURLs.count == 1
|
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
if let firstIconURL = faviconURLs.first {
|
|
|
|
|
let _ = self.favicon(with: firstIconURL, homePageURL: url)
|
|
|
|
|
self.remainingFaviconURLs[url] = faviconURLs.dropFirst()
|
|
|
|
|
}
|
2017-12-03 06:27:25 +01:00
|
|
|
|
}
|
2017-11-23 23:15:28 +01:00
|
|
|
|
}
|
2017-12-03 06:27:25 +01:00
|
|
|
|
|
|
|
|
|
return nil
|
2017-11-23 23:15:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-03 06:27:25 +01:00
|
|
|
|
// MARK: - Notifications
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
@objc func didLoadFavicon(_ note: Notification) {
|
2017-11-23 23:15:28 +01:00
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
guard let singleFaviconDownloader = note.object as? SingleFaviconDownloader else {
|
|
|
|
|
return
|
2017-11-20 22:29:20 +01:00
|
|
|
|
}
|
2019-11-26 02:54:09 +01:00
|
|
|
|
guard let homePageURL = singleFaviconDownloader.homePageURL else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
|
guard let _ = singleFaviconDownloader.iconImage else {
|
2019-11-26 02:54:09 +01:00
|
|
|
|
if let faviconURLs = remainingFaviconURLs[homePageURL] {
|
|
|
|
|
if let nextIconURL = faviconURLs.first {
|
|
|
|
|
let _ = favicon(with: nextIconURL, homePageURL: singleFaviconDownloader.homePageURL)
|
|
|
|
|
remainingFaviconURLs[homePageURL] = faviconURLs.dropFirst();
|
|
|
|
|
} else {
|
|
|
|
|
remainingFaviconURLs[homePageURL] = nil
|
2020-01-30 23:02:20 +01:00
|
|
|
|
|
|
|
|
|
if currentHomePageHasOnlyFaviconICO {
|
|
|
|
|
self.homePageURLsWithNoFaviconURLCache.insert(homePageURL)
|
|
|
|
|
self.homePageURLsWithNoFaviconURLCacheDirty = true
|
|
|
|
|
}
|
2019-11-26 02:54:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
|
return
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
remainingFaviconURLs[homePageURL] = nil
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
postFaviconDidBecomeAvailableNotification(singleFaviconDownloader.faviconURL)
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
2019-10-31 20:04:34 +01:00
|
|
|
|
|
|
|
|
|
@objc func saveHomePageToFaviconURLCacheIfNeeded() {
|
|
|
|
|
if homePageToFaviconURLCacheDirty {
|
|
|
|
|
saveHomePageToFaviconURLCache()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func saveHomePageURLsWithNoFaviconURLCacheIfNeeded() {
|
|
|
|
|
if homePageURLsWithNoFaviconURLCacheDirty {
|
|
|
|
|
saveHomePageURLsWithNoFaviconURLCache()
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private extension FaviconDownloader {
|
|
|
|
|
|
2017-12-03 06:27:25 +01:00
|
|
|
|
static let localeForLowercasing = Locale(identifier: "en_US")
|
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
func findFaviconURLs(with homePageURL: String, _ completion: @escaping ([String]?) -> Void) {
|
2017-12-03 06:27:25 +01:00
|
|
|
|
|
2021-07-19 00:01:59 +02:00
|
|
|
|
guard let url = URL(unicodeString: homePageURL) else {
|
2017-12-03 06:27:25 +01:00
|
|
|
|
completion(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 23:09:01 +01:00
|
|
|
|
FaviconURLFinder.findFaviconURLs(with: homePageURL) { (faviconURLs) in
|
2020-02-15 15:53:56 +01:00
|
|
|
|
guard var faviconURLs = faviconURLs else {
|
|
|
|
|
completion(nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
var defaultFaviconURL: String? = nil
|
2017-12-03 06:27:25 +01:00
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
if let scheme = url.scheme, let host = url.host {
|
|
|
|
|
defaultFaviconURL = "\(scheme)://\(host)/favicon.ico".lowercased(with: FaviconDownloader.localeForLowercasing)
|
2017-12-03 06:27:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 15:53:56 +01:00
|
|
|
|
if let defaultFaviconURL = defaultFaviconURL {
|
|
|
|
|
faviconURLs.append(defaultFaviconURL)
|
2017-12-03 06:27:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 15:53:56 +01:00
|
|
|
|
completion(faviconURLs)
|
2017-12-03 06:27:25 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-26 01:11:24 +01:00
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
func faviconDownloader(withURL faviconURL: String, homePageURL: String?) -> SingleFaviconDownloader {
|
2017-11-20 08:59:04 +01:00
|
|
|
|
|
2020-03-24 18:21:08 +01:00
|
|
|
|
var firstTimeSeeingHomepageURL = false
|
|
|
|
|
|
|
|
|
|
if let homePageURL = homePageURL, self.homePageToFaviconURLCache[homePageURL] == nil {
|
|
|
|
|
self.homePageToFaviconURLCache[homePageURL] = faviconURL
|
|
|
|
|
self.homePageToFaviconURLCacheDirty = true
|
|
|
|
|
firstTimeSeeingHomepageURL = true
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
if let downloader = singleFaviconDownloaderCache[faviconURL] {
|
2020-03-24 18:21:08 +01:00
|
|
|
|
if firstTimeSeeingHomepageURL && !downloader.downloadFaviconIfNeeded() {
|
|
|
|
|
// This is to handle the scenario where we have different homepages, but the same favicon.
|
|
|
|
|
// This happens for Twitter and probably other sites like Blogger. Because the favicon
|
|
|
|
|
// is cached, we wouldn't send out a notification that it is now available unless we send
|
|
|
|
|
// it here.
|
|
|
|
|
postFaviconDidBecomeAvailableNotification(faviconURL)
|
|
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
|
return downloader
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-26 02:54:09 +01:00
|
|
|
|
let downloader = SingleFaviconDownloader(faviconURL: faviconURL, homePageURL: homePageURL, diskCache: diskCache, queue: queue)
|
2017-11-24 22:12:18 +01:00
|
|
|
|
singleFaviconDownloaderCache[faviconURL] = downloader
|
|
|
|
|
return downloader
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
|
func postFaviconDidBecomeAvailableNotification(_ faviconURL: String) {
|
2017-11-23 23:15:28 +01:00
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
let userInfo: [AnyHashable: Any] = [UserInfoKey.faviconURL: faviconURL]
|
|
|
|
|
NotificationCenter.default.post(name: .FaviconDidBecomeAvailable, object: self, userInfo: userInfo)
|
|
|
|
|
}
|
2017-11-23 23:15:28 +01:00
|
|
|
|
}
|
2019-10-31 20:04:34 +01:00
|
|
|
|
|
|
|
|
|
func loadHomePageToFaviconURLCache() {
|
|
|
|
|
let url = URL(fileURLWithPath: homePageToFaviconURLCachePath)
|
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
|
homePageToFaviconURLCache = (try? decoder.decode([String: String].self, from: data)) ?? [String: String]()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func loadHomePageURLsWithNoFaviconURLCache() {
|
|
|
|
|
let url = URL(fileURLWithPath: homePageURLsWithNoFaviconURLCachePath)
|
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
|
let decoded = (try? decoder.decode([String].self, from: data)) ?? [String]()
|
|
|
|
|
homePageURLsWithNoFaviconURLCache = Set(decoded)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queueSaveHomePageToFaviconURLCacheIfNeeded() {
|
|
|
|
|
FaviconDownloader.saveQueue.add(self, #selector(saveHomePageToFaviconURLCacheIfNeeded))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queueSaveHomePageURLsWithNoFaviconURLCacheIfNeeded() {
|
|
|
|
|
FaviconDownloader.saveQueue.add(self, #selector(saveHomePageURLsWithNoFaviconURLCacheIfNeeded))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveHomePageToFaviconURLCache() {
|
|
|
|
|
homePageToFaviconURLCacheDirty = false
|
|
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
|
let url = URL(fileURLWithPath: homePageToFaviconURLCachePath)
|
|
|
|
|
do {
|
|
|
|
|
let data = try encoder.encode(homePageToFaviconURLCache)
|
|
|
|
|
try data.write(to: url)
|
|
|
|
|
} catch {
|
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveHomePageURLsWithNoFaviconURLCache() {
|
|
|
|
|
homePageURLsWithNoFaviconURLCacheDirty = false
|
|
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
|
let url = URL(fileURLWithPath: homePageURLsWithNoFaviconURLCachePath)
|
|
|
|
|
do {
|
|
|
|
|
let data = try encoder.encode(Array(homePageURLsWithNoFaviconURLCache))
|
|
|
|
|
try data.write(to: url)
|
|
|
|
|
} catch {
|
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 08:59:04 +01:00
|
|
|
|
}
|