SDWebImageDownloaderOperation.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <Foundation/Foundation.h>
  9. #import "SDWebImageDownloader.h"
  10. #import "SDWebImageOperation.h"
  11. /**
  12. Describes a downloader operation. If one wants to use a custom downloader op, it needs to inherit from `NSOperation` and conform to this protocol
  13. For the description about these methods, see `SDWebImageDownloaderOperation`
  14. @note If your custom operation class does not use `NSURLSession` at all, do not implement the optional methods and session delegate methods.
  15. */
  16. @protocol SDWebImageDownloaderOperation <NSURLSessionTaskDelegate, NSURLSessionDataDelegate>
  17. @required
  18. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  19. inSession:(nullable NSURLSession *)session
  20. options:(SDWebImageDownloaderOptions)options;
  21. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  22. inSession:(nullable NSURLSession *)session
  23. options:(SDWebImageDownloaderOptions)options
  24. context:(nullable SDWebImageContext *)context;
  25. - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  26. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  27. - (BOOL)cancel:(nullable id)token;
  28. @property (strong, nonatomic, readonly, nullable) NSURLRequest *request;
  29. @property (strong, nonatomic, readonly, nullable) NSURLResponse *response;
  30. @optional
  31. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  32. @property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
  33. // These operation-level config was inherited from downloader. See `SDWebImageDownloaderConfig` for documentation.
  34. @property (strong, nonatomic, nullable) NSURLCredential *credential;
  35. @property (assign, nonatomic) double minimumProgressInterval;
  36. @property (copy, nonatomic, nullable) NSIndexSet *acceptableStatusCodes;
  37. @property (copy, nonatomic, nullable) NSSet<NSString *> *acceptableContentTypes;
  38. @end
  39. /**
  40. The download operation class for SDWebImageDownloader.
  41. */
  42. @interface SDWebImageDownloaderOperation : NSOperation <SDWebImageDownloaderOperation>
  43. /**
  44. * The request used by the operation's task.
  45. */
  46. @property (strong, nonatomic, readonly, nullable) NSURLRequest *request;
  47. /**
  48. * The response returned by the operation's task.
  49. */
  50. @property (strong, nonatomic, readonly, nullable) NSURLResponse *response;
  51. /**
  52. * The operation's task
  53. */
  54. @property (strong, nonatomic, readonly, nullable) NSURLSessionTask *dataTask;
  55. /**
  56. * The collected metrics from `-URLSession:task:didFinishCollectingMetrics:`.
  57. * This can be used to collect the network metrics like download duration, DNS lookup duration, SSL handshake duration, etc. See Apple's documentation: https://developer.apple.com/documentation/foundation/urlsessiontaskmetrics
  58. */
  59. @property (strong, nonatomic, readonly, nullable) NSURLSessionTaskMetrics *metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
  60. /**
  61. * The credential used for authentication challenges in `-URLSession:task:didReceiveChallenge:completionHandler:`.
  62. *
  63. * This will be overridden by any shared credentials that exist for the username or password of the request URL, if present.
  64. */
  65. @property (strong, nonatomic, nullable) NSURLCredential *credential;
  66. /**
  67. * The minimum interval about progress percent during network downloading. Which means the next progress callback and current progress callback's progress percent difference should be larger or equal to this value. However, the final finish download progress callback does not get effected.
  68. * The value should be 0.0-1.0.
  69. * @note If you're using progressive decoding feature, this will also effect the image refresh rate.
  70. * @note This value may enhance the performance if you don't want progress callback too frequently.
  71. * Defaults to 0, which means each time we receive the new data from URLSession, we callback the progressBlock immediately.
  72. */
  73. @property (assign, nonatomic) double minimumProgressInterval;
  74. /**
  75. * Set the acceptable HTTP Response status code. The status code which beyond the range will mark the download operation failed.
  76. * For example, if we config [200, 400) but server response is 503, the download will fail with error code `SDWebImageErrorInvalidDownloadStatusCode`.
  77. * Defaults to [200,400). Nil means no validation at all.
  78. */
  79. @property (copy, nonatomic, nullable) NSIndexSet *acceptableStatusCodes;
  80. /**
  81. * Set the acceptable HTTP Response content type. The content type beyond the set will mark the download operation failed.
  82. * For example, if we config ["image/png"] but server response is "application/json", the download will fail with error code `SDWebImageErrorInvalidDownloadContentType`.
  83. * Normally you don't need this for image format detection because we use image's data file signature magic bytes: https://en.wikipedia.org/wiki/List_of_file_signatures
  84. * Defaults to nil. Nil means no validation at all.
  85. */
  86. @property (copy, nonatomic, nullable) NSSet<NSString *> *acceptableContentTypes;
  87. /**
  88. * The options for the receiver.
  89. */
  90. @property (assign, nonatomic, readonly) SDWebImageDownloaderOptions options;
  91. /**
  92. * The context for the receiver.
  93. */
  94. @property (copy, nonatomic, readonly, nullable) SDWebImageContext *context;
  95. /**
  96. * Initializes a `SDWebImageDownloaderOperation` object
  97. *
  98. * @see SDWebImageDownloaderOperation
  99. *
  100. * @param request the URL request
  101. * @param session the URL session in which this operation will run
  102. * @param options downloader options
  103. *
  104. * @return the initialized instance
  105. */
  106. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  107. inSession:(nullable NSURLSession *)session
  108. options:(SDWebImageDownloaderOptions)options;
  109. /**
  110. * Initializes a `SDWebImageDownloaderOperation` object
  111. *
  112. * @see SDWebImageDownloaderOperation
  113. *
  114. * @param request the URL request
  115. * @param session the URL session in which this operation will run
  116. * @param options downloader options
  117. * @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  118. *
  119. * @return the initialized instance
  120. */
  121. - (nonnull instancetype)initWithRequest:(nullable NSURLRequest *)request
  122. inSession:(nullable NSURLSession *)session
  123. options:(SDWebImageDownloaderOptions)options
  124. context:(nullable SDWebImageContext *)context NS_DESIGNATED_INITIALIZER;
  125. /**
  126. * Adds handlers for progress and completion. Returns a token that can be passed to -cancel: to cancel this set of
  127. * callbacks.
  128. *
  129. * @param progressBlock the block executed when a new chunk of data arrives.
  130. * @note the progress block is executed on a background queue
  131. * @param completedBlock the block executed when the download is done.
  132. * @note the completed block is executed on the main queue for success. If errors are found, there is a chance the block will be executed on a background queue
  133. *
  134. * @return the token to use to cancel this set of handlers
  135. */
  136. - (nullable id)addHandlersForProgress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  137. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  138. /**
  139. * Cancels a set of callbacks. Once all callbacks are canceled, the operation is cancelled.
  140. *
  141. * @param token the token representing a set of callbacks to cancel
  142. *
  143. * @return YES if the operation was stopped because this was the last token to be canceled. NO otherwise.
  144. */
  145. - (BOOL)cancel:(nullable id)token;
  146. @end