Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Copyright 2014 Google Inc. All rights reserved.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. // For best performance and convenient usage, fetchers should be generated by a common
  16. // GTMSessionFetcherService instance, like
  17. //
  18. // _fetcherService = [[GTMSessionFetcherService alloc] init];
  19. // GTMSessionFetcher* myFirstFetcher = [_fetcherService fetcherWithRequest:request1];
  20. // GTMSessionFetcher* mySecondFetcher = [_fetcherService fetcherWithRequest:request2];
  21. #import "GTMSessionFetcher.h"
  22. GTM_ASSUME_NONNULL_BEGIN
  23. // Notifications.
  24. // This notification indicates a reusable session has become invalid. It is intended mainly for the
  25. // service's unit tests.
  26. //
  27. // The notification object is the fetcher service.
  28. // The invalid session is provided via the userInfo kGTMSessionFetcherServiceSessionKey key.
  29. extern NSString *const kGTMSessionFetcherServiceSessionBecameInvalidNotification;
  30. extern NSString *const kGTMSessionFetcherServiceSessionKey;
  31. @interface GTMSessionFetcherService : NSObject<GTMSessionFetcherServiceProtocol>
  32. // Queues of delayed and running fetchers. Each dictionary contains arrays
  33. // of GTMSessionFetcher *fetchers, keyed by NSString *host
  34. @property(atomic, strong, readonly, GTM_NULLABLE) GTM_NSDictionaryOf(NSString *, NSArray *) *delayedFetchersByHost;
  35. @property(atomic, strong, readonly, GTM_NULLABLE) GTM_NSDictionaryOf(NSString *, NSArray *) *runningFetchersByHost;
  36. // A max value of 0 means no fetchers should be delayed.
  37. // The default limit is 10 simultaneous fetchers targeting each host.
  38. // This does not apply to fetchers whose useBackgroundSession property is YES. Since services are
  39. // not resurrected on an app relaunch, delayed fetchers would effectively be abandoned.
  40. @property(atomic, assign) NSUInteger maxRunningFetchersPerHost;
  41. // Properties to be applied to each fetcher; see GTMSessionFetcher.h for descriptions
  42. @property(atomic, strong, GTM_NULLABLE) NSURLSessionConfiguration *configuration;
  43. @property(atomic, copy, GTM_NULLABLE) GTMSessionFetcherConfigurationBlock configurationBlock;
  44. @property(atomic, strong, GTM_NULLABLE) NSHTTPCookieStorage *cookieStorage;
  45. @property(atomic, strong, GTM_NULL_RESETTABLE) dispatch_queue_t callbackQueue;
  46. @property(atomic, copy, GTM_NULLABLE) GTMSessionFetcherChallengeBlock challengeBlock;
  47. @property(atomic, strong, GTM_NULLABLE) NSURLCredential *credential;
  48. @property(atomic, strong) NSURLCredential *proxyCredential;
  49. @property(atomic, copy, GTM_NULLABLE) GTM_NSArrayOf(NSString *) *allowedInsecureSchemes;
  50. @property(atomic, assign) BOOL allowLocalhostRequest;
  51. @property(atomic, assign) BOOL allowInvalidServerCertificates;
  52. @property(atomic, assign, getter=isRetryEnabled) BOOL retryEnabled;
  53. @property(atomic, copy, GTM_NULLABLE) GTMSessionFetcherRetryBlock retryBlock;
  54. @property(atomic, assign) NSTimeInterval maxRetryInterval;
  55. @property(atomic, assign) NSTimeInterval minRetryInterval;
  56. @property(atomic, copy, GTM_NULLABLE) GTM_NSDictionaryOf(NSString *, id) *properties;
  57. #if GTM_BACKGROUND_TASK_FETCHING
  58. @property(atomic, assign) BOOL skipBackgroundTask;
  59. #endif
  60. // A default useragent of GTMFetcherStandardUserAgentString(nil) will be given to each fetcher
  61. // created by this service unless the request already has a user-agent header set.
  62. // This default will be added starting with builds with the SDKs for OS X 10.11 and iOS 9.
  63. //
  64. // To use the configuration's default user agent, set this property to nil.
  65. @property(atomic, copy, GTM_NULLABLE) NSString *userAgent;
  66. // The authorizer to attach to the created fetchers. If a specific fetcher should
  67. // not authorize its requests, the fetcher's authorizer property may be set to nil
  68. // before the fetch begins.
  69. @property(atomic, strong, GTM_NULLABLE) id<GTMFetcherAuthorizationProtocol> authorizer;
  70. // Delegate queue used by the session when calling back to the fetcher. The default
  71. // is the main queue. Changing this does not affect the queue used to call back to the
  72. // application; that is specified by the callbackQueue property above.
  73. @property(atomic, strong, GTM_NULL_RESETTABLE) NSOperationQueue *sessionDelegateQueue;
  74. // When enabled, indicates the same session should be used by subsequent fetchers.
  75. //
  76. // This is enabled by default.
  77. @property(atomic, assign) BOOL reuseSession;
  78. // Sets the delay until an unused session is invalidated.
  79. // The default interval is 60 seconds.
  80. //
  81. // If the interval is set to 0, then any reused session is not invalidated except by
  82. // explicitly invoking -resetSession. Be aware that setting the interval to 0 thus
  83. // causes the session's delegate to be retained until the session is explicitly reset.
  84. @property(atomic, assign) NSTimeInterval unusedSessionTimeout;
  85. // If shouldReuseSession is enabled, this will force creation of a new session when future
  86. // fetchers begin.
  87. - (void)resetSession;
  88. // Create a fetcher
  89. //
  90. // These methods will return a fetcher. If successfully created, the connection
  91. // will hold a strong reference to it for the life of the connection as well.
  92. // So the caller doesn't have to hold onto the fetcher explicitly unless they
  93. // want to be able to monitor or cancel it.
  94. - (GTMSessionFetcher *)fetcherWithRequest:(NSURLRequest *)request;
  95. - (GTMSessionFetcher *)fetcherWithURL:(NSURL *)requestURL;
  96. - (GTMSessionFetcher *)fetcherWithURLString:(NSString *)requestURLString;
  97. // Common method for fetcher creation.
  98. //
  99. // -fetcherWithRequest:fetcherClass: may be overridden to customize creation of
  100. // fetchers. This is the ONLY method in the GTMSessionFetcher library intended to
  101. // be overridden.
  102. - (id)fetcherWithRequest:(NSURLRequest *)request
  103. fetcherClass:(Class)fetcherClass;
  104. - (BOOL)isDelayingFetcher:(GTMSessionFetcher *)fetcher;
  105. - (NSUInteger)numberOfFetchers; // running + delayed fetchers
  106. - (NSUInteger)numberOfRunningFetchers;
  107. - (NSUInteger)numberOfDelayedFetchers;
  108. // Return a list of all running or delayed fetchers. This includes fetchers created
  109. // by the service which have been started and have not yet stopped.
  110. //
  111. // Returns an array of fetcher objects, or nil if none.
  112. - (GTM_NULLABLE GTM_NSArrayOf(GTMSessionFetcher *) *)issuedFetchers;
  113. // Search for running or delayed fetchers with the specified URL.
  114. //
  115. // Returns an array of fetcher objects found, or nil if none found.
  116. - (GTM_NULLABLE GTM_NSArrayOf(GTMSessionFetcher *) *)issuedFetchersWithRequestURL:(NSURL *)requestURL;
  117. - (void)stopAllFetchers;
  118. // Methods for use by the fetcher class only.
  119. - (GTM_NULLABLE NSURLSession *)session;
  120. - (GTM_NULLABLE NSURLSession *)sessionForFetcherCreation;
  121. - (GTM_NULLABLE id<NSURLSessionDelegate>)sessionDelegate;
  122. - (GTM_NULLABLE NSDate *)stoppedAllFetchersDate;
  123. // The testBlock can inspect its fetcher parameter's request property to
  124. // determine which fetcher is being faked.
  125. @property(atomic, copy, GTM_NULLABLE) GTMSessionFetcherTestBlock testBlock;
  126. @end
  127. @interface GTMSessionFetcherService (TestingSupport)
  128. // Convenience methods to create a fetcher service for testing.
  129. //
  130. // Fetchers generated by this mock fetcher service will not perform any
  131. // network operation, but will invoke callbacks and provide the supplied data
  132. // or error to the completion handler.
  133. //
  134. // You can make more customized mocks by setting the test block property of the service
  135. // or fetcher; the test block can inspect the fetcher's request or other properties.
  136. //
  137. // See the description of the testBlock property below.
  138. + (instancetype)mockFetcherServiceWithFakedData:(GTM_NULLABLE NSData *)fakedDataOrNil
  139. fakedError:(GTM_NULLABLE NSError *)fakedErrorOrNil;
  140. + (instancetype)mockFetcherServiceWithFakedData:(GTM_NULLABLE NSData *)fakedDataOrNil
  141. fakedResponse:(NSHTTPURLResponse *)fakedResponse
  142. fakedError:(GTM_NULLABLE NSError *)fakedErrorOrNil;
  143. // Spin the run loop and discard events (or, if not on the main thread, just sleep the thread)
  144. // until all running and delayed fetchers have completed.
  145. //
  146. // This is only for use in testing or in tools without a user interface.
  147. //
  148. // Synchronous fetches should never be done by shipping apps; they are
  149. // sufficient reason for rejection from the app store.
  150. //
  151. // Returns NO if timed out.
  152. - (BOOL)waitForCompletionOfAllFetchersWithTimeout:(NSTimeInterval)timeoutInSeconds;
  153. @end
  154. @interface GTMSessionFetcherService (BackwardsCompatibilityOnly)
  155. // Clients using GTMSessionFetcher should set the cookie storage explicitly themselves.
  156. // This method is just for compatibility with the old fetcher.
  157. @property(atomic, assign) NSInteger cookieStorageMethod;
  158. @end
  159. GTM_ASSUME_NONNULL_END