From 63e20cd8bba084ed2f51519b566db807dae14677 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 31 Jul 2017 14:48:43 -0400 Subject: [PATCH] Sketch out profile manager. // FREEBIE --- .../ViewControllers/DebugUI/DebugUIMessages.m | 2 +- .../src/ViewControllers/DebugUI/DebugUIMisc.m | 2 +- .../ViewControllers/NewGroupViewController.m | 2 +- .../UpdateGroupViewController.m | 2 +- .../src/Messages/OWSBlockingManager.h | 3 - .../src/Messages/OWSMessageSender.h | 6 +- .../src/Messages/OWSMessageSender.m | 10 ++ .../src/Profiles/OWSProfilesManager.h | 16 ++ .../src/Profiles/OWSProfilesManager.m | 144 ++++++++++++++++++ 9 files changed, 179 insertions(+), 8 deletions(-) create mode 100644 SignalServiceKit/src/Profiles/OWSProfilesManager.h create mode 100644 SignalServiceKit/src/Profiles/OWSProfilesManager.m diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 0d6470af6..86647fb13 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -5,7 +5,6 @@ #import "DebugUIMessages.h" #import "Environment.h" #import "OWSTableViewController.h" -#import "SecurityUtils.h" #import "Signal-Swift.h" #import "ThreadUtil.h" #import @@ -13,6 +12,7 @@ #import #import #import +#import #import #import #import diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m index 9a4b551ae..cdea5c066 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m @@ -6,7 +6,6 @@ #import "Environment.h" #import "OWSCountryMetadata.h" #import "OWSTableViewController.h" -#import "SecurityUtils.h" #import "Signal-Swift.h" #import "ThreadUtil.h" #import @@ -14,6 +13,7 @@ #import #import #import +#import #import #import #import diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index 000d84905..ab6236dab 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -11,7 +11,6 @@ #import "GroupViewHelper.h" #import "OWSContactsManager.h" #import "OWSTableViewController.h" -#import "SecurityUtils.h" #import "Signal-Swift.h" #import "SignalKeyingStorage.h" #import "TSOutgoingMessage.h" @@ -20,6 +19,7 @@ #import "UIViewController+OWS.h" #import #import +#import #import #import #import diff --git a/Signal/src/ViewControllers/UpdateGroupViewController.m b/Signal/src/ViewControllers/UpdateGroupViewController.m index 22e48a76c..0c4d67ced 100644 --- a/Signal/src/ViewControllers/UpdateGroupViewController.m +++ b/Signal/src/ViewControllers/UpdateGroupViewController.m @@ -11,7 +11,6 @@ #import "GroupViewHelper.h" #import "OWSContactsManager.h" #import "OWSTableViewController.h" -#import "SecurityUtils.h" #import "Signal-Swift.h" #import "SignalKeyingStorage.h" #import "TSOutgoingMessage.h" @@ -21,6 +20,7 @@ #import "ViewControllerUtils.h" #import #import +#import #import #import #import diff --git a/SignalServiceKit/src/Messages/OWSBlockingManager.h b/SignalServiceKit/src/Messages/OWSBlockingManager.h index ab7832995..7cfa80d54 100644 --- a/SignalServiceKit/src/Messages/OWSBlockingManager.h +++ b/SignalServiceKit/src/Messages/OWSBlockingManager.h @@ -4,9 +4,6 @@ NS_ASSUME_NONNULL_BEGIN -@class TSStorageManager; -@class OWSMessageSender; - extern NSString *const kNSNotificationName_BlockedPhoneNumbersDidChange; // This class can be safely accessed and used from any thread. diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.h b/SignalServiceKit/src/Messages/OWSMessageSender.h index 7c93fa6a3..b1cc2a6b1 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.h +++ b/SignalServiceKit/src/Messages/OWSMessageSender.h @@ -5,14 +5,16 @@ NS_ASSUME_NONNULL_BEGIN @class ContactsUpdater; +@class OWSBlockingManager; +@class OWSProfilesManager; @class OWSUploadingService; @class SignalRecipient; -@class OWSBlockingManager; @class TSInvalidIdentityKeySendingErrorMessage; @class TSNetworkManager; @class TSOutgoingMessage; @class TSStorageManager; @class TSThread; + @protocol ContactsManagerProtocol; /** @@ -57,6 +59,8 @@ NS_SWIFT_NAME(MessageSender) - (void)setBlockingManager:(OWSBlockingManager *)blockingManager; +- (void)setProfilesManager:(OWSProfilesManager *)profilesManager; + /** * Send and resend text messages or resend messages with existing attachments. * If you haven't yet created the attachment, see the `sendAttachmentData:` variants. diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index b97437e57..3202e2918 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -14,6 +14,7 @@ #import "OWSMessageServiceParams.h" #import "OWSOutgoingSentMessageTranscript.h" #import "OWSOutgoingSyncMessage.h" +#import "OWSProfilesManager.h" #import "OWSUploadingService.h" #import "PreKeyBundle+jsonDict.h" #import "SignalRecipient.h" @@ -356,6 +357,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; @property (nonatomic, readonly) TSNetworkManager *networkManager; @property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, readonly) OWSBlockingManager *blockingManager; +@property (nonatomic, readonly) OWSProfilesManager *profilesManager; @property (nonatomic, readonly) OWSUploadingService *uploadingService; @property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @property (nonatomic, readonly) id contactsManager; @@ -398,6 +400,14 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; _blockingManager = blockingManager; } +- (void)setProfilesManager:(OWSProfilesManager *)profilesManager +{ + OWSAssert(profilesManager); + OWSAssert(!_profilesManager); + + _profilesManager = profilesManager; +} + - (NSOperationQueue *)sendingQueueForMessage:(TSOutgoingMessage *)message { OWSAssert(message); diff --git a/SignalServiceKit/src/Profiles/OWSProfilesManager.h b/SignalServiceKit/src/Profiles/OWSProfilesManager.h new file mode 100644 index 000000000..139bb2728 --- /dev/null +++ b/SignalServiceKit/src/Profiles/OWSProfilesManager.h @@ -0,0 +1,16 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +NS_ASSUME_NONNULL_BEGIN + +// This class can be safely accessed and used from any thread. +@interface OWSProfilesManager : NSObject + +- (instancetype)init NS_UNAVAILABLE; + ++ (instancetype)sharedManager; + +@end + +NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Profiles/OWSProfilesManager.m b/SignalServiceKit/src/Profiles/OWSProfilesManager.m new file mode 100644 index 000000000..a46c7f437 --- /dev/null +++ b/SignalServiceKit/src/Profiles/OWSProfilesManager.m @@ -0,0 +1,144 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +#import "OWSProfilesManager.h" +#import "OWSMessageSender.h" +#import "SecurityUtils.h" +#import "TSStorageManager.h" +#import "TextSecureKitEnv.h" + +NS_ASSUME_NONNULL_BEGIN + +NSString *const kOWSProfilesManager_Collection = @"kOWSProfilesManager_Collection"; +// This key is used to persist the local user's profile key. +NSString *const kOWSProfilesManager_LocalProfileKey = @"kOWSProfilesManager_LocalProfileKey"; + +// TODO: +static const NSInteger kProfileKeyLength = 16; + +@interface OWSProfilesManager () + +@property (nonatomic, readonly) TSStorageManager *storageManager; +@property (nonatomic, readonly) OWSMessageSender *messageSender; + +@property (nonatomic, readonly, nullable) NSData *localProfileKey; + +@end + +#pragma mark - + +@implementation OWSProfilesManager + +@synthesize localProfileKey = _localProfileKey; + ++ (instancetype)sharedManager +{ + static OWSProfilesManager *sharedMyManager = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedMyManager = [[self alloc] initDefault]; + }); + return sharedMyManager; +} + +- (instancetype)initDefault +{ + TSStorageManager *storageManager = [TSStorageManager sharedManager]; + OWSMessageSender *messageSender = [TextSecureKitEnv sharedEnv].messageSender; + + return [self initWithStorageManager:storageManager messageSender:messageSender]; +} + +- (instancetype)initWithStorageManager:(TSStorageManager *)storageManager + messageSender:(OWSMessageSender *)messageSender +{ + self = [super init]; + + if (!self) { + return self; + } + + OWSAssert(storageManager); + OWSAssert(messageSender); + + _storageManager = storageManager; + _messageSender = messageSender; + + OWSSingletonAssert(); + + // Register this manager with the message sender. + // This is a circular dependency. + [messageSender setProfilesManager:self]; + + // Try to load. + _localProfileKey = [self.storageManager objectForKey:kOWSProfilesManager_LocalProfileKey + inCollection:kOWSProfilesManager_Collection]; + if (!_localProfileKey) { + // Generate + _localProfileKey = [OWSProfilesManager generateLocalProfileKey]; + // Persist + [self.storageManager setObject:_localProfileKey + forKey:kOWSProfilesManager_LocalProfileKey + inCollection:kOWSProfilesManager_Collection]; + } + OWSAssert(_localProfileKey.length == kProfileKeyLength); + + return self; +} + +- (void)dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; +} + +- (void)observeNotifications +{ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(applicationDidBecomeActive:) + name:UIApplicationDidBecomeActiveNotification + object:nil]; +} + +#pragma mark - Local Profile Key + ++ (NSData *)generateLocalProfileKey +{ + // TODO: + OWSFail(@"Profile key generation is not yet implemented."); + return [SecurityUtils generateRandomBytes:kProfileKeyLength]; +} + +- (nullable NSData *)localProfileKey +{ + OWSAssert(_localProfileKey.length == kProfileKeyLength); + return _localProfileKey; +} + +#pragma mark - Notifications + +- (void)applicationDidBecomeActive:(NSNotification *)notification +{ + OWSAssert([NSThread isMainThread]); + + @synchronized(self) + { + // TODO: Sync if necessary. + } +} + +#pragma mark - Logging + ++ (NSString *)tag +{ + return [NSString stringWithFormat:@"[%@]", self.class]; +} + +- (NSString *)tag +{ + return self.class.tag; +} + +@end + +NS_ASSUME_NONNULL_END