You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
9.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  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. @property(atomic, copy, GTM_NULLABLE)
  58. GTMSessionFetcherMetricsCollectionBlock metricsCollectionBlock API_AVAILABLE(
  59. ios(10.0), macosx(10.12), tvos(10.0), watchos(3.0));
  60. #if GTM_BACKGROUND_TASK_FETCHING
  61. @property(atomic, assign) BOOL skipBackgroundTask;
  62. #endif
  63. // A default useragent of GTMFetcherStandardUserAgentString(nil) will be given to each fetcher
  64. // created by this service unless the request already has a user-agent header set.
  65. // This default will be added starting with builds with the SDKs for OS X 10.11 and iOS 9.
  66. //
  67. // To use the configuration's default user agent, set this property to nil.
  68. @property(atomic, copy, GTM_NULLABLE) NSString *userAgent;
  69. // The authorizer to attach to the created fetchers. If a specific fetcher should
  70. // not authorize its requests, the fetcher's authorizer property may be set to nil
  71. // before the fetch begins.
  72. @property(atomic, strong, GTM_NULLABLE) id<GTMFetcherAuthorizationProtocol> authorizer;
  73. // Delegate queue used by the session when calling back to the fetcher. The default
  74. // is the main queue. Changing this does not affect the queue used to call back to the
  75. // application; that is specified by the callbackQueue property above.
  76. @property(atomic, strong, GTM_NULL_RESETTABLE) NSOperationQueue *sessionDelegateQueue;
  77. // When enabled, indicates the same session should be used by subsequent fetchers.
  78. //
  79. // This is enabled by default.
  80. @property(atomic, assign) BOOL reuseSession;
  81. // Sets the delay until an unused session is invalidated.
  82. // The default interval is 60 seconds.
  83. //
  84. // If the interval is set to 0, then any reused session is not invalidated except by
  85. // explicitly invoking -resetSession. Be aware that setting the interval to 0 thus
  86. // causes the session's delegate to be retained until the session is explicitly reset.
  87. @property(atomic, assign) NSTimeInterval unusedSessionTimeout;
  88. // If shouldReuseSession is enabled, this will force creation of a new session when future
  89. // fetchers begin.
  90. - (void)resetSession;
  91. // Create a fetcher
  92. //
  93. // These methods will return a fetcher. If successfully created, the connection
  94. // will hold a strong reference to it for the life of the connection as well.
  95. // So the caller doesn't have to hold onto the fetcher explicitly unless they
  96. // want to be able to monitor or cancel it.
  97. - (GTMSessionFetcher *)fetcherWithRequest:(NSURLRequest *)request;
  98. - (GTMSessionFetcher *)fetcherWithURL:(NSURL *)requestURL;
  99. - (GTMSessionFetcher *)fetcherWithURLString:(NSString *)requestURLString;
  100. // Common method for fetcher creation.
  101. //
  102. // -fetcherWithRequest:fetcherClass: may be overridden to customize creation of
  103. // fetchers. This is the ONLY method in the GTMSessionFetcher library intended to
  104. // be overridden.
  105. - (id)fetcherWithRequest:(NSURLRequest *)request
  106. fetcherClass:(Class)fetcherClass;
  107. - (BOOL)isDelayingFetcher:(GTMSessionFetcher *)fetcher;
  108. - (NSUInteger)numberOfFetchers; // running + delayed fetchers
  109. - (NSUInteger)numberOfRunningFetchers;
  110. - (NSUInteger)numberOfDelayedFetchers;
  111. // Return a list of all running or delayed fetchers. This includes fetchers created
  112. // by the service which have been started and have not yet stopped.
  113. //
  114. // Returns an array of fetcher objects, or nil if none.
  115. - (GTM_NULLABLE GTM_NSArrayOf(GTMSessionFetcher *) *)issuedFetchers;
  116. // Search for running or delayed fetchers with the specified URL.
  117. //
  118. // Returns an array of fetcher objects found, or nil if none found.
  119. - (GTM_NULLABLE GTM_NSArrayOf(GTMSessionFetcher *) *)issuedFetchersWithRequestURL:(NSURL *)requestURL;
  120. - (void)stopAllFetchers;
  121. // Methods for use by the fetcher class only.
  122. - (GTM_NULLABLE NSURLSession *)session;
  123. - (GTM_NULLABLE NSURLSession *)sessionForFetcherCreation;
  124. - (GTM_NULLABLE id<NSURLSessionDelegate>)sessionDelegate;
  125. - (GTM_NULLABLE NSDate *)stoppedAllFetchersDate;
  126. // The testBlock can inspect its fetcher parameter's request property to
  127. // determine which fetcher is being faked.
  128. @property(atomic, copy, GTM_NULLABLE) GTMSessionFetcherTestBlock testBlock;
  129. @end
  130. @interface GTMSessionFetcherService (TestingSupport)
  131. // Convenience methods to create a fetcher service for testing.
  132. //
  133. // Fetchers generated by this mock fetcher service will not perform any
  134. // network operation, but will invoke callbacks and provide the supplied data
  135. // or error to the completion handler.
  136. //
  137. // You can make more customized mocks by setting the test block property of the service
  138. // or fetcher; the test block can inspect the fetcher's request or other properties.
  139. //
  140. // See the description of the testBlock property below.
  141. + (instancetype)mockFetcherServiceWithFakedData:(GTM_NULLABLE NSData *)fakedDataOrNil
  142. fakedError:(GTM_NULLABLE NSError *)fakedErrorOrNil;
  143. + (instancetype)mockFetcherServiceWithFakedData:(GTM_NULLABLE NSData *)fakedDataOrNil
  144. fakedResponse:(NSHTTPURLResponse *)fakedResponse
  145. fakedError:(GTM_NULLABLE NSError *)fakedErrorOrNil;
  146. // Spin the run loop and discard events (or, if not on the main thread, just sleep the thread)
  147. // until all running and delayed fetchers have completed.
  148. //
  149. // This is only for use in testing or in tools without a user interface.
  150. //
  151. // Synchronous fetches should never be done by shipping apps; they are
  152. // sufficient reason for rejection from the app store.
  153. //
  154. // Returns NO if timed out.
  155. - (BOOL)waitForCompletionOfAllFetchersWithTimeout:(NSTimeInterval)timeoutInSeconds;
  156. @end
  157. @interface GTMSessionFetcherService (BackwardsCompatibilityOnly)
  158. // Clients using GTMSessionFetcher should set the cookie storage explicitly themselves.
  159. // This method is just for compatibility with the old fetcher.
  160. @property(atomic, assign) NSInteger cookieStorageMethod;
  161. @end
  162. GTM_ASSUME_NONNULL_END