Merge branch 'charlesmchen/offMainThread'

pull/1/head
Matthew Chen 8 years ago
commit b34076eeaf

@ -495,7 +495,7 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68;
// This will save the message. // This will save the message.
[message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)]; [message updateWithCustomMessage:NSLocalizedString(@"GROUP_CREATED", nil)];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (model.groupImage) { if (model.groupImage) {
NSData *data = UIImagePNGRepresentation(model.groupImage); NSData *data = UIImagePNGRepresentation(model.groupImage);
DataSource *_Nullable dataSource = DataSource *_Nullable dataSource =

@ -206,45 +206,46 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
#pragma mark - Signal Messages #pragma mark - Signal Messages
- (void)notifyUserForErrorMessage:(TSErrorMessage *)message inThread:(TSThread *)thread { - (void)notifyUserForErrorMessage:(TSErrorMessage *)message inThread:(TSThread *)thread {
OWSAssert([NSThread isMainThread]);
OWSAssert(message); OWSAssert(message);
OWSAssert(thread); OWSAssert(thread);
if (thread.isMuted) { dispatch_async(dispatch_get_main_queue(), ^{
return; if (thread.isMuted) {
} return;
}
BOOL shouldPlaySound = [self shouldPlaySoundForNotification]; BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
NSString *messageDescription = message.description; NSString *messageDescription = message.description;
if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageDescription) { if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageDescription) {
UILocalNotification *notification = [[UILocalNotification alloc] init]; UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = @{Signal_Thread_UserInfo_Key : thread.uniqueId}; notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
if (shouldPlaySound) { if (shouldPlaySound) {
notification.soundName = kNotificationsManagerNewMesssageSoundName; notification.soundName = kNotificationsManagerNewMesssageSoundName;
} }
NSString *alertBodyString = @"";
NSString *authorName = [thread name]; NSString *alertBodyString = @"";
switch (self.notificationPreviewType) {
case NotificationNamePreview: NSString *authorName = [thread name];
case NotificationNameNoPreview: switch (self.notificationPreviewType) {
alertBodyString = [NSString stringWithFormat:@"%@: %@", authorName, messageDescription]; case NotificationNamePreview:
break; case NotificationNameNoPreview:
case NotificationNoNameNoPreview: alertBodyString = [NSString stringWithFormat:@"%@: %@", authorName, messageDescription];
alertBodyString = messageDescription; break;
break; case NotificationNoNameNoPreview:
} alertBodyString = messageDescription;
notification.alertBody = alertBodyString; break;
}
notification.alertBody = alertBodyString;
[[PushManager sharedManager] presentNotification:notification checkForCancel:NO]; [[PushManager sharedManager] presentNotification:notification checkForCancel:NO];
} else { } else {
if (shouldPlaySound && [Environment.preferences soundInForeground]) { if (shouldPlaySound && [Environment.preferences soundInForeground]) {
AudioServicesPlayAlertSound(_newMessageSound); AudioServicesPlayAlertSound(_newMessageSound);
}
} }
} });
} }
- (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message - (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message
@ -255,83 +256,87 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
OWSAssert(thread); OWSAssert(thread);
OWSAssert(contactsManager); OWSAssert(contactsManager);
if (thread.isMuted) { dispatch_async(dispatch_get_main_queue(), ^{
return; if (thread.isMuted) {
} return;
}
BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
NSString *messageDescription = message.description; BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
NSString *senderName = [contactsManager displayNameForPhoneIdentifier:message.authorId];
NSString *groupName = [thread.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (groupName.length < 1) {
groupName = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"");
}
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive && messageDescription) { NSString *messageDescription = message.description;
UILocalNotification *notification = [[UILocalNotification alloc] init]; NSString *senderName = [contactsManager displayNameForPhoneIdentifier:message.authorId];
if (shouldPlaySound) { NSString *groupName = [thread.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
notification.soundName = kNotificationsManagerNewMesssageSoundName; if (groupName.length < 1) {
groupName = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"");
} }
switch (self.notificationPreviewType) { if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive && messageDescription) {
case NotificationNamePreview: { UILocalNotification *notification = [[UILocalNotification alloc] init];
if (shouldPlaySound) {
// Don't reply from lockscreen if anyone in this conversation is notification.soundName = kNotificationsManagerNewMesssageSoundName;
// "no longer verified". }
BOOL isNoLongerVerified = NO;
for (NSString *recipientId in thread.recipientIdentifiers) { switch (self.notificationPreviewType) {
if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId] case NotificationNamePreview: {
== OWSVerificationStateNoLongerVerified) {
isNoLongerVerified = YES; // Don't reply from lockscreen if anyone in this conversation is
break; // "no longer verified".
BOOL isNoLongerVerified = NO;
for (NSString *recipientId in thread.recipientIdentifiers) {
if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId]
== OWSVerificationStateNoLongerVerified) {
isNoLongerVerified = YES;
break;
}
} }
}
notification.category = (isNoLongerVerified ? Signal_Full_New_Message_Category_No_Longer_Verified notification.category = (isNoLongerVerified ? Signal_Full_New_Message_Category_No_Longer_Verified
: Signal_Full_New_Message_Category); : Signal_Full_New_Message_Category);
notification.userInfo = notification.userInfo = @{
@{Signal_Thread_UserInfo_Key : thread.uniqueId, Signal_Message_UserInfo_Key : message.uniqueId}; Signal_Thread_UserInfo_Key : thread.uniqueId,
Signal_Message_UserInfo_Key : message.uniqueId
if ([thread isGroupThread]) { };
NSString *threadName = [NSString stringWithFormat:@"\"%@\"", groupName];
// TODO: Format parameters might change order in l10n. We should use named parameters. if ([thread isGroupThread]) {
notification.alertBody = NSString *threadName = [NSString stringWithFormat:@"\"%@\"", groupName];
[NSString stringWithFormat:NSLocalizedString(@"APN_MESSAGE_IN_GROUP_DETAILED", nil), // TODO: Format parameters might change order in l10n. We should use named parameters.
senderName, notification.alertBody =
threadName, [NSString stringWithFormat:NSLocalizedString(@"APN_MESSAGE_IN_GROUP_DETAILED", nil),
messageDescription]; senderName,
} else { threadName,
notification.alertBody = [NSString stringWithFormat:@"%@: %@", senderName, messageDescription]; messageDescription];
} else {
notification.alertBody = [NSString stringWithFormat:@"%@: %@", senderName, messageDescription];
}
break;
} }
break; case NotificationNameNoPreview: {
} notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
case NotificationNameNoPreview: { if ([thread isGroupThread]) {
notification.userInfo = @{Signal_Thread_UserInfo_Key : thread.uniqueId}; notification.alertBody = [NSString
if ([thread isGroupThread]) { stringWithFormat:@"%@ \"%@\"", NSLocalizedString(@"APN_MESSAGE_IN_GROUP", nil), groupName];
notification.alertBody = [NSString } else {
stringWithFormat:@"%@ \"%@\"", NSLocalizedString(@"APN_MESSAGE_IN_GROUP", nil), groupName]; notification.alertBody = [NSString
} else { stringWithFormat:@"%@ %@", NSLocalizedString(@"APN_MESSAGE_FROM", nil), senderName];
notification.alertBody = }
[NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"APN_MESSAGE_FROM", nil), senderName]; break;
} }
break; case NotificationNoNameNoPreview:
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
break;
default:
DDLogWarn(@"unknown notification preview type: %lu", (unsigned long)self.notificationPreviewType);
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
break;
} }
case NotificationNoNameNoPreview:
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
break;
default:
DDLogWarn(@"unknown notification preview type: %lu", (unsigned long)self.notificationPreviewType);
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
break;
}
[[PushManager sharedManager] presentNotification:notification checkForCancel:YES]; [[PushManager sharedManager] presentNotification:notification checkForCancel:YES];
} else { } else {
if (shouldPlaySound && [Environment.preferences soundInForeground]) { if (shouldPlaySound && [Environment.preferences soundInForeground]) {
AudioServicesPlayAlertSound(_newMessageSound); AudioServicesPlayAlertSound(_newMessageSound);
}
} }
} });
} }
- (BOOL)shouldPlaySoundForNotification - (BOOL)shouldPlaySoundForNotification

