Rework thread safety in profile manager.

pull/1/head
Matthew Chen 8 years ago
parent ee300590e3
commit 74efcb9041

@ -108,7 +108,7 @@
3478506B1FD9B78A007B8332 /* NoopCallMessageHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347850671FD9B78A007B8332 /* NoopCallMessageHandler.swift */; };
3478506C1FD9B78A007B8332 /* NoopNotificationsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 347850681FD9B78A007B8332 /* NoopNotificationsManager.swift */; };
347850711FDAEB17007B8332 /* OWSUserProfile.m in Sources */ = {isa = PBXBuildFile; fileRef = 3478506F1FDAEB16007B8332 /* OWSUserProfile.m */; };
347850721FDAEB17007B8332 /* OWSUserProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = 347850701FDAEB16007B8332 /* OWSUserProfile.h */; };
347850721FDAEB17007B8332 /* OWSUserProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = 347850701FDAEB16007B8332 /* OWSUserProfile.h */; settings = {ATTRIBUTES = (Public, ); }; };
3497DBEC1ECE257500DB2605 /* OWSCountryMetadata.m in Sources */ = {isa = PBXBuildFile; fileRef = 3497DBEB1ECE257500DB2605 /* OWSCountryMetadata.m */; };
3497DBEF1ECE2E4700DB2605 /* DomainFrontingCountryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3497DBEE1ECE2E4700DB2605 /* DomainFrontingCountryViewController.m */; };
34B0796D1FCF46B100E248C2 /* MainAppContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 34B0796B1FCF46B000E248C2 /* MainAppContext.m */; };

@ -64,6 +64,7 @@
#import <MobileCoreServices/UTCoreTypes.h>
#import <SignalMessaging/OWSContactOffersInteraction.h>
#import <SignalMessaging/OWSFormat.h>
#import <SignalMessaging/OWSUserProfile.h>
#import <SignalMessaging/TSUnreadIndicatorInteraction.h>
#import <SignalMessaging/ThreadUtil.h>
#import <SignalMessaging/UIUtil.h>

@ -7,6 +7,7 @@
#import "Signal-Swift.h"
#import "ViewControllerUtils.h"
#import <SignalMessaging/OWSFormat.h>
#import <SignalMessaging/OWSUserProfile.h>
#import <SignalServiceKit/OWSMessageManager.h>
#import <SignalServiceKit/TSContactThread.h>
#import <SignalServiceKit/TSGroupThread.h>

@ -19,6 +19,7 @@
#import <Curve25519Kit/Curve25519.h>
#import <SignalMessaging/OWSAvatarBuilder.h>
#import <SignalMessaging/OWSProfileManager.h>
#import <SignalMessaging/OWSUserProfile.h>
#import <SignalMessaging/UIUtil.h>
#import <SignalServiceKit/NSDate+OWS.h>
#import <SignalServiceKit/OWSDisappearingConfigurationUpdateInfoMessage.h>

@ -30,6 +30,7 @@ FOUNDATION_EXPORT const unsigned char SignalMessagingVersionString[];
#import <SignalMessaging/OWSMath.h>
#import <SignalMessaging/OWSProfileManager.h>
#import <SignalMessaging/OWSTableViewController.h>
#import <SignalMessaging/OWSUserProfile.h>
#import <SignalMessaging/Release.h>
#import <SignalMessaging/SharingThreadPickerViewController.h>
#import <SignalMessaging/SignalKeyingStorage.h>

@ -6,6 +6,7 @@
#import "Environment.h"
#import "OWSContactAvatarBuilder.h"
#import "OWSContactsManager.h"
#import "OWSUserProfile.h"
#import "UIFont+OWS.h"
#import "UIUtil.h"
#import "UIView+OWS.h"

@ -6,6 +6,7 @@
#import "Environment.h"
#import "OWSFormat.h"
#import "OWSProfileManager.h"
#import "OWSUserProfile.h"
#import "ViewControllerUtils.h"
#import <SignalMessaging/SignalMessaging-Swift.h>
#import <SignalMessaging/UIColor+OWS.h>

@ -6,12 +6,7 @@
NS_ASSUME_NONNULL_BEGIN
extern NSString *const kNSNotificationName_LocalProfileDidChange;
extern NSString *const kNSNotificationName_OtherUsersProfileWillChange;
extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
extern NSString *const kNSNotificationName_ProfileWhitelistDidChange;
extern NSString *const kNSNotificationKey_ProfileRecipientId;
extern NSString *const kNSNotificationKey_ProfileGroupId;
extern const NSUInteger kOWSProfileManager_NameDataLength;
extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;

