diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index ca2e96d39..5801bfcf4 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -24,6 +24,16 @@ NS_ASSUME_NONNULL_BEGIN return [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler]; } +- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier +{ + [UIApplication.sharedApplication endBackgroundTask:backgroundTaskIdentifier]; +} + +- (void)setMainAppBadgeNumber:(NSInteger)value +{ + [[UIApplication sharedApplication] setApplicationIconBadgeNumber:value]; +} + @end NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Account/TSPreKeyManager.m b/SignalServiceKit/src/Account/TSPreKeyManager.m index 3b1c1d5bd..b9ae9477d 100644 --- a/SignalServiceKit/src/Account/TSPreKeyManager.m +++ b/SignalServiceKit/src/Account/TSPreKeyManager.m @@ -3,6 +3,7 @@ // #import "TSPreKeyManager.h" +#import "AppContext.h" #import "NSDate+OWS.h" #import "NSURLSessionDataTask+StatusCode.h" #import "OWSIdentityManager.h" @@ -90,8 +91,9 @@ static const NSTimeInterval kSignedPreKeyUpdateFailureMaxFailureDuration = 10 * + (void)checkPreKeysIfNecessary { - // FIXME SHARINGEXTENSION - // OWSAssert([UIApplication sharedApplication].applicationState == UIApplicationStateActive); + if (CurrentAppContext().isMainApp) { + OWSAssert(CurrentAppContext().isMainAppAndActive); + } // Update the prekey check timestamp. dispatch_async(TSPreKeyManager.prekeyQueue, ^{ diff --git a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m index f894f83d1..0de7221e4 100644 --- a/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m +++ b/SignalServiceKit/src/Messages/OWSBatchMessageProcessor.m @@ -3,6 +3,7 @@ // #import "OWSBatchMessageProcessor.h" +#import "AppContext.h" #import "NSArray+OWS.h" #import "OWSMessageManager.h" #import "OWSQueues.h" @@ -297,6 +298,11 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo - (void)drainQueue { + // Don't process incoming messages in app extensions. + if (!CurrentAppContext().isMainApp) { + return; + } + dispatch_async(self.serialQueue, ^{ if ([TSDatabaseView hasPendingViewRegistrations]) { // We don't want to process incoming messages until database diff --git a/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m b/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m index 3c19cfdee..d9c5cd15d 100644 --- a/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m +++ b/SignalServiceKit/src/Messages/OWSDisappearingMessagesJob.m @@ -3,6 +3,7 @@ // #import "OWSDisappearingMessagesJob.h" +#import "AppContext.h" #import "ContactsManagerProtocol.h" #import "NSDate+OWS.h" #import "NSTimer+OWS.h" @@ -320,12 +321,10 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(date); dispatch_async(dispatch_get_main_queue(), ^{ - - // FIXME SHARINGEXTENSION - // if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { - // // Don't schedule run when inactive. - // return; - // } + if (!CurrentAppContext().isMainAppAndActive) { + // Don't schedule run when inactive or not in main app. + return; + } NSDateFormatter *dateFormatter = [NSDateFormatter new]; dateFormatter.dateStyle = NSDateFormatterNoStyle; @@ -366,12 +365,10 @@ NS_ASSUME_NONNULL_BEGIN { OWSAssert([NSThread isMainThread]); - // FIXME SHARINGEXTENSION - // if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { - // // Don't run when inactive. - // OWSFail(@"%@ Disappearing messages job timer fired while app inactive.", self.logTag); - // return; - // } + if (!CurrentAppContext().isMainAppAndActive) { + // Don't schedule run when inactive or not in main app. + return; + } [self resetTimer]; diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index 0ce50d4c0..5478a930b 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -3,6 +3,7 @@ // #import "OWSIdentityManager.h" +#import "AppContext.h" #import "NSDate+OWS.h" #import "NSNotificationCenter+OWS.h" #import "NotificationsProtocol.h" @@ -19,8 +20,8 @@ #import "TSStorageManager+sessionStore.h" #import "TSStorageManager.h" #import "TextSecureKitEnv.h" -#import #import +#import NS_ASSUME_NONNULL_BEGIN @@ -443,13 +444,12 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa { OWSAssert([NSThread isMainThread]); - // FIXME SHARINGEXTENSION - // if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) { - // // Only try to sync if the app is active to avoid interfering with startup. - // // - // // applicationDidBecomeActive: will try to sync again when the app becomes active. - // return; - // } + if (!CurrentAppContext().isMainAppAndActive) { + // Only try to sync if the main app is active to avoid interfering with startup. + // + // applicationDidBecomeActive: will try to sync again when the main app becomes active. + return; + } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @synchronized(self) diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 389c4b52e..b812e5f00 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -3,6 +3,7 @@ // #import "OWSMessageManager.h" +#import "AppContext.h" #import "ContactsManagerProtocol.h" #import "Cryptography.h" #import "MimeTypeUtil.h" @@ -149,6 +150,7 @@ NS_ASSUME_NONNULL_BEGIN OWSAssert(envelope); OWSAssert(transaction); OWSAssert([TSAccountManager isRegistered]); + OWSAssert(CurrentAppContext().isMainApp); DDLogInfo(@"%@ handling decrypted envelope: %@", self.logTag, [self descriptionForEnvelope:envelope]); @@ -1136,9 +1138,12 @@ NS_ASSUME_NONNULL_BEGIN - (void)updateApplicationBadgeCount { + if (!CurrentAppContext().isMainApp) { + return; + } + NSUInteger numberOfItems = [self unreadMessagesCount]; - // FIXME SHARINGEXTENSION can't use UIApplication.sharedAplication - // [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfItems]; + [CurrentAppContext() setMainAppBadgeNumber:numberOfItems]; } - (NSUInteger)unreadMessagesInThread:(TSThread *)thread diff --git a/SignalServiceKit/src/Messages/OWSMessageReceiver.m b/SignalServiceKit/src/Messages/OWSMessageReceiver.m index 6831f7da3..6a0f6c58c 100644 --- a/SignalServiceKit/src/Messages/OWSMessageReceiver.m +++ b/SignalServiceKit/src/Messages/OWSMessageReceiver.m @@ -3,6 +3,7 @@ // #import "OWSMessageReceiver.h" +#import "AppContext.h" #import "NSArray+OWS.h" #import "OWSBatchMessageProcessor.h" #import "OWSMessageDecrypter.h" @@ -274,6 +275,11 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin - (void)drainQueue { + // Don't decrypt messages in app extensions. + if (!CurrentAppContext().isMainApp) { + return; + } + dispatch_async(self.serialQueue, ^{ if ([TSDatabaseView hasPendingViewRegistrations]) { // We don't want to process incoming messages until database diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 5e74490e7..38b4d90f7 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -3,6 +3,7 @@ // #import "OWSMessageSender.h" +#import "AppContext.h" #import "ContactsUpdater.h" #import "NSData+keyVersionByte.h" #import "NSData+messagePadding.h" diff --git a/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m b/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m index e90767344..4e3eb2fb3 100644 --- a/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m +++ b/SignalServiceKit/src/Network/WebSockets/TSSocketManager.m @@ -3,6 +3,7 @@ // #import "TSSocketManager.h" +#import "AppContext.h" #import "Cryptography.h" #import "NSNotificationCenter+OWS.h" #import "NSTimer+OWS.h" @@ -120,9 +121,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_ } self.hasObservedNotifications = YES; - // // FIXME SHARINGEXTENSION - // self.appIsActive = [UIApplication sharedApplication].applicationState == UIApplicationStateActive; - self.appIsActive = YES; + self.appIsActive = CurrentAppContext().isMainAppAndActive; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) @@ -495,6 +494,11 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_ { OWSAssert([NSThread isMainThread]); + // Don't open socket in app extensions. + if (!CurrentAppContext().isMainApp) { + return NO; + } + if (![TSAccountManager isRegistered]) { return NO; } @@ -547,16 +551,14 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_ // Additionally, we want the reconnect timer to work in the background too. [[NSRunLoop mainRunLoop] addTimer:self.backgroundKeepAliveTimer forMode:NSDefaultRunLoopMode]; - // FIXME SHARINGEXTENSION - // self.fetchingTaskIdentifier = - // [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ - // OWSAssert([NSThread isMainThread]); - // - // DDLogInfo(@"%s background task expired", __PRETTY_FUNCTION__); - // - // [self clearBackgroundState]; - // [self applyDesiredSocketState]; - // }]; + self.fetchingTaskIdentifier = [CurrentAppContext() beginBackgroundTaskWithExpirationHandler:^{ + OWSAssert([NSThread isMainThread]); + + DDLogInfo(@"%s background task expired", __PRETTY_FUNCTION__); + + [self clearBackgroundState]; + [self applyDesiredSocketState]; + }]; } else { OWSAssert(self.backgroundKeepAliveUntilDate); OWSAssert(self.backgroundKeepAliveTimer); @@ -623,8 +625,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_ self.backgroundKeepAliveTimer = nil; if (self.fetchingTaskIdentifier != UIBackgroundTaskInvalid) { - // FIXME SHARINGEXTENSION - // [[UIApplication sharedApplication] endBackgroundTask:self.fetchingTaskIdentifier]; + [CurrentAppContext() endBackgroundTask:self.fetchingTaskIdentifier]; self.fetchingTaskIdentifier = UIBackgroundTaskInvalid; } } diff --git a/SignalServiceKit/src/Storage/TSStorageManager.m b/SignalServiceKit/src/Storage/TSStorageManager.m index 5a2dd722c..e5163bb9d 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager.m +++ b/SignalServiceKit/src/Storage/TSStorageManager.m @@ -3,6 +3,7 @@ // #import "TSStorageManager.h" +#import "AppContext.h" #import "NSData+Base64.h" #import "OWSAnalytics.h" #import "OWSBatchMessageProcessor.h" diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h index 70b4f3ed0..825de3c24 100755 --- a/SignalServiceKit/src/Util/AppContext.h +++ b/SignalServiceKit/src/Util/AppContext.h @@ -11,12 +11,17 @@ typedef void (^BackgroundTaskExpirationHandler)(void); - (BOOL)isMainApp; - (BOOL)isMainAppAndActive; +// Is a NOOP if isMainApp is NO. - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: (BackgroundTaskExpirationHandler)expirationHandler; +- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier; + +// Should only be called if isMainApp is YES. +- (void)setMainAppBadgeNumber:(NSInteger)value; @end -id CurrentAppContext(); +id CurrentAppContext(void); void SetCurrentAppContext(id appContext); NS_ASSUME_NONNULL_END diff --git a/SignalServiceKit/src/Util/AppContext.m b/SignalServiceKit/src/Util/AppContext.m index 0216d1c7f..c9b32ff39 100755 --- a/SignalServiceKit/src/Util/AppContext.m +++ b/SignalServiceKit/src/Util/AppContext.m @@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN static id currentAppContext = nil; -id CurrentAppContext() +id CurrentAppContext(void) { OWSCAssert(currentAppContext); diff --git a/SignalServiceKit/src/Util/OWSAnalytics.m b/SignalServiceKit/src/Util/OWSAnalytics.m index f435628fc..8e8662474 100755 --- a/SignalServiceKit/src/Util/OWSAnalytics.m +++ b/SignalServiceKit/src/Util/OWSAnalytics.m @@ -3,6 +3,7 @@ // #import "OWSAnalytics.h" +#import "AppContext.h" #import "OWSQueues.h" #import "TSStorageManager.h" #import @@ -231,12 +232,11 @@ NSString *NSStringForOWSAnalyticsSeverity(OWSAnalyticsSeverity severity) DDLogDebug(@"%@ submitting: %@", self.logTag, eventKey); __block UIBackgroundTaskIdentifier task; - // FIXME SHARINGEXTENSION can't use UIApplication.sharedAplication - // task = [UIApplication.sharedApplication beginBackgroundTaskWithExpirationHandler:^{ - // failureBlock(); - // - // [UIApplication.sharedApplication endBackgroundTask:task]; - // }]; + task = [CurrentAppContext() beginBackgroundTaskWithExpirationHandler:^{ + failureBlock(); + + [CurrentAppContext() endBackgroundTask:task]; + }]; // Until we integrate with an analytics platform, behave as though all event delivery succeeds. dispatch_async(self.serialQueue, ^{ @@ -246,8 +246,7 @@ NSString *NSStringForOWSAnalyticsSeverity(OWSAnalyticsSeverity severity) } else { failureBlock(); } - // FIXME SHARINGEXTENSION can't use UIApplication.sharedAplication - // [UIApplication.sharedApplication endBackgroundTask:task]; + [CurrentAppContext() endBackgroundTask:task]; }); }