2017-11-20 08:59:04 +01:00
|
|
|
//
|
|
|
|
// FaviconDownloader.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/19/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Data
|
|
|
|
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 {
|
|
|
|
|
2017-11-24 19:45:22 +01:00
|
|
|
private var seekingFaviconCache = [String: SeekingFavicon]() // homePageURL: SeekingFavicon
|
2017-11-24 22:12:18 +01:00
|
|
|
private var singleFaviconDownloaderCache = [String: SingleFaviconDownloader]() // faviconURL: SingleFaviconDownloader
|
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-11-20 08:59:04 +01:00
|
|
|
private let queue: DispatchQueue
|
|
|
|
|
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
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(seekingFaviconDidSeek(_:)), name: .SeekingFaviconSeekDidComplete, object: nil)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(didLoadFavicon(_:)), name: .DidLoadFavicon, object: nil)
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - API
|
|
|
|
|
|
|
|
func favicon(for feed: Feed) -> NSImage? {
|
|
|
|
|
|
|
|
assert(Thread.isMainThread)
|
2017-11-24 19:45:22 +01:00
|
|
|
|
|
|
|
if let faviconURL = feed.faviconURL {
|
2017-11-24 22:12:18 +01:00
|
|
|
return favicon(with: faviconURL)
|
2017-11-24 19:45:22 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:29:20 +01:00
|
|
|
guard let homePageURL = feed.homePageURL else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-24 19:45:22 +01:00
|
|
|
return favicon(withHomePageURL: homePageURL)
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
func favicon(with faviconURL: String) -> NSImage? {
|
2017-11-24 19:45:22 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
let downloader = faviconDownloader(withURL: faviconURL)
|
|
|
|
return downloader.image
|
2017-11-24 19:45:22 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
func favicon(withHomePageURL homePageURL: String) -> NSImage? {
|
2017-11-20 22:29:20 +01:00
|
|
|
|
2017-11-26 01:11:24 +01:00
|
|
|
guard let seekingFavicon = seekingFavicon(with: normalizedHomePageURL(homePageURL)) else {
|
2017-11-24 22:12:18 +01:00
|
|
|
return nil
|
2017-11-24 19:45:22 +01:00
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
return favicon(withSeekingFavicon: seekingFavicon)
|
2017-11-24 19:45:22 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
// MARK: - Notifications
|
2017-11-24 19:45:22 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
@objc func seekingFaviconDidSeek(_ note: Notification) {
|
2017-11-24 19:45:22 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
guard let seekingFavicon = note.object as? SeekingFavicon else {
|
|
|
|
return
|
2017-11-23 23:15:28 +01:00
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
favicon(withSeekingFavicon: seekingFavicon)
|
2017-11-23 23:15:28 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
guard let _ = singleFaviconDownloader.image else {
|
|
|
|
return
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
postFaviconDidBecomeAvailableNotification(singleFaviconDownloader.faviconURL)
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension FaviconDownloader {
|
|
|
|
|
2017-11-26 01:11:24 +01:00
|
|
|
private static let localeForLowercasing = Locale(identifier: "en_US")
|
|
|
|
|
|
|
|
func normalizedHomePageURL(_ url: String) -> String {
|
|
|
|
|
|
|
|
// Many times the homePageURL is missing a trailing /.
|
|
|
|
// We add one when needed.
|
|
|
|
|
|
|
|
guard !url.hasSuffix("/") else {
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
let lowercasedURL = url.lowercased(with: FaviconDownloader.localeForLowercasing)
|
|
|
|
guard lowercasedURL.hasPrefix("http://") || lowercasedURL.hasPrefix("https://") else {
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
guard url.components(separatedBy: "/").count < 4 else {
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
return url + "/"
|
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
@discardableResult
|
|
|
|
func favicon(withSeekingFavicon seekingFavicon: SeekingFavicon) -> NSImage? {
|
2017-11-23 23:15:28 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
guard let faviconURL = seekingFavicon.faviconURL else {
|
2017-11-23 23:15:28 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
return favicon(with: faviconURL)
|
2017-11-23 23:15:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
func faviconDownloader(withURL faviconURL: String) -> SingleFaviconDownloader {
|
2017-11-20 08:59:04 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
if let downloader = singleFaviconDownloaderCache[faviconURL] {
|
|
|
|
downloader.downloadFaviconIfNeeded()
|
|
|
|
return downloader
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
let downloader = SingleFaviconDownloader(faviconURL: faviconURL, diskCache: diskCache, queue: queue)
|
|
|
|
singleFaviconDownloaderCache[faviconURL] = downloader
|
|
|
|
return downloader
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
func seekingFavicon(with homePageURL: String) -> SeekingFavicon? {
|
2017-11-20 08:59:04 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
if let seekingFavicon = seekingFaviconCache[homePageURL] {
|
|
|
|
return seekingFavicon
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
guard let seekingFavicon = SeekingFavicon(homePageURL: homePageURL) else {
|
2017-11-20 08:59:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
2017-11-24 22:12:18 +01:00
|
|
|
seekingFaviconCache[homePageURL] = seekingFavicon
|
|
|
|
return seekingFavicon
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|
2017-11-23 23:15:28 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
func postFaviconDidBecomeAvailableNotification(_ faviconURL: String) {
|
2017-11-23 23:15:28 +01:00
|
|
|
|
2017-11-24 22:12:18 +01:00
|
|
|
let userInfo: [AnyHashable: Any] = [UserInfoKey.faviconURL: faviconURL]
|
2017-11-23 23:15:28 +01:00
|
|
|
NotificationCenter.default.post(name: .FaviconDidBecomeAvailable, object: self, userInfo: userInfo)
|
|
|
|
}
|
2017-11-20 08:59:04 +01:00
|
|
|
}
|