Skip to content

Commit

Permalink
Reorganize util code.
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesmchen-signal committed Nov 19, 2018
1 parent 88026be commit dae80ad
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 137 deletions.
96 changes: 58 additions & 38 deletions Signal.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Signal/Signal.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
<string>CloudKit</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
Expand Down
2 changes: 0 additions & 2 deletions Signal/src/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,6 @@ - (void)checkIfAppIsReady

[self ensureRootViewController];

[OWSBackup.sharedManager setup];

[self.messageManager startObserving];

#ifdef DEBUG
Expand Down
5 changes: 5 additions & 0 deletions Signal/src/environment/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import SignalMessaging
@objc
public var sessionResetJobQueue: SessionResetJobQueue

@objc
public var backup: OWSBackup

private override init() {
self.callMessageHandler = WebRTCCallMessageHandler()
self.callService = CallService()
Expand All @@ -66,6 +69,8 @@ import SignalMessaging
self.pushRegistrationManager = PushRegistrationManager()
self.pushManager = PushManager()
self.sessionResetJobQueue = SessionResetJobQueue()
assert(SSKEnvironment.shared.primaryStorage != nil)
self.backup = OWSBackup(primaryStorage: SSKEnvironment.shared.primaryStorage)

super.init()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ typedef NS_ENUM(NSUInteger, OWSBackupState) {
};

@class OWSBackupIO;
@class OWSPrimaryStorage;
@class TSAttachmentStream;
@class TSThread;

@interface OWSBackup : NSObject

- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage NS_DESIGNATED_INITIALIZER;

+ (instancetype)sharedManager NS_SWIFT_NAME(shared());

- (void)setup;

#pragma mark - Backup Export

@property (nonatomic, readonly) OWSBackupState backupExportState;
Expand Down
33 changes: 16 additions & 17 deletions Signal/src/util/OWSBackup.m → Signal/src/util/Backup/OWSBackup.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,9 @@ @implementation OWSBackup

+ (instancetype)sharedManager
{
static OWSBackup *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] initDefault];
});
return sharedMyManager;
}

- (instancetype)initDefault
{
OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager];
OWSAssertDebug(AppEnvironment.shared.backup);

return [self initWithPrimaryStorage:primaryStorage];
return AppEnvironment.shared.backup;
}

- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage
Expand All @@ -78,6 +68,10 @@ - (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage

OWSSingletonAssert();

[AppReadiness runNowOrWhenAppDidBecomeReady:^{
[self setup];
}];

return self;
}

Expand Down Expand Up @@ -251,6 +245,10 @@ - (void)ensureBackupExportState
{
OWSAssertIsOnMainThread();

if (!CurrentAppContext().isMainApp) {
return;
}

// Start or abort a backup export if neccessary.
if (!self.shouldHaveBackupExport && self.backupExportJob) {
[self.backupExportJob cancel];
Expand Down Expand Up @@ -297,11 +295,12 @@ - (void)checkCanImportBackup:(OWSBackupBoolBlock)success failure:(OWSBackupError

OWSLogInfo(@"");

[OWSBackupAPI checkForManifestInCloudWithSuccess:^(BOOL value) {
dispatch_async(dispatch_get_main_queue(), ^{
success(value);
});
}
[OWSBackupAPI
checkForManifestInCloudWithSuccess:^(BOOL value) {
dispatch_async(dispatch_get_main_queue(), ^{
success(value);
});
}
failure:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
failure(error);
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -439,19 +439,20 @@ - (void)fetchAllRecordsWithCompletion:(OWSBackupJobBoolCompletion)completion
OWSLogVerbose(@"");

__weak OWSBackupExportJob *weakSelf = self;
[OWSBackupAPI fetchAllRecordNamesWithSuccess:^(NSArray<NSString *> *recordNames) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (strongSelf.isComplete) {
return;
}
strongSelf.lastValidRecordNames = [NSSet setWithArray:recordNames];
completion(YES);
});
}
[OWSBackupAPI
fetchAllRecordNamesWithSuccess:^(NSArray<NSString *> *recordNames) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
OWSBackupExportJob *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (strongSelf.isComplete) {
return;
}
strongSelf.lastValidRecordNames = [NSSet setWithArray:recordNames];
completion(YES);
});
}
failure:^(NSError *error) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
completion(NO);
Expand Down Expand Up @@ -490,29 +491,28 @@ typedef NSUInteger (^ExportBlock)(YapDatabaseReadTransaction *,
EntityFilter _Nullable filter,
SignalIOSProtoBackupSnapshotBackupEntityType entityType) {
__block NSUInteger count = 0;
[transaction
enumerateKeysAndObjectsInCollection:collection
usingBlock:^(NSString *key, id object, BOOL *stop) {
if (self.isComplete) {
*stop = YES;
return;
}
if (filter && !filter(object)) {
return;
}
if (![object isKindOfClass:expectedClass]) {
OWSFailDebug(@"unexpected class: %@", [object class]);
return;
}
TSYapDatabaseObject *entity = object;
count++;

if (![exportStream writeObject:entity entityType:entityType]) {
*stop = YES;
aborted = YES;
return;
}
}];
[transaction enumerateKeysAndObjectsInCollection:collection
usingBlock:^(NSString *key, id object, BOOL *stop) {
if (self.isComplete) {
*stop = YES;
return;
}
if (filter && !filter(object)) {
return;
}
if (![object isKindOfClass:expectedClass]) {
OWSFailDebug(@"unexpected class: %@", [object class]);
return;
}
TSYapDatabaseObject *entity = object;
count++;

if (![exportStream writeObject:entity entityType:entityType]) {
*stop = YES;
aborted = YES;
return;
}
}];
return count;
};

Expand Down Expand Up @@ -1022,23 +1022,24 @@ - (void)cleanUpCloudWithActiveRecordNames:(NSSet<NSString *> *)activeRecordNames
}

