Use AppContext to resolve share extension FIXMEs.

This commit is contained in:
Matthew Chen 2017-11-29 15:27:19 -05:00
parent 94436f7b04
commit e712e8bfc4
13 changed files with 82 additions and 49 deletions

View File

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

View File

@ -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, ^{

View File

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

View File

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

View File

@ -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 <Curve25519Kit/Curve25519.h>
#import <AxolotlKit/NSData+keyVersionByte.h>
#import <Curve25519Kit/Curve25519.h>
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)

View File

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

View File

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

View File

@ -3,6 +3,7 @@
//
#import "OWSMessageSender.h"
#import "AppContext.h"
#import "ContactsUpdater.h"
#import "NSData+keyVersionByte.h"
#import "NSData+messagePadding.h"

View File

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

View File

@ -3,6 +3,7 @@
//
#import "TSStorageManager.h"
#import "AppContext.h"
#import "NSData+Base64.h"
#import "OWSAnalytics.h"
#import "OWSBatchMessageProcessor.h"

View File

@ -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<AppContext> CurrentAppContext();
id<AppContext> CurrentAppContext(void);
void SetCurrentAppContext(id<AppContext> appContext);
NS_ASSUME_NONNULL_END

View File

@ -8,7 +8,7 @@ NS_ASSUME_NONNULL_BEGIN
static id<AppContext> currentAppContext = nil;
id<AppContext> CurrentAppContext()
id<AppContext> CurrentAppContext(void)
{
OWSCAssert(currentAppContext);

View File

@ -3,6 +3,7 @@
//
#import "OWSAnalytics.h"
#import "AppContext.h"
#import "OWSQueues.h"
#import "TSStorageManager.h"
#import <CocoaLumberjack/CocoaLumberjack.h>
@ -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];
});
}