@ -183,7 +183,7 @@ NSString *const kOWSBlockingManager_SyncedBlockedPhoneNumbersKey = @"kOWSBlockin
forKey:kOWSBlockingManager_BlockedPhoneNumbersKey forKey:kOWSBlockingManager_BlockedPhoneNumbersKey
inCollection:kOWSBlockingManager_BlockedPhoneNumbersCollection]; inCollection:kOWSBlockingManager_BlockedPhoneNumbersCollection];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (sendSyncMessage) { if (sendSyncMessage) {
[self sendBlockedPhoneNumbersMessage:blockedPhoneNumbers]; [self sendBlockedPhoneNumbersMessage:blockedPhoneNumbers];
} else { } else {
@ -238,9 +238,7 @@ NSString *const kOWSBlockingManager_SyncedBlockedPhoneNumbersKey = @"kOWSBlockin
NSSet *syncedBlockedPhoneNumberSet = [[NSSet alloc] initWithArray:(syncedBlockedPhoneNumbers ?: [NSArray new])]; NSSet *syncedBlockedPhoneNumberSet = [[NSSet alloc] initWithArray:(syncedBlockedPhoneNumbers ?: [NSArray new])];
if (![_blockedPhoneNumberSet isEqualToSet:syncedBlockedPhoneNumberSet]) { if (![_blockedPhoneNumberSet isEqualToSet:syncedBlockedPhoneNumberSet]) {
DDLogInfo(@"%@ retrying sync of blocked phone numbers", self.tag); DDLogInfo(@"%@ retrying sync of blocked phone numbers", self.tag);
dispatch_async(dispatch_get_main_queue(), ^{ [self sendBlockedPhoneNumbersMessage:self.blockedPhoneNumbers];
[self sendBlockedPhoneNumbersMessage:self.blockedPhoneNumbers];
});
} }
} }

