Try to keep-alive registration lock w/ bg fetch

If a user hasn't launched or received a message on Signal in a week,
they could inadvertently lose their registration lock.

// FREEBIE
This commit is contained in:
Michael Kirk 2018-03-05 15:13:19 -05:00
parent a1de99f1f1
commit 8dfc584c2c
2 changed files with 53 additions and 2 deletions

View File

@ -108,6 +108,7 @@
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>fetch</string>
<string>remote-notification</string>
<string>voip</string>
</array>

View File

@ -20,6 +20,7 @@
#import "SignalsNavigationController.h"
#import "ViewControllerUtils.h"
#import <AxolotlKit/SessionCipher.h>
#import <PromiseKit/AnyPromise.h>
#import <SignalMessaging/AppSetup.h>
#import <SignalMessaging/Environment.h>
#import <SignalMessaging/OWSContactsManager.h>
@ -206,6 +207,10 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
selector:@selector(registrationStateDidChange)
name:RegistrationStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(registrationLockDidChange:)
name:NSNotificationName_2FAStateDidChange
object:nil];
DDLogInfo(@"%@ application: didFinishLaunchingWithOptions completed.", self.logTag);
@ -594,6 +599,20 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
DDLogInfo(@"%@ applicationDidBecomeActive completed.", self.logTag);
}
- (void)enableBackgroundRefreshIfNecessary
{
[AppReadiness runNowOrWhenAppIsReady:^{
if (OWS2FAManager.sharedManager.is2FAEnabled && [TSAccountManager isRegistered]) {
// Ping server once a day to keep-alive 2FA clients.
const NSTimeInterval kBackgroundRefreshInterval = 24 * 60 * 60;
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:kBackgroundRefreshInterval];
} else {
[[UIApplication sharedApplication]
setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalNever];
}
}];
}
- (void)handleActivation
{
OWSAssertIsOnMainThread();
@ -619,6 +638,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
// and continue cleaning in the background.
[[OWSDisappearingMessagesJob sharedJob] startIfNecessary];
[self enableBackgroundRefreshIfNecessary];
// Mark all "attempting out" messages as "unsent", i.e. any messages that were not successfully
// sent before the app exited should be marked as failures.
[[[OWSFailedMessagesJob alloc] initWithPrimaryStorage:[OWSPrimaryStorage sharedManager]] run];
@ -989,7 +1010,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
completionHandler:(void (^)())completionHandler
completionHandler:(void (^)(void))completionHandler
{
OWSAssertIsOnMainThread();
@ -1016,7 +1037,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
withResponseInfo:(NSDictionary *)responseInfo
completionHandler:(void (^)())completionHandler
completionHandler:(void (^)(void))completionHandler
{
OWSAssertIsOnMainThread();
@ -1040,6 +1061,28 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
}];
}
- (void)application:(UIApplication *)application
performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
DDLogInfo(@"%@ performing background fetch", self.logTag);
[AppReadiness runNowOrWhenAppIsReady:^{
__block AnyPromise *job = [[SignalApp sharedApp].messageFetcherJob run].then(^{
// HACK: Call completion handler after n seconds.
//
// We don't currently have a convenient API to know when message fetching is *done* when
// working with the websocket.
//
// We *could* substantially rewrite the TSSocketManager to take advantage of the `empty` message
// But once our REST endpoint is fixed to properly de-enqueue fallback notifications, we can easily
// use the rest endpoint here rather than the websocket and circumvent making changes to critical code.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
completionHandler(UIBackgroundFetchResultNewData);
job = nil;
});
});
}];
}
- (void)versionMigrationsDidComplete
{
OWSAssertIsOnMainThread();
@ -1136,6 +1179,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
DDLogInfo(@"registrationStateDidChange");
[self enableBackgroundRefreshIfNecessary];
if ([TSAccountManager isRegistered]) {
DDLogInfo(@"localNumber: %@", [TSAccountManager localNumber]);
@ -1153,6 +1198,11 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
}
}
- (void)registrationLockDidChange:(NSNotification *)notification
{
[self enableBackgroundRefreshIfNecessary];
}
- (void)ensureRootViewController
{
OWSAssertIsOnMainThread();