Files
firebase-ios-sdk/GoogleDataTransport/GDTCORLibrary/GDTCOREvent.m
T

156 lines
5.9 KiB
Objective-C
Raw Normal View History

2019-04-12 19:27:00 -07:00
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-07-31 08:12:23 -07:00
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
2019-04-12 19:27:00 -07:00
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORPlatform.h"
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageProtocol.h"
2020-07-31 08:12:23 -07:00
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
2019-04-12 19:27:00 -07:00
2020-06-24 17:50:15 -07:00
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCOREvent_Private.h"
2019-04-12 19:27:00 -07:00
2019-09-09 15:21:29 -07:00
@implementation GDTCOREvent
2019-04-12 19:27:00 -07:00
2020-06-25 09:46:01 -04:00
+ (NSString *)nextEventID {
// TODO: Consider a way to make the eventIDs incremental without introducing a storage dependency
// to the object.
//
// Replace special non-alphanumeric characters to avoid potential conflicts with storage logic.
return [[NSUUID UUID].UUIDString stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
- (nullable instancetype)initWithMappingID:(NSString *)mappingID target:(GDTCORTarget)target {
2019-09-09 15:21:29 -07:00
GDTCORAssert(mappingID.length > 0, @"Please give a valid mapping ID");
GDTCORAssert(target > 0, @"A target cannot be negative or 0");
2020-06-25 09:46:01 -04:00
if (mappingID.length == 0 || target <= 0) {
return nil;
}
2019-04-12 19:27:00 -07:00
self = [super init];
if (self) {
2020-06-25 09:46:01 -04:00
_eventID = [GDTCOREvent nextEventID];
2019-04-12 19:27:00 -07:00
_mappingID = mappingID;
_target = target;
2019-09-09 15:21:29 -07:00
_qosTier = GDTCOREventQosDefault;
_expirationDate = [NSDate dateWithTimeIntervalSinceNow:604800]; // 7 days.
2020-06-25 09:46:01 -04:00
GDTCORLogDebug(@"Event %@ created. ID:%@ mappingID: %@ target:%ld", self, _eventID, mappingID,
(long)target);
2019-04-12 19:27:00 -07:00
}
2020-06-25 09:46:01 -04:00
2019-04-12 19:27:00 -07:00
return self;
}
- (instancetype)copy {
2019-09-09 15:21:29 -07:00
GDTCOREvent *copy = [[GDTCOREvent alloc] initWithMappingID:_mappingID target:_target];
copy->_eventID = _eventID;
2019-04-12 19:27:00 -07:00
copy.dataObject = _dataObject;
copy.qosTier = _qosTier;
copy.clockSnapshot = _clockSnapshot;
copy.customBytes = _customBytes;
GDTCORLogDebug(@"Copying event %@ to event %@", self, copy);
2019-04-12 19:27:00 -07:00
return copy;
}
- (NSUInteger)hash {
// This loses some precision, but it's probably fine.
NSUInteger eventIDHash = [_eventID hash];
2019-04-12 19:27:00 -07:00
NSUInteger mappingIDHash = [_mappingID hash];
NSUInteger timeHash = [_clockSnapshot hash];
NSInteger serializedBytesHash = [_serializedDataObjectBytes hash];
return eventIDHash ^ mappingIDHash ^ _target ^ _qosTier ^ timeHash ^ serializedBytesHash;
2019-04-12 19:27:00 -07:00
}
- (BOOL)isEqual:(id)object {
return [self hash] == [object hash];
}
#pragma mark - Property overrides
2019-09-09 15:21:29 -07:00
- (void)setDataObject:(id<GDTCOREventDataObject>)dataObject {
2019-04-12 19:27:00 -07:00
// If you're looking here because of a performance issue in -transportBytes slowing the assignment
// of -dataObject, one way to address this is to add a queue to this class,
// dispatch_(barrier_ if concurrent)async here, and implement the getter with a dispatch_sync.
if (dataObject != _dataObject) {
_dataObject = dataObject;
}
self->_serializedDataObjectBytes = [dataObject transportBytes];
2019-04-12 19:27:00 -07:00
}
#pragma mark - NSSecureCoding and NSCoding Protocols
/** NSCoding key for eventID property. */
static NSString *kEventIDKey = @"GDTCOREventEventIDKey";
2019-04-12 19:27:00 -07:00
/** NSCoding key for mappingID property. */
static NSString *kMappingIDKey = @"GDTCOREventMappingIDKey";
2019-04-12 19:27:00 -07:00
/** NSCoding key for target property. */
static NSString *kTargetKey = @"GDTCOREventTargetKey";
2019-04-12 19:27:00 -07:00
/** NSCoding key for qosTier property. */
static NSString *kQoSTierKey = @"GDTCOREventQoSTierKey";
2019-04-12 19:27:00 -07:00
/** NSCoding key for clockSnapshot property. */
static NSString *kClockSnapshotKey = @"GDTCOREventClockSnapshotKey";
2019-04-12 19:27:00 -07:00
/** NSCoding key for expirationDate property. */
static NSString *kExpirationDateKey = @"GDTCOREventExpirationDateKey";
/** NSCoding key for serializedDataObjectBytes property. */
static NSString *kSerializedDataObjectBytes = @"GDTCOREventSerializedDataObjectBytesKey";
/** NSCoding key for customData property. */
static NSString *kCustomDataKey = @"GDTCOREventCustomDataKey";
2019-04-12 19:27:00 -07:00
+ (BOOL)supportsSecureCoding {
return YES;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
2019-04-12 19:27:00 -07:00
if (self) {
_mappingID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kMappingIDKey];
_target = [aDecoder decodeIntegerForKey:kTargetKey];
2020-06-25 09:46:01 -04:00
_eventID = [aDecoder decodeObjectOfClass:[NSString class] forKey:kEventIDKey]
?: [GDTCOREvent nextEventID];
_qosTier = [aDecoder decodeIntegerForKey:kQoSTierKey];
_clockSnapshot = [aDecoder decodeObjectOfClass:[GDTCORClock class] forKey:kClockSnapshotKey];
_customBytes = [aDecoder decodeObjectOfClass:[NSData class] forKey:kCustomDataKey];
_expirationDate = [aDecoder decodeObjectOfClass:[NSDate class] forKey:kExpirationDateKey];
_serializedDataObjectBytes = [aDecoder decodeObjectOfClass:[NSData class]
forKey:kSerializedDataObjectBytes];
if (!_serializedDataObjectBytes) {
return nil;
}
2019-04-12 19:27:00 -07:00
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_eventID forKey:kEventIDKey];
[aCoder encodeObject:_mappingID forKey:kMappingIDKey];
[aCoder encodeInteger:_target forKey:kTargetKey];
[aCoder encodeInteger:_qosTier forKey:kQoSTierKey];
[aCoder encodeObject:_clockSnapshot forKey:kClockSnapshotKey];
[aCoder encodeObject:_customBytes forKey:kCustomDataKey];
[aCoder encodeObject:_expirationDate forKey:kExpirationDateKey];
[aCoder encodeObject:self.serializedDataObjectBytes forKey:kSerializedDataObjectBytes];
2019-04-12 19:27:00 -07:00
}
@end