SDWebImageDownloaderOperation.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. // This file is based on third party code, see below for the original author
  2. // and original license.
  3. // Modifications are (c) by Threema GmbH and licensed under the AGPLv3.
  4. /*
  5. * This file is part of the SDWebImage package.
  6. * (c) Olivier Poitrey <rs@dailymotion.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. #import "SDWebImageDownloaderOperation.h"
  12. #import "SDWebImageDecoder.h"
  13. #import "UIImage+MultiFormat.h"
  14. #import <ImageIO/ImageIO.h>
  15. #import "SDWebImageManager.h"
  16. @interface SDWebImageDownloaderOperation () <NSURLConnectionDataDelegate>
  17. @property (copy, nonatomic) SDWebImageDownloaderProgressBlock progressBlock;
  18. @property (copy, nonatomic) SDWebImageDownloaderCompletedBlock completedBlock;
  19. @property (copy, nonatomic) SDWebImageNoParamsBlock cancelBlock;
  20. @property (assign, nonatomic, getter = isExecuting) BOOL executing;
  21. @property (assign, nonatomic, getter = isFinished) BOOL finished;
  22. @property (assign, nonatomic) NSInteger expectedSize;
  23. @property (strong, nonatomic) NSMutableData *imageData;
  24. @property (strong, nonatomic) NSURLConnection *connection;
  25. @property (strong, atomic) NSThread *thread;
  26. #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
  27. @property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundTaskId;
  28. #endif
  29. @end
  30. @implementation SDWebImageDownloaderOperation {
  31. size_t width, height;
  32. UIImageOrientation orientation;
  33. BOOL responseFromCached;
  34. }
  35. @synthesize executing = _executing;
  36. @synthesize finished = _finished;
  37. - (id)initWithRequest:(NSURLRequest *)request
  38. options:(SDWebImageDownloaderOptions)options
  39. progress:(SDWebImageDownloaderProgressBlock)progressBlock
  40. completed:(SDWebImageDownloaderCompletedBlock)completedBlock
  41. cancelled:(SDWebImageNoParamsBlock)cancelBlock {
  42. if ((self = [super init])) {
  43. _request = request;
  44. _shouldDecompressImages = YES;
  45. _shouldUseCredentialStorage = YES;
  46. _options = options;
  47. _progressBlock = [progressBlock copy];
  48. _completedBlock = [completedBlock copy];
  49. _cancelBlock = [cancelBlock copy];
  50. _executing = NO;
  51. _finished = NO;
  52. _expectedSize = 0;
  53. responseFromCached = YES; // Initially wrong until `connection:willCacheResponse:` is called or not called
  54. }
  55. return self;
  56. }
  57. - (void)start {
  58. @synchronized (self) {
  59. if (self.isCancelled) {
  60. self.finished = YES;
  61. [self reset];
  62. return;
  63. }
  64. /***** BEGIN THREEMA MODIFICATION: disable to make code comply with extension restrictions *********/
  65. #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 && FALSE
  66. /***** END THREEMA MODIFICATION *********/
  67. if ([self shouldContinueWhenAppEntersBackground]) {
  68. __weak __typeof__ (self) wself = self;
  69. self.backgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
  70. __strong __typeof (wself) sself = wself;
  71. if (sself) {
  72. [sself cancel];
  73. [[UIApplication sharedApplication] endBackgroundTask:sself.backgroundTaskId];
  74. sself.backgroundTaskId = UIBackgroundTaskInvalid;
  75. }
  76. }];
  77. }
  78. #endif
  79. self.executing = YES;
  80. self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
  81. self.thread = [NSThread currentThread];
  82. }
  83. [self.connection start];
  84. if (self.connection) {
  85. if (self.progressBlock) {
  86. self.progressBlock(0, NSURLResponseUnknownLength);
  87. }
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStartNotification object:self];
  90. });
  91. if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_5_1) {
  92. // Make sure to run the runloop in our background thread so it can process downloaded data
  93. // Note: we use a timeout to work around an issue with NSURLConnection cancel under iOS 5
  94. // not waking up the runloop, leading to dead threads (see https://github.com/rs/SDWebImage/issues/466)
  95. CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false);
  96. }
  97. else {
  98. CFRunLoopRun();
  99. }
  100. if (!self.isFinished) {
  101. [self.connection cancel];
  102. [self connection:self.connection didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorTimedOut userInfo:@{NSURLErrorFailingURLErrorKey : self.request.URL}]];
  103. }
  104. }
  105. else {
  106. if (self.completedBlock) {
  107. self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Connection can't be initialized"}], YES);
  108. }
  109. }
  110. /***** BEGIN THREEMA MODIFICATION: disable to make code comply with extension restrictions *********/
  111. #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 && FALSE
  112. /***** END THREEMA MODIFICATION *********/
  113. if (self.backgroundTaskId != UIBackgroundTaskInvalid) {
  114. [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskId];
  115. self.backgroundTaskId = UIBackgroundTaskInvalid;
  116. }
  117. #endif
  118. }
  119. - (void)cancel {
  120. @synchronized (self) {
  121. if (self.thread) {
  122. [self performSelector:@selector(cancelInternalAndStop) onThread:self.thread withObject:nil waitUntilDone:NO];
  123. }
  124. else {
  125. [self cancelInternal];
  126. }
  127. }
  128. }
  129. - (void)cancelInternalAndStop {
  130. if (self.isFinished) return;
  131. [self cancelInternal];
  132. CFRunLoopStop(CFRunLoopGetCurrent());
  133. }
  134. - (void)cancelInternal {
  135. if (self.isFinished) return;
  136. [super cancel];
  137. if (self.cancelBlock) self.cancelBlock();
  138. if (self.connection) {
  139. [self.connection cancel];
  140. dispatch_async(dispatch_get_main_queue(), ^{
  141. [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
  142. });
  143. // As we cancelled the connection, its callback won't be called and thus won't
  144. // maintain the isFinished and isExecuting flags.
  145. if (self.isExecuting) self.executing = NO;
  146. if (!self.isFinished) self.finished = YES;
  147. }
  148. [self reset];
  149. }
  150. - (void)done {
  151. self.finished = YES;
  152. self.executing = NO;
  153. [self reset];
  154. }
  155. - (void)reset {
  156. self.cancelBlock = nil;
  157. self.completedBlock = nil;
  158. self.progressBlock = nil;
  159. self.connection = nil;
  160. self.imageData = nil;
  161. self.thread = nil;
  162. }
  163. - (void)setFinished:(BOOL)finished {
  164. [self willChangeValueForKey:@"isFinished"];
  165. _finished = finished;
  166. [self didChangeValueForKey:@"isFinished"];
  167. }
  168. - (void)setExecuting:(BOOL)executing {
  169. [self willChangeValueForKey:@"isExecuting"];
  170. _executing = executing;
  171. [self didChangeValueForKey:@"isExecuting"];
  172. }
  173. - (BOOL)isConcurrent {
  174. return YES;
  175. }
  176. #pragma mark NSURLConnection (delegate)
  177. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
  178. //'304 Not Modified' is an exceptional one
  179. if ((![response respondsToSelector:@selector(statusCode)] || [((NSHTTPURLResponse *)response) statusCode] < 400) && [((NSHTTPURLResponse *)response) statusCode] != 304) {
  180. NSInteger expected = response.expectedContentLength > 0 ? (NSInteger)response.expectedContentLength : 0;
  181. self.expectedSize = expected;
  182. if (self.progressBlock) {
  183. self.progressBlock(0, expected);
  184. }
  185. self.imageData = [[NSMutableData alloc] initWithCapacity:expected];
  186. }
  187. else {
  188. NSUInteger code = [((NSHTTPURLResponse *)response) statusCode];
  189. //This is the case when server returns '304 Not Modified'. It means that remote image is not changed.
  190. //In case of 304 we need just cancel the operation and return cached image from the cache.
  191. if (code == 304) {
  192. [self cancelInternal];
  193. } else {
  194. [self.connection cancel];
  195. }
  196. dispatch_async(dispatch_get_main_queue(), ^{
  197. [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  198. });
  199. if (self.completedBlock) {
  200. self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES);
  201. }
  202. CFRunLoopStop(CFRunLoopGetCurrent());
  203. [self done];
  204. }
  205. }
  206. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  207. [self.imageData appendData:data];
  208. if ((self.options & SDWebImageDownloaderProgressiveDownload) && self.expectedSize > 0 && self.completedBlock) {
  209. // The following code is from http://www.cocoaintheshell.com/2011/05/progressive-images-download-imageio/
  210. // Thanks to the author @Nyx0uf
  211. // Get the total bytes downloaded
  212. const NSInteger totalSize = self.imageData.length;
  213. // Update the data source, we must pass ALL the data, not just the new bytes
  214. CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)self.imageData, NULL);
  215. if (width + height == 0) {
  216. CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
  217. if (properties) {
  218. NSInteger orientationValue = -1;
  219. CFTypeRef val = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
  220. if (val) CFNumberGetValue(val, kCFNumberLongType, &height);
  221. val = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
  222. if (val) CFNumberGetValue(val, kCFNumberLongType, &width);
  223. val = CFDictionaryGetValue(properties, kCGImagePropertyOrientation);
  224. if (val) CFNumberGetValue(val, kCFNumberNSIntegerType, &orientationValue);
  225. CFRelease(properties);
  226. // When we draw to Core Graphics, we lose orientation information,
  227. // which means the image below born of initWithCGIImage will be
  228. // oriented incorrectly sometimes. (Unlike the image born of initWithData
  229. // in connectionDidFinishLoading.) So save it here and pass it on later.
  230. orientation = [[self class] orientationFromPropertyValue:(orientationValue == -1 ? 1 : orientationValue)];
  231. }
  232. }
  233. if (width + height > 0 && totalSize < self.expectedSize) {
  234. // Create the image
  235. CGImageRef partialImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
  236. #ifdef TARGET_OS_IPHONE
  237. // Workaround for iOS anamorphic image
  238. if (partialImageRef) {
  239. const size_t partialHeight = CGImageGetHeight(partialImageRef);
  240. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  241. CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
  242. CGColorSpaceRelease(colorSpace);
  243. if (bmContext) {
  244. CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = partialHeight}, partialImageRef);
  245. CGImageRelease(partialImageRef);
  246. partialImageRef = CGBitmapContextCreateImage(bmContext);
  247. CGContextRelease(bmContext);
  248. }
  249. else {
  250. CGImageRelease(partialImageRef);
  251. partialImageRef = nil;
  252. }
  253. }
  254. #endif
  255. if (partialImageRef) {
  256. UIImage *image = [UIImage imageWithCGImage:partialImageRef scale:1 orientation:orientation];
  257. NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
  258. UIImage *scaledImage = [self scaledImageForKey:key image:image];
  259. if (self.shouldDecompressImages) {
  260. image = [UIImage decodedImageWithImage:scaledImage];
  261. }
  262. else {
  263. image = scaledImage;
  264. }
  265. CGImageRelease(partialImageRef);
  266. dispatch_main_sync_safe(^{
  267. if (self.completedBlock) {
  268. self.completedBlock(image, nil, nil, NO);
  269. }
  270. });
  271. }
  272. }
  273. CFRelease(imageSource);
  274. }
  275. if (self.progressBlock) {
  276. self.progressBlock(self.imageData.length, self.expectedSize);
  277. }
  278. }
  279. + (UIImageOrientation)orientationFromPropertyValue:(NSInteger)value {
  280. switch (value) {
  281. case 1:
  282. return UIImageOrientationUp;
  283. case 3:
  284. return UIImageOrientationDown;
  285. case 8:
  286. return UIImageOrientationLeft;
  287. case 6:
  288. return UIImageOrientationRight;
  289. case 2:
  290. return UIImageOrientationUpMirrored;
  291. case 4:
  292. return UIImageOrientationDownMirrored;
  293. case 5:
  294. return UIImageOrientationLeftMirrored;
  295. case 7:
  296. return UIImageOrientationRightMirrored;
  297. default:
  298. return UIImageOrientationUp;
  299. }
  300. }
  301. - (UIImage *)scaledImageForKey:(NSString *)key image:(UIImage *)image {
  302. return SDScaledImageForKey(key, image);
  303. }
  304. - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
  305. SDWebImageDownloaderCompletedBlock completionBlock = self.completedBlock;
  306. @synchronized(self) {
  307. CFRunLoopStop(CFRunLoopGetCurrent());
  308. self.thread = nil;
  309. self.connection = nil;
  310. dispatch_async(dispatch_get_main_queue(), ^{
  311. [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  312. });
  313. }
  314. if (![[NSURLCache sharedURLCache] cachedResponseForRequest:_request]) {
  315. responseFromCached = NO;
  316. }
  317. if (completionBlock) {
  318. if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached) {
  319. completionBlock(nil, nil, nil, YES);
  320. }
  321. else {
  322. UIImage *image = [UIImage sd_imageWithData:self.imageData];
  323. NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
  324. image = [self scaledImageForKey:key image:image];
  325. // Do not force decoding animated GIFs
  326. if (!image.images) {
  327. if (self.shouldDecompressImages) {
  328. image = [UIImage decodedImageWithImage:image];
  329. }
  330. }
  331. if (CGSizeEqualToSize(image.size, CGSizeZero)) {
  332. completionBlock(nil, nil, [NSError errorWithDomain:@"SDWebImageErrorDomain" code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}], YES);
  333. }
  334. else {
  335. completionBlock(image, self.imageData, nil, YES);
  336. }
  337. }
  338. }
  339. self.completionBlock = nil;
  340. [self done];
  341. }
  342. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  343. @synchronized(self) {
  344. CFRunLoopStop(CFRunLoopGetCurrent());
  345. self.thread = nil;
  346. self.connection = nil;
  347. dispatch_async(dispatch_get_main_queue(), ^{
  348. [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:nil];
  349. });
  350. }
  351. if (self.completedBlock) {
  352. self.completedBlock(nil, nil, error, YES);
  353. }
  354. self.completionBlock = nil;
  355. [self done];
  356. }
  357. - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
  358. responseFromCached = NO; // If this method is called, it means the response wasn't read from cache
  359. if (self.request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData) {
  360. // Prevents caching of responses
  361. return nil;
  362. }
  363. else {
  364. return cachedResponse;
  365. }
  366. }
  367. - (BOOL)shouldContinueWhenAppEntersBackground {
  368. return self.options & SDWebImageDownloaderContinueInBackground;
  369. }
  370. - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection __unused *)connection {
  371. return self.shouldUseCredentialStorage;
  372. }
  373. - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
  374. if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
  375. if (!(self.options & SDWebImageDownloaderAllowInvalidSSLCertificates) &&
  376. [challenge.sender respondsToSelector:@selector(performDefaultHandlingForAuthenticationChallenge:)]) {
  377. [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
  378. } else {
  379. NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
  380. [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
  381. }
  382. } else {
  383. if ([challenge previousFailureCount] == 0) {
  384. if (self.credential) {
  385. [[challenge sender] useCredential:self.credential forAuthenticationChallenge:challenge];
  386. } else {
  387. [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  388. }
  389. } else {
  390. [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  391. }
  392. }
  393. }
  394. @end