From 8a4712bf4c116f2eb3070c36ab7a2f1a40058d78 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 2 Aug 2017 15:15:31 -0400 Subject: [PATCH 1/3] Only access localNumber on AccountManager and cache it // FREEBIE --- .../TSVideoAttachmentAdapter.m | 1 - Signal/src/Signal-Bridging-Header.h | 1 - .../CodeVerificationViewController.m | 1 - .../FingerprintViewController.m | 10 ++--- .../FingerprintViewScanController.m | 7 ++- .../OWSConversationSettingsViewController.m | 6 +-- .../OWSLinkDeviceViewController.m | 4 +- .../src/Account/TSAccountManager.h | 3 +- .../src/Account/TSAccountManager.m | 45 +++++++++++++++++-- SignalServiceKit/src/Account/TSAttributes.m | 3 +- .../src/Contacts/SignalRecipient.m | 8 ++-- .../src/Messages/OWSIdentityManager.m | 3 +- .../src/Messages/OWSMessageSender.m | 7 ++- .../src/Network/WebSockets/TSSocketManager.m | 7 ++- .../src/Security/OWSFingerprintBuilder.h | 4 +- .../src/Security/OWSFingerprintBuilder.m | 10 ++--- .../src/Storage/TSStorageHeaders.h | 1 - .../Storage/TSStorageManager+keyingMaterial.h | 14 +----- .../Storage/TSStorageManager+keyingMaterial.m | 11 ----- 19 files changed, 77 insertions(+), 69 deletions(-) diff --git a/Signal/src/Models/TSMessageAdapaters/TSVideoAttachmentAdapter.m b/Signal/src/Models/TSMessageAdapaters/TSVideoAttachmentAdapter.m index c504a34f0..948a35103 100644 --- a/Signal/src/Models/TSMessageAdapaters/TSVideoAttachmentAdapter.m +++ b/Signal/src/Models/TSMessageAdapaters/TSVideoAttachmentAdapter.m @@ -9,7 +9,6 @@ #import "Signal-Swift.h" #import "TSAttachmentStream.h" #import "TSMessagesManager.h" -#import "TSStorageManager+keyingMaterial.h" #import "UIColor+JSQMessages.h" #import "UIColor+OWS.h" #import "UIDevice+TSHardwareVersion.h" diff --git a/Signal/src/Signal-Bridging-Header.h b/Signal/src/Signal-Bridging-Header.h index 00d707a04..960986d75 100644 --- a/Signal/src/Signal-Bridging-Header.h +++ b/Signal/src/Signal-Bridging-Header.h @@ -86,7 +86,6 @@ #import #import #import -#import #import #import #import diff --git a/Signal/src/ViewControllers/CodeVerificationViewController.m b/Signal/src/ViewControllers/CodeVerificationViewController.m index d34dca07d..505fbd4d8 100644 --- a/Signal/src/ViewControllers/CodeVerificationViewController.m +++ b/Signal/src/ViewControllers/CodeVerificationViewController.m @@ -13,7 +13,6 @@ #import #import #import -#import NS_ASSUME_NONNULL_BEGIN diff --git a/Signal/src/ViewControllers/FingerprintViewController.m b/Signal/src/ViewControllers/FingerprintViewController.m index 84dbf2e55..1ec1b0c56 100644 --- a/Signal/src/ViewControllers/FingerprintViewController.m +++ b/Signal/src/ViewControllers/FingerprintViewController.m @@ -17,9 +17,9 @@ #import #import #import +#import #import #import -#import NS_ASSUME_NONNULL_BEGIN @@ -74,7 +74,7 @@ typedef void (^CustomLayoutBlock)(); @property (nonatomic) NSString *recipientId; @property (nonatomic) NSData *identityKey; -@property (nonatomic) TSStorageManager *storageManager; +@property (nonatomic) TSAccountManager *accountManager; @property (nonatomic) OWSFingerprint *fingerprint; @property (nonatomic) NSString *contactName; @@ -118,6 +118,8 @@ typedef void (^CustomLayoutBlock)(); return self; } + _accountManager = [TSAccountManager sharedInstance]; + [self observeNotifications]; return self; @@ -142,8 +144,6 @@ typedef void (^CustomLayoutBlock)(); self.recipientId = recipientId; - self.storageManager = [TSStorageManager sharedManager]; - OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager; self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId]; @@ -155,7 +155,7 @@ typedef void (^CustomLayoutBlock)(); self.identityKey = recipientIdentity.identityKey; OWSFingerprintBuilder *builder = - [[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; + [[OWSFingerprintBuilder alloc] initWithAccountManager:self.accountManager contactsManager:contactsManager]; self.fingerprint = [builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey]; } diff --git a/Signal/src/ViewControllers/FingerprintViewScanController.m b/Signal/src/ViewControllers/FingerprintViewScanController.m index b8561db6b..d1a461dab 100644 --- a/Signal/src/ViewControllers/FingerprintViewScanController.m +++ b/Signal/src/ViewControllers/FingerprintViewScanController.m @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN @interface FingerprintViewScanController () -@property (nonatomic) TSStorageManager *storageManager; +@property (nonatomic) TSAccountManager *accountManager; @property (nonatomic) NSString *recipientId; @property (nonatomic) NSData *identityKey; @property (nonatomic) OWSFingerprint *fingerprint; @@ -39,8 +39,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(recipientId.length > 0); self.recipientId = recipientId; - - self.storageManager = [TSStorageManager sharedManager]; + self.accountManager = [TSAccountManager sharedInstance]; OWSContactsManager *contactsManager = [Environment getCurrent].contactsManager; self.contactName = [contactsManager displayNameForPhoneIdentifier:recipientId]; @@ -53,7 +52,7 @@ NS_ASSUME_NONNULL_BEGIN self.identityKey = recipientIdentity.identityKey; OWSFingerprintBuilder *builder = - [[OWSFingerprintBuilder alloc] initWithStorageManager:self.storageManager contactsManager:contactsManager]; + [[OWSFingerprintBuilder alloc] initWithAccountManager:self.accountManager contactsManager:contactsManager]; self.fingerprint = [builder fingerprintWithTheirSignalId:recipientId theirIdentityKey:recipientIdentity.identityKey]; } diff --git a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m index ddc528090..d13979576 100644 --- a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m +++ b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m @@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) NSArray *disappearingMessagesDurations; @property (nonatomic) OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration; -@property (nonatomic, readonly) TSStorageManager *storageManager; +@property (nonatomic, readonly) TSAccountManager *accountManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager; @property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSBlockingManager *blockingManager; @@ -93,7 +93,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)commonInit { - _storageManager = [TSStorageManager sharedManager]; + _accountManager = [TSAccountManager sharedInstance]; _contactsManager = [Environment getCurrent].contactsManager; _messageSender = [Environment getCurrent].messageSender; _blockingManager = [OWSBlockingManager sharedManager]; @@ -925,7 +925,7 @@ NS_ASSUME_NONNULL_BEGIN }]; NSMutableArray *newGroupMemberIds = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds]; - [newGroupMemberIds removeObject:[self.storageManager localNumber]]; + [newGroupMemberIds removeObject:[self.accountManager localNumber]]; gThread.groupModel.groupMemberIds = newGroupMemberIds; [gThread save]; diff --git a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m index 650bc6679..1e5faf2da 100644 --- a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m +++ b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m @@ -8,7 +8,7 @@ #import #import #import -#import +#import NS_ASSUME_NONNULL_BEGIN @@ -131,7 +131,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(identityKeyPair); NSData *myPublicKey = identityKeyPair.publicKey; NSData *myPrivateKey = identityKeyPair.ows_privateKey; - NSString *accountIdentifier = [TSStorageManager localNumber]; + NSString *accountIdentifier = [TSAccountManager localNumber]; OWSDeviceProvisioner *provisioner = [[OWSDeviceProvisioner alloc] initWithMyPublicKey:myPublicKey myPrivateKey:myPrivateKey diff --git a/SignalServiceKit/src/Account/TSAccountManager.h b/SignalServiceKit/src/Account/TSAccountManager.h index 1b7a65124..84a42d8c6 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.h +++ b/SignalServiceKit/src/Account/TSAccountManager.h @@ -22,7 +22,7 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange; - (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager - storageManager:(TSStorageManager *)storageManager; + storageManager:(TSStorageManager *)storageManager NS_DESIGNATED_INITIALIZER; + (instancetype)sharedInstance; @@ -43,6 +43,7 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange; * @return E164 formatted phone number */ + (nullable NSString *)localNumber; +- (nullable NSString *)localNumber; /** * The registration ID is unique to an installation of TextSecure, it allows to know if the app was reinstalled diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m index 560516067..52cfe72e7 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.m +++ b/SignalServiceKit/src/Account/TSAccountManager.m @@ -23,12 +23,39 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName @interface TSAccountManager () @property (nonatomic, nullable) NSString *phoneNumberAwaitingVerification; +@property (nonatomic, nullable) NSString *cachedLocalNumber; @property (nonatomic, readonly) TSStorageManager *storageManager; @end #pragma mark - +@interface TSStorageManager (TSAccountManagerStorage) + +/** + * Stored registered phone number + * + * @return E164 string of the registered phone number + */ ++ (nullable NSString *)localNumber; +- (nullable NSString *)localNumber; + +@end + +@implementation TSStorageManager (TSAccountManagerStorage) + ++ (nullable NSString *)localNumber +{ + return [[self sharedManager] localNumber]; +} + +- (nullable NSString *)localNumber +{ + return [self stringForKey:TSStorageRegisteredNumberKey inCollection:TSStorageUserAccountCollection]; +} + +@end + @implementation TSAccountManager - (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager @@ -95,13 +122,25 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName + (nullable NSString *)localNumber { - TSAccountManager *sharedManager = [self sharedInstance]; - NSString *awaitingVerif = sharedManager.phoneNumberAwaitingVerification; + return [[self sharedInstance] localNumber]; +} + +- (nullable NSString *)localNumber +{ + NSString *awaitingVerif = self.phoneNumberAwaitingVerification; if (awaitingVerif) { return awaitingVerif; } - return [TSStorageManager localNumber]; + // Cache this since we access this a lot, and once set it will not change. + @synchronized(self) + { + if (self.cachedLocalNumber == nil) { + self.cachedLocalNumber = [TSStorageManager localNumber]; + } + } + + return self.cachedLocalNumber; } + (uint32_t)getOrGenerateRegistrationId { diff --git a/SignalServiceKit/src/Account/TSAttributes.m b/SignalServiceKit/src/Account/TSAttributes.m index ebad7ca00..7528dfe6a 100644 --- a/SignalServiceKit/src/Account/TSAttributes.m +++ b/SignalServiceKit/src/Account/TSAttributes.m @@ -3,9 +3,8 @@ // #import "TSAttributes.h" - #import "TSAccountManager.h" -#import "TSStorageHeaders.h" +#import "TSStorageManager+keyingMaterial.h" @implementation TSAttributes diff --git a/SignalServiceKit/src/Contacts/SignalRecipient.m b/SignalServiceKit/src/Contacts/SignalRecipient.m index 68c6e313c..55effc18a 100644 --- a/SignalServiceKit/src/Contacts/SignalRecipient.m +++ b/SignalServiceKit/src/Contacts/SignalRecipient.m @@ -4,7 +4,8 @@ #import "SignalRecipient.h" #import "OWSIdentityManager.h" -#import "TSStorageHeaders.h" +#import "TSAccountManager.h" +#import NS_ASSUME_NONNULL_BEGIN @@ -45,10 +46,9 @@ NS_ASSUME_NONNULL_BEGIN + (instancetype)selfRecipient { - SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSStorageManager localNumber]]; + SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSAccountManager localNumber]]; if (!myself) { - myself = [[self alloc] initWithTextSecureIdentifier:[TSStorageManager localNumber] - relay:nil]; + myself = [[self alloc] initWithTextSecureIdentifier:[TSAccountManager localNumber] relay:nil]; } return myself; } diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index 31f7a6fc3..767107c4f 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -15,7 +15,6 @@ #import "TSContactThread.h" #import "TSErrorMessage.h" #import "TSGroupThread.h" -#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager+sessionStore.h" #import "TSStorageManager.h" #import "TextSecureKitEnv.h" @@ -326,7 +325,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa @synchronized(self) { - if ([[self.storageManager localNumber] isEqualToString:recipientId]) { + if ([[TSAccountManager localNumber] isEqualToString:recipientId]) { if ([[self identityKeyPair].publicKey isEqualToData:identityKey]) { return YES; } else { diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 7de303db2..09731c6db 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -30,7 +30,6 @@ #import "TSPreKeyManager.h" #import "TSStorageManager+PreKeyStore.h" #import "TSStorageManager+SignedPreKeyStore.h" -#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager+sessionStore.h" #import "TSStorageManager.h" #import "TSThread.h" @@ -623,7 +622,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; || [message isKindOfClass:[OWSOutgoingSyncMessage class]]) { TSContactThread *contactThread = (TSContactThread *)thread; - if ([contactThread.contactIdentifier isEqualToString:self.storageManager.localNumber] + if ([contactThread.contactIdentifier isEqualToString:[TSAccountManager localNumber]] && ![message isKindOfClass:[OWSOutgoingSyncMessage class]]) { [self handleSendToMyself:message]; @@ -632,7 +631,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } NSString *recipientContactId = [message isKindOfClass:[OWSOutgoingSyncMessage class]] - ? self.storageManager.localNumber + ? [TSAccountManager localNumber] : contactThread.contactIdentifier; // If we block a user, don't send 1:1 messages to them. The UI @@ -734,7 +733,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; NSString *recipientId = recipient.recipientId; // We don't need to send the message to ourselves... - if ([recipientId isEqualToString:[TSStorageManager localNumber]]) { + if ([recipientId isEqualToString:[TSAccountManager localNumber]]) { continue; } // We don't need to sent the message to all group members if diff --git a/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m b/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m index 0c2631e0a..7359c6bd0 100644 --- a/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m +++ b/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m @@ -476,10 +476,9 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_ } - (NSString *)webSocketAuthenticationString { - return [NSString - stringWithFormat:@"?login=%@&password=%@", - [[TSStorageManager localNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"], - [TSStorageManager serverAuthToken]]; + return [NSString stringWithFormat:@"?login=%@&password=%@", + [[TSAccountManager localNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"], + [TSStorageManager serverAuthToken]]; } #pragma mark - Socket LifeCycle diff --git a/SignalServiceKit/src/Security/OWSFingerprintBuilder.h b/SignalServiceKit/src/Security/OWSFingerprintBuilder.h index 5c7ae4360..ced8c7a50 100644 --- a/SignalServiceKit/src/Security/OWSFingerprintBuilder.h +++ b/SignalServiceKit/src/Security/OWSFingerprintBuilder.h @@ -4,14 +4,14 @@ NS_ASSUME_NONNULL_BEGIN -@class TSStorageManager; +@class TSAccountManager; @class OWSFingerprint; @protocol ContactsManagerProtocol; @interface OWSFingerprintBuilder : NSObject - (instancetype)init NS_UNAVAILABLE; -- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager +- (instancetype)initWithAccountManager:(TSAccountManager *)accountManager contactsManager:(id)contactsManager NS_DESIGNATED_INITIALIZER; /** diff --git a/SignalServiceKit/src/Security/OWSFingerprintBuilder.m b/SignalServiceKit/src/Security/OWSFingerprintBuilder.m index 0d609dfe5..78ea497bc 100644 --- a/SignalServiceKit/src/Security/OWSFingerprintBuilder.m +++ b/SignalServiceKit/src/Security/OWSFingerprintBuilder.m @@ -6,21 +6,21 @@ #import "ContactsManagerProtocol.h" #import "OWSFingerprint.h" #import "OWSIdentityManager.h" -#import "TSStorageManager+keyingMaterial.h" +#import "TSAccountManager.h" #import <25519/Curve25519.h> NS_ASSUME_NONNULL_BEGIN @interface OWSFingerprintBuilder () -@property (nonatomic, readonly) TSStorageManager *storageManager; +@property (nonatomic, readonly) TSAccountManager *accountManager; @property (nonatomic, readonly) id contactsManager; @end @implementation OWSFingerprintBuilder -- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager +- (instancetype)initWithAccountManager:(TSAccountManager *)accountManager contactsManager:(id)contactsManager { self = [super init]; @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN return self; } - _storageManager = storageManager; + _accountManager = accountManager; _contactsManager = contactsManager; return self; @@ -50,7 +50,7 @@ NS_ASSUME_NONNULL_BEGIN { NSString *theirName = [self.contactsManager displayNameForPhoneIdentifier:theirSignalId]; - NSString *mySignalId = [self.storageManager localNumber]; + NSString *mySignalId = [self.accountManager localNumber]; NSData *myIdentityKey = [[OWSIdentityManager sharedManager] identityKeyPair].publicKey; return [OWSFingerprint fingerprintWithMyStableId:mySignalId diff --git a/SignalServiceKit/src/Storage/TSStorageHeaders.h b/SignalServiceKit/src/Storage/TSStorageHeaders.h index 99c2c5af0..24bdc0638 100644 --- a/SignalServiceKit/src/Storage/TSStorageHeaders.h +++ b/SignalServiceKit/src/Storage/TSStorageHeaders.h @@ -12,7 +12,6 @@ #import "TSStorageManager+SessionStore.h" #import "TSStorageManager+SignedPreKeyStore.h" #import "TSStorageManager+keyFromIntLong.h" -#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager+messageIDs.h" #endif diff --git a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h index 580eff4b5..0487b2a7b 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h +++ b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h @@ -1,9 +1,5 @@ // -// TSStorageManager+keyingMaterial.h -// TextSecureKit -// -// Created by Frederic Jacobs on 06/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import "TSStorageManager.h" @@ -28,14 +24,6 @@ + (NSString *)serverAuthToken; -/** - * Registered phone number - * - * @return E164 string of the registered phone number - */ -- (NSString *)localNumber; -+ (NSString *)localNumber; - - (void)ifLocalNumberPresent:(BOOL)isPresent runAsync:(void (^)())block; + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey; diff --git a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m index b85fa7f2d..9e784581f 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m +++ b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m @@ -6,17 +6,6 @@ @implementation TSStorageManager (keyingMaterial) -+ (NSString *)localNumber -{ - return [[self sharedManager] localNumber]; -} - -- (NSString *)localNumber -{ - // TODO cache this? It only changes once, ever, and otherwise causes "surprising" transactions to occur. - return [self stringForKey:TSStorageRegisteredNumberKey inCollection:TSStorageUserAccountCollection]; -} - - (void)ifLocalNumberPresent:(BOOL)runIfPresent runAsync:(void (^)())block; { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ From 01e808febefafc8ae89da76a9f930520a807fb13 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 2 Aug 2017 15:37:52 -0400 Subject: [PATCH 2/3] localNumber persistance from Category -> TSAccountManager Following the pattern that singleton methods for effectively global state should get/set on their own dedicated dbConnection for consistency and to avoid being blocked by unrelated writes. --- .../Example/TSKitiOSTestApp/Podfile.lock | 2 +- .../src/Account/TSAccountManager.m | 118 +++++++++++------- SignalServiceKit/src/Storage/TSStorageKeys.h | 8 +- .../Storage/TSStorageManager+keyingMaterial.h | 4 - .../Storage/TSStorageManager+keyingMaterial.m | 36 +----- 5 files changed, 75 insertions(+), 93 deletions(-) diff --git a/SignalServiceKit/Example/TSKitiOSTestApp/Podfile.lock b/SignalServiceKit/Example/TSKitiOSTestApp/Podfile.lock index f243593f1..50160c58b 100644 --- a/SignalServiceKit/Example/TSKitiOSTestApp/Podfile.lock +++ b/SignalServiceKit/Example/TSKitiOSTestApp/Podfile.lock @@ -113,7 +113,7 @@ EXTERNAL SOURCES: AxolotlKit: :git: https://github.com/WhisperSystems/SignalProtocolKit.git SignalServiceKit: - :path: "../../../SignalServiceKit.podspec" + :path: ../../../SignalServiceKit.podspec SocketRocket: :git: https://github.com/facebook/SocketRocket.git diff --git a/SignalServiceKit/src/Account/TSAccountManager.m b/SignalServiceKit/src/Account/TSAccountManager.m index 52cfe72e7..caa07f63d 100644 --- a/SignalServiceKit/src/Account/TSAccountManager.m +++ b/SignalServiceKit/src/Account/TSAccountManager.m @@ -20,44 +20,24 @@ NSString *const TSRegistrationErrorUserInfoHTTPStatus = @"TSHTTPStatus"; NSString *const kNSNotificationName_RegistrationStateDidChange = @"kNSNotificationName_RegistrationStateDidChange"; NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName_LocalNumberDidChange"; +NSString *const TSAccountManager_RegisteredNumberKey = @"TSStorageRegisteredNumberKey"; +NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegistrationId"; + @interface TSAccountManager () +@property (nonatomic, readonly) BOOL isRegistered; @property (nonatomic, nullable) NSString *phoneNumberAwaitingVerification; @property (nonatomic, nullable) NSString *cachedLocalNumber; -@property (nonatomic, readonly) TSStorageManager *storageManager; +@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @end #pragma mark - -@interface TSStorageManager (TSAccountManagerStorage) - -/** - * Stored registered phone number - * - * @return E164 string of the registered phone number - */ -+ (nullable NSString *)localNumber; -- (nullable NSString *)localNumber; - -@end - -@implementation TSStorageManager (TSAccountManagerStorage) - -+ (nullable NSString *)localNumber -{ - return [[self sharedManager] localNumber]; -} - -- (nullable NSString *)localNumber -{ - return [self stringForKey:TSStorageRegisteredNumberKey inCollection:TSStorageUserAccountCollection]; -} - -@end - @implementation TSAccountManager +@synthesize isRegistered = _isRegistered; + - (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager storageManager:(TSStorageManager *)storageManager { @@ -67,7 +47,7 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName } _networkManager = networkManager; - _storageManager = storageManager; + _dbConnection = [storageManager newDatabaseConnection]; OWSSingletonAssert(); @@ -95,13 +75,42 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName userInfo:nil]; } -+ (BOOL)isRegistered { - return [TSStorageManager localNumber] ? YES : NO; ++ (BOOL)isRegistered +{ + return [[self sharedInstance] isRegistered]; } -- (void)ifRegistered:(BOOL)isRegistered runAsync:(void (^)())block +- (BOOL)isRegistered { - [self.storageManager ifLocalNumberPresent:isRegistered runAsync:block]; + if (_isRegistered) { + return YES; + } else { + @synchronized (self) { + // Cache this once it's true since it's called alot, involves a dbLookup, and once set - it doesn't change. + _isRegistered = [self storedLocalNumber] != nil; + } + } + return _isRegistered; +} + +- (void)ifRegistered:(BOOL)runIfRegistered runAsync:(void (^)())block +{ + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + if ([self isRegistered] == runIfRegistered) { + if (runIfRegistered) { + DDLogDebug(@"%@ Running existing-user block", self.tag); + } else { + DDLogDebug(@"%@ Running new-user block", self.tag); + } + block(); + } else { + if (runIfRegistered) { + DDLogDebug(@"%@ Skipping existing-user block for new-user", self.tag); + } else { + DDLogDebug(@"%@ Skipping new-user block for existing-user", self.tag); + } + } + }); } - (void)didRegister @@ -113,7 +122,7 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName @throw [NSException exceptionWithName:@"RegistrationFail" reason:@"Internal Corrupted State" userInfo:nil]; } - [self.storageManager storePhoneNumber:phoneNumber]; + [self storeLocalNumber:phoneNumber]; [[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_RegistrationStateDidChange object:nil @@ -136,30 +145,47 @@ NSString *const kNSNotificationName_LocalNumberDidChange = @"kNSNotificationName @synchronized(self) { if (self.cachedLocalNumber == nil) { - self.cachedLocalNumber = [TSStorageManager localNumber]; + self.cachedLocalNumber = self.storedLocalNumber; } } return self.cachedLocalNumber; } -+ (uint32_t)getOrGenerateRegistrationId { - YapDatabaseConnection *dbConn = [[TSStorageManager sharedManager] newDatabaseConnection]; - __block uint32_t registrationID = 0; +- (nullable NSString *)storedLocalNumber +{ + @synchronized (self) { + return [self.dbConnection stringForKey:TSAccountManager_RegisteredNumberKey + inCollection:TSStorageUserAccountCollection]; + } +} + +- (void)storeLocalNumber:(NSString *)localNumber +{ + @synchronized (self) { + [self.dbConnection setObject:localNumber + forKey:TSAccountManager_RegisteredNumberKey + inCollection:TSStorageUserAccountCollection]; + } +} + ++ (uint32_t)getOrGenerateRegistrationId +{ + return [[self sharedInstance] getOrGenerateRegistrationId]; +} - [dbConn readWithBlock:^(YapDatabaseReadTransaction *transaction) { - registrationID = [[transaction objectForKey:TSStorageLocalRegistrationId - inCollection:TSStorageUserAccountCollection] unsignedIntValue]; - }]; +- (uint32_t)getOrGenerateRegistrationId +{ + uint32_t registrationID = [[self.dbConnection objectForKey:TSAccountManager_LocalRegistrationIdKey + inCollection:TSStorageUserAccountCollection] unsignedIntValue]; if (registrationID == 0) { registrationID = (uint32_t)arc4random_uniform(16380) + 1; + DDLogWarn(@"%@ Generated a new registrationID: %u", self.tag, registrationID); - [dbConn readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [transaction setObject:[NSNumber numberWithUnsignedInteger:registrationID] - forKey:TSStorageLocalRegistrationId - inCollection:TSStorageUserAccountCollection]; - }]; + [self.dbConnection setObject:[NSNumber numberWithUnsignedInteger:registrationID] + forKey:TSAccountManager_LocalRegistrationIdKey + inCollection:TSStorageUserAccountCollection]; } return registrationID; diff --git a/SignalServiceKit/src/Storage/TSStorageKeys.h b/SignalServiceKit/src/Storage/TSStorageKeys.h index ed48677f3..f969915e6 100644 --- a/SignalServiceKit/src/Storage/TSStorageKeys.h +++ b/SignalServiceKit/src/Storage/TSStorageKeys.h @@ -1,9 +1,5 @@ // -// TSStorageKeys.h -// TextSecureKit -// -// Created by Frederic Jacobs on 28/10/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #ifndef TextSecureKit_TSStorageKeys_h @@ -13,10 +9,8 @@ #define TSStorageUserAccountCollection @"TSStorageUserAccountCollection" -#define TSStorageRegisteredNumberKey @"TSStorageRegisteredNumberKey" #define TSStorageServerAuthToken @"TSStorageServerAuthToken" #define TSStorageServerSignalingKey @"TSStorageServerSignalingKey" -#define TSStorageLocalRegistrationId @"TSStorageLocalRegistrationId" /** * Preferences exposed to the user diff --git a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h index 0487b2a7b..390f85cb0 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h +++ b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.h @@ -24,10 +24,6 @@ + (NSString *)serverAuthToken; -- (void)ifLocalNumberPresent:(BOOL)isPresent runAsync:(void (^)())block; - + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey; -- (void)storePhoneNumber:(NSString *)phoneNumber; - @end diff --git a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m index 9e784581f..bc0703c2c 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m +++ b/SignalServiceKit/src/Storage/TSStorageManager+keyingMaterial.m @@ -4,34 +4,9 @@ #import "TSStorageManager+keyingMaterial.h" +// TODO merge this category extension's functionality into TSAccountManager @implementation TSStorageManager (keyingMaterial) -- (void)ifLocalNumberPresent:(BOOL)runIfPresent runAsync:(void (^)())block; -{ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - __block BOOL isPresent; - [self.newDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { - isPresent = [[transaction objectForKey:TSStorageRegisteredNumberKey - inCollection:TSStorageUserAccountCollection] boolValue]; - }]; - - if (isPresent == runIfPresent) { - if (runIfPresent) { - DDLogDebug(@"%@ Running existing-user block", self.logTag); - } else { - DDLogDebug(@"%@ Running new-user block", self.logTag); - } - block(); - } else { - if (runIfPresent) { - DDLogDebug(@"%@ Skipping existing-user block for new-user", self.logTag); - } else { - DDLogDebug(@"%@ Skipping new-user block for existing-user", self.logTag); - } - } - }); -} - + (NSString *)signalingKey { return [[self sharedManager] stringForKey:TSStorageServerSignalingKey inCollection:TSStorageUserAccountCollection]; } @@ -40,15 +15,6 @@ return [[self sharedManager] stringForKey:TSStorageServerAuthToken inCollection:TSStorageUserAccountCollection]; } -- (void)storePhoneNumber:(NSString *)phoneNumber -{ - [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - [transaction setObject:phoneNumber - forKey:TSStorageRegisteredNumberKey - inCollection:TSStorageUserAccountCollection]; - }]; -} - + (void)storeServerToken:(NSString *)authToken signalingKey:(NSString *)signalingKey { TSStorageManager *sharedManager = self.sharedManager; [sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { From 935b51aa1955a9e4ed14e80c0c4185a3665522ae Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 2 Aug 2017 16:51:26 -0400 Subject: [PATCH 3/3] Fixup tests // FREEBIE --- SignalServiceKit/src/Contacts/SignalRecipient.m | 1 + SignalServiceKit/tests/Contacts/SignalRecipientTest.m | 10 ++++++++-- SignalServiceKit/tests/Messages/OWSMessageSenderTest.m | 10 ++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/SignalServiceKit/src/Contacts/SignalRecipient.m b/SignalServiceKit/src/Contacts/SignalRecipient.m index 55effc18a..dc71f3a01 100644 --- a/SignalServiceKit/src/Contacts/SignalRecipient.m +++ b/SignalServiceKit/src/Contacts/SignalRecipient.m @@ -44,6 +44,7 @@ NS_ASSUME_NONNULL_BEGIN return recipient; } +// TODO This method should probably live on the TSAccountManager rather than grabbing a global singleton. + (instancetype)selfRecipient { SignalRecipient *myself = [self recipientWithTextSecureIdentifier:[TSAccountManager localNumber]]; diff --git a/SignalServiceKit/tests/Contacts/SignalRecipientTest.m b/SignalServiceKit/tests/Contacts/SignalRecipientTest.m index b5fd06045..10d46127f 100644 --- a/SignalServiceKit/tests/Contacts/SignalRecipientTest.m +++ b/SignalServiceKit/tests/Contacts/SignalRecipientTest.m @@ -3,10 +3,16 @@ // #import "SignalRecipient.h" +#import "TSAccountManager.h" #import "TSStorageManager+keyingMaterial.h" -#import "TSStorageManager.h" #import +@interface TSAccountManager (Testing) + +- (void)storeLocalNumber:(NSString *)localNumber; + +@end + @interface SignalRecipientTest : XCTestCase @property (nonatomic) NSString *localNumber; @@ -19,7 +25,7 @@ { [super setUp]; self.localNumber = @"+13231231234"; - [[TSStorageManager sharedManager] storePhoneNumber:self.localNumber]; + [[TSAccountManager sharedInstance] storeLocalNumber:self.localNumber]; } - (void)testSelfRecipientWithExistingRecord diff --git a/SignalServiceKit/tests/Messages/OWSMessageSenderTest.m b/SignalServiceKit/tests/Messages/OWSMessageSenderTest.m index 7a6c4f2cc..d7a8aa048 100644 --- a/SignalServiceKit/tests/Messages/OWSMessageSenderTest.m +++ b/SignalServiceKit/tests/Messages/OWSMessageSenderTest.m @@ -10,13 +10,13 @@ #import "OWSFakeNetworkManager.h" #import "OWSMessageSender.h" #import "OWSUploadingService.h" +#import "TSAccountManager.h" #import "TSContactThread.h" #import "TSGroupModel.h" #import "TSGroupThread.h" #import "TSMessagesManager.h" #import "TSNetworkManager.h" #import "TSOutgoingMessage.h" -#import "TSStorageManager+keyingMaterial.h" #import "TSStorageManager.h" #import #import @@ -174,6 +174,12 @@ NS_ASSUME_NONNULL_BEGIN @end +@interface TSAccountManager (Testing) + +- (void)storeLocalNumber:(NSString *)localNumber; + +@end + @interface OWSMessageSenderTest : XCTestCase @property (nonatomic) TSThread *thread; @@ -192,7 +198,7 @@ NS_ASSUME_NONNULL_BEGIN [super setUp]; // Hack to make sure we don't explode when sending sync message. - [[TSStorageManager sharedManager] storePhoneNumber:@"+13231231234"]; + [[TSAccountManager sharedInstance] storeLocalNumber:@"+13231231234"]; self.thread = [[TSContactThread alloc] initWithUniqueId:@"fake-thread-id"]; [self.thread save];