2017-12-07 19:53:13 +01:00
|
|
|
//
|
2019-01-08 17:25:03 +01:00
|
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
2017-12-07 19:53:13 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
#import "AppSetup.h"
|
|
|
|
#import "Environment.h"
|
|
|
|
#import "VersionMigrations.h"
|
2020-11-11 06:30:54 +01:00
|
|
|
#import <SignalUtilitiesKit/OWSDatabaseMigration.h>
|
|
|
|
#import <SignalUtilitiesKit/OWSProfileManager.h>
|
2020-11-25 06:15:16 +01:00
|
|
|
#import <SessionMessagingKit/OWSBackgroundTask.h>
|
|
|
|
#import <SessionMessagingKit/OWSBlockingManager.h>
|
|
|
|
#import <SessionMessagingKit/OWSDisappearingMessagesJob.h>
|
|
|
|
#import <SessionMessagingKit/OWSIdentityManager.h>
|
|
|
|
#import <SessionMessagingKit/OWSOutgoingReceiptManager.h>
|
|
|
|
#import <SessionMessagingKit/OWSReadReceiptManager.h>
|
2020-11-26 00:37:56 +01:00
|
|
|
#import <SessionMessagingKit/OWSSounds.h>
|
2020-11-25 06:15:16 +01:00
|
|
|
#import <SessionMessagingKit/OWSStorage.h>
|
|
|
|
#import <SessionMessagingKit/SSKEnvironment.h>
|
2020-11-11 01:50:01 +01:00
|
|
|
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
2020-11-16 00:34:47 +01:00
|
|
|
|
2017-12-07 19:53:13 +01:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@implementation AppSetup
|
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
+ (void)setupEnvironmentWithAppSpecificSingletonBlock:(dispatch_block_t)appSpecificSingletonBlock
|
|
|
|
migrationCompletion:(dispatch_block_t)migrationCompletion
|
2017-12-07 19:53:13 +01:00
|
|
|
{
|
2018-09-17 15:27:58 +02:00
|
|
|
OWSAssertDebug(appSpecificSingletonBlock);
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(migrationCompletion);
|
2018-04-23 21:49:20 +02:00
|
|
|
|
|
|
|
__block OWSBackgroundTask *_Nullable backgroundTask =
|
|
|
|
[OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
2017-12-07 19:53:13 +01:00
|
|
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2018-02-27 17:14:25 +01:00
|
|
|
// Order matters here.
|
2018-09-17 15:27:58 +02:00
|
|
|
//
|
|
|
|
// All of these "singletons" should have any dependencies used in their
|
|
|
|
// initializers injected.
|
2018-02-27 17:14:25 +01:00
|
|
|
[[OWSBackgroundTaskManager sharedManager] observeNotifications];
|
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
OWSPrimaryStorage *primaryStorage = [[OWSPrimaryStorage alloc] initStorage];
|
|
|
|
[OWSPrimaryStorage protectFiles];
|
2017-12-07 19:53:13 +01:00
|
|
|
|
2018-10-03 00:12:28 +02:00
|
|
|
// AFNetworking (via CFNetworking) spools it's attachments to NSTemporaryDirectory().
|
|
|
|
// If you receive a media message while the device is locked, the download will fail if the temporary directory
|
|
|
|
// is NSFileProtectionComplete
|
|
|
|
BOOL success = [OWSFileSystem protectFileOrFolderAtPath:NSTemporaryDirectory()
|
|
|
|
fileProtectionType:NSFileProtectionCompleteUntilFirstUserAuthentication];
|
|
|
|
OWSAssert(success);
|
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
OWSPreferences *preferences = [OWSPreferences new];
|
2017-12-07 19:53:13 +01:00
|
|
|
|
2018-10-10 14:55:20 +02:00
|
|
|
OWSProfileManager *profileManager = [[OWSProfileManager alloc] initWithPrimaryStorage:primaryStorage];
|
2018-09-20 18:52:43 +02:00
|
|
|
OWSBlockingManager *blockingManager = [[OWSBlockingManager alloc] initWithPrimaryStorage:primaryStorage];
|
|
|
|
OWSIdentityManager *identityManager = [[OWSIdentityManager alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-10 23:09:35 +02:00
|
|
|
TSAccountManager *tsAccountManager = [[TSAccountManager alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-10 23:36:41 +02:00
|
|
|
OWSDisappearingMessagesJob *disappearingMessagesJob =
|
|
|
|
[[OWSDisappearingMessagesJob alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-11 18:51:58 +02:00
|
|
|
OWSReadReceiptManager *readReceiptManager =
|
|
|
|
[[OWSReadReceiptManager alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-11 21:29:01 +02:00
|
|
|
OWSOutgoingReceiptManager *outgoingReceiptManager =
|
|
|
|
[[OWSOutgoingReceiptManager alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-22 17:42:53 +02:00
|
|
|
id<SSKReachabilityManager> reachabilityManager = [SSKReachabilityManagerImpl new];
|
2018-10-31 00:18:17 +01:00
|
|
|
id<OWSTypingIndicators> typingIndicators = [[OWSTypingIndicatorsImpl alloc] init];
|
|
|
|
|
2018-10-23 02:55:39 +02:00
|
|
|
OWSAudioSession *audioSession = [OWSAudioSession new];
|
2018-10-12 22:39:40 +02:00
|
|
|
OWSSounds *sounds = [[OWSSounds alloc] initWithPrimaryStorage:primaryStorage];
|
2018-10-24 22:38:05 +02:00
|
|
|
id<OWSProximityMonitoringManager> proximityMonitoringManager = [OWSProximityMonitoringManagerImpl new];
|
2018-10-12 22:39:40 +02:00
|
|
|
OWSWindowManager *windowManager = [[OWSWindowManager alloc] initDefault];
|
2018-10-31 16:42:41 +01:00
|
|
|
|
2018-10-23 02:55:39 +02:00
|
|
|
[Environment setShared:[[Environment alloc] initWithAudioSession:audioSession
|
|
|
|
preferences:preferences
|
2018-10-24 22:38:05 +02:00
|
|
|
proximityMonitoringManager:proximityMonitoringManager
|
2018-10-23 02:55:39 +02:00
|
|
|
sounds:sounds
|
|
|
|
windowManager:windowManager]];
|
2018-09-17 15:27:58 +02:00
|
|
|
|
2020-11-16 00:34:47 +01:00
|
|
|
[SSKEnvironment setShared:[[SSKEnvironment alloc] initWithProfileManager:profileManager
|
|
|
|
primaryStorage:primaryStorage
|
|
|
|
blockingManager:blockingManager
|
|
|
|
identityManager:identityManager
|
|
|
|
tsAccountManager:tsAccountManager
|
|
|
|
disappearingMessagesJob:disappearingMessagesJob
|
|
|
|
readReceiptManager:readReceiptManager
|
|
|
|
outgoingReceiptManager:outgoingReceiptManager
|
|
|
|
reachabilityManager:reachabilityManager
|
2020-11-25 06:15:16 +01:00
|
|
|
typingIndicators:typingIndicators]];
|
2018-09-17 15:27:58 +02:00
|
|
|
|
|
|
|
appSpecificSingletonBlock();
|
|
|
|
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.isComplete);
|
2021-01-26 23:25:40 +01:00
|
|
|
|
|
|
|
[SNConfiguration performMainSetup]; // Must happen before the performUpdateCheck call below
|
2017-12-07 19:53:13 +01:00
|
|
|
|
2018-01-10 16:54:17 +01:00
|
|
|
// Register renamed classes.
|
|
|
|
[NSKeyedUnarchiver setClass:[OWSUserProfile class] forClassName:[OWSUserProfile collection]];
|
|
|
|
[NSKeyedUnarchiver setClass:[OWSDatabaseMigration class] forClassName:[OWSDatabaseMigration collection]];
|
|
|
|
|
2018-04-24 19:15:11 +02:00
|
|
|
[OWSStorage registerExtensionsWithMigrationBlock:^() {
|
2018-09-19 22:29:15 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
// Don't start database migrations until storage is ready.
|
|
|
|
[VersionMigrations performUpdateCheckWithCompletion:^() {
|
|
|
|
OWSAssertIsOnMainThread();
|
2018-04-23 21:49:20 +02:00
|
|
|
|
2018-09-19 22:29:15 +02:00
|
|
|
migrationCompletion();
|
2018-04-23 21:49:20 +02:00
|
|
|
|
2018-09-19 22:29:15 +02:00
|
|
|
OWSAssertDebug(backgroundTask);
|
|
|
|
backgroundTask = nil;
|
|
|
|
}];
|
|
|
|
});
|
2018-04-23 21:49:20 +02:00
|
|
|
}];
|
2017-12-07 19:53:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|