@ -495,11 +495,9 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
[messages addObject:message]; [messages addObject:message];
} }
if (messages.count > 0) { if (messages.count > 0) {
dispatch_async(dispatch_get_main_queue(), ^{ for (OWSVerificationStateSyncMessage *message in messages) {
for (OWSVerificationStateSyncMessage *message in messages) { [self sendSyncVerificationStateMessage:message];
[self sendSyncVerificationStateMessage:message]; }
}
});
} }
} }
}); });
@ -509,7 +507,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
{ {
OWSAssert(message); OWSAssert(message);
OWSAssert(message.verificationForRecipientId.length > 0); OWSAssert(message.verificationForRecipientId.length > 0);
OWSAssert([NSThread isMainThread]);
TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:message.verificationForRecipientId]; TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:message.verificationForRecipientId];
@ -519,20 +516,17 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
verificationStateSyncMessage:message]; verificationStateSyncMessage:message];
[self.messageSender sendMessage:nullMessage [self.messageSender sendMessage:nullMessage
success:^{ success:^{
dispatch_async(dispatch_get_main_queue(), ^{ DDLogInfo(@"%@ Successfully sent verification state NullMessage", self.tag);
DDLogInfo(@"%@ Successfully sent verification state NullMessage", self.tag); [self.messageSender sendMessage:message
[self.messageSender sendMessage:message success:^{
success:^{ DDLogInfo(@"%@ Successfully sent verification state sync message", self.tag);
DDLogInfo(@"%@ Successfully sent verification state sync message", self.tag);
// Record that this verification state was successfully synced.
// Record that this verification state was successfully synced. [self clearSyncMessageForRecipientId:message.verificationForRecipientId];
[self clearSyncMessageForRecipientId:message.verificationForRecipientId]; }
} failure:^(NSError *error) {
failure:^(NSError *error) { DDLogError(@"%@ Failed to send verification state sync message with error: %@", self.tag, error);
DDLogError( }];
@"%@ Failed to send verification state sync message with error: %@", self.tag, error);
}];
});
} }
failure:^(NSError *_Nonnull error) { failure:^(NSError *_Nonnull error) {
DDLogError(@"%@ Failed to send verification state NullMessage with error: %@", self.tag, error); DDLogError(@"%@ Failed to send verification state NullMessage with error: %@", self.tag, error);
@ -575,26 +569,28 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa
} }
NSData *identityKey = [rawIdentityKey removeKeyType]; NSData *identityKey = [rawIdentityKey removeKeyType];
switch (verified.state) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
case OWSSignalServiceProtosVerifiedStateDefault: switch (verified.state) {
[self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateDefault case OWSSignalServiceProtosVerifiedStateDefault:
recipientId:recipientId [self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateDefault
identityKey:identityKey recipientId:recipientId
overwriteOnConflict:NO]; identityKey:identityKey
break; overwriteOnConflict:NO];
case OWSSignalServiceProtosVerifiedStateVerified: break;
[self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateVerified case OWSSignalServiceProtosVerifiedStateVerified:
recipientId:recipientId [self tryToApplyVerificationStateFromSyncMessage:OWSVerificationStateVerified
identityKey:identityKey recipientId:recipientId
overwriteOnConflict:YES]; identityKey:identityKey
break; overwriteOnConflict:YES];
case OWSSignalServiceProtosVerifiedStateUnverified: break;
OWSFail(@"Verification state sync message for recipientId: %@ has unexpected value: %@.", case OWSSignalServiceProtosVerifiedStateUnverified:
recipientId, OWSFail(@"Verification state sync message for recipientId: %@ has unexpected value: %@.",
OWSVerificationStateToString(OWSVerificationStateNoLongerVerified)); recipientId,
return; OWSVerificationStateToString(OWSVerificationStateNoLongerVerified));
} return;
[self fireIdentityStateChangeNotification]; }
[self fireIdentityStateChangeNotification];
});
} }
- (void)tryToApplyVerificationStateFromSyncMessage:(OWSVerificationState)verificationState - (void)tryToApplyVerificationStateFromSyncMessage:(OWSVerificationState)verificationState