@ -33,12 +33,7 @@
NS_ASSUME_NONNULL_BEGIN
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
NSString *const kNSNotificationName_OtherUsersProfileWillChange = @"kNSNotificationName_OtherUsersProfileWillChange";
NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange";
NSString *const kNSNotificationName_ProfileWhitelistDidChange = @"kNSNotificationName_ProfileWhitelistDidChange";
NSString *const kNSNotificationKey_ProfileRecipientId = @"kNSNotificationKey_ProfileRecipientId";
NSString *const kNSNotificationKey_ProfileGroupId = @"kNSNotificationKey_ProfileGroupId";
NSString *const kOWSProfileManager_UserWhitelistCollection = @"kOWSProfileManager_UserWhitelistCollection";
NSString *const kOWSProfileManager_GroupWhitelistCollection = @"kOWSProfileManager_GroupWhitelistCollection";
@ -146,44 +141,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
#pragma mark - User Profile Accessor
- (OWSUserProfileCompletion)userProfileSaveCompletion:(OWSUserProfile *)userProfile
{
OWSAssert(userProfile);
return ^{
BOOL isLocalUserProfile = userProfile == self.localUserProfile;
dispatch_async(dispatch_get_main_queue(), ^{
if (isLocalUserProfile) {
// We populate an initial (empty) profile on launch of a new install, but until
// we have a registered account, syncing will fail (and there could not be any
// linked device to sync to at this point anyway).
if ([TSAccountManager isRegistered]) {
[CurrentAppContext() doMultiDeviceUpdateWithProfileKey:userProfile.profileKey];
}
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
object:nil
userInfo:nil];
} else {
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_OtherUsersProfileWillChange
object:nil
userInfo:@{
kNSNotificationKey_ProfileRecipientId : userProfile.recipientId,
}];
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_OtherUsersProfileDidChange
object:nil
userInfo:@{
kNSNotificationKey_ProfileRecipientId : userProfile.recipientId,
}];
}
});
};
}
- (void)ensureLocalProfileCached
{
// Since localUserProfile can create a transaction, we want to make sure it's not called for the first

@ -10,6 +10,13 @@ typedef void (^OWSUserProfileCompletion)(void);
@class OWSAES256Key;
extern NSString *const kNSNotificationName_LocalProfileDidChange;
extern NSString *const kNSNotificationName_OtherUsersProfileWillChange;
extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
extern NSString *const kNSNotificationKey_ProfileRecipientId;
extern NSString *const kNSNotificationKey_ProfileGroupId;
extern NSString *const kLocalProfileUniqueId;
// This class should be completely thread-safe.

@ -4,11 +4,21 @@
#import "OWSUserProfile.h"
#import "NSString+OWS.h"
#import <SignalServiceKit/AppContext.h>
#import <SignalServiceKit/Cryptography.h>
#import <SignalServiceKit/NSNotificationCenter+OWS.h>
#import <SignalServiceKit/TSAccountManager.h>
#import <YapDatabase/YapDatabaseConnection.h>
NS_ASSUME_NONNULL_BEGIN
NSString *const kNSNotificationName_LocalProfileDidChange = @"kNSNotificationName_LocalProfileDidChange";
NSString *const kNSNotificationName_OtherUsersProfileWillChange = @"kNSNotificationName_OtherUsersProfileWillChange";
NSString *const kNSNotificationName_OtherUsersProfileDidChange = @"kNSNotificationName_OtherUsersProfileDidChange";
NSString *const kNSNotificationKey_ProfileRecipientId = @"kNSNotificationKey_ProfileRecipientId";
NSString *const kNSNotificationKey_ProfileGroupId = @"kNSNotificationKey_ProfileGroupId";
NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
@interface OWSUserProfile ()
@ -66,6 +76,46 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
#pragma mark - Update With... Methods
- (OWSUserProfileCompletion)updateWithCompletion:(nullable OWSUserProfileCompletion)externalCompletion
{
return ^{
if (externalCompletion) {
externalCompletion();
}
BOOL isLocalUserProfile = [self.recipientId isEqualToString:kLocalProfileUniqueId];
dispatch_async(dispatch_get_main_queue(), ^{
if (isLocalUserProfile) {
// We populate an initial (empty) profile on launch of a new install, but until
// we have a registered account, syncing will fail (and there could not be any
// linked device to sync to at this point anyway).
if ([TSAccountManager isRegistered]) {
[CurrentAppContext() doMultiDeviceUpdateWithProfileKey:self.profileKey];
}
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
object:nil
userInfo:nil];
} else {
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_OtherUsersProfileWillChange
object:nil
userInfo:@{
kNSNotificationKey_ProfileRecipientId : self.recipientId,
}];
[[NSNotificationCenter defaultCenter]
postNotificationNameAsync:kNSNotificationName_OtherUsersProfileDidChange
object:nil
userInfo:@{
kNSNotificationKey_ProfileRecipientId : self.recipientId,
}];
}
});
};
}
- (void)updateWithProfileName:(nullable NSString *)profileName
avatarUrlPath:(nullable NSString *)avatarUrlPath
avatarFileName:(nullable NSString *)avatarFileName
@ -80,7 +130,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:avatarFileName];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithProfileName:(nullable NSString *)profileName
@ -99,7 +149,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithProfileName:(nullable NSString *)profileName
@ -116,7 +166,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithProfileName:(nullable NSString *)profileName
@ -135,7 +185,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:avatarFileName];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath
@ -150,7 +200,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:avatarFileName];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithAvatarFileName:(nullable NSString *)avatarFileName
@ -163,7 +213,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:avatarFileName];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateWithLastUpdateDate:(nullable NSDate *)lastUpdateDate
@ -176,7 +226,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setLastUpdateDate:lastUpdateDate];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)clearWithProfileKey:(OWSAES256Key *)profileKey
@ -195,7 +245,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setLastUpdateDate:nil];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
- (void)updateImmediatelyWithProfileKey:(OWSAES256Key *)profileKey
@ -212,7 +262,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setProfileKey:profileKey];
}];
}
completionBlock:completion];
completionBlock:[self updateWithCompletion:completion]];
}
#pragma mark - Database Connection Accessors

Loading…
Cancel
Save