1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-24 00:34:04 +00:00

Load the tracker icon on the main thread (#1086)

* Load the tracker icon on the main thread

* Call loadTrackerIcon: directly
This commit is contained in:
Mitch Livingston 2020-01-03 00:12:59 -05:00 committed by GitHub
parent 10dbe92780
commit f041f229bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -195,11 +195,8 @@ NSMutableSet * fTrackerIconLoading;
baseAddress = [NSString stringWithFormat: @"http://%@", host]; baseAddress = [NSString stringWithFormat: @"http://%@", host];
icon = [fTrackerIconCache objectForKey: baseAddress]; icon = [fTrackerIconCache objectForKey: baseAddress];
if (!icon && ![fTrackerIconLoading containsObject: baseAddress]) if (!icon)
{ [self loadTrackerIcon: baseAddress];
[fTrackerIconLoading addObject: baseAddress];
[NSThread detachNewThreadSelector: @selector(loadTrackerIcon:) toTarget: self withObject: baseAddress];
}
} }
return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon"]; return (icon && icon != [NSNull null]) ? icon : [NSImage imageNamed: @"FavIcon"];
@ -208,38 +205,43 @@ NSMutableSet * fTrackerIconLoading;
#warning better favicon detection #warning better favicon detection
- (void) loadTrackerIcon: (NSString *) baseAddress - (void) loadTrackerIcon: (NSString *) baseAddress
{ {
@autoreleasepool if ([fTrackerIconLoading containsObject: baseAddress]) {
{ return;
//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];
} }
[fTrackerIconLoading addObject: baseAddress];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSImage *icon = nil;
NSArray<NSString *> *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 - (NSRect) imageRectForBounds: (NSRect) bounds