GDTCORUploadPackage.m 5.0 KB

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