diff --git a/macosx/TrackerCell.m b/macosx/TrackerCell.m index 1a4224f93..3c3721360 100644 --- a/macosx/TrackerCell.m +++ b/macosx/TrackerCell.m @@ -195,11 +195,8 @@ NSMutableSet * fTrackerIconLoading; baseAddress = [NSString stringWithFormat: @"http://%@", host]; icon = [fTrackerIconCache objectForKey: baseAddress]; - if (!icon && ![fTrackerIconLoading containsObject: baseAddress]) - { - [fTrackerIconLoading addObject: baseAddress]; - [NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress]; - } + if (!icon) + [self loadTrackerIcon: baseAddress]; } return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon"]; @@ -208,38 +205,43 @@ NSMutableSet * fTrackerIconLoading; #warning better favicon detection - (void) loadTrackerIcon: (NSString *) baseAddress { - @autoreleasepool - { - //try favicon.png - NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.png"]]; - - NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy - timeoutInterval: 30.0]; - NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL]; - NSImage * icon = [[NSImage alloc] initWithData: iconData]; - - //try favicon.ico - if (!icon) - { - favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent: @"favicon.ico"]]; - - request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy - timeoutInterval: 30.0]; - iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL]; - icon = [[NSImage alloc] initWithData: iconData]; - } - - if (icon) - { - [fTrackerIconCache setObject: icon forKey: baseAddress]; - - [[self controlView] setNeedsDisplay: YES]; - } - else - [fTrackerIconCache setObject: [NSNull null] forKey: baseAddress]; - - [fTrackerIconLoading removeObject: baseAddress]; + if ([fTrackerIconLoading containsObject: baseAddress]) { + return; } + [fTrackerIconLoading addObject: baseAddress]; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSImage *icon = nil; + + NSArray *filenamesToTry = @[ @"favicon.png", @"favicon.ico" ]; + for (NSString *filename in filenamesToTry) { + NSURL * favIconUrl = [NSURL URLWithString: [baseAddress stringByAppendingPathComponent:filename]]; + + NSURLRequest * request = [NSURLRequest requestWithURL: favIconUrl cachePolicy: NSURLRequestUseProtocolCachePolicy + timeoutInterval: 30.0]; + + NSData * iconData = [NSURLConnection sendSynchronousRequest: request returningResponse: NULL error: NULL]; + if (iconData) { + icon = [[NSImage alloc] initWithData: iconData]; + if (icon) { + break; + } + } + } + + dispatch_async(dispatch_get_main_queue(), ^{ + if (icon) + { + [fTrackerIconCache setObject: icon forKey: baseAddress]; + + [[self controlView] setNeedsDisplay: YES]; + } + else + [fTrackerIconCache setObject: [NSNull null] forKey: baseAddress]; + + [fTrackerIconLoading removeObject: baseAddress]; + }); + }); } - (NSRect) imageRectForBounds: (NSRect) bounds