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>
|
2020-11-25 06:15:16 +01:00
|
|
|
#import <SessionMessagingKit/OWSBackgroundTask.h>
|
|
|
|
#import <SessionMessagingKit/OWSStorage.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
|
2022-03-24 00:03:51 +01:00
|
|
|
migrationCompletion:(OWSDatabaseMigrationCompletion)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-22 17:42:53 +02:00
|
|
|
id<SSKReachabilityManager> reachabilityManager = [SSKReachabilityManagerImpl new];
|
2018-10-31 00:18:17 +01:00
|
|
|
|
2018-10-23 02:55:39 +02:00
|
|
|
OWSAudioSession *audioSession = [OWSAudioSession new];
|
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
|
|
|
windowManager:windowManager]];
|
2018-09-17 15:27:58 +02:00
|
|
|
|
2022-04-21 08:42:35 +02:00
|
|
|
// TODO: Add this back
|
|
|
|
// TODO: Refactor this file to Swift
|
2022-04-06 07:43:26 +02:00
|
|
|
[SSKEnvironment setShared:[[SSKEnvironment alloc] initWithPrimaryStorage:primaryStorage
|
2022-05-15 06:39:21 +02:00
|
|
|
reachabilityManager:reachabilityManager]];
|
2018-09-17 15:27:58 +02:00
|
|
|
|
|
|
|
appSpecificSingletonBlock();
|
|
|
|
|
2022-04-21 08:42:35 +02:00
|
|
|
// TODO: Add this back?
|
|
|
|
// 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:[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.
|
2022-03-24 00:03:51 +01:00
|
|
|
[VersionMigrations performUpdateCheckWithCompletion:^(BOOL successful, BOOL needsConfigSync) {
|
2018-09-19 22:29:15 +02:00
|
|
|
OWSAssertIsOnMainThread();
|
2018-04-23 21:49:20 +02:00
|
|
|
|
2022-03-24 00:03:51 +01:00
|
|
|
migrationCompletion(successful, needsConfigSync);
|
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
|
|
|
}];
|
2022-04-08 08:56:33 +02:00
|
|
|
|
|
|
|
// Must happen after the performUpdateCheck above to ensure all legacy database migrations have run
|
|
|
|
[SNConfiguration performDatabaseSetup];
|
2017-12-07 19:53:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|