@ -305,10 +305,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope - (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
{ {
TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source]; TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source];
dispatch_async(dispatch_get_main_queue(), ^{ [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage inThread:contactThread];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage
inThread:contactThread];
});
} }
#pragma mark - Logging #pragma mark - Logging

@ -604,9 +604,8 @@ NS_ASSUME_NONNULL_BEGIN
DDLogWarn(@"%@ ignoring unsupported sync request message", self.tag); DDLogWarn(@"%@ ignoring unsupported sync request message", self.tag);
} }
} else if (syncMessage.hasBlocked) { } else if (syncMessage.hasBlocked) {
// TODO: Do this synchronously. NSArray<NSString *> *blockedPhoneNumbers = [syncMessage.blocked.numbers copy];
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray<NSString *> *blockedPhoneNumbers = [syncMessage.blocked.numbers copy];
[_blockingManager setBlockedPhoneNumbers:blockedPhoneNumbers sendSyncMessage:NO]; [_blockingManager setBlockedPhoneNumbers:blockedPhoneNumbers sendSyncMessage:NO];
}); });
} else if (syncMessage.read.count > 0) { } else if (syncMessage.read.count > 0) {
@ -616,10 +615,7 @@ NS_ASSUME_NONNULL_BEGIN
transaction:transaction]; transaction:transaction];
} else if (syncMessage.hasVerified) { } else if (syncMessage.hasVerified) {
DDLogInfo(@"%@ Received verification state for %@", self.tag, syncMessage.verified.destination); DDLogInfo(@"%@ Received verification state for %@", self.tag, syncMessage.verified.destination);
// TODO: Do this synchronously. [self.identityManager processIncomingSyncMessage:syncMessage.verified];
dispatch_async(dispatch_get_main_queue(), ^{
[self.identityManager processIncomingSyncMessage:syncMessage.verified];
});
} else { } else {
DDLogWarn(@"%@ Ignoring unsupported sync message.", self.tag); DDLogWarn(@"%@ Ignoring unsupported sync message.", self.tag);
} }
@ -944,18 +940,15 @@ NS_ASSUME_NONNULL_BEGIN
[OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage [OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
transaction:transaction]; transaction:transaction];
// TODO: Do this synchronously. [OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage
dispatch_async(dispatch_get_main_queue(), ^{ contactsManager:self.contactsManager];
[OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage
contactsManager:self.contactsManager];
// Update thread preview in inbox // Update thread preview in inbox
[thread touch]; [thread touchWithTransaction:transaction];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage
inThread:thread inThread:thread
contactsManager:self.contactsManager]; contactsManager:self.contactsManager];
});
} }
return incomingMessage; return incomingMessage;

