session-ios/SignalUtilitiesKit/Utilities/AppSetup.m

118 lines
5.9 KiB
Mathematica
Raw Permalink Normal View History

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"
#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>
#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);
OWSAssertDebug(migrationCompletion);
__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
// 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
OWSProfileManager *profileManager = [[OWSProfileManager alloc] initWithPrimaryStorage:primaryStorage];
OWSBlockingManager *blockingManager = [[OWSBlockingManager alloc] initWithPrimaryStorage:primaryStorage];
OWSIdentityManager *identityManager = [[OWSIdentityManager alloc] initWithPrimaryStorage:primaryStorage];
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];
id<OWSTypingIndicators> typingIndicators = [[OWSTypingIndicatorsImpl alloc] init];
OWSAudioSession *audioSession = [OWSAudioSession new];
2018-10-12 22:39:40 +02:00
OWSSounds *sounds = [[OWSSounds alloc] initWithPrimaryStorage:primaryStorage];
id<OWSProximityMonitoringManager> proximityMonitoringManager = [OWSProximityMonitoringManagerImpl new];
2018-10-12 22:39:40 +02:00
OWSWindowManager *windowManager = [[OWSWindowManager alloc] initDefault];
[Environment setShared:[[Environment alloc] initWithAudioSession:audioSession
preferences:preferences
proximityMonitoringManager:proximityMonitoringManager
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);
[SNConfiguration performMainSetup]; // Must happen before the performUpdateCheck call below
2017-12-07 19:53:13 +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:^() {
dispatch_async(dispatch_get_main_queue(), ^{
// Don't start database migrations until storage is ready.
[VersionMigrations performUpdateCheckWithCompletion:^() {
OWSAssertIsOnMainThread();
migrationCompletion();
OWSAssertDebug(backgroundTask);
backgroundTask = nil;
}];
});
}];
2017-12-07 19:53:13 +01:00
});
}
@end
NS_ASSUME_NONNULL_END