GDTCOREvent.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <GoogleDataTransport/GDTCOREvent.h>
  17. #import <GoogleDataTransport/GDTCORAssert.h>
  18. #import <GoogleDataTransport/GDTCORStoredEvent.h>
  19. #import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
  20. @implementation GDTCOREvent
  21. - (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(NSInteger)target {
  22. GDTCORAssert(mappingID.length > 0, @"Please give a valid mapping ID");
  23. GDTCORAssert(target > 0, @"A target cannot be negative or 0");
  24. if (mappingID == nil || mappingID.length == 0 || target <= 0) {
  25. return nil;
  26. }
  27. self = [super init];
  28. if (self) {
  29. _mappingID = mappingID;
  30. _target = target;
  31. _qosTier = GDTCOREventQosDefault;
  32. }
  33. return self;
  34. }
  35. - (instancetype)copy {
  36. GDTCOREvent *copy = [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
  37. copy.dataObject = _dataObject;
  38. copy.dataObjectTransportBytes = _dataObjectTransportBytes;
  39. copy.qosTier = _qosTier;
  40. copy.clockSnapshot = _clockSnapshot;
  41. copy.customPrioritizationParams = _customPrioritizationParams;
  42. return copy;
  43. }
  44. - (NSUInteger)hash {
  45. // This loses some precision, but it's probably fine.
  46. NSUInteger mappingIDHash = [_mappingID hash];
  47. NSUInteger timeHash = [_clockSnapshot hash];
  48. NSUInteger dataObjectTransportBytesHash = [_dataObjectTransportBytes hash];
  49. return mappingIDHash ^ _target ^ dataObjectTransportBytesHash ^ _qosTier ^ timeHash;
  50. }
  51. - (BOOL)isEqual:(id)object {
  52. return [self hash] == [object hash];
  53. }
  54. - (void)setDataObject:(id<GDTCOREventDataObject>)dataObject {
  55. // If you're looking here because of a performance issue in -transportBytes slowing the assignment
  56. // of -dataObject, one way to address this is to add a queue to this class,
  57. // dispatch_(barrier_ if concurrent)async here, and implement the getter with a dispatch_sync.
  58. if (dataObject != _dataObject) {
  59. _dataObject = dataObject;
  60. _dataObjectTransportBytes = [dataObject transportBytes];
  61. }
  62. }
  63. - (GDTCORStoredEvent *)storedEventWithDataFuture:(GDTCORDataFuture *)dataFuture {
  64. return [[GDTCORStoredEvent alloc] initWithEvent:self dataFuture:dataFuture];
  65. }
  66. #pragma mark - NSSecureCoding and NSCoding Protocols
  67. /** NSCoding key for mappingID property. */
  68. static NSString *mappingIDKey = @"_mappingID";
  69. /** NSCoding key for target property. */
  70. static NSString *targetKey = @"_target";
  71. /** NSCoding key for dataObjectTransportBytes property. */
  72. static NSString *dataObjectTransportBytesKey = @"_dataObjectTransportBytesKey";
  73. /** NSCoding key for qosTier property. */
  74. static NSString *qosTierKey = @"_qosTier";
  75. /** NSCoding key for clockSnapshot property. */
  76. static NSString *clockSnapshotKey = @"_clockSnapshot";
  77. + (BOOL)supportsSecureCoding {
  78. return YES;
  79. }
  80. - (id)initWithCoder:(NSCoder *)aDecoder {
  81. NSString *mappingID = [aDecoder decodeObjectOfClass:[NSObject class] forKey:mappingIDKey];
  82. NSInteger target = [aDecoder decodeIntegerForKey:targetKey];
  83. self = [self initWithMappingID:mappingID target:target];
  84. if (self) {
  85. _dataObjectTransportBytes = [aDecoder decodeObjectOfClass:[NSData class]
  86. forKey:dataObjectTransportBytesKey];
  87. _qosTier = [aDecoder decodeIntegerForKey:qosTierKey];
  88. _clockSnapshot = [aDecoder decodeObjectOfClass:[GDTCORClock class] forKey:clockSnapshotKey];
  89. }
  90. return self;
  91. }
  92. - (void)encodeWithCoder:(NSCoder *)aCoder {
  93. [aCoder encodeObject:_mappingID forKey:mappingIDKey];
  94. [aCoder encodeInteger:_target forKey:targetKey];
  95. [aCoder encodeObject:_dataObjectTransportBytes forKey:dataObjectTransportBytesKey];
  96. [aCoder encodeInteger:_qosTier forKey:qosTierKey];
  97. [aCoder encodeObject:_clockSnapshot forKey:clockSnapshotKey];
  98. }
  99. @end