Ensure app delegate hooks are ignored until app is ready.

pull/1/head
Matthew Chen 8 years ago
parent d46914831c
commit f9f60bc14f

@ -1,15 +1,12 @@
// //
// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // Copyright (c) 2018 Open Whisper Systems. All rights reserved.
// //
#import <UIKit/UIKit.h>
#import "HomeViewController.h" #import "HomeViewController.h"
#import <UIKit/UIKit.h>
extern NSString *const AppDelegateStoryboardMain; extern NSString *const AppDelegateStoryboardMain;
@interface AppDelegate : UIResponder <UIApplicationDelegate> @interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (atomic) BOOL isEnvironmentSetup;
@end @end

@ -347,12 +347,16 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{ {
OWSAssertIsOnMainThread();
DDLogInfo(@"%@ registered vanilla push token: %@", self.logTag, deviceToken); DDLogInfo(@"%@ registered vanilla push token: %@", self.logTag, deviceToken);
[PushRegistrationManager.sharedManager didReceiveVanillaPushToken:deviceToken]; [PushRegistrationManager.sharedManager didReceiveVanillaPushToken:deviceToken];
} }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{ {
OWSAssertIsOnMainThread();
DDLogError(@"%@ failed to register vanilla push token with error: %@", self.logTag, error); DDLogError(@"%@ failed to register vanilla push token with error: %@", self.logTag, error);
#ifdef DEBUG #ifdef DEBUG
DDLogWarn( DDLogWarn(
@ -367,6 +371,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application - (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{ {
OWSAssertIsOnMainThread();
DDLogInfo(@"%@ registered user notification settings", self.logTag); DDLogInfo(@"%@ registered user notification settings", self.logTag);
[PushRegistrationManager.sharedManager didRegisterUserNotificationSettings]; [PushRegistrationManager.sharedManager didRegisterUserNotificationSettings];
} }
@ -376,6 +382,14 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
sourceApplication:(NSString *)sourceApplication sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation annotation:(id)annotation
{ {
OWSAssertIsOnMainThread();
if (!AppReadiness.isAppReady) {
DDLogWarn(@"%@ Ignoring openURL: app not ready.", self.logTag);
// TODO: Consider using [AppReadiness runNowOrWhenAppIsReady:].
return NO;
}
if ([url.scheme isEqualToString:kURLSchemeSGNLKey]) { if ([url.scheme isEqualToString:kURLSchemeSGNLKey]) {
if ([url.host hasPrefix:kURLHostVerifyPrefix] && ![TSAccountManager isRegistered]) { if ([url.host hasPrefix:kURLHostVerifyPrefix] && ![TSAccountManager isRegistered]) {
id signupController = SignalApp.sharedApp.signUpFlowNavigationController; id signupController = SignalApp.sharedApp.signUpFlowNavigationController;
@ -402,6 +416,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
} }
- (void)applicationDidBecomeActive:(UIApplication *)application { - (void)applicationDidBecomeActive:(UIApplication *)application {
OWSAssertIsOnMainThread();
DDLogWarn(@"%@ applicationDidBecomeActive.", self.logTag); DDLogWarn(@"%@ applicationDidBecomeActive.", self.logTag);
if (CurrentAppContext().isRunningTests) { if (CurrentAppContext().isRunningTests) {
return; return;
@ -499,6 +515,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
} }
- (void)applicationWillResignActive:(UIApplication *)application { - (void)applicationWillResignActive:(UIApplication *)application {
OWSAssertIsOnMainThread();
DDLogWarn(@"%@ applicationWillResignActive.", self.logTag); DDLogWarn(@"%@ applicationWillResignActive.", self.logTag);
__block OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__]; __block OWSBackgroundTask *backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
@ -525,6 +543,14 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application - (void)application:(UIApplication *)application
performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem
completionHandler:(void (^)(BOOL succeeded))completionHandler { completionHandler:(void (^)(BOOL succeeded))completionHandler {
OWSAssertIsOnMainThread();
if (!AppReadiness.isAppReady) {
DDLogWarn(@"%@ Ignoring performActionForShortcutItem: app not ready.", self.logTag);
// TODO: Consider using [AppReadiness runNowOrWhenAppIsReady:].
completionHandler(NO);
}
if ([TSAccountManager isRegistered]) { if ([TSAccountManager isRegistered]) {
[SignalApp.sharedApp.homeViewController showNewConversationView]; [SignalApp.sharedApp.homeViewController showNewConversationView];
completionHandler(YES); completionHandler(YES);
@ -551,8 +577,18 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
/** /**
* Among other things, this is used by "call back" callkit dialog and calling from native contacts app. * Among other things, this is used by "call back" callkit dialog and calling from native contacts app.
*/ */
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray * _Nullable))restorationHandler - (BOOL)application:(UIApplication *)application
continueUserActivity:(nonnull NSUserActivity *)userActivity
restorationHandler:(nonnull void (^)(NSArray *_Nullable))restorationHandler
{ {
OWSAssertIsOnMainThread();
if (!AppReadiness.isAppReady) {
DDLogWarn(@"%@ Ignoring continueUserActivity: app not ready.", self.logTag);
// TODO: Consider using [AppReadiness runNowOrWhenAppIsReady:].
return NO;
}
if ([userActivity.activityType isEqualToString:@"INStartVideoCallIntent"]) { if ([userActivity.activityType isEqualToString:@"INStartVideoCallIntent"]) {
if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) { if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(10, 0)) {
DDLogError(@"%@ unexpectedly received INStartVideoCallIntent pre iOS10", self.logTag); DDLogError(@"%@ unexpectedly received INStartVideoCallIntent pre iOS10", self.logTag);
@ -678,12 +714,13 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
return NO; return NO;
} }
/** /**
* Screen protection obscures the app screen shown in the app switcher. * Screen protection obscures the app screen shown in the app switcher.
*/ */
- (void)prepareScreenProtection - (void)prepareScreenProtection
{ {
OWSAssertIsOnMainThread();
UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds]; UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds];
window.hidden = YES; window.hidden = YES;
window.opaque = YES; window.opaque = YES;
@ -698,24 +735,34 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)showScreenProtection - (void)showScreenProtection
{ {
OWSAssertIsOnMainThread();
if (Environment.preferences.screenSecurityIsEnabled) { if (Environment.preferences.screenSecurityIsEnabled) {
self.screenProtectionWindow.hidden = NO; self.screenProtectionWindow.hidden = NO;
} }
} }
- (void)removeScreenProtection { - (void)removeScreenProtection {
OWSAssertIsOnMainThread();
self.screenProtectionWindow.hidden = YES; self.screenProtectionWindow.hidden = YES;
} }
#pragma mark Push Notifications Delegate Methods #pragma mark Push Notifications Delegate Methods
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
OWSAssertIsOnMainThread();
// It is safe to continue even if the app isn't ready.
[[PushManager sharedManager] application:application didReceiveRemoteNotification:userInfo]; [[PushManager sharedManager] application:application didReceiveRemoteNotification:userInfo];
} }
- (void)application:(UIApplication *)application - (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
OWSAssertIsOnMainThread();
// It is safe to continue even if the app isn't ready.
[[PushManager sharedManager] application:application [[PushManager sharedManager] application:application
didReceiveRemoteNotification:userInfo didReceiveRemoteNotification:userInfo
fetchCompletionHandler:completionHandler]; fetchCompletionHandler:completionHandler];
@ -724,17 +771,12 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
OWSAssertIsOnMainThread(); OWSAssertIsOnMainThread();
if (!self.isEnvironmentSetup) {
OWSFail(@"%@ ignoring %s because environment is not yet set up: %@.",
self.logTag,
__PRETTY_FUNCTION__,
notification);
return;
}
DDLogInfo(@"%@ %s %@", self.logTag, __PRETTY_FUNCTION__, notification); DDLogInfo(@"%@ %s %@", self.logTag, __PRETTY_FUNCTION__, notification);
[AppStoreRating preventPromptAtNextTest]; [AppStoreRating preventPromptAtNextTest];
[AppReadiness runNowOrWhenAppIsReady:^{
[[PushManager sharedManager] application:application didReceiveLocalNotification:notification]; [[PushManager sharedManager] application:application didReceiveLocalNotification:notification];
}];
} }
- (void)application:(UIApplication *)application - (void)application:(UIApplication *)application
@ -742,8 +784,11 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
forLocalNotification:(UILocalNotification *)notification forLocalNotification:(UILocalNotification *)notification
completionHandler:(void (^)())completionHandler completionHandler:(void (^)())completionHandler
{ {
if (!self.isEnvironmentSetup) { OWSAssertIsOnMainThread();
OWSFail(@"%@ ignoring %s because environment is not yet set up.", self.logTag, __PRETTY_FUNCTION__);
if (!AppReadiness.isAppReady) {
DDLogWarn(@"%@ Ignoring handleActionWithIdentifier: app not ready.", self.logTag);
// TODO: Consider using [AppReadiness runNowOrWhenAppIsReady:].
return; return;
} }
@ -759,8 +804,11 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
withResponseInfo:(NSDictionary *)responseInfo withResponseInfo:(NSDictionary *)responseInfo
completionHandler:(void (^)())completionHandler completionHandler:(void (^)())completionHandler
{ {
if (!self.isEnvironmentSetup) { OWSAssertIsOnMainThread();
OWSFail(@"%@ ignoring %s because environment is not yet set up.", self.logTag, __PRETTY_FUNCTION__);
if (!AppReadiness.isAppReady) {
DDLogWarn(@"%@ Ignoring handleActionWithIdentifier: app not ready.", self.logTag);
// TODO: Consider using [AppReadiness runNowOrWhenAppIsReady:].
return; return;
} }
@ -806,8 +854,6 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[[OWSProfileManager sharedManager] ensureLocalProfileCached]; [[OWSProfileManager sharedManager] ensureLocalProfileCached];
self.isEnvironmentSetup = YES;
//#ifdef DEBUG //#ifdef DEBUG
// // A bug in orphan cleanup could be disastrous so let's only // // A bug in orphan cleanup could be disastrous so let's only
// // run it in DEBUG builds for a few releases. // // run it in DEBUG builds for a few releases.

@ -134,9 +134,11 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
// If we want to re-introduce silent pushes we can remove this assert. // If we want to re-introduce silent pushes we can remove this assert.
OWSFail(@"Unexpected content-available push."); OWSFail(@"Unexpected content-available push.");
[AppReadiness runNowOrWhenAppIsReady:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
completionHandler(UIBackgroundFetchResultNewData); completionHandler(UIBackgroundFetchResultNewData);
}); });
}];
} }
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Loading…
Cancel
Save