Signal-iOS/SignalMessaging/environment/Environment.m
Sasha Weiss 242dfd2bce
Add all JobQueues to environment, via a wrapper
Currently, only some `JobQueue` types are initialized during startup
(as part of `Environment`, or `SSKEnvironment`). Initialization is
required, however, for a `JobQueue` type to restart any latent jobs.
That means that, for example, a durable `SendGiftBadge` job that failed,
and should be reattempted at a later date, will not in fact be restarted
since no `SendGiftBadgeJobQueue` will be initialized at launch.

This change adds `SSKJobQueues` and `SignalMessagingJobQueues` types,
which are intended to be singletons that hold within them a singleton
job queue for each of our `JobQueue` types. These wrappers are added to
`SSKEnvironment` and `Environment` (from SignalMessaging) respectively,
ensuring that all the `JobQueue`s they contain are initialized as part
of environment setup. The wrappers also avoid the need to add a new
property to the (already large) environment types for each new future
`JobQueue`.

This change also updates all existing call sites that accessed a
`JobQueue` from an environment object, to direct that access now through
the wrapper type.
2022-11-09 14:08:44 -06:00

87 lines
2.4 KiB
Objective-C

//
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
#import "Environment.h"
#import "OWSPreferences.h"
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/SSKEnvironment.h>
NS_ASSUME_NONNULL_BEGIN
static Environment *sharedEnvironment = nil;
@interface Environment ()
@property (nonatomic) OWSPreferences *preferencesRef;
@property (nonatomic) id<OWSProximityMonitoringManager> proximityMonitoringManagerRef;
@property (nonatomic) OWSSounds *soundsRef;
@property (nonatomic) LaunchJobs *launchJobsRef;
@property (nonatomic) OWSOrphanDataCleaner *orphanDataCleanerRef;
@property (nonatomic) AvatarBuilder *avatarBuilderRef;
@property (nonatomic) SignalMessagingJobQueues *signalMessagingJobQueuesRef;
@end
#pragma mark -
@implementation Environment
+ (Environment *)shared
{
OWSAssertDebug(sharedEnvironment);
return sharedEnvironment;
}
+ (void)setShared:(Environment *)environment
{
// The main app environment should only be set once.
//
// App extensions may be opened multiple times in the same process,
// so statics will persist.
OWSAssertDebug(environment);
OWSAssertDebug(!sharedEnvironment || !CurrentAppContext().isMainApp || CurrentAppContext().isRunningTests);
sharedEnvironment = environment;
}
- (instancetype)initWithLaunchJobs:(LaunchJobs *)launchJobs
preferences:(OWSPreferences *)preferences
proximityMonitoringManager:(id<OWSProximityMonitoringManager>)proximityMonitoringManager
sounds:(OWSSounds *)sounds
orphanDataCleaner:(OWSOrphanDataCleaner *)orphanDataCleaner
avatarBuilder:(AvatarBuilder *)avatarBuilder
smJobQueues:(SignalMessagingJobQueues *)smJobQueues
{
self = [super init];
if (!self) {
return self;
}
OWSAssertDebug(launchJobs);
OWSAssertDebug(preferences);
OWSAssertDebug(proximityMonitoringManager);
OWSAssertDebug(sounds);
OWSAssertDebug(orphanDataCleaner);
OWSAssertDebug(avatarBuilder);
OWSAssertDebug(smJobQueues);
_launchJobsRef = launchJobs;
_preferencesRef = preferences;
_proximityMonitoringManagerRef = proximityMonitoringManager;
_soundsRef = sounds;
_orphanDataCleanerRef = orphanDataCleaner;
_avatarBuilderRef = avatarBuilder;
_signalMessagingJobQueuesRef = smJobQueues;
OWSSingletonAssert();
return self;
}
@end
NS_ASSUME_NONNULL_END