Skip to content

Commit

Permalink
feat(storage): moved operationQueue synchronization to the entry poin…
Browse files Browse the repository at this point in the history
…ts of the crud operations

SUITEDEV-36925

Co-authored-by: LasOri <24588073+LasOri@users.noreply.github.com>
  • Loading branch information
davidSchuppa and LasOri committed Nov 7, 2024
1 parent 1bee102 commit db374b1
Showing 1 changed file with 63 additions and 68 deletions.
131 changes: 63 additions & 68 deletions Sources/Core/Store/EMSStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,27 @@ - (OSStatus)setData:(nullable NSData *)data
forKey:(NSString *)key
accessGroup:(nullable NSString *)accessGroup {
NSParameterAssert(key);
OSStatus status = 0;
NSData *existingValue = [self readValueForKey:key
withAccessGroup:accessGroup];
if (data) {
if (existingValue) {
status = [self updateValue:data
forKey:key
withAccessGroup:accessGroup];
__block OSStatus status = 0;
[self.operationQueue runSynchronized:^{
NSData *existingValue = [self readValueForKey:key
withAccessGroup:accessGroup];
if (data) {
if (existingValue) {
status = [self updateValue:data
forKey:key
withAccessGroup:accessGroup];
} else {
status = [self createValue:data
forKey:key
withAccessGroup:accessGroup];
}
} else {
status = [self createValue:data
forKey:key
withAccessGroup:accessGroup];
}
} else {
if (existingValue) {
status = [self deleteValueForKey:key
withAccessGroup:accessGroup];
if (existingValue) {
status = [self deleteValueForKey:key
withAccessGroup:accessGroup];
}
}
}
}];
return status;
}

Expand Down Expand Up @@ -239,8 +241,13 @@ - (nullable NSData *)dataForKey:(NSString *)key {
- (nullable NSData *)dataForKey:(NSString *)key
accessGroup:(nullable NSString *)accessGroup {
NSParameterAssert(key);
return [self readValueForKey:key
withAccessGroup:accessGroup];
__block NSData *result = nil;
[self.operationQueue runSynchronized:^{
result = [self readValueForKey:key
withAccessGroup:accessGroup];

}];
return result;
}

- (nullable NSString *)stringForKey:(NSString *)key {
Expand Down Expand Up @@ -357,72 +364,60 @@ - (NSMutableDictionary *)appendValueToQuery:(NSMutableDictionary *)query
- (OSStatus)createValue:(NSData *)value
forKey:(NSString *)key
withAccessGroup:(nullable NSString *)accessGroup {
__block OSStatus result;
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
NSMutableDictionary *mutableQuery = [weakSelf createQueryWithKey:key
accessGroup:accessGroup];
mutableQuery = [weakSelf appendAccessModifierToQuery:mutableQuery];
mutableQuery = [weakSelf appendValueToQuery:mutableQuery
value:value];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];
result = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
}];
OSStatus result;
NSMutableDictionary *mutableQuery = [self createQueryWithKey:key
accessGroup:accessGroup];
mutableQuery = [self appendAccessModifierToQuery:mutableQuery];
mutableQuery = [self appendValueToQuery:mutableQuery
value:value];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];
result = SecItemAdd((__bridge CFDictionaryRef) query, NULL);
return result;
}

- (nullable NSData *)readValueForKey:(NSString *)key
withAccessGroup:(nullable NSString *)accessGroup {
__block NSData *result = nil;
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
NSMutableDictionary *mutableQuery = [weakSelf createQueryWithKey:key
accessGroup:accessGroup];
mutableQuery = [weakSelf appendResultAttributesToQuery:mutableQuery];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];

CFTypeRef resultRef = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) query, &resultRef);
if (status == errSecSuccess) {
NSDictionary *resultDict = (__bridge NSDictionary *) resultRef;
NSString *returnedAccessGroup = resultDict[(id) kSecAttrAccessGroup];
if ((!accessGroup && ![returnedAccessGroup isEqual:accessGroup]) || (accessGroup && [accessGroup isEqual:returnedAccessGroup])) {
result = resultDict[(id) kSecValueData];
}
}
if (resultRef) {
CFRelease(resultRef);
NSData *result = nil;
NSMutableDictionary *mutableQuery = [self createQueryWithKey:key
accessGroup:accessGroup];
mutableQuery = [self appendResultAttributesToQuery:mutableQuery];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];

CFTypeRef resultRef = NULL;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef) query, &resultRef);
if (status == errSecSuccess) {
NSDictionary *resultDict = (__bridge NSDictionary *) resultRef;
NSString *returnedAccessGroup = resultDict[(id) kSecAttrAccessGroup];
if ((!accessGroup && ![returnedAccessGroup isEqual:accessGroup]) || (accessGroup && [accessGroup isEqual:returnedAccessGroup])) {
result = resultDict[(id) kSecValueData];
}
}];
}
if (resultRef) {
CFRelease(resultRef);
}
return result;
}

- (OSStatus)updateValue:(NSData *)value
forKey:(NSString *)key
withAccessGroup:(nullable NSString *)accessGroup {
__block OSStatus result;
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
NSDictionary *query = [weakSelf createQueryWithKey:key
accessGroup:accessGroup];
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
attributesDictionary = [weakSelf appendValueToQuery:attributesDictionary
value:value];
result = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef) attributesDictionary);
}];
OSStatus result;
NSDictionary *query = [self createQueryWithKey:key
accessGroup:accessGroup];
NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionary];
attributesDictionary = [self appendValueToQuery:attributesDictionary
value:value];
result = SecItemUpdate((__bridge CFDictionaryRef) query, (__bridge CFDictionaryRef) attributesDictionary);
return result;
}

- (OSStatus)deleteValueForKey:(NSString *)key
withAccessGroup:(nullable NSString *)accessGroup {
__block OSStatus result;
__weak typeof(self) weakSelf = self;
[self.operationQueue runSynchronized:^{
NSMutableDictionary *mutableQuery = [weakSelf createQueryWithKey:key
accessGroup:accessGroup];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];
result = SecItemDelete((__bridge CFDictionaryRef) query);
}];
OSStatus result;
NSMutableDictionary *mutableQuery = [self createQueryWithKey:key
accessGroup:accessGroup];
NSDictionary *query = [NSDictionary dictionaryWithDictionary:mutableQuery];
result = SecItemDelete((__bridge CFDictionaryRef) query);
return result;
}

Expand Down

0 comments on commit db374b1

Please sign in to comment.