Add asserts around Swift singletons.

This commit is contained in:
Matthew Chen 2017-12-07 10:33:27 -05:00
parent 26bd1f2e65
commit 7a1e47cd26
22 changed files with 105 additions and 12 deletions

View File

@ -914,7 +914,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[[TSStorageManager sharedManager].newDatabaseConnection
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[[ExperienceUpgradeFinder new] markAllAsSeenWithTransaction:transaction];
[ExperienceUpgradeFinder.sharedManager markAllAsSeenWithTransaction:transaction];
}];
// Start running the disappearing messages job in case the newly registered user
// enables this feature

View File

@ -20,6 +20,10 @@ class MessageFetcherJob: NSObject {
self.messageReceiver = messageReceiver
self.networkManager = networkManager
self.signalService = signalService
super.init()
SwiftSingletons.register(self)
}
public func run() -> Promise<Void> {

View File

@ -26,6 +26,10 @@ class AccountManager: NSObject {
self.networkManager = textSecureAccountManager.networkManager
self.textSecureAccountManager = textSecureAccountManager
self.preferences = preferences
super.init()
SwiftSingletons.register(self)
}
// MARK: registration

View File

@ -24,6 +24,10 @@ class CallNotificationsAdapter: NSObject {
// } else {
adaptee = SignalApp.shared().notificationsManager
// }
super.init()
SwiftSingletons.register(self)
}
func presentIncomingCall(_ call: SignalCall, callerName: String) {

View File

@ -72,8 +72,11 @@ class UserNotificationsAdaptee: NSObject, OWSCallNotificationsAdaptee, UNUserNot
override init() {
self.center = UNUserNotificationCenter.current()
super.init()
SwiftSingletons.register(self)
center.delegate = self
// FIXME TODO only do this after user has registered.

View File

@ -56,7 +56,6 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
@property (nonatomic, readonly) AccountManager *accountManager;
@property (nonatomic, readonly) OWSContactsManager *contactsManager;
@property (nonatomic, readonly) ExperienceUpgradeFinder *experienceUpgradeFinder;
@property (nonatomic, readonly) OWSMessageManager *messagesManager;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@property (nonatomic, readonly) OWSBlockingManager *blockingManager;
@ -111,7 +110,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
_blockingManager = [OWSBlockingManager sharedManager];
_blockedPhoneNumberSet = [NSSet setWithArray:[_blockingManager blockedPhoneNumbers]];
_experienceUpgradeFinder = [ExperienceUpgradeFinder new];
ExperienceUpgradeFinder.sharedManager;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(blockedPhoneNumbersDidChange:)
@ -530,7 +529,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
__block NSArray<ExperienceUpgrade *> *unseenUpgrades;
[self.editingDbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
unseenUpgrades = [self.experienceUpgradeFinder allUnseenWithTransaction:transaction];
unseenUpgrades = [ExperienceUpgradeFinder.sharedManager allUnseenWithTransaction:transaction];
}];
return unseenUpgrades;
}
@ -540,7 +539,7 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
AssertIsOnMainThread();
[self.editingDbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[self.experienceUpgradeFinder markAllAsSeenWithTransaction:transaction];
[ExperienceUpgradeFinder.sharedManager markAllAsSeenWithTransaction:transaction];
}];
}

View File

@ -148,6 +148,8 @@ struct AudioSource: Hashable {
super.init()
SwiftSingletons.register(self)
// Configure audio session so we don't prompt user with Record permission until call is connected.
audioSession.configure()
}

View File

