Skip to content

Commit

Permalink
Fix mozilla-mobile#6480: generateDefaultFaviconImage make thread safe…
Browse files Browse the repository at this point in the history
… to fix crash
  • Loading branch information
garvankeeley committed May 6, 2020
1 parent d398587 commit 9cd2509
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions Storage/SQL/SQLiteHistoryFavicons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -363,29 +363,30 @@ extension SQLiteHistory: Favicons {
// Generates a "default" favicon based on the first character in the
// site's domain name or gets an already-generated icon from the cache.
fileprivate func generateDefaultFaviconImage(forSite site: Site) -> Deferred<Maybe<UIImage>> {
guard let url = URL(string: site.url),
let character = url.baseDomain?.first else {
return deferMaybe(defaultFavicon)
}
let deferred = Deferred<Maybe<UIImage>>()

let faviconLetter = String(character).uppercased()
DispatchQueue.main.async {
guard let url = URL(string: site.url), let character = url.baseDomain?.first else {
deferred.fill(Maybe(success: defaultFavicon))
return
}

if let cachedFavicon = defaultFaviconImageCache[faviconLetter] {
return deferMaybe(cachedFavicon)
}
let faviconLetter = String(character).uppercased()

func generateBackgroundColor(forURL url: URL) -> UIColor {
guard let hash = url.baseDomain?.hashValue else {
return UIColor.Photon.Grey50
if let cachedFavicon = defaultFaviconImageCache[faviconLetter] {
deferred.fill(Maybe(success: cachedFavicon))
return
}
let index = abs(hash) % (DefaultFaviconBackgroundColors.count - 1)
let colorHex = DefaultFaviconBackgroundColors[index]
return UIColor(colorString: colorHex)
}

let deferred = Deferred<Maybe<UIImage>>()
func generateBackgroundColor(forURL url: URL) -> UIColor {
guard let hash = url.baseDomain?.hashValue else {
return UIColor.Photon.Grey50
}
let index = abs(hash) % (DefaultFaviconBackgroundColors.count - 1)
let colorHex = DefaultFaviconBackgroundColors[index]
return UIColor(colorString: colorHex)
}

DispatchQueue.main.async {
var image = UIImage()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
label.text = faviconLetter
Expand All @@ -404,7 +405,6 @@ extension SQLiteHistory: Favicons {
defaultFaviconImageCache[faviconLetter] = image
deferred.fill(Maybe(success: image))
}

return deferred
}
}

0 comments on commit 9cd2509

Please sign in to comment.