From b7958262baa61d0fba7bfa3fae484c8748ab50b2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 14 Feb 2018 16:06:47 -0500 Subject: [PATCH] Respond to CR. --- .../src/Messages/OWSBatchMessageProcessor.m | 50 +++++-------------- .../src/Messages/OWSMessageReceiver.m | 30 ++--------- .../src/Messages/OWSReadReceiptManager.m | 28 +++-------- SignalServiceKit/src/Util/AppReadiness.h | 2 - SignalServiceKit/src/Util/AppReadiness.m | 11 +--- 5 files changed, 25 insertions(+), 96 deletions(-) diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m index 1cb4932f7..0f167a592 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m @@ -265,35 +265,14 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo _finder = finder; _isDrainingQueue = NO; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(appIsReady) - name:AppIsReadyNotification - object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(yapDatabaseModified:) - name:YapDatabaseModifiedNotification - object:TSStorageManager.sharedManager.dbNotificationObject]; + // Start processing. + [AppReadiness runNowOrWhenAppIsReady:^{ + [self drainQueue]; + }]; return self; } -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - -- (void)appIsReady -{ - [self drainQueue]; -} - -- (void)yapDatabaseModified:(NSNotification *)notification -{ - OWSAssertIsOnMainThread(); - - [self drainQueue]; -} - #pragma mark - instance methods - (dispatch_queue_t)serialQueue @@ -319,24 +298,14 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo - (void)drainQueue { + OWSAssert(AppReadiness.isAppReady); + // Don't process incoming messages in app extensions. if (!CurrentAppContext().isMainApp) { return; } dispatch_async(self.serialQueue, ^{ - if (!AppReadiness.isAppReady) { - // We don't want to process incoming messages until storage is ready. - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [AppReadiness runNowOrWhenAppIsReady:^{ - [self drainQueue]; - }]; - }); - - return; - } - if (self.isDrainingQueue) { return; } @@ -489,8 +458,13 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo // We need to persist the decrypted envelope data ASAP to prevent data loss. [self.processingQueue enqueueEnvelopeData:envelopeData plaintextData:plaintextData transaction:transaction]; + // The new envelope won't be visible to the finder until this transaction commits, - // so don't bother calling drainQueue here. + // so drainQueue in the transaction completion. + [transaction addCompletionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) + completionBlock:^{ + [self.processingQueue drainQueue]; + }]; } @end diff --git a/SignalServiceKit/src/Messages/OWSMessageReceiver.m b/SignalServiceKit/src/Messages/OWSMessageReceiver.m index d77013174..a5de76018 100644 --- a/SignalServiceKit/src/Messages/OWSMessageReceiver.m +++ b/SignalServiceKit/src/Messages/OWSMessageReceiver.m @@ -243,24 +243,13 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin _finder = finder; _isDrainingQueue = NO; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(appIsReady) - name:AppIsReadyNotification - object:nil]; + [AppReadiness runNowOrWhenAppIsReady:^{ + [self drainQueue]; + }]; return self; } -- (void)dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - -- (void)appIsReady -{ - [self drainQueue]; -} - #pragma mark - instance methods - (dispatch_queue_t)serialQueue @@ -280,23 +269,14 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin - (void)drainQueue { + OWSAssert(AppReadiness.isAppReady); + // Don't decrypt messages in app extensions. if (!CurrentAppContext().isMainApp) { return; } dispatch_async(self.serialQueue, ^{ - if (!AppReadiness.isAppReady) { - // We don't want to process incoming messages until storage is ready. - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [AppReadiness runNowOrWhenAppIsReady:^{ - [self drainQueue]; - }]; - }); - return; - } - if (self.isDrainingQueue) { return; } diff --git a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m index 1e364c0ad..97dc644a8 100644 --- a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m @@ -177,13 +177,10 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE OWSSingletonAssert(); - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(appIsReady) - name:AppIsReadyNotification - object:nil]; - - // Try to start processing. - [self scheduleProcessing]; + // Start processing. + [AppReadiness runNowOrWhenAppIsReady:^{ + [self scheduleProcessing]; + }]; return self; } @@ -193,27 +190,14 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE [[NSNotificationCenter defaultCenter] removeObserver:self]; } -- (void)appIsReady -{ - [self scheduleProcessing]; -} - // Schedules a processing pass, unless one is already scheduled. - (void)scheduleProcessing { + OWSAssert(AppReadiness.isAppReady); + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @synchronized(self) { - if (!AppReadiness.isAppReady) { - DDLogInfo(@"%@ Deferring read receipt processing; storage not yet ready.", self.logTag); - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [AppReadiness runNowOrWhenAppIsReady:^{ - [self scheduleProcessing]; - }]; - }); - return; - } if (self.isProcessing) { return; } diff --git a/SignalServiceKit/src/Util/AppReadiness.h b/SignalServiceKit/src/Util/AppReadiness.h index 837b13752..8a446fe93 100755 --- a/SignalServiceKit/src/Util/AppReadiness.h +++ b/SignalServiceKit/src/Util/AppReadiness.h @@ -4,8 +4,6 @@ NS_ASSUME_NONNULL_BEGIN -extern NSString *const AppIsReadyNotification; - typedef void (^AppReadyBlock)(void); @interface AppReadiness : NSObject diff --git a/SignalServiceKit/src/Util/AppReadiness.m b/SignalServiceKit/src/Util/AppReadiness.m index 4f67437ed..95ba13e7c 100755 --- a/SignalServiceKit/src/Util/AppReadiness.m +++ b/SignalServiceKit/src/Util/AppReadiness.m @@ -3,12 +3,9 @@ // #import "AppReadiness.h" -#import "NSNotificationCenter+OWS.h" NS_ASSUME_NONNULL_BEGIN -NSString *const AppIsReadyNotification = @"AppIsReadyNotification"; - @interface AppReadiness () @property (atomic) BOOL isAppReady; @@ -85,8 +82,6 @@ NSString *const AppIsReadyNotification = @"AppIsReadyNotification"; self.isAppReady = YES; [self runAppReadyBlocks]; - - [[NSNotificationCenter defaultCenter] postNotificationNameAsync:AppIsReadyNotification object:nil userInfo:nil]; } - (void)runAppReadyBlocks @@ -94,12 +89,10 @@ NSString *const AppIsReadyNotification = @"AppIsReadyNotification"; OWSAssertIsOnMainThread(); OWSAssert(self.isAppReady); - // Make a local copy, then clear this state. - NSArray *appReadyBlocks = self.appReadyBlocks; - self.appReadyBlocks = nil; - for (AppReadyBlock block in appReadyBlocks) { + for (AppReadyBlock block in self.appReadyBlocks) { block(); } + self.appReadyBlocks = nil; } @end