GDTCORUploadPackage.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2019 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/Public/GDTCORUploadPackage.h"
  17. #import <GoogleDataTransport/GDTCORClock.h>
  18. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  19. #import "GDTCORLibrary/Private/GDTCORStorage_Private.h"
  20. #import "GDTCORLibrary/Private/GDTCORUploadCoordinator.h"
  21. #import "GDTCORLibrary/Private/GDTCORUploadPackage_Private.h"
  22. @implementation GDTCORUploadPackage {
  23. /** If YES, the package's -completeDelivery method has been called. */
  24. BOOL _isDelivered;
  25. /** If YES, is being handled by the handler. */
  26. BOOL _isHandled;
  27. /** A timer that will regularly check to see whether this package has expired or not. */
  28. NSTimer *_expirationTimer;
  29. }
  30. - (instancetype)initWithTarget:(GDTCORTarget)target {
  31. self = [super init];
  32. if (self) {
  33. _target = target;
  34. _storage = [GDTCORStorage sharedInstance];
  35. _deliverByTime = [GDTCORClock clockSnapshotInTheFuture:180000];
  36. _handler = [GDTCORUploadCoordinator sharedInstance];
  37. _expirationTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
  38. target:self
  39. selector:@selector(checkIfPackageIsExpired:)
  40. userInfo:nil
  41. repeats:YES];
  42. }
  43. GDTCORLogDebug("Upload package created %@", self);
  44. return self;
  45. }
  46. - (instancetype)copy {
  47. GDTCORUploadPackage *newPackage = [[GDTCORUploadPackage alloc] initWithTarget:_target];
  48. newPackage->_events = [_events copy];
  49. GDTCORLogDebug("Copying UploadPackage %@ to %@", self, newPackage);
  50. return newPackage;
  51. }
  52. - (NSUInteger)hash {
  53. return [_events hash];
  54. }
  55. - (BOOL)isEqual:(id)object {
  56. return [self hash] == [object hash];
  57. }
  58. - (void)dealloc {
  59. [_expirationTimer invalidate];
  60. }
  61. - (void)setStorage:(GDTCORStorage *)storage {
  62. if (storage != _storage) {
  63. _storage = storage;
  64. }
  65. }
  66. - (void)completeDelivery {
  67. if (_isDelivered) {
  68. GDTCORLogError(GDTCORMCEDeliverTwice, @"%@",
  69. @"It's an API violation to call -completeDelivery twice.");
  70. }
  71. _isDelivered = YES;
  72. if (!_isHandled && _handler &&
  73. [_handler respondsToSelector:@selector(packageDelivered:successful:)]) {
  74. [_expirationTimer invalidate];
  75. _isHandled = YES;
  76. [_handler packageDelivered:self successful:YES];
  77. }
  78. GDTCORLogDebug("Upload package delivered: %@", self);
  79. }
  80. - (void)retryDeliveryInTheFuture {
  81. if (!_isHandled && _handler &&
  82. [_handler respondsToSelector:@selector(packageDelivered:successful:)]) {
  83. [_expirationTimer invalidate];
  84. _isHandled = YES;
  85. [_handler packageDelivered:self successful:NO];
  86. }
  87. GDTCORLogDebug("Upload package will retry in the future: %@", self);
  88. }
  89. - (void)checkIfPackageIsExpired:(NSTimer *)timer {
  90. if ([[GDTCORClock snapshot] isAfter:_deliverByTime]) {
  91. if (_handler && [_handler respondsToSelector:@selector(packageExpired:)]) {
  92. _isHandled = YES;
  93. [_expirationTimer invalidate];
  94. GDTCORLogDebug("Upload package expired: %@", self);
  95. [_handler packageExpired:self];
  96. }
  97. }
  98. }
  99. #pragma mark - NSSecureCoding
  100. /** The keyed archiver key for the events property. */
  101. static NSString *const kEventsKey = @"GDTCORUploadPackageEventsKey";
  102. /** The keyed archiver key for the _isHandled property. */
  103. static NSString *const kDeliverByTimeKey = @"GDTCORUploadPackageDeliveryByTimeKey";
  104. /** The keyed archiver key for the _isHandled ivar. */
  105. static NSString *const kIsHandledKey = @"GDTCORUploadPackageIsHandledKey";
  106. /** The keyed archiver key for the handler property. */
  107. static NSString *const kHandlerKey = @"GDTCORUploadPackageHandlerKey";
  108. /** The keyed archiver key for the target property. */
  109. static NSString *const kTargetKey = @"GDTCORUploadPackageTargetKey";
  110. + (BOOL)supportsSecureCoding {
  111. return YES;
  112. }
  113. - (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
  114. [aCoder encodeObject:_events forKey:kEventsKey];
  115. [aCoder encodeObject:_deliverByTime forKey:kDeliverByTimeKey];
  116. [aCoder encodeBool:_isHandled forKey:kIsHandledKey];
  117. [aCoder encodeObject:_handler forKey:kHandlerKey];
  118. [aCoder encodeInteger:_target forKey:kTargetKey];
  119. }
  120. - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
  121. // Sets a global translation mapping to decode GDTCORStoredEvent objects encoded as instances of
  122. // GDTCOREvent instead.
  123. [NSKeyedUnarchiver setClass:[GDTCOREvent class] forClassName:@"GDTCORStoredEvent"];
  124. GDTCORTarget target = [aDecoder decodeIntegerForKey:kTargetKey];
  125. self = [self initWithTarget:target];
  126. if (self) {
  127. NSSet *classes = [NSSet setWithObjects:[NSSet class], [GDTCOREvent class], nil];
  128. _events = [aDecoder decodeObjectOfClasses:classes forKey:kEventsKey];
  129. _deliverByTime = [aDecoder decodeObjectOfClass:[GDTCORClock class] forKey:kDeliverByTimeKey];
  130. _isHandled = [aDecoder decodeBoolForKey:kIsHandledKey];
  131. // _handler isn't technically NSSecureCoding, because we don't know the class of this object.
  132. // but it gets decoded anyway.
  133. }
  134. return self;
  135. }
  136. @end