From d3e16583eb2de998a68a5715f83be3e2276e2422 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 30 Jan 2018 15:49:36 -0500 Subject: [PATCH] Add protocol context to protocol kit. --- Signal/src/Jobs/SessionResetJob.swift | 14 ++-- .../ConversationViewController.m | 21 ++--- .../ViewControllers/DebugUI/DebugUIMessages.m | 6 +- .../DebugUI/DebugUISessionState.m | 76 +++++++++---------- .../FingerprintViewController.m | 27 ++++--- .../FingerprintViewScanController.m | 15 ++-- .../src/ViewControllers/HomeViewController.m | 5 +- .../src/ViewControllers/InboxTableViewCell.m | 2 +- .../OWSConversationSettingsViewController.m | 3 +- .../OWSLinkDeviceViewController.m | 2 +- .../SafetyNumberConfirmationAlert.swift | 6 +- .../ShowGroupMembersViewController.m | 19 +++-- Signal/src/environment/NotificationsManager.m | 5 +- .../SharingThreadPickerViewController.m | 13 ++-- .../profiles/ProfileFetcherJob.swift | 8 +- .../src/Messages/OWSIdentityManager.h | 7 +- .../src/Messages/OWSIdentityManager.m | 41 +++++----- 17 files changed, 145 insertions(+), 125 deletions(-) diff --git a/Signal/src/Jobs/SessionResetJob.swift b/Signal/src/Jobs/SessionResetJob.swift index 91ec7ea94..bb1964355 100644 --- a/Signal/src/Jobs/SessionResetJob.swift +++ b/Signal/src/Jobs/SessionResetJob.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // import Foundation @@ -26,19 +26,19 @@ class SessionResetJob: NSObject { func run() { Logger.info("\(TAG) Local user reset session.") - OWSDispatch.sessionStoreQueue().async { + TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in Logger.info("\(self.TAG) deleting sessions for recipient: \(self.recipientId)") - self.storageManager.deleteAllSessions(forContact: self.recipientId) + self.storageManager.deleteAllSessions(forContact: self.recipientId, protocolContext: transaction) DispatchQueue.main.async { let endSessionMessage = EndSessionMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: self.thread) self.messageSender.enqueue(endSessionMessage, success: { - OWSDispatch.sessionStoreQueue().async { + TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in // Archive the just-created session since the recipient should delete their corresponding // session upon receiving and decrypting our EndSession message. // Otherwise if we send another message before them, they wont have the session to decrypt it. - self.storageManager.archiveAllSessions(forContact: self.recipientId) + self.storageManager.archiveAllSessions(forContact: self.recipientId, protocolContext: transaction) } Logger.info("\(self.TAG) successfully sent EndSessionMessage.") let message = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), @@ -46,7 +46,7 @@ class SessionResetJob: NSObject { messageType: TSInfoMessageType.typeSessionDidEnd) message.save() }, failure: {error in - OWSDispatch.sessionStoreQueue().async { + TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in // Even though this is the error handler - which means probably the recipient didn't receive the message // there's a chance that our send did succeed and the server just timed out our repsonse or something. // Since the cost of sending a future message using a session the recipient doesn't have is so high, @@ -55,7 +55,7 @@ class SessionResetJob: NSObject { // Archive the just-created session since the recipient should delete their corresponding // session upon receiving and decrypting our EndSession message. // Otherwise if we send another message before them, they wont have the session to decrypt it. - self.storageManager.archiveAllSessions(forContact: self.recipientId) + self.storageManager.archiveAllSessions(forContact: self.recipientId, protocolContext: transaction) } Logger.error("\(self.TAG) failed to send EndSessionMessage with error: \(error.localizedDescription)") }) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 2f586cca2..3ac6a52c9 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -80,6 +80,7 @@ #import #import #import +#import #import #import #import @@ -202,7 +203,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { @property (nonatomic, readonly) ContactsUpdater *contactsUpdater; @property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) TSStorageManager *storageManager; -@property (nonatomic, readonly) OWSMessageManager *messagesManager; @property (nonatomic, readonly) TSNetworkManager *networkManager; @property (nonatomic, readonly) OutboundCallInitiator *outboundCallInitiator; @property (nonatomic, readonly) OWSBlockingManager *blockingManager; @@ -274,7 +274,6 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { _messageSender = [Environment current].messageSender; _outboundCallInitiator = SignalApp.sharedApp.outboundCallInitiator; _storageManager = [TSStorageManager sharedManager]; - _messagesManager = [OWSMessageManager sharedManager]; _networkManager = [TSNetworkManager sharedManager]; _blockingManager = [OWSBlockingManager sharedManager]; _contactsViewHelper = [[ContactsViewHelper alloc] initWithDelegate:self]; @@ -700,7 +699,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { { NSMutableArray *result = [NSMutableArray new]; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] == OWSVerificationStateNoLongerVerified) { [result addObject:recipientId]; } @@ -927,10 +926,14 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { continue; } - [OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateDefault - identityKey:identityKey - recipientId:recipientId - isUserInitiatedChange:YES]; + [TSStorageManager.protocolStoreDBConnection + asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateDefault + identityKey:identityKey + recipientId:recipientId + isUserInitiatedChange:YES + protocolContext:transaction]; + }]; } } @@ -1300,7 +1303,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { BOOL isVerified = YES; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] != OWSVerificationStateVerified) { isVerified = NO; break; @@ -3586,7 +3589,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { - (void)updateBackButtonUnreadCount { OWSAssertIsOnMainThread(); - self.backButtonUnreadCount = [self.messagesManager unreadMessagesCountExcept:self.thread]; + self.backButtonUnreadCount = [OWSMessageUtils.sharedManager unreadMessagesCountExcept:self.thread]; } - (void)setBackButtonUnreadCount:(NSUInteger)unreadCount diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 3f53fbe08..1e982b778 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -1258,7 +1258,11 @@ NS_ASSUME_NONNULL_BEGIN NSData *envelopeData = [envelopeBuilder build].data; OWSAssert(envelopeData); - [[OWSBatchMessageProcessor sharedInstance] enqueueEnvelopeData:envelopeData plaintextData:plaintextData]; + [TSStorageManager.protocolStoreDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [[OWSBatchMessageProcessor sharedInstance] enqueueEnvelopeData:envelopeData + plaintextData:plaintextData + transaction:transaction]; + }]; } + (void)performRandomActions:(int)counter thread:(TSThread *)thread diff --git a/Signal/src/ViewControllers/DebugUI/DebugUISessionState.m b/Signal/src/ViewControllers/DebugUI/DebugUISessionState.m index f2ce05caa..7d21310b8 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUISessionState.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUISessionState.m @@ -30,9 +30,7 @@ NS_ASSUME_NONNULL_BEGIN }], [OWSTableItem itemWithTitle:@"Log All Sessions" actionBlock:^{ - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] printAllSessions]; - }); + [[TSStorageManager sharedManager] printAllSessions]; }], [OWSTableItem itemWithTitle:@"Toggle Key Change" actionBlock:^{ @@ -41,33 +39,39 @@ NS_ASSUME_NONNULL_BEGIN OWSIdentityManager *identityManager = [OWSIdentityManager sharedManager]; NSString *recipientId = [thread contactIdentifier]; - NSData *currentKey = [identityManager identityKeyForRecipientIdWOT:recipientId]; - NSMutableData *flippedKey = [NSMutableData new]; - const char *currentKeyBytes = currentKey.bytes; - for (NSUInteger i = 0; i < currentKey.length; i++) { - const char xorByte = currentKeyBytes[i] ^ 0xff; - [flippedKey appendBytes:&xorByte length:1]; - } - OWSAssert(flippedKey.length == currentKey.length); - [identityManager saveRemoteIdentity:flippedKey - recipientId:recipientId - protocolContext:protocolContext]; + [TSStorageManager.protocolStoreDBConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + NSData *currentKey = [identityManager identityKeyForRecipientId:recipientId + protocolContext:transaction]; + NSMutableData *flippedKey = [NSMutableData new]; + const char *currentKeyBytes = currentKey.bytes; + for (NSUInteger i = 0; i < currentKey.length; i++) { + const char xorByte = currentKeyBytes[i] ^ 0xff; + [flippedKey appendBytes:&xorByte length:1]; + } + OWSAssert(flippedKey.length == currentKey.length); + [identityManager saveRemoteIdentity:flippedKey + recipientId:recipientId + protocolContext:transaction]; + }]; }], [OWSTableItem itemWithTitle:@"Delete all sessions" actionBlock:^{ - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] - deleteAllSessionsForContact:thread.contactIdentifier - protocolContext:protocolContext]; - }); + [TSStorageManager.protocolStoreDBConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [[TSStorageManager sharedManager] + deleteAllSessionsForContact:thread.contactIdentifier + protocolContext:transaction]; + }]; }], [OWSTableItem itemWithTitle:@"Archive all sessions" actionBlock:^{ - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] - archiveAllSessionsForContact:thread.contactIdentifier - protocolContext:protocolContext]; - }); + [TSStorageManager.protocolStoreDBConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [[TSStorageManager sharedManager] + archiveAllSessionsForContact:thread.contactIdentifier + protocolContext:transaction]; + }]; }], [OWSTableItem itemWithTitle:@"Send session reset" actionBlock:^{ @@ -101,32 +105,20 @@ NS_ASSUME_NONNULL_BEGIN #if DEBUG + (void)clearSessionAndIdentityStore { - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] resetSessionStore]; - dispatch_async(dispatch_get_main_queue(), ^{ - [[OWSIdentityManager sharedManager] clearIdentityState]; - }); - }); + [[TSStorageManager sharedManager] resetSessionStore]; + [[OWSIdentityManager sharedManager] clearIdentityState]; } + (void)snapshotSessionAndIdentityStore { - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] snapshotSessionStore]; - dispatch_async(dispatch_get_main_queue(), ^{ - [[OWSIdentityManager sharedManager] snapshotIdentityState]; - }); - }); + [[TSStorageManager sharedManager] snapshotSessionStore]; + [[OWSIdentityManager sharedManager] snapshotIdentityState]; } + (void)restoreSessionAndIdentityStore { - dispatch_async([OWSDispatch sessionStoreQueue], ^{ - [[TSStorageManager sharedManager] restoreSessionStore]; - dispatch_async(dispatch_get_main_queue(), ^{ - [[OWSIdentityManager sharedManager] restoreIdentityState]; - }); - }); + [[TSStorageManager sharedManager] restoreSessionStore]; + [[OWSIdentityManager sharedManager] restoreIdentityState]; } #endif diff --git a/Signal/src/ViewControllers/FingerprintViewController.m b/Signal/src/ViewControllers/FingerprintViewController.m index 355c57c7b..3af6a0a8c 100644 --- a/Signal/src/ViewControllers/FingerprintViewController.m +++ b/Signal/src/ViewControllers/FingerprintViewController.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "FingerprintViewController.h" @@ -338,7 +338,8 @@ typedef void (^CustomLayoutBlock)(void); { OWSAssert(self.recipientId.length > 0); - BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId] + BOOL isVerified = + [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:self.recipientId] == OWSVerificationStateVerified; if (isVerified) { @@ -512,15 +513,19 @@ typedef void (^CustomLayoutBlock)(void); - (void)verifyUnverifyButtonTapped:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateRecognized) { - BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId] - == OWSVerificationStateVerified; - - OWSVerificationState newVerificationState - = (isVerified ? OWSVerificationStateDefault : OWSVerificationStateVerified); - [[OWSIdentityManager sharedManager] setVerificationState:newVerificationState - identityKey:self.identityKey - recipientId:self.recipientId - isUserInitiatedChange:YES]; + [TSStorageManager.protocolStoreDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId + transaction:transaction] + == OWSVerificationStateVerified; + + OWSVerificationState newVerificationState + = (isVerified ? OWSVerificationStateDefault : OWSVerificationStateVerified); + [[OWSIdentityManager sharedManager] setVerificationState:newVerificationState + identityKey:self.identityKey + recipientId:self.recipientId + isUserInitiatedChange:YES + protocolContext:transaction]; + }]; [self dismissViewControllerAnimated:YES completion:nil]; } diff --git a/Signal/src/ViewControllers/FingerprintViewScanController.m b/Signal/src/ViewControllers/FingerprintViewScanController.m index fa95c2c06..4578d700f 100644 --- a/Signal/src/ViewControllers/FingerprintViewScanController.m +++ b/Signal/src/ViewControllers/FingerprintViewScanController.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "FingerprintViewScanController.h" @@ -200,10 +200,15 @@ NS_ASSUME_NONNULL_BEGIN @"Button that marks user as verified after a successful fingerprint scan.") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { - [OWSIdentityManager.sharedManager setVerificationState:OWSVerificationStateVerified - identityKey:identityKey - recipientId:recipientId - isUserInitiatedChange:YES]; + [TSStorageManager.protocolStoreDBConnection + asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [OWSIdentityManager.sharedManager + setVerificationState:OWSVerificationStateVerified + identityKey:identityKey + recipientId:recipientId + isUserInitiatedChange:YES + protocolContext:transaction]; + }]; [viewController dismissViewControllerAnimated:true completion:nil]; }]]; UIAlertAction *dismissAction = diff --git a/Signal/src/ViewControllers/HomeViewController.m b/Signal/src/ViewControllers/HomeViewController.m index 63984c626..9d5d90744 100644 --- a/Signal/src/ViewControllers/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeViewController.m @@ -25,6 +25,7 @@ #import #import #import +#import #import #import #import @@ -57,7 +58,6 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; @property (nonatomic, readonly) AccountManager *accountManager; @property (nonatomic, readonly) OWSContactsManager *contactsManager; -@property (nonatomic, readonly) OWSMessageManager *messagesManager; @property (nonatomic, readonly) OWSMessageSender *messageSender; @property (nonatomic, readonly) OWSBlockingManager *blockingManager; @@ -106,7 +106,6 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; { _accountManager = SignalApp.sharedApp.accountManager; _contactsManager = [Environment current].contactsManager; - _messagesManager = [OWSMessageManager sharedManager]; _messageSender = [Environment current].messageSender; _blockingManager = [OWSBlockingManager sharedManager]; _blockedPhoneNumberSet = [NSSet setWithArray:[_blockingManager blockedPhoneNumbers]]; @@ -752,7 +751,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState }; - (void)updateInboxCountLabel { - NSUInteger numberOfItems = [self.messagesManager unreadMessagesCount]; + NSUInteger numberOfItems = [OWSMessageUtils.sharedManager unreadMessagesCount]; NSString *unreadString = NSLocalizedString(@"WHISPER_NAV_BAR_TITLE", nil); if (numberOfItems > 0) { diff --git a/Signal/src/ViewControllers/InboxTableViewCell.m b/Signal/src/ViewControllers/InboxTableViewCell.m index 95f1b8318..1132a5e28 100644 --- a/Signal/src/ViewControllers/InboxTableViewCell.m +++ b/Signal/src/ViewControllers/InboxTableViewCell.m @@ -205,7 +205,7 @@ const NSUInteger kAvatarViewDiameter = 52; } NSAttributedString *attributedDate = [self dateAttributedString:thread.lastMessageDate]; - NSUInteger unreadCount = [[OWSMessageManager sharedManager] unreadMessagesInThread:thread]; + NSUInteger unreadCount = [[OWSMessageUtils sharedManager] unreadMessagesInThread:thread]; [[NSNotificationCenter defaultCenter] addObserver:self diff --git a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m index 0f493de33..0440e84bd 100644 --- a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m +++ b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m @@ -682,7 +682,8 @@ NS_ASSUME_NONNULL_BEGIN } } - BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] + BOOL isVerified = + [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] == OWSVerificationStateVerified; if (isVerified) { NSMutableAttributedString *subtitle = [NSMutableAttributedString new]; diff --git a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m index 2c178d215..81313d237 100644 --- a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m +++ b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m @@ -149,7 +149,7 @@ NS_ASSUME_NONNULL_BEGIN // Optimistically set this flag. [OWSDeviceManager.sharedManager setMayHaveLinkedDevices]; - ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; + ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext]; OWSAssert(identityKeyPair); NSData *myPublicKey = identityKeyPair.publicKey; NSData *myPrivateKey = identityKeyPair.ows_privateKey; diff --git a/Signal/src/ViewControllers/SafetyNumberConfirmationAlert.swift b/Signal/src/ViewControllers/SafetyNumberConfirmationAlert.swift index 129a2fcd9..5d5d12eef 100644 --- a/Signal/src/ViewControllers/SafetyNumberConfirmationAlert.swift +++ b/Signal/src/ViewControllers/SafetyNumberConfirmationAlert.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // import Foundation @@ -55,8 +55,8 @@ class SafetyNumberConfirmationAlert: NSObject { let confirmAction = UIAlertAction(title: confirmationText, style: .default) { _ in Logger.info("\(self.TAG) Confirmed identity: \(untrustedIdentity)") - OWSDispatch.sessionStoreQueue().async { - OWSIdentityManager.shared().setVerificationState(.default, identityKey: untrustedIdentity.identityKey, recipientId: untrustedIdentity.recipientId, isUserInitiatedChange: true) + TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in + OWSIdentityManager.shared().setVerificationState(.default, identityKey: untrustedIdentity.identityKey, recipientId: untrustedIdentity.recipientId, isUserInitiatedChange: true, protocolContext: transaction) DispatchQueue.main.async { completion(true) } diff --git a/Signal/src/ViewControllers/ShowGroupMembersViewController.m b/Signal/src/ViewControllers/ShowGroupMembersViewController.m index d73258406..b0f17d2dd 100644 --- a/Signal/src/ViewControllers/ShowGroupMembersViewController.m +++ b/Signal/src/ViewControllers/ShowGroupMembersViewController.m @@ -177,7 +177,7 @@ NS_ASSUME_NONNULL_BEGIN ContactTableViewCell *cell = [ContactTableViewCell new]; SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId]; OWSVerificationState verificationState = - [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]; + [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]; BOOL isVerified = verificationState == OWSVerificationStateVerified; BOOL isNoLongerVerified = verificationState == OWSVerificationStateNoLongerVerified; BOOL isBlocked = [helper isRecipientIdBlocked:recipientId]; @@ -244,17 +244,22 @@ NS_ASSUME_NONNULL_BEGIN OWSIdentityManager *identityManger = [OWSIdentityManager sharedManager]; NSArray *recipientIds = [self noLongerVerifiedRecipientIds]; for (NSString *recipientId in recipientIds) { - OWSVerificationState verificationState = [identityManger verificationStateForRecipientId:recipientId]; + OWSVerificationState verificationState = + [identityManger verificationStateForRecipientIdWithoutTransaction:recipientId]; if (verificationState == OWSVerificationStateNoLongerVerified) { NSData *identityKey = [identityManger identityKeyForRecipientIdWOT:recipientId]; if (identityKey.length < 1) { OWSFail(@"Missing identity key for: %@", recipientId); continue; } - [identityManger setVerificationState:OWSVerificationStateDefault - identityKey:identityKey - recipientId:recipientId - isUserInitiatedChange:YES]; + [TSStorageManager.protocolStoreDBConnection + asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [identityManger setVerificationState:OWSVerificationStateDefault + identityKey:identityKey + recipientId:recipientId + isUserInitiatedChange:YES + protocolContext:transaction]; + }]; } } @@ -266,7 +271,7 @@ NS_ASSUME_NONNULL_BEGIN { NSMutableArray *result = [NSMutableArray new]; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] == OWSVerificationStateNoLongerVerified) { [result addObject:recipientId]; } diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 8cc67bb1a..19d343a21 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "NotificationsManager.h" @@ -291,7 +291,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc"; // "no longer verified". BOOL isNoLongerVerified = NO; for (NSString *recipientId in thread.recipientIdentifiers) { - if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId] + if ([OWSIdentityManager.sharedManager + verificationStateForRecipientIdWithoutTransaction:recipientId] == OWSVerificationStateNoLongerVerified) { isNoLongerVerified = YES; break; diff --git a/SignalMessaging/attachments/SharingThreadPickerViewController.m b/SignalMessaging/attachments/SharingThreadPickerViewController.m index 7338ddec1..21e32cdf6 100644 --- a/SignalMessaging/attachments/SharingThreadPickerViewController.m +++ b/SignalMessaging/attachments/SharingThreadPickerViewController.m @@ -385,9 +385,10 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion); DDLogDebug(@"%@ Confirming identity for recipient: %@", self.logTag, recipientId); - dispatch_async([OWSDispatch sessionStoreQueue], ^(void) { + [TSStorageManager.protocolStoreDBConnection asyncReadWriteWithBlock:^( + YapDatabaseReadWriteTransaction *transaction) { OWSVerificationState verificationState = - [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]; + [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId transaction:transaction]; switch (verificationState) { case OWSVerificationStateVerified: { OWSFail(@"%@ Shouldn't need to confirm identity if it was already verified", self.logTag); @@ -404,12 +405,14 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion); } case OWSVerificationStateNoLongerVerified: { DDLogInfo(@"%@ marked recipient: %@ as default verification status.", self.logTag, recipientId); - NSData *identityKey = [[OWSIdentityManager sharedManager] identityKeyForRecipientIdWOT:recipientId]; + NSData *identityKey = [[OWSIdentityManager sharedManager] identityKeyForRecipientId:recipientId + protocolContext:transaction]; OWSAssert(identityKey); [[OWSIdentityManager sharedManager] setVerificationState:OWSVerificationStateDefault identityKey:identityKey recipientId:recipientId - isUserInitiatedChange:YES]; + isUserInitiatedChange:YES + protocolContext:transaction]; break; } } @@ -417,7 +420,7 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion); dispatch_async(dispatch_get_main_queue(), ^(void) { [self resendMessage:message fromViewController:fromViewController]; }); - }); + }]; } - (void)resendMessage:(TSOutgoingMessage *)message fromViewController:(UIViewController *)fromViewController diff --git a/SignalMessaging/profiles/ProfileFetcherJob.swift b/SignalMessaging/profiles/ProfileFetcherJob.swift index ddf3afa74..2a36487f1 100644 --- a/SignalMessaging/profiles/ProfileFetcherJob.swift +++ b/SignalMessaging/profiles/ProfileFetcherJob.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // import Foundation @@ -130,10 +130,10 @@ public class ProfileFetcherJob: NSObject { } private func verifyIdentityUpToDateAsync(recipientId: String, latestIdentityKey: Data) { - OWSDispatch.sessionStoreQueue().async { - if OWSIdentityManager.shared().saveRemoteIdentity(latestIdentityKey, recipientId: recipientId) { + TSStorageManager.protocolStoreDBConnection().asyncReadWrite { (transaction) in + if OWSIdentityManager.shared().saveRemoteIdentity(latestIdentityKey, recipientId: recipientId, protocolContext: transaction) { Logger.info("\(self.TAG) updated identity key with fetched profile for recipient: \(recipientId)") - self.storageManager.archiveAllSessions(forContact: recipientId) + self.storageManager.archiveAllSessions(forContact: recipientId, protocolContext: transaction) } else { // no change in identity. } diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.h b/SignalServiceKit/src/Messages/OWSIdentityManager.h index 40863a0a2..5e039d692 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.h +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.h @@ -41,7 +41,9 @@ extern const NSUInteger kIdentityKeyLength; isUserInitiatedChange:(BOOL)isUserInitiatedChange protocolContext:(nullable id)protocolContext; -- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId; +- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId; +- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId + transaction:(YapDatabaseReadWriteTransaction *)transaction; - (nullable OWSRecipientIdentity *)recipientIdentityForRecipientId:(NSString *)recipientId; @@ -50,8 +52,7 @@ extern const NSUInteger kIdentityKeyLength; * @returns nil if the recipient does not exist, or is trusted for sending * else returns the untrusted recipient. */ -- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId - protocolContext:(nullable id)protocolContext; +- (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId; // This method can be called from any thread. - (void)processIncomingSyncMessage:(OWSSignalServiceProtosVerified *)verified; diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index a1eca6f18..a189d3365 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -305,7 +305,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa [self fireIdentityStateChangeNotification]; } -- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId +- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId { __block OWSVerificationState result; // Use a read/write transaction to block on latest. @@ -345,30 +345,31 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa } - (nullable OWSRecipientIdentity *)untrustedIdentityForSendingToRecipientId:(NSString *)recipientId - protocolContext:(nullable id)protocolContext { OWSAssert(recipientId.length > 0); - OWSAssert([protocolContext isKindOfClass:[YapDatabaseReadWriteTransaction class]]); - YapDatabaseReadWriteTransaction *transaction = protocolContext; - - OWSRecipientIdentity *_Nullable recipientIdentity = - [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId transaction:transaction]; + __block OWSRecipientIdentity *_Nullable result; + // Use a read/write transaction to block on latest. + [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + OWSRecipientIdentity *_Nullable recipientIdentity = + [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId transaction:transaction]; - if (recipientIdentity == nil) { - // trust on first use - return nil; - } + if (recipientIdentity == nil) { + // trust on first use + return; + } - BOOL isTrusted = [self isTrustedIdentityKey:recipientIdentity.identityKey - recipientId:recipientId - direction:TSMessageDirectionOutgoing - protocolContext:protocolContext]; - if (isTrusted) { - return nil; - } else { - return recipientIdentity; - } + BOOL isTrusted = [self isTrustedIdentityKey:recipientIdentity.identityKey + recipientId:recipientId + direction:TSMessageDirectionOutgoing + protocolContext:transaction]; + if (isTrusted) { + return; + } else { + result = recipientIdentity; + } + }]; + return result; } - (void)fireIdentityStateChangeNotification