Use AppContext to resolve share extension FIXMEs.

pull/1/head
Matthew Chen 8 years ago
parent e712e8bfc4
commit d17ccadea2

@ -18,6 +18,11 @@ NS_ASSUME_NONNULL_BEGIN
return [UIApplication sharedApplication].applicationState == UIApplicationStateActive; return [UIApplication sharedApplication].applicationState == UIApplicationStateActive;
} }
- (UIApplicationState)mainApplicationState
{
return [UIApplication sharedApplication].applicationState;
}
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler: - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler (BackgroundTaskExpirationHandler)expirationHandler
{ {

@ -226,17 +226,15 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4;
AssertIsOnMainThread(); AssertIsOnMainThread();
OWSAssert(self.backgroundTaskIdentifier == UIBackgroundTaskInvalid); OWSAssert(self.backgroundTaskIdentifier == UIBackgroundTaskInvalid);
// FIXME SHARINGEXTENSION self.backgroundTaskIdentifier = [CurrentAppContext() beginBackgroundTaskWithExpirationHandler:^{
// self.backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ DDLogWarn(@"%@ Timed out while in background trying to send message: %@", self.logTag, self.message);
// DDLogWarn(@"%@ Timed out while in background trying to send message: %@", self.logTag, self.message); [self endBackgroundTask];
// [self endBackgroundTask]; }];
// }];
} }
- (void)endBackgroundTask - (void)endBackgroundTask
{ {
// FIXME SHARINGEXTENSION [CurrentAppContext() endBackgroundTask:self.backgroundTaskIdentifier];
// [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskIdentifier];
} }
- (void)setBackgroundTaskIdentifier:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier - (void)setBackgroundTaskIdentifier:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier
@ -267,8 +265,7 @@ NSUInteger const OWSSendMessageOperationMaxRetries = 4;
// Should call `startBackgroundTask` before enqueuing the operation // Should call `startBackgroundTask` before enqueuing the operation
// to ensure we don't get suspended before the operation completes. // to ensure we don't get suspended before the operation completes.
// FIXME SHARINGEXTENSION OWSAssert(!CurrentAppContext().isMainApp || self.backgroundTaskIdentifier != UIBackgroundTaskInvalid);
// OWSAssert(self.backgroundTaskIdentifier != UIBackgroundTaskInvalid);
[self willChangeValueForKey:OWSSendMessageOperationKeyIsExecuting]; [self willChangeValueForKey:OWSSendMessageOperationKeyIsExecuting];
self.operationState = OWSSendMessageOperationStateExecuting; self.operationState = OWSSendMessageOperationStateExecuting;
@ -460,8 +457,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
// We call `startBackgroundTask` here to prevent our app from suspending while being backgrounded // 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. // 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), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSOperationQueue *sendingQueue = [self sendingQueueForMessage:message]; NSOperationQueue *sendingQueue = [self sendingQueueForMessage:message];

@ -492,8 +492,8 @@ void setDatabaseInitialized()
- (void)backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:(NSString *)errorDescription - (void)backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:(NSString *)errorDescription
{ {
// FIXME SHARINGEXTENSION OWSAssert(
// OWSAssert([UIApplication sharedApplication].applicationState == UIApplicationStateBackground); CurrentAppContext().isMainApp && CurrentAppContext().mainApplicationState == UIApplicationStateBackground);
// Sleep to give analytics events time to be delivered. // Sleep to give analytics events time to be delivered.
[NSThread sleepForTimeInterval:5.0f]; [NSThread sleepForTimeInterval:5.0f];
@ -514,20 +514,26 @@ void setDatabaseInitialized()
[SAMKeychain passwordForService:keychainService account:keychainDBPassAccount error:&keyFetchError]; [SAMKeychain passwordForService:keychainService account:keychainDBPassAccount error:&keyFetchError];
if (keyFetchError) { if (keyFetchError) {
// FIXME SHARINGEXTENSION NSString *errorDescription =
// UIApplicationState applicationState = [UIApplication sharedApplication].applicationState; [NSString stringWithFormat:@"Database password inaccessible. No unlock since device restart? Error: %@",
// NSString *errorDescription = [NSString stringWithFormat:@"Database password inaccessible. No unlock keyFetchError];
// since device restart? Error: %@ ApplicationState: %d", keyFetchError, (int)applicationState]; if (CurrentAppContext().isMainApp) {
// DDLogError(@"%@ %@", self.logTag, errorDescription); UIApplicationState applicationState = CurrentAppContext().mainApplicationState;
// [DDLog flushLog]; errorDescription =
[errorDescription stringByAppendingFormat:@", ApplicationState: %d", (int)applicationState];
// FIXME SHARINGEXTENSION }
// if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) { DDLogError(@"%@ %@", self.logTag, errorDescription);
// // TODO: Rather than crash here, we should detect the situation earlier [DDLog flushLog];
// // and exit gracefully - (in the app delegate?). See the `
// // This is a last ditch effort to avoid blowing away the user's database. if (CurrentAppContext().isMainApp) {
// [self backgroundedAppDatabasePasswordInaccessibleWithErrorDescription:errorDescription]; 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 // 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 // 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. // Try to reset app by deleting database.
// FIXME SHARINGEXTENSION // FIXME SHARINGEXTENSION
OWSFail(@"disabled while Share extension is WIP"); OWSFail(@"disabled while Share extension is WIP");
// [self resetSignalStorage]; [self resetSignalStorage];
// dbPassword = [self createAndSetNewDatabasePassword]; dbPassword = [self createAndSetNewDatabasePassword];
} }
return [dbPassword dataUsingEncoding:NSUTF8StringEncoding]; return [dbPassword dataUsingEncoding:NSUTF8StringEncoding];

@ -11,9 +11,16 @@ typedef void (^BackgroundTaskExpirationHandler)(void);
- (BOOL)isMainApp; - (BOOL)isMainApp;
- (BOOL)isMainAppAndActive; - (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: - (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler; (BackgroundTaskExpirationHandler)expirationHandler;
// Should be a NOOP if isMainApp is NO.
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier; - (void)endBackgroundTask:(UIBackgroundTaskIdentifier)backgroundTaskIdentifier;
// Should only be called if isMainApp is YES. // Should only be called if isMainApp is YES.

Loading…
Cancel
Save