@ -178,9 +178,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
object:nil]; object:nil];
// Try to start processing. // Try to start processing.
dispatch_async(dispatch_get_main_queue(), ^{ [self scheduleProcessing];
[self scheduleProcessing];
});
return self; return self;
} }
@ -198,7 +196,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
// Schedules a processing pass, unless one is already scheduled. // Schedules a processing pass, unless one is already scheduled.
- (void)scheduleProcessing - (void)scheduleProcessing
{ {
DispatchMainThreadSafe(^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@synchronized(self) @synchronized(self)
{ {
if ([TSDatabaseView hasPendingViewRegistrations]) { if ([TSDatabaseView hasPendingViewRegistrations]) {
@ -241,17 +239,15 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
OWSReadReceiptsForLinkedDevicesMessage *message = OWSReadReceiptsForLinkedDevicesMessage *message =
[[OWSReadReceiptsForLinkedDevicesMessage alloc] initWithReadReceipts:readReceiptsForLinkedDevices]; [[OWSReadReceiptsForLinkedDevicesMessage alloc] initWithReadReceipts:readReceiptsForLinkedDevices];
dispatch_async(dispatch_get_main_queue(), ^{ [self.messageSender sendMessage:message
[self.messageSender sendMessage:message success:^{
success:^{ DDLogInfo(@"%@ Successfully sent %zd read receipt to linked devices.",
DDLogInfo(@"%@ Successfully sent %zd read receipt to linked devices.", self.tag,
self.tag, readReceiptsForLinkedDevices.count);
readReceiptsForLinkedDevices.count); }
} failure:^(NSError *error) {
failure:^(NSError *error) { DDLogError(@"%@ Failed to send read receipt to linked devices with error: %@", self.tag, error);
DDLogError(@"%@ Failed to send read receipt to linked devices with error: %@", self.tag, error); }];
}];
});
} }
NSArray<OWSReadReceipt *> *readReceiptsToSend = [self.toLinkedDevicesReadReceiptMap allValues]; NSArray<OWSReadReceipt *> *readReceiptsToSend = [self.toLinkedDevicesReadReceiptMap allValues];
@ -266,17 +262,14 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
[[OWSReadReceiptsForSenderMessage alloc] initWithThread:thread [[OWSReadReceiptsForSenderMessage alloc] initWithThread:thread
messageTimestamps:timestamps.allObjects]; messageTimestamps:timestamps.allObjects];
dispatch_async(dispatch_get_main_queue(), ^{ [self.messageSender sendMessage:message
[self.messageSender sendMessage:message success:^{
success:^{ DDLogInfo(
DDLogInfo(@"%@ Successfully sent %zd read receipts to sender.", @"%@ Successfully sent %zd read receipts to sender.", self.tag, readReceiptsToSend.count);
self.tag, }
readReceiptsToSend.count); failure:^(NSError *error) {
} DDLogError(@"%@ Failed to send read receipts to sender with error: %@", self.tag, error);
failure:^(NSError *error) { }];
DDLogError(@"%@ Failed to send read receipts to sender with error: %@", self.tag, error);
}];
});
} }
[self.toSenderReadReceiptMap removeAllObjects]; [self.toSenderReadReceiptMap removeAllObjects];
} }

Loading…
Cancel
Save