@ -224,6 +224,8 @@ protocol CallServiceObserver: class {
super.init()
SwiftSingletons.register(self)
self.createCallUIAdapter()
NotificationCenter.default.addObserver(self,

View File

@ -9,7 +9,7 @@ import SignalMessaging
/**
* Manage call related UI in a pre-CallKit world.
*/
class NonCallKitCallUIAdaptee: CallUIAdaptee {
class NonCallKitCallUIAdaptee: NSObject, CallUIAdaptee {
let TAG = "[NonCallKitCallUIAdaptee]"
@ -20,8 +20,14 @@ class NonCallKitCallUIAdaptee: CallUIAdaptee {
let hasManualRinger = true
required init(callService: CallService, notificationsAdapter: CallNotificationsAdapter) {
AssertIsOnMainThread()
self.callService = callService
self.notificationsAdapter = notificationsAdapter
super.init()
SwiftSingletons.register(self)
}
func startOutgoingCall(handle: String) -> SignalCall {

View File

@ -20,6 +20,8 @@ import SignalMessaging
self.contactsUpdater = contactsUpdater
super.init()
SwiftSingletons.register(self)
}
/**

View File

@ -20,6 +20,14 @@ final class CallKitCallManager: NSObject {
let callController = CXCallController()
static let kAnonymousCallHandlePrefix = "Signal:"
override required init() {
AssertIsOnMainThread()
super.init()
SwiftSingletons.register(self)
}
// MARK: Actions
func startCall(_ call: SignalCall) {

View File

@ -63,6 +63,8 @@ final class CallKitCallUIAdaptee: NSObject, CallUIAdaptee, CXProviderDelegate {
super.init()
SwiftSingletons.register(self)
self.provider.setDelegate(self, queue: nil)
}

View File

@ -103,6 +103,8 @@ extension CallUIAdaptee {
super.init()
SwiftSingletons.register(self)
callService.addObserverAndSyncState(observer: self)
}

View File

@ -25,6 +25,10 @@ class WebRTCCallMessageHandler: NSObject, OWSCallMessageHandler {
self.accountManager = accountManager
self.callService = callService
self.messageSender = messageSender
super.init()
SwiftSingletons.register(self)
}
// MARK: - Call Handlers

View File

@ -13,7 +13,17 @@ enum ExperienceUpgradeId: String {
}
class ExperienceUpgradeFinder: NSObject {
public let TAG = "[ExperienceUpgradeFinder]"
// MARK - Singleton class
@objc(sharedManager)
public static let shared = ExperienceUpgradeFinder()
private override init() {
super.init()
SwiftSingletons.register(self)
}
var videoCalling: ExperienceUpgrade {
return ExperienceUpgrade(uniqueId: ExperienceUpgradeId.videoCalling.rawValue,
@ -64,7 +74,7 @@ class ExperienceUpgradeFinder: NSObject {
}
public func markAllAsSeen(transaction: YapDatabaseReadWriteTransaction) {
Logger.info("\(TAG) marking experience upgrades as seen")
Logger.info("\(logTag) marking experience upgrades as seen")
allExperienceUpgrades.forEach { $0.save(with: transaction) }
}
}

View File

@ -31,6 +31,8 @@ public enum PushRegistrationError: Error {
private override init() {
super.init()
SwiftSingletons.register(self)
}
private var userNotificationSettingsPromise: Promise<Void>?

View File

@ -271,7 +271,11 @@ extension GiphyError: LocalizedError {
static let sharedInstance = GiphyAPI()
// Force usage as a singleton
override private init() {}
override private init() {
super.init()
SwiftSingletons.register(self)
}
deinit {
NotificationCenter.default.removeObserver(self)

View File

@ -450,6 +450,8 @@ extension URLSessionTask {
super.init()
SwiftSingletons.register(self)
ensureGifFolder()
}

View File

@ -428,7 +428,7 @@
"DATABASE_VIEW_OVERLAY_TITLE" = "Updating Database";
/* Message indicating that the debug log is being uploaded. */
"DEBUG_LOG_ACTIVITY_INDICATOR" = "Sending debug log...";
"DEBUG_LOG_ACTIVITY_INDICATOR" = "Sending Debug Log...";
/* Message of the debug log alert. */
"DEBUG_LOG_ALERT_MESSAGE" = "What would you like to do with the link to your debug log?";
@ -449,13 +449,13 @@
"DEBUG_LOG_ALERT_TITLE" = "One More Step";
/* Title of the alert indicating the debug log upload failed. */
"DEBUG_LOG_FAILURE_ALERT_TITLE" = "Failed to submit debug log";
"DEBUG_LOG_FAILURE_ALERT_TITLE" = "Failed to Submit Debug Log";
/* Message of the alert before redirecting to Github Issues. */
"DEBUG_LOG_GITHUB_ISSUE_ALERT_MESSAGE" = "The gist link was copied in your clipboard. You are about to be redirected to the GitHub issue list.";
/* Title of the alert before redirecting to Github Issues. */
"DEBUG_LOG_GITHUB_ISSUE_ALERT_TITLE" = "GitHub redirection";
"DEBUG_LOG_GITHUB_ISSUE_ALERT_TITLE" = "GitHub Redirection";
/* {{Short Date}} when device last communicated with Signal Server. */
"DEVICE_LAST_ACTIVE_AT_LABEL" = "Last active: %@";

View File

@ -351,6 +351,10 @@ public class SystemContactsFetcher: NSObject {
override init() {
self.contactStoreAdapter = ContactStoreAdapter()
super.init()
SwiftSingletons.register(self)
}
@objc

View File

@ -38,6 +38,8 @@ public class DeviceSleepManager: NSObject {
private override init() {
super.init()
SwiftSingletons.register(self)
NotificationCenter.default.addObserver(self,
selector:#selector(didEnterBackground),
name:NSNotification.Name.UIApplicationDidEnterBackground,

View File

@ -20,3 +20,30 @@ public func owsFail(_ message: String) {
Logger.flush()
assertionFailure(message)
}
public class SwiftSingletons: NSObject {
public static let shared = SwiftSingletons()
private var classSet = Set<String>()
private override init() {
super.init()
}
public func register(_ singleton: AnyObject) {
guard _isDebugAssertConfiguration() else {
return
}
let singletonClassName = String(describing:type(of:singleton))
guard !classSet.contains(singletonClassName) else {
owsFail("\(self.logTag()) in \(#function) Duplicate singleton: \(singletonClassName).")
return
}
Logger.verbose("\(self.logTag()) in \(#function) Registering singleton: \(singletonClassName).")
classSet.insert(singletonClassName)
}
public static func register(_ singleton: AnyObject) {
shared.register(singleton)
}
}