From a6e75df412c278d0d0553de2e243def96a0d785d Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 24 Mar 2020 12:21:08 -0500 Subject: [PATCH] Send out an event if we found a cached favicon downloader for new homepage URL. Issue #1940 --- Shared/Favicons/FaviconDownloader.swift | 21 +++++++++++++------ Shared/Favicons/SingleFaviconDownloader.swift | 8 ++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Shared/Favicons/FaviconDownloader.swift b/Shared/Favicons/FaviconDownloader.swift index 25c83bcc1..99d8dd2e6 100644 --- a/Shared/Favicons/FaviconDownloader.swift +++ b/Shared/Favicons/FaviconDownloader.swift @@ -178,11 +178,6 @@ final class FaviconDownloader { remainingFaviconURLs[homePageURL] = nil - if self.homePageToFaviconURLCache[homePageURL] == nil { - self.homePageToFaviconURLCache[homePageURL] = singleFaviconDownloader.faviconURL - self.homePageToFaviconURLCacheDirty = true - } - postFaviconDidBecomeAvailableNotification(singleFaviconDownloader.faviconURL) } @@ -232,8 +227,22 @@ private extension FaviconDownloader { func faviconDownloader(withURL faviconURL: String, homePageURL: String?) -> SingleFaviconDownloader { + var firstTimeSeeingHomepageURL = false + + if let homePageURL = homePageURL, self.homePageToFaviconURLCache[homePageURL] == nil { + self.homePageToFaviconURLCache[homePageURL] = faviconURL + self.homePageToFaviconURLCacheDirty = true + firstTimeSeeingHomepageURL = true + } + if let downloader = singleFaviconDownloaderCache[faviconURL] { - downloader.downloadFaviconIfNeeded() + 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) + } return downloader } diff --git a/Shared/Favicons/SingleFaviconDownloader.swift b/Shared/Favicons/SingleFaviconDownloader.swift index 8a37486f3..9ef2b61df 100644 --- a/Shared/Favicons/SingleFaviconDownloader.swift +++ b/Shared/Favicons/SingleFaviconDownloader.swift @@ -48,21 +48,23 @@ final class SingleFaviconDownloader { findFavicon() } - func downloadFaviconIfNeeded() { + func downloadFaviconIfNeeded() -> Bool { // If we don’t have an image, and lastDownloadAttemptDate is a while ago, try again. if let _ = iconImage { - return + return false } let retryInterval: TimeInterval = 30 * 60 // 30 minutes if Date().timeIntervalSince(lastDownloadAttemptDate) < retryInterval { - return + return false } lastDownloadAttemptDate = Date() findFavicon() + + return true } }