diff --git a/Signal/src/util/MainAppContext.m b/Signal/src/util/MainAppContext.m index 5801bfcf4..89e9a685c 100644 --- a/Signal/src/util/MainAppContext.m +++ b/Signal/src/util/MainAppContext.m @@ -18,6 +18,11 @@ NS_ASSUME_NONNULL_BEGIN return [UIApplication sharedApplication].applicationState == UIApplicationStateActive; } +- (UIApplicationState)mainApplicationState +{ + return [UIApplication sharedApplication].applicationState; +} + - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: (BackgroundTaskExpirationHandler)expirationHandler { diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 38b4d90f7..06c74c3a3 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -226,17 +226,15 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4; AssertIsOnMainThread(); OWSAssert(self.backgroundTaskIdentifier == UIBackgroundTaskInvalid); - // FIXME SHARINGEXTENSION - // self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ - // DDLogWarn(@"%@ Timed out while in background trying to send message: %@", self.logTag, self.message); - // [self endBackgroundTask]; - // }]; + self.backgroundTaskIdentifier = [CurrentAppContext() beginBackgroundTaskWithExpirationHandler:^{ + DDLogWarn(@"%@ Timed out while in background trying to send message: %@", self.logTag, self.message); + [self endBackgroundTask]; + }]; } - (void)endBackgroundTask { - // FIXME SHARINGEXTENSION - // [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier]; + [CurrentAppContext() endBackgroundTask:self.backgroundTaskIdentifier]; } - (void)setBackgroundTaskIdentifier:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier @@ -267,8 +265,7 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4; // Should call `startBackgroundTask` before enqueuing the operation // to ensure we don't get suspended before the operation completes. - // FIXME SHARINGEXTENSION - // OWSAssert(self.backgroundTaskIdentifier != UIBackgroundTaskInvalid); + OWSAssert(!CurrentAppContext().isMainApp || self.backgroundTaskIdentifier != UIBackgroundTaskInvalid); [self willChangeValueForKey:OWSSendMessageOperationKeyIsExecuting]; self.operationState = OWSSendMessageOperationStateExecuting; @@ -460,8 +457,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; // We call `startBackgroundTask` here to prevent our app from suspending while being backgrounded // until the operation is completed - at which point the OWSSendMessageOperation ends it's background task. - // FIXME SHARINGEXTENSION - // [sendMessageOperation startBackgroundTask]; + [sendMessageOperation startBackgroundTask]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSOperationQueue *sendingQueue = [self sendingQueueForMessage:message]; diff --git a/SignalServiceKit/src/Storage/TSStorageManager.m b/SignalServiceKit/src/Storage/TSStorageManager.m index e5163bb9d..79e37e2c3 100644 --- a/SignalServiceKit/src/Storage/TSStorageManager.m +++ b/SignalServiceKit/src/Storage/TSStorageManager.m @@ -492,8 +492,8 @@ void setDatabaseInitialized() - (void)backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:(NSString *)errorDescription { - // FIXME SHARINGEXTENSION - // OWSAssert([UIApplication sharedApplication].applicationState == UIApplicationStateBackground); + OWSAssert( + CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground); // Sleep to give analytics events time to be delivered. [NSThread sleepForTimeInterval:5.0f]; @@ -514,20 +514,26 @@ void setDatabaseInitialized() [SAMKeychain passwordForService:keychainService account:keychainDBPassAccount error:&keyFetchError]; if (keyFetchError) { - // FIXME SHARINGEXTENSION - // UIApplicationState applicationState = [UIApplication sharedApplication].applicationState; - // NSString *errorDescription = [NSString stringWithFormat:@"Database password inaccessible. No unlock - // since device restart? Error: %@ ApplicationState: %d", keyFetchError, (int)applicationState]; - // DDLogError(@"%@ %@", self.logTag, errorDescription); - // [DDLog flushLog]; - - // FIXME SHARINGEXTENSION - // if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { - // // TODO: Rather than crash here, we should detect the situation earlier - // // and exit gracefully - (in the app delegate?). See the ` - // // This is a last ditch effort to avoid blowing away the user's database. - // [self backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:errorDescription]; - // } + NSString *errorDescription = + [NSString stringWithFormat:@"Database password inaccessible. No unlock since device restart? Error: %@", + keyFetchError]; + if (CurrentAppContext().isMainApp) { + UIApplicationState applicationState = CurrentAppContext().mainApplicationState; + errorDescription = + [errorDescription stringByAppendingFormat:@", ApplicationState: %d", (int)applicationState]; + } + DDLogError(@"%@ %@", self.logTag, errorDescription); + [DDLog flushLog]; + + if (CurrentAppContext().isMainApp) { + UIApplicationState applicationState = CurrentAppContext().mainApplicationState; + if (applicationState == UIApplicationStateBackground) { + // TODO: Rather than crash here, we should detect the situation earlier + // and exit gracefully - (in the app delegate?). See the ` + // This is a last ditch effort to avoid blowing away the user's database. + [self backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:errorDescription]; + } + } // At this point, either this is a new install so there's no existing password to retrieve // or the keychain has become corrupt. Either way, we want to get back to a @@ -541,9 +547,9 @@ void setDatabaseInitialized() // Try to reset app by deleting database. // FIXME SHARINGEXTENSION OWSFail(@"disabled while Share extension is WIP"); - // [self resetSignalStorage]; + [self resetSignalStorage]; - // dbPassword = [self createAndSetNewDatabasePassword]; + dbPassword = [self createAndSetNewDatabasePassword]; } return [dbPassword dataUsingEncoding:NSUTF8StringEncoding]; diff --git a/SignalServiceKit/src/Util/AppContext.h b/SignalServiceKit/src/Util/AppContext.h index 825de3c24..cb4d179ba 100755 --- a/SignalServiceKit/src/Util/AppContext.h +++ b/SignalServiceKit/src/Util/AppContext.h @@ -11,9 +11,16 @@ typedef void (^BackgroundTaskExpirationHandler)(void); - (BOOL)isMainApp; - (BOOL)isMainAppAndActive; -// Is a NOOP if isMainApp is NO. +// Should only be called if isMainApp is YES. +// +// In general, isMainAppAndActive will probably yield more readable code. +- (UIApplicationState)mainApplicationState; + +// Should start a background task if isMainApp is YES. +// Should just return UIBackgroundTaskInvalid if isMainApp is NO. - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: (BackgroundTaskExpirationHandler)expirationHandler; +// Should be a NOOP if isMainApp is NO. - (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier; // Should only be called if isMainApp is YES.