GDTCORUploadCoordinator.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
  17. #import <GoogleDataTransport/GDTCORAssert.h>
  18. #import <GoogleDataTransport/GDTCORClock.h>
  19. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  20. #import <GoogleDataTransport/GDTCORReachability.h>
  21. #import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  22. @implementation GDTCORUploadCoordinator
  23. + (instancetype)sharedInstance {
  24. static GDTCORUploadCoordinator *sharedUploader;
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. sharedUploader = [[GDTCORUploadCoordinator alloc] init];
  28. [sharedUploader startTimer];
  29. });
  30. return sharedUploader;
  31. }
  32. - (instancetype)init {
  33. self = [super init];
  34. if (self) {
  35. _coordinationQueue =
  36. dispatch_queue_create("com.google.GDTCORUploadCoordinator", DISPATCH_QUEUE_SERIAL);
  37. _registrar = [GDTCORRegistrar sharedInstance];
  38. _timerInterval = 30 * NSEC_PER_SEC;
  39. _timerLeeway = 5 * NSEC_PER_SEC;
  40. _targetToInFlightPackages = [[NSMutableDictionary alloc] init];
  41. }
  42. return self;
  43. }
  44. - (void)forceUploadForTarget:(GDTCORTarget)target {
  45. dispatch_async(_coordinationQueue, ^{
  46. GDTCORLogDebug(@"Forcing an upload of target %ld", (long)target);
  47. GDTCORUploadConditions conditions = [self uploadConditions];
  48. conditions |= GDTCORUploadConditionHighPriority;
  49. [self uploadTargets:@[ @(target) ] conditions:conditions];
  50. });
  51. }
  52. #pragma mark - Private helper methods
  53. /** Starts a timer that checks whether or not events can be uploaded at regular intervals. It will
  54. * check the next-upload clocks of all targets to determine if an upload attempt can be made.
  55. */
  56. - (void)startTimer {
  57. dispatch_async(_coordinationQueue, ^{
  58. self->_timer =
  59. dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self->_coordinationQueue);
  60. dispatch_source_set_timer(self->_timer, DISPATCH_TIME_NOW, self->_timerInterval,
  61. self->_timerLeeway);
  62. dispatch_source_set_event_handler(self->_timer, ^{
  63. if (![[GDTCORApplication sharedApplication] isRunningInBackground]) {
  64. GDTCORUploadConditions conditions = [self uploadConditions];
  65. GDTCORLogDebug(@"%@", @"Upload timer fired");
  66. [self uploadTargets:[self.registrar.targetToUploader allKeys] conditions:conditions];
  67. }
  68. });
  69. GDTCORLogDebug(@"%@", @"Upload timer started");
  70. dispatch_resume(self->_timer);
  71. });
  72. }
  73. /** Stops the currently running timer. */
  74. - (void)stopTimer {
  75. if (_timer) {
  76. dispatch_source_cancel(_timer);
  77. }
  78. }
  79. /** Triggers the uploader implementations for the given targets to upload.
  80. *
  81. * @param targets An array of targets to trigger.
  82. * @param conditions The set of upload conditions.
  83. */
  84. - (void)uploadTargets:(NSArray<NSNumber *> *)targets conditions:(GDTCORUploadConditions)conditions {
  85. dispatch_async(_coordinationQueue, ^{
  86. if ((conditions & GDTCORUploadConditionNoNetwork) == GDTCORUploadConditionNoNetwork) {
  87. return;
  88. }
  89. for (NSNumber *target in targets) {
  90. // Don't trigger uploads for targets that have an in-flight package already.
  91. if (self->_targetToInFlightPackages[target]) {
  92. GDTCORLogDebug(@"Target %@ will not upload, there's an upload in flight", target);
  93. continue;
  94. }
  95. // Ask the uploader if they can upload and do so, if it can.
  96. id<GDTCORUploader> uploader = self.registrar.targetToUploader[target];
  97. if ([uploader readyToUploadTarget:target.intValue conditions:conditions]) {
  98. id<GDTCORPrioritizer> prioritizer = self.registrar.targetToPrioritizer[target];
  99. GDTCORUploadPackage *package = [prioritizer uploadPackageWithTarget:target.intValue
  100. conditions:conditions];
  101. if (package.events.count) {
  102. self->_targetToInFlightPackages[target] = package;
  103. GDTCORLogDebug(@"Package of %ld events is being handed over to an uploader",
  104. (long)package.events.count);
  105. [uploader uploadPackage:package];
  106. } else {
  107. [package completeDelivery];
  108. }
  109. }
  110. GDTCORLogDebug(@"Target %@ is not ready to upload", target);
  111. }
  112. });
  113. }
  114. /** Returns the registered storage for the given NSNumber wrapped GDTCORTarget.
  115. *
  116. * @param target The NSNumber wrapping of a GDTCORTarget to find the storage instance of.
  117. * @return The storage instance for the given target.
  118. */
  119. - (nullable id<GDTCORStorageProtocol>)storageForTarget:(NSNumber *)target {
  120. id<GDTCORStorageProtocol> storage = [GDTCORRegistrar sharedInstance].targetToStorage[target];
  121. GDTCORAssert(storage, @"A storage must be registered for target %@", target);
  122. return storage;
  123. }
  124. /** Returns the current upload conditions after making determinations about the network connection.
  125. *
  126. * @return The current upload conditions.
  127. */
  128. - (GDTCORUploadConditions)uploadConditions {
  129. GDTCORNetworkReachabilityFlags currentFlags = [GDTCORReachability currentFlags];
  130. BOOL networkConnected = GDTCORReachabilityFlagsReachable(currentFlags);
  131. if (!networkConnected) {
  132. return GDTCORUploadConditionNoNetwork;
  133. }
  134. BOOL isWWAN = GDTCORReachabilityFlagsContainWWAN(currentFlags);
  135. if (isWWAN) {
  136. return GDTCORUploadConditionMobileData;
  137. } else {
  138. return GDTCORUploadConditionWifiData;
  139. }
  140. }
  141. #pragma mark - NSSecureCoding support
  142. /** The NSKeyedCoder key for the targetToInFlightPackages property. */
  143. static NSString *const ktargetToInFlightPackagesKey =
  144. @"GDTCORUploadCoordinatortargetToInFlightPackages";
  145. + (BOOL)supportsSecureCoding {
  146. return YES;
  147. }
  148. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  149. GDTCORUploadCoordinator *sharedCoordinator = [GDTCORUploadCoordinator sharedInstance];
  150. dispatch_sync(sharedCoordinator->_coordinationQueue, ^{
  151. @try {
  152. NSSet *classes =
  153. [NSSet setWithObjects:[NSMutableDictionary class], [GDTCORUploadPackage class], nil];
  154. sharedCoordinator->_targetToInFlightPackages =
  155. [aDecoder decodeObjectOfClasses:classes forKey:ktargetToInFlightPackagesKey];
  156. } @catch (NSException *exception) {
  157. sharedCoordinator->_targetToInFlightPackages = [NSMutableDictionary dictionary];
  158. }
  159. });
  160. return sharedCoordinator;
  161. }
  162. - (void)encodeWithCoder:(NSCoder *)aCoder {
  163. dispatch_sync(_coordinationQueue, ^{
  164. // All packages that have been given to uploaders need to be tracked so that their expiration
  165. // timers can be called.
  166. if (self->_targetToInFlightPackages.count > 0) {
  167. [aCoder encodeObject:self->_targetToInFlightPackages forKey:ktargetToInFlightPackagesKey];
  168. }
  169. });
  170. }
  171. #pragma mark - GDTCORLifecycleProtocol
  172. - (void)appWillForeground:(GDTCORApplication *)app {
  173. // -startTimer is thread-safe.
  174. [self startTimer];
  175. }
  176. - (void)appWillBackground:(GDTCORApplication *)app {
  177. dispatch_async(_coordinationQueue, ^{
  178. [self stopTimer];
  179. });
  180. }
  181. - (void)appWillTerminate:(GDTCORApplication *)application {
  182. dispatch_sync(_coordinationQueue, ^{
  183. [self stopTimer];
  184. });
  185. }
  186. #pragma mark - GDTCORUploadPackageProtocol
  187. - (void)packageDelivered:(GDTCORUploadPackage *)package successful:(BOOL)successful {
  188. if (!_coordinationQueue) {
  189. return;
  190. }
  191. dispatch_async(_coordinationQueue, ^{
  192. NSNumber *targetNumber = @(package.target);
  193. NSMutableDictionary<NSNumber *, GDTCORUploadPackage *> *targetToInFlightPackages =
  194. self->_targetToInFlightPackages;
  195. GDTCORRegistrar *registrar = self->_registrar;
  196. if (targetToInFlightPackages) {
  197. [targetToInFlightPackages removeObjectForKey:targetNumber];
  198. }
  199. NSSet<GDTCOREvent *> *packageEvents = [package.events copy];
  200. if (registrar) {
  201. id<GDTCORPrioritizer> prioritizer = registrar.targetToPrioritizer[targetNumber];
  202. if (!prioritizer) {
  203. GDTCORLogError(GDTCORMCEPrioritizerError,
  204. @"A prioritizer should be registered for this target: %@", targetNumber);
  205. }
  206. if ([prioritizer respondsToSelector:@selector(packageDelivered:successful:)]) {
  207. [prioritizer packageDelivered:[package copy] successful:successful];
  208. }
  209. }
  210. if (successful && packageEvents.count) {
  211. NSMutableSet *eventIDs = [[NSMutableSet alloc] init];
  212. for (GDTCOREvent *event in packageEvents) {
  213. NSNumber *eventID = event.eventID;
  214. if (eventID != nil) {
  215. [eventIDs addObject:eventID];
  216. } else {
  217. GDTCORLogDebug(@"An event was missing its ID: %@", event);
  218. }
  219. }
  220. [[self storageForTarget:@(package.target)] removeEvents:eventIDs];
  221. }
  222. });
  223. }
  224. - (void)packageExpired:(GDTCORUploadPackage *)package {
  225. if (!_coordinationQueue) {
  226. return;
  227. }
  228. dispatch_async(_coordinationQueue, ^{
  229. NSNumber *targetNumber = @(package.target);
  230. NSMutableDictionary<NSNumber *, GDTCORUploadPackage *> *targetToInFlightPackages =
  231. self->_targetToInFlightPackages;
  232. GDTCORRegistrar *registrar = self->_registrar;
  233. if (targetToInFlightPackages) {
  234. [targetToInFlightPackages removeObjectForKey:targetNumber];
  235. }
  236. if (registrar) {
  237. id<GDTCORPrioritizer> prioritizer = registrar.targetToPrioritizer[targetNumber];
  238. id<GDTCORUploader> uploader = registrar.targetToUploader[targetNumber];
  239. if ([prioritizer respondsToSelector:@selector(packageExpired:)]) {
  240. [prioritizer packageExpired:package];
  241. }
  242. if ([uploader respondsToSelector:@selector(packageExpired:)]) {
  243. [uploader packageExpired:package];
  244. }
  245. }
  246. });
  247. }
  248. @end