SDWebImageDownloader.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDWebImageDownloader.h"
  9. #import "SDWebImageDownloaderConfig.h"
  10. #import "SDWebImageDownloaderOperation.h"
  11. #import "SDWebImageError.h"
  12. #import "SDWebImageCacheKeyFilter.h"
  13. #import "SDImageCacheDefine.h"
  14. #import "SDInternalMacros.h"
  15. #import "objc/runtime.h"
  16. NSNotificationName const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
  17. NSNotificationName const SDWebImageDownloadReceiveResponseNotification = @"SDWebImageDownloadReceiveResponseNotification";
  18. NSNotificationName const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
  19. NSNotificationName const SDWebImageDownloadFinishNotification = @"SDWebImageDownloadFinishNotification";
  20. static void * SDWebImageDownloaderContext = &SDWebImageDownloaderContext;
  21. static void * SDWebImageDownloaderOperationKey = &SDWebImageDownloaderOperationKey;
  22. BOOL SDWebImageDownloaderOperationGetCompleted(id<SDWebImageDownloaderOperation> operation) {
  23. NSCParameterAssert(operation);
  24. NSNumber *value = objc_getAssociatedObject(operation, SDWebImageDownloaderOperationKey);
  25. if (value != nil) {
  26. return value.boolValue;
  27. } else {
  28. return NO;
  29. }
  30. }
  31. void SDWebImageDownloaderOperationSetCompleted(id<SDWebImageDownloaderOperation> operation, BOOL isCompleted) {
  32. NSCParameterAssert(operation);
  33. objc_setAssociatedObject(operation, SDWebImageDownloaderOperationKey, @(isCompleted), OBJC_ASSOCIATION_RETAIN);
  34. }
  35. @interface SDWebImageDownloadToken ()
  36. @property (nonatomic, strong, nullable, readwrite) NSURL *url;
  37. @property (nonatomic, strong, nullable, readwrite) NSURLRequest *request;
  38. @property (nonatomic, strong, nullable, readwrite) NSURLResponse *response;
  39. @property (nonatomic, strong, nullable, readwrite) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
  40. @property (nonatomic, weak, nullable, readwrite) id downloadOperationCancelToken;
  41. @property (nonatomic, weak, nullable) NSOperation<SDWebImageDownloaderOperation> *downloadOperation;
  42. @property (nonatomic, assign, getter=isCancelled) BOOL cancelled;
  43. - (nonnull instancetype)init NS_UNAVAILABLE;
  44. + (nonnull instancetype)new NS_UNAVAILABLE;
  45. - (nonnull instancetype)initWithDownloadOperation:(nullable NSOperation<SDWebImageDownloaderOperation> *)downloadOperation;
  46. @end
  47. @interface SDWebImageDownloader () <NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
  48. @property (strong, nonatomic, nonnull) NSOperationQueue *downloadQueue;
  49. @property (strong, nonatomic, nonnull) NSMutableDictionary<NSURL *, NSOperation<SDWebImageDownloaderOperation> *> *URLOperations;
  50. @property (strong, nonatomic, nullable) NSMutableDictionary<NSString *, NSString *> *HTTPHeaders;
  51. // The session in which data tasks will run
  52. @property (strong, nonatomic) NSURLSession *session;
  53. @end
  54. @implementation SDWebImageDownloader {
  55. SD_LOCK_DECLARE(_HTTPHeadersLock); // A lock to keep the access to `HTTPHeaders` thread-safe
  56. SD_LOCK_DECLARE(_operationsLock); // A lock to keep the access to `URLOperations` thread-safe
  57. }
  58. + (void)initialize {
  59. // Bind SDNetworkActivityIndicator if available (download it here: http://github.com/rs/SDNetworkActivityIndicator )
  60. // To use it, just add #import "SDNetworkActivityIndicator.h" in addition to the SDWebImage import
  61. if (NSClassFromString(@"SDNetworkActivityIndicator")) {
  62. #pragma clang diagnostic push
  63. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  64. id activityIndicator = [NSClassFromString(@"SDNetworkActivityIndicator") performSelector:NSSelectorFromString(@"sharedActivityIndicator")];
  65. #pragma clang diagnostic pop
  66. // Remove observer in case it was previously added.
  67. [[NSNotificationCenter defaultCenter] removeObserver:activityIndicator name:SDWebImageDownloadStartNotification object:nil];
  68. [[NSNotificationCenter defaultCenter] removeObserver:activityIndicator name:SDWebImageDownloadStopNotification object:nil];
  69. [[NSNotificationCenter defaultCenter] addObserver:activityIndicator
  70. selector:NSSelectorFromString(@"startActivity")
  71. name:SDWebImageDownloadStartNotification object:nil];
  72. [[NSNotificationCenter defaultCenter] addObserver:activityIndicator
  73. selector:NSSelectorFromString(@"stopActivity")
  74. name:SDWebImageDownloadStopNotification object:nil];
  75. }
  76. }
  77. + (nonnull instancetype)sharedDownloader {
  78. static dispatch_once_t once;
  79. static id instance;
  80. dispatch_once(&once, ^{
  81. instance = [self new];
  82. });
  83. return instance;
  84. }
  85. - (nonnull instancetype)init {
  86. return [self initWithConfig:SDWebImageDownloaderConfig.defaultDownloaderConfig];
  87. }
  88. - (instancetype)initWithConfig:(SDWebImageDownloaderConfig *)config {
  89. self = [super init];
  90. if (self) {
  91. if (!config) {
  92. config = SDWebImageDownloaderConfig.defaultDownloaderConfig;
  93. }
  94. _config = [config copy];
  95. [_config addObserver:self forKeyPath:NSStringFromSelector(@selector(maxConcurrentDownloads)) options:0 context:SDWebImageDownloaderContext];
  96. _downloadQueue = [NSOperationQueue new];
  97. _downloadQueue.maxConcurrentOperationCount = _config.maxConcurrentDownloads;
  98. _downloadQueue.name = @"com.hackemist.SDWebImageDownloader.downloadQueue";
  99. _URLOperations = [NSMutableDictionary new];
  100. NSMutableDictionary<NSString *, NSString *> *headerDictionary = [NSMutableDictionary dictionary];
  101. NSString *userAgent = nil;
  102. #if SD_UIKIT
  103. // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
  104. userAgent = [NSString stringWithFormat:@"%@/%@ (%@; iOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], [[UIScreen mainScreen] scale]];
  105. #elif SD_WATCH
  106. // User-Agent Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
  107. userAgent = [NSString stringWithFormat:@"%@/%@ (%@; watchOS %@; Scale/%0.2f)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[WKInterfaceDevice currentDevice] model], [[WKInterfaceDevice currentDevice] systemVersion], [[WKInterfaceDevice currentDevice] screenScale]];
  108. #elif SD_MAC
  109. userAgent = [NSString stringWithFormat:@"%@/%@ (Mac OS X %@)", [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleExecutableKey] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleIdentifierKey], [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] ?: [[NSBundle mainBundle] infoDictionary][(__bridge NSString *)kCFBundleVersionKey], [[NSProcessInfo processInfo] operatingSystemVersionString]];
  110. #endif
  111. if (userAgent) {
  112. if (![userAgent canBeConvertedToEncoding:NSASCIIStringEncoding]) {
  113. NSMutableString *mutableUserAgent = [userAgent mutableCopy];
  114. if (CFStringTransform((__bridge CFMutableStringRef)(mutableUserAgent), NULL, (__bridge CFStringRef)@"Any-Latin; Latin-ASCII; [:^ASCII:] Remove", false)) {
  115. userAgent = mutableUserAgent;
  116. }
  117. }
  118. headerDictionary[@"User-Agent"] = userAgent;
  119. }
  120. headerDictionary[@"Accept"] = @"image/*,*/*;q=0.8";
  121. _HTTPHeaders = headerDictionary;
  122. SD_LOCK_INIT(_HTTPHeadersLock);
  123. SD_LOCK_INIT(_operationsLock);
  124. NSURLSessionConfiguration *sessionConfiguration = _config.sessionConfiguration;
  125. if (!sessionConfiguration) {
  126. sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
  127. }
  128. /**
  129. * Create the session for this task
  130. * We send nil as delegate queue so that the session creates a serial operation queue for performing all delegate
  131. * method calls and completion handler calls.
  132. */
  133. _session = [NSURLSession sessionWithConfiguration:sessionConfiguration
  134. delegate:self
  135. delegateQueue:nil];
  136. }
  137. return self;
  138. }
  139. - (void)dealloc {
  140. [self.downloadQueue cancelAllOperations];
  141. [self.config removeObserver:self forKeyPath:NSStringFromSelector(@selector(maxConcurrentDownloads)) context:SDWebImageDownloaderContext];
  142. // Invalide the URLSession after all operations been cancelled
  143. [self.session invalidateAndCancel];
  144. self.session = nil;
  145. }
  146. - (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations {
  147. if (self == [SDWebImageDownloader sharedDownloader]) {
  148. return;
  149. }
  150. if (cancelPendingOperations) {
  151. [self.session invalidateAndCancel];
  152. } else {
  153. [self.session finishTasksAndInvalidate];
  154. }
  155. }
  156. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field {
  157. if (!field) {
  158. return;
  159. }
  160. SD_LOCK(_HTTPHeadersLock);
  161. [self.HTTPHeaders setValue:value forKey:field];
  162. SD_UNLOCK(_HTTPHeadersLock);
  163. }
  164. - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field {
  165. if (!field) {
  166. return nil;
  167. }
  168. SD_LOCK(_HTTPHeadersLock);
  169. NSString *value = [self.HTTPHeaders objectForKey:field];
  170. SD_UNLOCK(_HTTPHeadersLock);
  171. return value;
  172. }
  173. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(NSURL *)url
  174. completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
  175. return [self downloadImageWithURL:url options:0 progress:nil completed:completedBlock];
  176. }
  177. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(NSURL *)url
  178. options:(SDWebImageDownloaderOptions)options
  179. progress:(SDWebImageDownloaderProgressBlock)progressBlock
  180. completed:(SDWebImageDownloaderCompletedBlock)completedBlock {
  181. return [self downloadImageWithURL:url options:options context:nil progress:progressBlock completed:completedBlock];
  182. }
  183. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  184. options:(SDWebImageDownloaderOptions)options
  185. context:(nullable SDWebImageContext *)context
  186. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  187. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock {
  188. // The URL will be used as the key to the callbacks dictionary so it cannot be nil. If it is nil immediately call the completed block with no image or data.
  189. if (url == nil) {
  190. if (completedBlock) {
  191. NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidURL userInfo:@{NSLocalizedDescriptionKey : @"Image url is nil"}];
  192. completedBlock(nil, nil, error, YES);
  193. }
  194. return nil;
  195. }
  196. id downloadOperationCancelToken;
  197. // When different thumbnail size download with same url, we need to make sure each callback called with desired size
  198. id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
  199. NSString *cacheKey;
  200. if (cacheKeyFilter) {
  201. cacheKey = [cacheKeyFilter cacheKeyForURL:url];
  202. } else {
  203. cacheKey = url.absoluteString;
  204. }
  205. SDImageCoderOptions *decodeOptions = SDGetDecodeOptionsFromContext(context, [self.class imageOptionsFromDownloaderOptions:options], cacheKey);
  206. SD_LOCK(_operationsLock);
  207. NSOperation<SDWebImageDownloaderOperation> *operation = [self.URLOperations objectForKey:url];
  208. // There is a case that the operation may be marked as finished or cancelled, but not been removed from `self.URLOperations`.
  209. BOOL shouldNotReuseOperation;
  210. if (operation) {
  211. @synchronized (operation) {
  212. shouldNotReuseOperation = operation.isFinished || operation.isCancelled || SDWebImageDownloaderOperationGetCompleted(operation);
  213. }
  214. } else {
  215. shouldNotReuseOperation = YES;
  216. }
  217. if (shouldNotReuseOperation) {
  218. operation = [self createDownloaderOperationWithUrl:url options:options context:context];
  219. if (!operation) {
  220. SD_UNLOCK(_operationsLock);
  221. if (completedBlock) {
  222. NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorInvalidDownloadOperation userInfo:@{NSLocalizedDescriptionKey : @"Downloader operation is nil"}];
  223. completedBlock(nil, nil, error, YES);
  224. }
  225. return nil;
  226. }
  227. @weakify(self);
  228. operation.completionBlock = ^{
  229. @strongify(self);
  230. if (!self) {
  231. return;
  232. }
  233. SD_LOCK(self->_operationsLock);
  234. [self.URLOperations removeObjectForKey:url];
  235. SD_UNLOCK(self->_operationsLock);
  236. };
  237. [self.URLOperations setObject:operation forKey:url];
  238. // Add the handlers before submitting to operation queue, avoid the race condition that operation finished before setting handlers.
  239. downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock decodeOptions:decodeOptions];
  240. // Add operation to operation queue only after all configuration done according to Apple's doc.
  241. // `addOperation:` does not synchronously execute the `operation.completionBlock` so this will not cause deadlock.
  242. [self.downloadQueue addOperation:operation];
  243. } else {
  244. // When we reuse the download operation to attach more callbacks, there may be thread safe issue because the getter of callbacks may in another queue (decoding queue or delegate queue)
  245. // So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes.
  246. @synchronized (operation) {
  247. downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock decodeOptions:decodeOptions];
  248. }
  249. }
  250. SD_UNLOCK(_operationsLock);
  251. SDWebImageDownloadToken *token = [[SDWebImageDownloadToken alloc] initWithDownloadOperation:operation];
  252. token.url = url;
  253. token.request = operation.request;
  254. token.downloadOperationCancelToken = downloadOperationCancelToken;
  255. return token;
  256. }
  257. #pragma mark Helper methods
  258. + (SDWebImageOptions)imageOptionsFromDownloaderOptions:(SDWebImageDownloaderOptions)downloadOptions {
  259. SDWebImageOptions options = 0;
  260. if (downloadOptions & SDWebImageDownloaderScaleDownLargeImages) options |= SDWebImageScaleDownLargeImages;
  261. if (downloadOptions & SDWebImageDownloaderDecodeFirstFrameOnly) options |= SDWebImageDecodeFirstFrameOnly;
  262. if (downloadOptions & SDWebImageDownloaderPreloadAllFrames) options |= SDWebImagePreloadAllFrames;
  263. if (downloadOptions & SDWebImageDownloaderAvoidDecodeImage) options |= SDWebImageAvoidDecodeImage;
  264. if (downloadOptions & SDWebImageDownloaderMatchAnimatedImageClass) options |= SDWebImageMatchAnimatedImageClass;
  265. return options;
  266. }
  267. - (nullable NSOperation<SDWebImageDownloaderOperation> *)createDownloaderOperationWithUrl:(nonnull NSURL *)url
  268. options:(SDWebImageDownloaderOptions)options
  269. context:(nullable SDWebImageContext *)context {
  270. NSTimeInterval timeoutInterval = self.config.downloadTimeout;
  271. if (timeoutInterval == 0.0) {
  272. timeoutInterval = 15.0;
  273. }
  274. // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
  275. NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
  276. NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval];
  277. mutableRequest.HTTPShouldHandleCookies = SD_OPTIONS_CONTAINS(options, SDWebImageDownloaderHandleCookies);
  278. mutableRequest.HTTPShouldUsePipelining = YES;
  279. SD_LOCK(_HTTPHeadersLock);
  280. mutableRequest.allHTTPHeaderFields = self.HTTPHeaders;
  281. SD_UNLOCK(_HTTPHeadersLock);
  282. // Context Option
  283. SDWebImageMutableContext *mutableContext;
  284. if (context) {
  285. mutableContext = [context mutableCopy];
  286. } else {
  287. mutableContext = [NSMutableDictionary dictionary];
  288. }
  289. // Request Modifier
  290. id<SDWebImageDownloaderRequestModifier> requestModifier;
  291. if ([context valueForKey:SDWebImageContextDownloadRequestModifier]) {
  292. requestModifier = [context valueForKey:SDWebImageContextDownloadRequestModifier];
  293. } else {
  294. requestModifier = self.requestModifier;
  295. }
  296. NSURLRequest *request;
  297. if (requestModifier) {
  298. NSURLRequest *modifiedRequest = [requestModifier modifiedRequestWithRequest:[mutableRequest copy]];
  299. // If modified request is nil, early return
  300. if (!modifiedRequest) {
  301. return nil;
  302. } else {
  303. request = [modifiedRequest copy];
  304. }
  305. } else {
  306. request = [mutableRequest copy];
  307. }
  308. // Response Modifier
  309. id<SDWebImageDownloaderResponseModifier> responseModifier;
  310. if ([context valueForKey:SDWebImageContextDownloadResponseModifier]) {
  311. responseModifier = [context valueForKey:SDWebImageContextDownloadResponseModifier];
  312. } else {
  313. responseModifier = self.responseModifier;
  314. }
  315. if (responseModifier) {
  316. mutableContext[SDWebImageContextDownloadResponseModifier] = responseModifier;
  317. }
  318. // Decryptor
  319. id<SDWebImageDownloaderDecryptor> decryptor;
  320. if ([context valueForKey:SDWebImageContextDownloadDecryptor]) {
  321. decryptor = [context valueForKey:SDWebImageContextDownloadDecryptor];
  322. } else {
  323. decryptor = self.decryptor;
  324. }
  325. if (decryptor) {
  326. mutableContext[SDWebImageContextDownloadDecryptor] = decryptor;
  327. }
  328. context = [mutableContext copy];
  329. // Operation Class
  330. Class operationClass = self.config.operationClass;
  331. if (!operationClass) {
  332. operationClass = [SDWebImageDownloaderOperation class];
  333. }
  334. NSOperation<SDWebImageDownloaderOperation> *operation = [[operationClass alloc] initWithRequest:request inSession:self.session options:options context:context];
  335. if ([operation respondsToSelector:@selector(setCredential:)]) {
  336. if (self.config.urlCredential) {
  337. operation.credential = self.config.urlCredential;
  338. } else if (self.config.username && self.config.password) {
  339. operation.credential = [NSURLCredential credentialWithUser:self.config.username password:self.config.password persistence:NSURLCredentialPersistenceForSession];
  340. }
  341. }
  342. if ([operation respondsToSelector:@selector(setMinimumProgressInterval:)]) {
  343. operation.minimumProgressInterval = MIN(MAX(self.config.minimumProgressInterval, 0), 1);
  344. }
  345. if ([operation respondsToSelector:@selector(setAcceptableStatusCodes:)]) {
  346. operation.acceptableStatusCodes = self.config.acceptableStatusCodes;
  347. }
  348. if ([operation respondsToSelector:@selector(setAcceptableContentTypes:)]) {
  349. operation.acceptableContentTypes = self.config.acceptableContentTypes;
  350. }
  351. if (options & SDWebImageDownloaderHighPriority) {
  352. operation.queuePriority = NSOperationQueuePriorityHigh;
  353. } else if (options & SDWebImageDownloaderLowPriority) {
  354. operation.queuePriority = NSOperationQueuePriorityLow;
  355. }
  356. if (self.config.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
  357. // Emulate LIFO execution order by systematically, each previous adding operation can dependency the new operation
  358. // This can gurantee the new operation to be execulated firstly, even if when some operations finished, meanwhile you appending new operations
  359. // Just make last added operation dependents new operation can not solve this problem. See test case #test15DownloaderLIFOExecutionOrder
  360. for (NSOperation *pendingOperation in self.downloadQueue.operations) {
  361. [pendingOperation addDependency:operation];
  362. }
  363. }
  364. return operation;
  365. }
  366. - (void)cancelAllDownloads {
  367. [self.downloadQueue cancelAllOperations];
  368. }
  369. #pragma mark - Properties
  370. - (BOOL)isSuspended {
  371. return self.downloadQueue.isSuspended;
  372. }
  373. - (void)setSuspended:(BOOL)suspended {
  374. self.downloadQueue.suspended = suspended;
  375. }
  376. - (NSUInteger)currentDownloadCount {
  377. return self.downloadQueue.operationCount;
  378. }
  379. - (NSURLSessionConfiguration *)sessionConfiguration {
  380. return self.session.configuration;
  381. }
  382. #pragma mark - KVO
  383. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  384. if (context == SDWebImageDownloaderContext) {
  385. if ([keyPath isEqualToString:NSStringFromSelector(@selector(maxConcurrentDownloads))]) {
  386. self.downloadQueue.maxConcurrentOperationCount = self.config.maxConcurrentDownloads;
  387. }
  388. } else {
  389. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  390. }
  391. }
  392. #pragma mark Helper methods
  393. - (NSOperation<SDWebImageDownloaderOperation> *)operationWithTask:(NSURLSessionTask *)task {
  394. NSOperation<SDWebImageDownloaderOperation> *returnOperation = nil;
  395. for (NSOperation<SDWebImageDownloaderOperation> *operation in self.downloadQueue.operations) {
  396. if ([operation respondsToSelector:@selector(dataTask)]) {
  397. // So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes.
  398. NSURLSessionTask *operationTask;
  399. @synchronized (operation) {
  400. operationTask = operation.dataTask;
  401. }
  402. if (operationTask.taskIdentifier == task.taskIdentifier) {
  403. returnOperation = operation;
  404. break;
  405. }
  406. }
  407. }
  408. return returnOperation;
  409. }
  410. #pragma mark NSURLSessionDataDelegate
  411. - (void)URLSession:(NSURLSession *)session
  412. dataTask:(NSURLSessionDataTask *)dataTask
  413. didReceiveResponse:(NSURLResponse *)response
  414. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  415. // Identify the operation that runs this task and pass it the delegate method
  416. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  417. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveResponse:completionHandler:)]) {
  418. [dataOperation URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler];
  419. } else {
  420. if (completionHandler) {
  421. completionHandler(NSURLSessionResponseAllow);
  422. }
  423. }
  424. }
  425. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  426. // Identify the operation that runs this task and pass it the delegate method
  427. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  428. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) {
  429. [dataOperation URLSession:session dataTask:dataTask didReceiveData:data];
  430. }
  431. }
  432. - (void)URLSession:(NSURLSession *)session
  433. dataTask:(NSURLSessionDataTask *)dataTask
  434. willCacheResponse:(NSCachedURLResponse *)proposedResponse
  435. completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
  436. // Identify the operation that runs this task and pass it the delegate method
  437. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  438. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:willCacheResponse:completionHandler:)]) {
  439. [dataOperation URLSession:session dataTask:dataTask willCacheResponse:proposedResponse completionHandler:completionHandler];
  440. } else {
  441. if (completionHandler) {
  442. completionHandler(proposedResponse);
  443. }
  444. }
  445. }
  446. #pragma mark NSURLSessionTaskDelegate
  447. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  448. // Identify the operation that runs this task and pass it the delegate method
  449. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  450. if (dataOperation) {
  451. @synchronized (dataOperation) {
  452. // Mark the downloader operation `isCompleted = YES`, no longer re-use this operation when new request comes in
  453. SDWebImageDownloaderOperationSetCompleted(dataOperation, YES);
  454. }
  455. }
  456. if ([dataOperation respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) {
  457. [dataOperation URLSession:session task:task didCompleteWithError:error];
  458. }
  459. }
  460. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {
  461. // Identify the operation that runs this task and pass it the delegate method
  462. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  463. if ([dataOperation respondsToSelector:@selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)]) {
  464. [dataOperation URLSession:session task:task willPerformHTTPRedirection:response newRequest:request completionHandler:completionHandler];
  465. } else {
  466. if (completionHandler) {
  467. completionHandler(request);
  468. }
  469. }
  470. }
  471. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
  472. // Identify the operation that runs this task and pass it the delegate method
  473. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  474. if ([dataOperation respondsToSelector:@selector(URLSession:task:didReceiveChallenge:completionHandler:)]) {
  475. [dataOperation URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
  476. } else {
  477. if (completionHandler) {
  478. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  479. }
  480. }
  481. }
  482. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) {
  483. // Identify the operation that runs this task and pass it the delegate method
  484. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  485. if ([dataOperation respondsToSelector:@selector(URLSession:task:didFinishCollectingMetrics:)]) {
  486. [dataOperation URLSession:session task:task didFinishCollectingMetrics:metrics];
  487. }
  488. }
  489. @end
  490. @implementation SDWebImageDownloadToken
  491. - (void)dealloc {
  492. [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:nil];
  493. [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadStopNotification object:nil];
  494. }
  495. - (instancetype)initWithDownloadOperation:(NSOperation<SDWebImageDownloaderOperation> *)downloadOperation {
  496. self = [super init];
  497. if (self) {
  498. _downloadOperation = downloadOperation;
  499. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation];
  500. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidStop:) name:SDWebImageDownloadStopNotification object:downloadOperation];
  501. }
  502. return self;
  503. }
  504. - (void)downloadDidReceiveResponse:(NSNotification *)notification {
  505. NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
  506. if (downloadOperation && downloadOperation == self.downloadOperation) {
  507. self.response = downloadOperation.response;
  508. }
  509. }
  510. - (void)downloadDidStop:(NSNotification *)notification {
  511. NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
  512. if (downloadOperation && downloadOperation == self.downloadOperation) {
  513. if ([downloadOperation respondsToSelector:@selector(metrics)]) {
  514. if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) {
  515. self.metrics = downloadOperation.metrics;
  516. }
  517. }
  518. }
  519. }
  520. - (void)cancel {
  521. @synchronized (self) {
  522. if (self.isCancelled) {
  523. return;
  524. }
  525. self.cancelled = YES;
  526. [self.downloadOperation cancel:self.downloadOperationCancelToken];
  527. self.downloadOperationCancelToken = nil;
  528. }
  529. }
  530. @end
  531. @implementation SDWebImageDownloader (SDImageLoader)
  532. - (BOOL)canRequestImageForURL:(NSURL *)url {
  533. return [self canRequestImageForURL:url options:0 context:nil];
  534. }
  535. - (BOOL)canRequestImageForURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context {
  536. if (!url) {
  537. return NO;
  538. }
  539. // Always pass YES to let URLSession or custom download operation to determine
  540. return YES;
  541. }
  542. - (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
  543. UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage];
  544. SDWebImageDownloaderOptions downloaderOptions = 0;
  545. if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
  546. if (options & SDWebImageProgressiveLoad) downloaderOptions |= SDWebImageDownloaderProgressiveLoad;
  547. if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;
  548. if (options & SDWebImageContinueInBackground) downloaderOptions |= SDWebImageDownloaderContinueInBackground;
  549. if (options & SDWebImageHandleCookies) downloaderOptions |= SDWebImageDownloaderHandleCookies;
  550. if (options & SDWebImageAllowInvalidSSLCertificates) downloaderOptions |= SDWebImageDownloaderAllowInvalidSSLCertificates;
  551. if (options & SDWebImageHighPriority) downloaderOptions |= SDWebImageDownloaderHighPriority;
  552. if (options & SDWebImageScaleDownLargeImages) downloaderOptions |= SDWebImageDownloaderScaleDownLargeImages;
  553. if (options & SDWebImageAvoidDecodeImage) downloaderOptions |= SDWebImageDownloaderAvoidDecodeImage;
  554. if (options & SDWebImageDecodeFirstFrameOnly) downloaderOptions |= SDWebImageDownloaderDecodeFirstFrameOnly;
  555. if (options & SDWebImagePreloadAllFrames) downloaderOptions |= SDWebImageDownloaderPreloadAllFrames;
  556. if (options & SDWebImageMatchAnimatedImageClass) downloaderOptions |= SDWebImageDownloaderMatchAnimatedImageClass;
  557. if (cachedImage && options & SDWebImageRefreshCached) {
  558. // force progressive off if image already cached but forced refreshing
  559. downloaderOptions &= ~SDWebImageDownloaderProgressiveLoad;
  560. // ignore image read from NSURLCache if image if cached but force refreshing
  561. downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
  562. }
  563. return [self downloadImageWithURL:url options:downloaderOptions context:context progress:progressBlock completed:completedBlock];
  564. }
  565. - (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error {
  566. return [self shouldBlockFailedURLWithURL:url error:error options:0 context:nil];
  567. }
  568. - (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error options:(SDWebImageOptions)options context:(SDWebImageContext *)context {
  569. BOOL shouldBlockFailedURL;
  570. // Filter the error domain and check error codes
  571. if ([error.domain isEqualToString:SDWebImageErrorDomain]) {
  572. shouldBlockFailedURL = ( error.code == SDWebImageErrorInvalidURL
  573. || error.code == SDWebImageErrorBadImageData);
  574. } else if ([error.domain isEqualToString:NSURLErrorDomain]) {
  575. shouldBlockFailedURL = ( error.code != NSURLErrorNotConnectedToInternet
  576. && error.code != NSURLErrorCancelled
  577. && error.code != NSURLErrorTimedOut
  578. && error.code != NSURLErrorInternationalRoamingOff
  579. && error.code != NSURLErrorDataNotAllowed
  580. && error.code != NSURLErrorCannotFindHost
  581. && error.code != NSURLErrorCannotConnectToHost
  582. && error.code != NSURLErrorNetworkConnectionLost);
  583. } else {
  584. shouldBlockFailedURL = NO;
  585. }
  586. return shouldBlockFailedURL;
  587. }
  588. @end