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;
}
- (UIApplicationState)mainApplicationState
{
return [UIApplication sharedApplication].applicationState;
}
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:
(BackgroundTaskExpirationHandler)expirationHandler
{

@ -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];

@ -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];

@ -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.

Loading…
Cancel
Save