__weak OWSBackupExportJob *weakSelf = self;
[OWSBackupAPI fetchAllRecordNamesWithSuccess:^(NSArray<NSString *> *recordNames) {
// Ensure that we continue to work off the main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableSet<NSString *> *obsoleteRecordNames = [NSMutableSet new];
[obsoleteRecordNames addObjectsFromArray:recordNames];
[obsoleteRecordNames minusSet:activeRecordNames];

OWSLogVerbose(@"recordNames: %zd - activeRecordNames: %zd = obsoleteRecordNames: %zd",
recordNames.count,
activeRecordNames.count,
obsoleteRecordNames.count);

[weakSelf deleteRecordsFromCloud:[obsoleteRecordNames.allObjects mutableCopy]
deletedCount:0
completion:completion];
});
}
[OWSBackupAPI
fetchAllRecordNamesWithSuccess:^(NSArray<NSString *> *recordNames) {
// Ensure that we continue to work off the main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableSet<NSString *> *obsoleteRecordNames = [NSMutableSet new];
[obsoleteRecordNames addObjectsFromArray:recordNames];
[obsoleteRecordNames minusSet:activeRecordNames];

OWSLogVerbose(@"recordNames: %zd - activeRecordNames: %zd = obsoleteRecordNames: %zd",
recordNames.count,
activeRecordNames.count,
obsoleteRecordNames.count);

[weakSelf deleteRecordsFromCloud:[obsoleteRecordNames.allObjects mutableCopy]
deletedCount:0
completion:completion];
});
}
failure:^(NSError *error) {
// Ensure that we continue to work off the main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,21 @@ - (void)start
progress:nil];

__weak OWSBackupImportJob *weakSelf = self;
[weakSelf downloadAndProcessManifestWithSuccess:^(OWSBackupManifestContents *manifest) {
OWSBackupImportJob *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (self.isComplete) {
return;
[weakSelf
downloadAndProcessManifestWithSuccess:^(OWSBackupManifestContents *manifest) {
OWSBackupImportJob *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
if (self.isComplete) {
return;
}
OWSCAssertDebug(manifest.databaseItems.count > 0);
OWSCAssertDebug(manifest.attachmentsItems);
strongSelf.databaseItems = manifest.databaseItems;
strongSelf.attachmentsItems = manifest.attachmentsItems;
[strongSelf downloadAndProcessImport];
}
OWSCAssertDebug(manifest.databaseItems.count > 0);
OWSCAssertDebug(manifest.attachmentsItems);
strongSelf.databaseItems = manifest.databaseItems;
strongSelf.attachmentsItems = manifest.attachmentsItems;
[strongSelf downloadAndProcessImport];
}
failure:^(NSError *manifestError) {
[weakSelf failWithError:manifestError];
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,20 @@ - (void)downloadAndProcessManifestWithSuccess:(OWSBackupJobManifestSuccess)succe
OWSLogVerbose(@"");

__weak OWSBackupJob *weakSelf = self;
[OWSBackupAPI downloadManifestFromCloudWithSuccess:^(NSData *data) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf processManifest:data
success:success
failure:^{
failure(OWSErrorWithCodeDescription(OWSErrorCodeImportBackupFailed,
NSLocalizedString(@"BACKUP_IMPORT_ERROR_COULD_NOT_IMPORT",
@"Error indicating the backup import could not import the user's data.")));
}
backupIO:backupIO];
});
}
[OWSBackupAPI
downloadManifestFromCloudWithSuccess:^(NSData *data) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[weakSelf
processManifest:data
success:success
failure:^{
failure(OWSErrorWithCodeDescription(OWSErrorCodeImportBackupFailed,
NSLocalizedString(@"BACKUP_IMPORT_ERROR_COULD_NOT_IMPORT",
@"Error indicating the backup import could not import the user's data.")));
}
backupIO:backupIO];
});
}
failure:^(NSError *error) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// The manifest file is critical so any error downloading it is unrecoverable.
Expand Down

0 comments on commit dae80ad

Please sign in to comment.