Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proper RSH8b and RSH3g2a behavior. #1847

Merged
merged 17 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions Source/ARTLocalDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@ @interface ARTLocalDevice ()

@implementation ARTLocalDevice

- (instancetype)initWithClientId:(NSString *)clientId storage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger {
- (instancetype)initWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger {
if (self = [super init]) {
self.clientId = clientId;
self.storage = storage;
_logger = logger;
}
return self;
}

+ (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger {
ARTLocalDevice *device = [[ARTLocalDevice alloc] initWithClientId:clientId storage:storage logger:logger];
+ (instancetype)deviceWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger {
ARTLocalDevice *device = [[ARTLocalDevice alloc] initWithStorage:storage logger:logger];
device.platform = ARTDevicePlatform;
#if TARGET_OS_IOS
switch ([[UIDevice currentDevice] userInterfaceIdiom]) {
Expand All @@ -72,24 +71,18 @@ + (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)stor
#endif
device.push.recipient[@"transportType"] = ARTDevicePushTransportType;

NSString *deviceId = [storage objectForKey:ARTDeviceIdKey];
NSString *deviceId = [storage objectForKey:ARTDeviceIdKey] ?: @""; // PCD2, not nullable
ttypic marked this conversation as resolved.
Show resolved Hide resolved
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
NSString *deviceSecret = deviceId == nil ? nil : [storage secretForDevice:deviceId];

if (deviceId == nil || deviceSecret == nil) { // generate both at the same time
deviceId = [self generateId];
deviceSecret = [self generateSecret];

[storage setObject:deviceId forKey:ARTDeviceIdKey];
[storage setSecret:deviceSecret forDevice:deviceId];
}

device.id = deviceId;
device.secret = deviceSecret;

id identityTokenDetailsInfo = [storage objectForKey:ARTDeviceIdentityTokenKey];
ARTDeviceIdentityTokenDetails *identityTokenDetails = [ARTDeviceIdentityTokenDetails unarchive:identityTokenDetailsInfo withLogger:logger];
device->_identityTokenDetails = identityTokenDetails;

device.clientId = identityTokenDetails.clientId;
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved

NSArray *supportedTokenTypes = @[
ARTAPNSDeviceDefaultTokenType,
ARTAPNSDeviceLocationTokenType
Expand All @@ -102,6 +95,33 @@ + (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)stor
return device;
}

- (BOOL)setupDetailsWithClientId:(NSString *)clientId {
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
NSString *deviceId = self.id;
NSString *deviceSecret = self.secret;

if ([deviceId isEqualToString:@""] || deviceSecret == nil) { // generate both at the same time
deviceId = [self.class generateId];
deviceSecret = [self.class generateSecret];

[_storage setObject:deviceId forKey:ARTDeviceIdKey];
[_storage setSecret:deviceSecret forDevice:deviceId];
}

self.id = deviceId;
self.secret = deviceSecret;

NSString *localClientId = self.clientId;

if (localClientId && clientId && ![localClientId isEqualToString:clientId]) {
ARTLogError(self.logger, @"ARTLocalDevice: `clientId`s don't match: %@ vs %@.", clientId, localClientId);
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
return NO;
}
if (clientId) {
self.clientId = clientId;
}
return YES;
}

+ (NSString *)generateId {
return [NSUUID new].UUIDString;
}
Expand Down
3 changes: 3 additions & 0 deletions Source/ARTPushActivationState.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ - (ARTPushActivationState *)transition:(ARTPushActivationEvent *)event {
}
else if ([event isKindOfClass:[ARTPushActivationEventCalledActivate class]]) {
[self.machine registerForAPNS];
#if TARGET_OS_IOS
[self.machine.rest setupLocalDevice];
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
#endif
return validateAndSync(self.machine, event, self.logger);
}
return nil;
Expand Down
17 changes: 12 additions & 5 deletions Source/ARTRest.m
Original file line number Diff line number Diff line change
Expand Up @@ -744,10 +744,9 @@ - (ARTLocalDevice *)device {
}

- (ARTLocalDevice *)device_nosync {
NSString *clientId = self.auth.clientId_nosync;
__block ARTLocalDevice *ret;
dispatch_sync(ARTRestInternal.deviceAccessQueue, ^{
ret = [self deviceWithClientId_onlyCallOnDeviceAccessQueue:clientId];
dispatch_sync([ARTRestInternal deviceAccessQueue], ^{
ret = [self sharedDevice_onlyCallOnDeviceAccessQueue];
});
return ret;
}
Expand All @@ -765,7 +764,7 @@ + (dispatch_queue_t)deviceAccessQueue {

static BOOL sharedDeviceNeedsLoading_onlyAccessOnDeviceAccessQueue = YES;

- (ARTLocalDevice *)deviceWithClientId_onlyCallOnDeviceAccessQueue:(NSString *)clientId {
- (ARTLocalDevice *)sharedDevice_onlyCallOnDeviceAccessQueue {
// The device is shared in a static variable because it's a reflection
// of what's persisted. Having a device instance per ARTRest instance
// could leave some instances in a stale state, if, through another
Expand All @@ -776,12 +775,20 @@ - (ARTLocalDevice *)deviceWithClientId_onlyCallOnDeviceAccessQueue:(NSString *)c

static id device;
if (sharedDeviceNeedsLoading_onlyAccessOnDeviceAccessQueue) {
device = [ARTLocalDevice load:clientId storage:self.storage logger:self.logger];
device = [ARTLocalDevice deviceWithStorage:self.storage logger:self.logger];
sharedDeviceNeedsLoading_onlyAccessOnDeviceAccessQueue = NO;
}
return device;
}

- (void)setupLocalDevice {
lawrence-forooghian marked this conversation as resolved.
Show resolved Hide resolved
ARTLocalDevice *device = [self device_nosync];
NSString *clientId = self.auth.clientId_nosync;
dispatch_sync([ARTRestInternal deviceAccessQueue], ^{
[device setupDetailsWithClientId:clientId];
});
}

- (void)resetDeviceSingleton {
dispatch_sync([ARTRestInternal deviceAccessQueue], ^{
sharedDeviceNeedsLoading_onlyAccessOnDeviceAccessQueue = YES;
Expand Down
3 changes: 2 additions & 1 deletion Source/PrivateHeaders/Ably/ARTLocalDevice+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ NSString* ARTAPNSDeviceTokenKeyOfType(NSString * _Nullable tokenType);

@property (nonatomic) id<ARTDeviceStorage> storage;

+ (ARTLocalDevice *)load:(NSString *)clientId storage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger;
+ (instancetype)deviceWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger;
- (nullable NSString *)apnsDeviceToken;
- (void)setAndPersistAPNSDeviceToken:(nullable NSString *)deviceToken tokenType:(NSString *)tokenType;
- (void)setAndPersistAPNSDeviceToken:(nullable NSString *)deviceToken;
- (void)setAndPersistIdentityTokenDetails:(nullable ARTDeviceIdentityTokenDetails *)tokenDetails;
- (BOOL)isRegistered;
- (void)clearIdentityTokenDetailsAndClientId;
- (BOOL)setupDetailsWithClientId:(nullable NSString *)clientId;

+ (NSString *)generateId;
+ (NSString *)generateSecret;
Expand Down
1 change: 1 addition & 0 deletions Source/PrivateHeaders/Ably/ARTRest+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSObject<ARTCancellable> *)internetIsUp:(void (^)(BOOL isUp))cb;

#if TARGET_OS_IOS
- (void)setupLocalDevice;
// This is only intended to be called from test code.
- (void)resetDeviceSingleton;

Expand Down