SDWebImageDownloader.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. #pragma clang diagnostic push
  259. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  260. + (SDWebImageOptions)imageOptionsFromDownloaderOptions:(SDWebImageDownloaderOptions)downloadOptions {
  261. SDWebImageOptions options = 0;
  262. if (downloadOptions & SDWebImageDownloaderScaleDownLargeImages) options |= SDWebImageScaleDownLargeImages;
  263. if (downloadOptions & SDWebImageDownloaderDecodeFirstFrameOnly) options |= SDWebImageDecodeFirstFrameOnly;
  264. if (downloadOptions & SDWebImageDownloaderPreloadAllFrames) options |= SDWebImagePreloadAllFrames;
  265. if (downloadOptions & SDWebImageDownloaderAvoidDecodeImage) options |= SDWebImageAvoidDecodeImage;
  266. if (downloadOptions & SDWebImageDownloaderMatchAnimatedImageClass) options |= SDWebImageMatchAnimatedImageClass;
  267. return options;
  268. }
  269. #pragma clang diagnostic pop
  270. - (nullable NSOperation<SDWebImageDownloaderOperation> *)createDownloaderOperationWithUrl:(nonnull NSURL *)url
  271. options:(SDWebImageDownloaderOptions)options
  272. context:(nullable SDWebImageContext *)context {
  273. NSTimeInterval timeoutInterval = self.config.downloadTimeout;
  274. if (timeoutInterval == 0.0) {
  275. timeoutInterval = 15.0;
  276. }
  277. // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise
  278. NSURLRequestCachePolicy cachePolicy = options & SDWebImageDownloaderUseNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData;
  279. NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:cachePolicy timeoutInterval:timeoutInterval];
  280. mutableRequest.HTTPShouldHandleCookies = SD_OPTIONS_CONTAINS(options, SDWebImageDownloaderHandleCookies);
  281. mutableRequest.HTTPShouldUsePipelining = YES;
  282. SD_LOCK(_HTTPHeadersLock);
  283. mutableRequest.allHTTPHeaderFields = self.HTTPHeaders;
  284. SD_UNLOCK(_HTTPHeadersLock);
  285. // Context Option
  286. SDWebImageMutableContext *mutableContext;
  287. if (context) {
  288. mutableContext = [context mutableCopy];
  289. } else {
  290. mutableContext = [NSMutableDictionary dictionary];
  291. }
  292. // Request Modifier
  293. id<SDWebImageDownloaderRequestModifier> requestModifier;
  294. if ([context valueForKey:SDWebImageContextDownloadRequestModifier]) {
  295. requestModifier = [context valueForKey:SDWebImageContextDownloadRequestModifier];
  296. } else {
  297. requestModifier = self.requestModifier;
  298. }
  299. NSURLRequest *request;
  300. if (requestModifier) {
  301. NSURLRequest *modifiedRequest = [requestModifier modifiedRequestWithRequest:[mutableRequest copy]];
  302. // If modified request is nil, early return
  303. if (!modifiedRequest) {
  304. return nil;
  305. } else {
  306. request = [modifiedRequest copy];
  307. }
  308. } else {
  309. request = [mutableRequest copy];
  310. }
  311. // Response Modifier
  312. id<SDWebImageDownloaderResponseModifier> responseModifier;
  313. if ([context valueForKey:SDWebImageContextDownloadResponseModifier]) {
  314. responseModifier = [context valueForKey:SDWebImageContextDownloadResponseModifier];
  315. } else {
  316. responseModifier = self.responseModifier;
  317. }
  318. if (responseModifier) {
  319. mutableContext[SDWebImageContextDownloadResponseModifier] = responseModifier;
  320. }
  321. // Decryptor
  322. id<SDWebImageDownloaderDecryptor> decryptor;
  323. if ([context valueForKey:SDWebImageContextDownloadDecryptor]) {
  324. decryptor = [context valueForKey:SDWebImageContextDownloadDecryptor];
  325. } else {
  326. decryptor = self.decryptor;
  327. }
  328. if (decryptor) {
  329. mutableContext[SDWebImageContextDownloadDecryptor] = decryptor;
  330. }
  331. context = [mutableContext copy];
  332. // Operation Class
  333. Class operationClass = self.config.operationClass;
  334. if (!operationClass) {
  335. operationClass = [SDWebImageDownloaderOperation class];
  336. }
  337. NSOperation<SDWebImageDownloaderOperation> *operation = [[operationClass alloc] initWithRequest:request inSession:self.session options:options context:context];
  338. if ([operation respondsToSelector:@selector(setCredential:)]) {
  339. if (self.config.urlCredential) {
  340. operation.credential = self.config.urlCredential;
  341. } else if (self.config.username && self.config.password) {
  342. operation.credential = [NSURLCredential credentialWithUser:self.config.username password:self.config.password persistence:NSURLCredentialPersistenceForSession];
  343. }
  344. }
  345. if ([operation respondsToSelector:@selector(setMinimumProgressInterval:)]) {
  346. operation.minimumProgressInterval = MIN(MAX(self.config.minimumProgressInterval, 0), 1);
  347. }
  348. if ([operation respondsToSelector:@selector(setAcceptableStatusCodes:)]) {
  349. operation.acceptableStatusCodes = self.config.acceptableStatusCodes;
  350. }
  351. if ([operation respondsToSelector:@selector(setAcceptableContentTypes:)]) {
  352. operation.acceptableContentTypes = self.config.acceptableContentTypes;
  353. }
  354. if (options & SDWebImageDownloaderHighPriority) {
  355. operation.queuePriority = NSOperationQueuePriorityHigh;
  356. } else if (options & SDWebImageDownloaderLowPriority) {
  357. operation.queuePriority = NSOperationQueuePriorityLow;
  358. }
  359. if (self.config.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) {
  360. // Emulate LIFO execution order by systematically, each previous adding operation can dependency the new operation
  361. // This can gurantee the new operation to be execulated firstly, even if when some operations finished, meanwhile you appending new operations
  362. // Just make last added operation dependents new operation can not solve this problem. See test case #test15DownloaderLIFOExecutionOrder
  363. for (NSOperation *pendingOperation in self.downloadQueue.operations) {
  364. [pendingOperation addDependency:operation];
  365. }
  366. }
  367. return operation;
  368. }
  369. - (void)cancelAllDownloads {
  370. [self.downloadQueue cancelAllOperations];
  371. }
  372. #pragma mark - Properties
  373. - (BOOL)isSuspended {
  374. return self.downloadQueue.isSuspended;
  375. }
  376. - (void)setSuspended:(BOOL)suspended {
  377. self.downloadQueue.suspended = suspended;
  378. }
  379. - (NSUInteger)currentDownloadCount {
  380. return self.downloadQueue.operationCount;
  381. }
  382. - (NSURLSessionConfiguration *)sessionConfiguration {
  383. return self.session.configuration;
  384. }
  385. #pragma mark - KVO
  386. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
  387. if (context == SDWebImageDownloaderContext) {
  388. if ([keyPath isEqualToString:NSStringFromSelector(@selector(maxConcurrentDownloads))]) {
  389. self.downloadQueue.maxConcurrentOperationCount = self.config.maxConcurrentDownloads;
  390. }
  391. } else {
  392. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  393. }
  394. }
  395. #pragma mark Helper methods
  396. - (NSOperation<SDWebImageDownloaderOperation> *)operationWithTask:(NSURLSessionTask *)task {
  397. NSOperation<SDWebImageDownloaderOperation> *returnOperation = nil;
  398. for (NSOperation<SDWebImageDownloaderOperation> *operation in self.downloadQueue.operations) {
  399. if ([operation respondsToSelector:@selector(dataTask)]) {
  400. // So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes.
  401. NSURLSessionTask *operationTask;
  402. @synchronized (operation) {
  403. operationTask = operation.dataTask;
  404. }
  405. if (operationTask.taskIdentifier == task.taskIdentifier) {
  406. returnOperation = operation;
  407. break;
  408. }
  409. }
  410. }
  411. return returnOperation;
  412. }
  413. #pragma mark NSURLSessionDataDelegate
  414. - (void)URLSession:(NSURLSession *)session
  415. dataTask:(NSURLSessionDataTask *)dataTask
  416. didReceiveResponse:(NSURLResponse *)response
  417. completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
  418. // Identify the operation that runs this task and pass it the delegate method
  419. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  420. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveResponse:completionHandler:)]) {
  421. [dataOperation URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler];
  422. } else {
  423. if (completionHandler) {
  424. completionHandler(NSURLSessionResponseAllow);
  425. }
  426. }
  427. }
  428. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  429. // Identify the operation that runs this task and pass it the delegate method
  430. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  431. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) {
  432. [dataOperation URLSession:session dataTask:dataTask didReceiveData:data];
  433. }
  434. }
  435. - (void)URLSession:(NSURLSession *)session
  436. dataTask:(NSURLSessionDataTask *)dataTask
  437. willCacheResponse:(NSCachedURLResponse *)proposedResponse
  438. completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler {
  439. // Identify the operation that runs this task and pass it the delegate method
  440. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:dataTask];
  441. if ([dataOperation respondsToSelector:@selector(URLSession:dataTask:willCacheResponse:completionHandler:)]) {
  442. [dataOperation URLSession:session dataTask:dataTask willCacheResponse:proposedResponse completionHandler:completionHandler];
  443. } else {
  444. if (completionHandler) {
  445. completionHandler(proposedResponse);
  446. }
  447. }
  448. }
  449. #pragma mark NSURLSessionTaskDelegate
  450. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  451. // Identify the operation that runs this task and pass it the delegate method
  452. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  453. if (dataOperation) {
  454. @synchronized (dataOperation) {
  455. // Mark the downloader operation `isCompleted = YES`, no longer re-use this operation when new request comes in
  456. SDWebImageDownloaderOperationSetCompleted(dataOperation, YES);
  457. }
  458. }
  459. if ([dataOperation respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) {
  460. [dataOperation URLSession:session task:task didCompleteWithError:error];
  461. }
  462. }
  463. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {
  464. // Identify the operation that runs this task and pass it the delegate method
  465. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  466. if ([dataOperation respondsToSelector:@selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)]) {
  467. [dataOperation URLSession:session task:task willPerformHTTPRedirection:response newRequest:request completionHandler:completionHandler];
  468. } else {
  469. if (completionHandler) {
  470. completionHandler(request);
  471. }
  472. }
  473. }
  474. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
  475. // Identify the operation that runs this task and pass it the delegate method
  476. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  477. if ([dataOperation respondsToSelector:@selector(URLSession:task:didReceiveChallenge:completionHandler:)]) {
  478. [dataOperation URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler];
  479. } else {
  480. if (completionHandler) {
  481. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  482. }
  483. }
  484. }
  485. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) {
  486. // Identify the operation that runs this task and pass it the delegate method
  487. NSOperation<SDWebImageDownloaderOperation> *dataOperation = [self operationWithTask:task];
  488. if ([dataOperation respondsToSelector:@selector(URLSession:task:didFinishCollectingMetrics:)]) {
  489. [dataOperation URLSession:session task:task didFinishCollectingMetrics:metrics];
  490. }
  491. }
  492. @end
  493. @implementation SDWebImageDownloadToken
  494. - (void)dealloc {
  495. [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadReceiveResponseNotification object:nil];
  496. [[NSNotificationCenter defaultCenter] removeObserver:self name:SDWebImageDownloadStopNotification object:nil];
  497. }
  498. - (instancetype)initWithDownloadOperation:(NSOperation<SDWebImageDownloaderOperation> *)downloadOperation {
  499. self = [super init];
  500. if (self) {
  501. _downloadOperation = downloadOperation;
  502. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidReceiveResponse:) name:SDWebImageDownloadReceiveResponseNotification object:downloadOperation];
  503. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(downloadDidStop:) name:SDWebImageDownloadStopNotification object:downloadOperation];
  504. }
  505. return self;
  506. }
  507. - (void)downloadDidReceiveResponse:(NSNotification *)notification {
  508. NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
  509. if (downloadOperation && downloadOperation == self.downloadOperation) {
  510. self.response = downloadOperation.response;
  511. }
  512. }
  513. - (void)downloadDidStop:(NSNotification *)notification {
  514. NSOperation<SDWebImageDownloaderOperation> *downloadOperation = notification.object;
  515. if (downloadOperation && downloadOperation == self.downloadOperation) {
  516. if ([downloadOperation respondsToSelector:@selector(metrics)]) {
  517. if (@available(iOS 10.0, tvOS 10.0, macOS 10.12, watchOS 3.0, *)) {
  518. self.metrics = downloadOperation.metrics;
  519. }
  520. }
  521. }
  522. }
  523. - (void)cancel {
  524. @synchronized (self) {
  525. if (self.isCancelled) {
  526. return;
  527. }
  528. self.cancelled = YES;
  529. [self.downloadOperation cancel:self.downloadOperationCancelToken];
  530. self.downloadOperationCancelToken = nil;
  531. }
  532. }
  533. @end
  534. @implementation SDWebImageDownloader (SDImageLoader)
  535. - (BOOL)canRequestImageForURL:(NSURL *)url {
  536. return [self canRequestImageForURL:url options:0 context:nil];
  537. }
  538. - (BOOL)canRequestImageForURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context {
  539. if (!url) {
  540. return NO;
  541. }
  542. // Always pass YES to let URLSession or custom download operation to determine
  543. return YES;
  544. }
  545. #pragma clang diagnostic push
  546. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  547. - (id<SDWebImageOperation>)requestImageWithURL:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
  548. UIImage *cachedImage = context[SDWebImageContextLoaderCachedImage];
  549. SDWebImageDownloaderOptions downloaderOptions = 0;
  550. if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
  551. if (options & SDWebImageProgressiveLoad) downloaderOptions |= SDWebImageDownloaderProgressiveLoad;
  552. if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;
  553. if (options & SDWebImageContinueInBackground) downloaderOptions |= SDWebImageDownloaderContinueInBackground;
  554. if (options & SDWebImageHandleCookies) downloaderOptions |= SDWebImageDownloaderHandleCookies;
  555. if (options & SDWebImageAllowInvalidSSLCertificates) downloaderOptions |= SDWebImageDownloaderAllowInvalidSSLCertificates;
  556. if (options & SDWebImageHighPriority) downloaderOptions |= SDWebImageDownloaderHighPriority;
  557. if (options & SDWebImageScaleDownLargeImages) downloaderOptions |= SDWebImageDownloaderScaleDownLargeImages;
  558. if (options & SDWebImageAvoidDecodeImage) downloaderOptions |= SDWebImageDownloaderAvoidDecodeImage;
  559. if (options & SDWebImageDecodeFirstFrameOnly) downloaderOptions |= SDWebImageDownloaderDecodeFirstFrameOnly;
  560. if (options & SDWebImagePreloadAllFrames) downloaderOptions |= SDWebImageDownloaderPreloadAllFrames;
  561. if (options & SDWebImageMatchAnimatedImageClass) downloaderOptions |= SDWebImageDownloaderMatchAnimatedImageClass;
  562. if (cachedImage && options & SDWebImageRefreshCached) {
  563. // force progressive off if image already cached but forced refreshing
  564. downloaderOptions &= ~SDWebImageDownloaderProgressiveLoad;
  565. // ignore image read from NSURLCache if image if cached but force refreshing
  566. downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
  567. }
  568. return [self downloadImageWithURL:url options:downloaderOptions context:context progress:progressBlock completed:completedBlock];
  569. }
  570. #pragma clang diagnostic pop
  571. - (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error {
  572. return [self shouldBlockFailedURLWithURL:url error:error options:0 context:nil];
  573. }
  574. - (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error options:(SDWebImageOptions)options context:(SDWebImageContext *)context {
  575. BOOL shouldBlockFailedURL;
  576. // Filter the error domain and check error codes
  577. if ([error.domain isEqualToString:SDWebImageErrorDomain]) {
  578. shouldBlockFailedURL = ( error.code == SDWebImageErrorInvalidURL
  579. || error.code == SDWebImageErrorBadImageData);
  580. } else if ([error.domain isEqualToString:NSURLErrorDomain]) {
  581. shouldBlockFailedURL = ( error.code != NSURLErrorNotConnectedToInternet
  582. && error.code != NSURLErrorCancelled
  583. && error.code != NSURLErrorTimedOut
  584. && error.code != NSURLErrorInternationalRoamingOff
  585. && error.code != NSURLErrorDataNotAllowed
  586. && error.code != NSURLErrorCannotFindHost
  587. && error.code != NSURLErrorCannotConnectToHost
  588. && error.code != NSURLErrorNetworkConnectionLost);
  589. } else {
  590. shouldBlockFailedURL = NO;
  591. }
  592. return shouldBlockFailedURL;
  593. }
  594. @end