From 8f55f533297f2a31ca12631634ce8832c14fe56c Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 30 Jul 2018 10:22:45 -0400 Subject: [PATCH 1/5] Tidy files. --- ...nversationConfigurationSyncOperation.swift | 2 ++ .../Jobs/MultiDeviceProfileKeyUpdateJob.swift | 1 + .../ConversationViewController.m | 4 +-- .../DebugUI/DebugUISyncMessages.m | 2 ++ .../ViewControllers/NewGroupViewController.m | 12 +++---- SignalMessaging/contacts/OWSContactsSyncing.m | 3 +- .../Messages/Interactions/TSOutgoingMessage.m | 35 +++++++++++++++++++ .../src/Messages/OWSMessageManager.m | 6 ++-- SignalServiceKit/src/Util/DataSource.m | 4 +-- 9 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift index 6c7681073..db6e27879 100644 --- a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift +++ b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift @@ -68,6 +68,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { self.dbConnection.readWrite { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) + dataSource?.setShouldDeleteOnDeallocation() } guard let attachmentDataSource = dataSource else { @@ -89,6 +90,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { self.dbConnection.read { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) + dataSource?.setShouldDeleteOnDeallocation() } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift index c8f3df93e..81d33798e 100644 --- a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift +++ b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift @@ -51,6 +51,7 @@ import SignalMessaging var dataSource: DataSource? = nil self.editingDatabaseConnection.readWrite { transaction in dataSource = DataSourceValue.dataSource(withSyncMessageData: syncContactsMessage.buildPlainTextAttachmentData(with: transaction)) + dataSource?.setShouldDeleteOnDeallocation() } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 0aafdd920..3eca21e07 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3948,9 +3948,9 @@ typedef enum : NSUInteger { if (newGroupModel.groupImage) { NSData *data = UIImagePNGRepresentation(newGroupModel.groupImage); DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [self.messageSender enqueueAttachment:dataSource + [dataSource setShouldDeleteOnDeallocation]; + [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng - sourceFilename:nil inMessage:message success:^{ DDLogDebug(@"%@ Successfully sent group update with avatar", self.logTag); diff --git a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m index 3cf600cef..c8159e032 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m @@ -104,6 +104,7 @@ NS_ASSUME_NONNULL_BEGIN dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; + [dataSource setShouldDeleteOnDeallocation]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource @@ -124,6 +125,7 @@ NS_ASSUME_NONNULL_BEGIN [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; + [dataSource setShouldDeleteOnDeallocation]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index 901ea951e..ed6bc9b39 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -490,12 +490,12 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68; NSData *data = UIImagePNGRepresentation(model.groupImage); DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [self.messageSender enqueueAttachment:dataSource - contentType:OWSMimeTypeImagePng - sourceFilename:nil - inMessage:message - success:successHandler - failure:failureHandler]; + [dataSource setShouldDeleteOnDeallocation]; + [self.messageSender enqueueTemporaryAttachment:dataSource + contentType:OWSMimeTypeImagePng + inMessage:message + success:successHandler + failure:failureHandler]; } else { [self.messageSender enqueueMessage:message success:successHandler failure:failureHandler]; } diff --git a/SignalMessaging/contacts/OWSContactsSyncing.m b/SignalMessaging/contacts/OWSContactsSyncing.m index f2285ab09..77ff77376 100644 --- a/SignalMessaging/contacts/OWSContactsSyncing.m +++ b/SignalMessaging/contacts/OWSContactsSyncing.m @@ -126,7 +126,7 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey __block NSData *messageData; __block NSData *lastMessageData; - [self.editingDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { + [self.editingDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { messageData = [syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction]; lastMessageData = [transaction objectForKey:kOWSPrimaryStorageOWSContactsSyncingLastMessageKey inCollection:kOWSPrimaryStorageOWSContactsSyncingCollection]; @@ -140,6 +140,7 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey self.isRequestInFlight = YES; DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:messageData]; + [dataSource setShouldDeleteOnDeallocation]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 24259c7b7..53e81b7bf 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -354,6 +354,41 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt return self; } +- (void)dealloc +{ + [self cleanUpTemporaryAttachmentsIfNecessary]; +} + +// Each message has the responsibility for eagerly cleaning up its attachments. +// Normally this is done in [TSMessage removeWithTransaction], but that doesn't +// apply for "transient", unsaved messages (i.e. shouldBeSaved == NO). These +// messages should clean up their attachments upon deallocation. +- (void)cleanUpTemporaryAttachmentsIfNecessary +{ + if (self.shouldBeSaved) { + // Message in not transient; no need to clean up attachments. + return; + } + NSArray *_Nullable attachmentIds = self.attachmentIds; + if (attachmentIds.count < 1) { + return; + } + [self.dbReadWriteConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + for (NSString *attachmentId in attachmentIds) { + // We need to fetch each attachment, since [TSAttachment removeWithTransaction:] does important work. + TSAttachment *_Nullable attachment = + [TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction]; + if (!attachment) { + OWSCFail(@"%@ couldn't load interaction's attachment for deletion.", TSOutgoingMessage.logTag); + continue; + } + [attachment removeWithTransaction:transaction]; + }; + }]; +} + +#pragma mark - + - (TSOutgoingMessageState)messageState { TSOutgoingMessageState newMessageState = diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 7becd7cf5..ec91be68f 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -658,6 +658,7 @@ NS_ASSUME_NONNULL_BEGIN DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; + [dataSource setShouldDeleteOnDeallocation]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage @@ -674,6 +675,7 @@ NS_ASSUME_NONNULL_BEGIN DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; + [dataSource setShouldDeleteOnDeallocation]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncGroupsMessage @@ -825,9 +827,9 @@ NS_ASSUME_NONNULL_BEGIN if (gThread.groupModel.groupImage) { NSData *data = UIImagePNGRepresentation(gThread.groupModel.groupImage); DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [self.messageSender enqueueAttachment:dataSource + [dataSource setShouldDeleteOnDeallocation]; + [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng - sourceFilename:nil inMessage:message success:^{ DDLogDebug(@"%@ Successfully sent group update with avatar", self.logTag); diff --git a/SignalServiceKit/src/Util/DataSource.m b/SignalServiceKit/src/Util/DataSource.m index df21acf9f..82dd3964b 100755 --- a/SignalServiceKit/src/Util/DataSource.m +++ b/SignalServiceKit/src/Util/DataSource.m @@ -102,7 +102,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) NSString *fileExtension; // This property is lazy-populated. -@property (nonatomic) NSString *cachedFilePath; +@property (nonatomic, nullable) NSString *cachedFilePath; @end @@ -113,7 +113,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)dealloc { if (self.shouldDeleteOnDeallocation) { - NSString *filePath = self.cachedFilePath; + NSString *_Nullable filePath = self.cachedFilePath; if (filePath) { dispatch_async(dispatch_get_main_queue(), ^{ NSError *error; From 2c194743971468c66afc36f4afd7feac8c0bdde2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 30 Jul 2018 10:35:58 -0400 Subject: [PATCH 2/5] Tidy files. --- .../ConversationView/ConversationViewController.m | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 3eca21e07..f475a0af1 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3176,14 +3176,6 @@ typedef enum : NSUInteger { }]; } -- (NSURL *)videoTempFolder -{ - NSString *temporaryDirectory = NSTemporaryDirectory(); - NSString *videoDirPath = [temporaryDirectory stringByAppendingPathComponent:@"videos"]; - [OWSFileSystem ensureDirectoryExists:videoDirPath]; - return [NSURL fileURLWithPath:videoDirPath]; -} - - (void)sendQualityAdjustedAttachmentForVideo:(NSURL *)movieURL filename:(NSString *)filename skipApprovalDialog:(BOOL)skipApprovalDialog From e6bc37d9415bd63eab9cd79b4765398e61ccfaee Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 30 Jul 2018 10:56:19 -0400 Subject: [PATCH 3/5] Tidy files. --- ...nversationConfigurationSyncOperation.swift | 6 +-- .../Jobs/MultiDeviceProfileKeyUpdateJob.swift | 4 +- .../ConversationViewController.m | 22 ++++++----- .../ViewControllers/DebugUI/DebugUIMessages.m | 19 +++++---- .../src/ViewControllers/DebugUI/DebugUIMisc.m | 4 +- .../DebugUI/DebugUISyncMessages.m | 9 ++--- .../GifPicker/GifPickerViewController.swift | 3 +- .../ViewControllers/NewGroupViewController.m | 6 +-- .../attachments/SignalAttachment.swift | 18 ++++----- SignalMessaging/contacts/OWSContactsSyncing.m | 4 +- .../src/Messages/OWSMessageManager.m | 12 +++--- SignalServiceKit/src/Util/DataSource.h | 23 ++++++----- SignalServiceKit/src/Util/DataSource.m | 39 +++++++++++-------- .../ShareViewController.swift | 9 +++-- 14 files changed, 98 insertions(+), 80 deletions(-) diff --git a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift index db6e27879..3565ece84 100644 --- a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift +++ b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift @@ -67,8 +67,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { var dataSource: DataSource? = nil self.dbConnection.readWrite { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) - dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) - dataSource?.setShouldDeleteOnDeallocation() + dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData, shouldDeleteOnDeallocation: true) } guard let attachmentDataSource = dataSource else { @@ -89,8 +88,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { var dataSource: DataSource? = nil self.dbConnection.read { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) - dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) - dataSource?.setShouldDeleteOnDeallocation() + dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData, shouldDeleteOnDeallocation: true) } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift index 81d33798e..d976bd922 100644 --- a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift +++ b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift @@ -50,8 +50,8 @@ import SignalMessaging var dataSource: DataSource? = nil self.editingDatabaseConnection.readWrite { transaction in - dataSource = DataSourceValue.dataSource(withSyncMessageData: syncContactsMessage.buildPlainTextAttachmentData(with: transaction)) - dataSource?.setShouldDeleteOnDeallocation() + dataSource = DataSourceValue.dataSource(withSyncMessageData: syncContactsMessage.buildPlainTextAttachmentData(with: transaction), + shouldDeleteOnDeallocation: true) } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index f475a0af1..5f6b20ff9 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -2884,7 +2884,7 @@ typedef enum : NSUInteger { OWSAssert(type); OWSAssert(filename); - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithURL:url]; + DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithURL:url shouldDeleteOnDeallocation:NO]; if (!dataSource) { OWSFail(@"%@ attachment data was unexpectedly empty for picked document url: %@", self.logTag, url); @@ -3099,8 +3099,9 @@ typedef enum : NSUInteger { } OWSAssertIsOnMainThread(); - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:imageData utiType:dataUTI]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:imageData + utiType:dataUTI + shouldDeleteOnDeallocation:YES]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:dataUTI @@ -3186,7 +3187,8 @@ typedef enum : NSUInteger { presentFromViewController:self canCancel:YES backgroundBlock:^(ModalActivityIndicatorViewController *modalActivityIndicator) { - DataSource *dataSource = [DataSourcePath dataSourceWithURL:movieURL]; + DataSource *dataSource = + [DataSourcePath dataSourceWithURL:movieURL shouldDeleteOnDeallocation:NO]; dataSource.sourceFilename = filename; VideoCompressionResult *compressionResult = [SignalAttachment compressVideoAsMp4WithDataSource:dataSource @@ -3667,7 +3669,8 @@ typedef enum : NSUInteger { return; } - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithURL:self.audioRecorder.url]; + DataSource *_Nullable dataSource = + [DataSourcePath dataSourceWithURL:self.audioRecorder.url shouldDeleteOnDeallocation:YES]; self.audioRecorder = nil; if (!dataSource) { @@ -3679,8 +3682,6 @@ typedef enum : NSUInteger { NSString *filename = [NSLocalizedString(@"VOICE_MESSAGE_FILE_NAME", @"Filename for voice messages.") stringByAppendingPathExtension:@"m4a"]; [dataSource setSourceFilename:filename]; - // Remove temporary file when complete. - [dataSource setShouldDeleteOnDeallocation]; SignalAttachment *attachment = [SignalAttachment voiceMessageAttachmentWithDataSource:dataSource dataUTI:(NSString *)kUTTypeMPEG4Audio]; DDLogVerbose(@"%@ voice memo duration: %f, file size: %zd", self.logTag, durationSeconds, [dataSource dataLength]); @@ -3939,8 +3940,8 @@ typedef enum : NSUInteger { if (newGroupModel.groupImage) { NSData *data = UIImagePNGRepresentation(newGroupModel.groupImage); - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [dataSource setShouldDeleteOnDeallocation]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:data fileExtension:@"png" shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message @@ -4432,7 +4433,8 @@ typedef enum : NSUInteger { TSOutgoingMessage *message; if ([text lengthOfBytesUsingEncoding:NSUTF8StringEncoding] >= kOversizeTextMessageSizeThreshold) { - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithOversizeText:text]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithOversizeText:text shouldDeleteOnDeallocation:YES]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:kOversizeTextAttachmentUTI]; // TODO we should redundantly send the first n chars in the body field so it can be viewed diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index d2609948c..32d30c8f5 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -351,7 +351,7 @@ NS_ASSUME_NONNULL_BEGIN OWSMessageSender *messageSender = [Environment current].messageSender; NSString *filename = [filePath lastPathComponent]; NSString *utiType = [MIMETypeUtil utiTypeForFileExtension:filename.pathExtension]; - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath]; + DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath shouldDeleteOnDeallocation:NO]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType imageQuality:TSImageQualityOriginal]; @@ -1701,7 +1701,7 @@ NS_ASSUME_NONNULL_BEGIN NSString *filename = [filePath lastPathComponent]; NSString *utiType = [MIMETypeUtil utiTypeForFileExtension:filename.pathExtension]; - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath]; + DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath shouldDeleteOnDeallocation:NO]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType imageQuality:TSImageQualityOriginal]; @@ -3324,7 +3324,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac { OWSMessageSender *messageSender = [Environment current].messageSender; NSString *message = [self randomOversizeText]; - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithOversizeText:message]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithOversizeText:message shouldDeleteOnDeallocation:YES]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:kOversizeTextAttachmentUTI]; [ThreadUtil sendMessageWithAttachment:attachment @@ -3354,8 +3355,9 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac + (void)sendRandomAttachment:(TSThread *)thread uti:(NSString *)uti length:(NSUInteger)length { OWSMessageSender *messageSender = [Environment current].messageSender; - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:length] utiType:uti]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:length] + utiType:uti + shouldDeleteOnDeallocation:YES]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:uti imageQuality:TSImageQualityOriginal]; @@ -4362,7 +4364,9 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac NSString *utiType = (NSString *)kUTTypeData; const NSUInteger kDataLength = 32; DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:kDataLength] utiType:utiType]; + [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:kDataLength] + utiType:utiType + shouldDeleteOnDeallocation:YES]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType imageQuality:TSImageQualityOriginal]; @@ -4593,7 +4597,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac OWSAssert(transaction); if (isAttachmentDownloaded) { - DataSource *dataSource = [DataSourcePath dataSourceWithFilePath:fakeAssetLoader.filePath]; + DataSource *dataSource = + [DataSourcePath dataSourceWithFilePath:fakeAssetLoader.filePath shouldDeleteOnDeallocation:NO]; NSString *filename = dataSource.sourceFilename; // To support "fake missing" attachments, we sometimes lie about the // length of the data. diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m index 1b3229e41..fb6f36c86 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMisc.m @@ -233,7 +233,7 @@ NS_ASSUME_NONNULL_BEGIN OWSMessageSender *messageSender = [Environment current].messageSender; NSString *utiType = [MIMETypeUtil utiTypeForFileExtension:fileName.pathExtension]; - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath]; + DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath shouldDeleteOnDeallocation:YES]; [dataSource setSourceFilename:fileName]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType]; NSData *databasePassword = [OWSPrimaryStorage.sharedManager databasePassword]; @@ -262,7 +262,7 @@ NS_ASSUME_NONNULL_BEGIN OWSMessageSender *messageSender = [Environment current].messageSender; NSString *utiType = [MIMETypeUtil utiTypeForFileExtension:fileName.pathExtension]; - DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath]; + DataSource *_Nullable dataSource = [DataSourcePath dataSourceWithFilePath:filePath shouldDeleteOnDeallocation:YES]; [dataSource setSourceFilename:fileName]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType]; if (!attachment || [attachment hasError]) { diff --git a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m index c8159e032..b14e095f8 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m @@ -102,9 +102,8 @@ NS_ASSUME_NONNULL_BEGIN __block DataSource *dataSource; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { dataSource = [DataSourceValue - dataSourceWithSyncMessageData:[syncContactsMessage - buildPlainTextAttachmentDataWithTransaction:transaction]]; - [dataSource setShouldDeleteOnDeallocation]; + dataSourceWithSyncMessageData:[syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction] + shouldDeleteOnDeallocation:YES]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource @@ -124,8 +123,8 @@ NS_ASSUME_NONNULL_BEGIN __block DataSource *dataSource; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { dataSource = [DataSourceValue - dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; - [dataSource setShouldDeleteOnDeallocation]; + dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction] + shouldDeleteOnDeallocation:YES]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream diff --git a/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift b/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift index 537fc9064..b4bbf8f4c 100644 --- a/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift +++ b/Signal/src/ViewControllers/GifPicker/GifPickerViewController.swift @@ -370,7 +370,8 @@ class GifPickerViewController: OWSViewController, UISearchBarDelegate, UICollect } let filePath = asset.filePath - guard let dataSource = DataSourcePath.dataSource(withFilePath: filePath) else { + guard let dataSource = DataSourcePath.dataSource(withFilePath: filePath, + shouldDeleteOnDeallocation: false) else { owsFail("\(strongSelf.TAG) couldn't load asset.") return } diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index ed6bc9b39..70ccc4845 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -488,9 +488,9 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (model.groupImage) { NSData *data = UIImagePNGRepresentation(model.groupImage); - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [dataSource setShouldDeleteOnDeallocation]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data + fileExtension:@"png" + shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message diff --git a/SignalMessaging/attachments/SignalAttachment.swift b/SignalMessaging/attachments/SignalAttachment.swift index 147b18ae2..ba6020853 100644 --- a/SignalMessaging/attachments/SignalAttachment.swift +++ b/SignalMessaging/attachments/SignalAttachment.swift @@ -519,7 +519,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) // Pasted images _SHOULD _NOT_ be resized, if possible. return attachment(dataSource: dataSource, dataUTI: dataUTI, imageQuality: .medium) } @@ -530,7 +530,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) return videoAttachment(dataSource: dataSource, dataUTI: dataUTI) } } @@ -540,7 +540,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) return audioAttachment(dataSource: dataSource, dataUTI: dataUTI) } } @@ -550,7 +550,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) return genericAttachment(dataSource: dataSource, dataUTI: dataUTI) } @@ -723,7 +723,7 @@ public class SignalAttachment: NSObject { return attachment } - guard let dataSource = DataSourceValue.dataSource(with: jpgImageData, fileExtension: "jpg") else { + guard let dataSource = DataSourceValue.dataSource(with: jpgImageData, fileExtension: "jpg", shouldDeleteOnDeallocation: false) else { attachment.error = .couldNotConvertToJpeg return attachment } @@ -892,7 +892,7 @@ public class SignalAttachment: NSObject { } if CGImageDestinationFinalize(destination) { - guard let dataSource = DataSourceValue.dataSource(with: mutableData as Data, utiType: attachment.dataUTI) else { + guard let dataSource = DataSourceValue.dataSource(with: mutableData as Data, utiType: attachment.dataUTI, shouldDeleteOnDeallocation: false) else { attachment.error = .couldNotRemoveMetadata return attachment } @@ -980,7 +980,8 @@ public class SignalAttachment: NSObject { let baseFilename = dataSource.sourceFilename let mp4Filename = baseFilename?.filenameWithoutExtension.appendingFileExtension("mp4") - guard let dataSource = DataSourcePath.dataSource(with: exportURL) else { + guard let dataSource = DataSourcePath.dataSource(with: exportURL, + shouldDeleteOnDeallocation: true) else { owsFail("Failed to build data source for exported video URL") let attachment = SignalAttachment(dataSource: DataSourceValue.emptyDataSource(), dataUTI: dataUTI) attachment.error = .couldNotConvertToMpeg4 @@ -988,7 +989,6 @@ public class SignalAttachment: NSObject { return } - dataSource.setShouldDeleteOnDeallocation() dataSource.sourceFilename = mp4Filename let attachment = SignalAttachment(dataSource: dataSource, dataUTI: kUTTypeMPEG4 as String) @@ -1070,7 +1070,7 @@ public class SignalAttachment: NSObject { // NOTE: The attachment returned by this method may not be valid. // Check the attachment's error property. private class func oversizeTextAttachment(text: String?) -> SignalAttachment { - let dataSource = DataSourceValue.dataSource(withOversizeText: text) + let dataSource = DataSourceValue.dataSource(withOversizeText: text, shouldDeleteOnDeallocation: false) return newAttachment(dataSource: dataSource, dataUTI: kOversizeTextAttachmentUTI, validUTISet: nil, diff --git a/SignalMessaging/contacts/OWSContactsSyncing.m b/SignalMessaging/contacts/OWSContactsSyncing.m index 77ff77376..efc9aaca4 100644 --- a/SignalMessaging/contacts/OWSContactsSyncing.m +++ b/SignalMessaging/contacts/OWSContactsSyncing.m @@ -139,8 +139,8 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey self.isRequestInFlight = YES; - DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:messageData]; - [dataSource setShouldDeleteOnDeallocation]; + DataSource *dataSource = + [DataSourceValue dataSourceWithSyncMessageData:messageData shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index ec91be68f..d46089d3a 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -657,8 +657,8 @@ NS_ASSUME_NONNULL_BEGIN profileManager:self.profileManager]; DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncContactsMessage - buildPlainTextAttachmentDataWithTransaction:transaction]]; - [dataSource setShouldDeleteOnDeallocation]; + buildPlainTextAttachmentDataWithTransaction:transaction] + shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage @@ -674,8 +674,8 @@ NS_ASSUME_NONNULL_BEGIN OWSSyncGroupsMessage *syncGroupsMessage = [[OWSSyncGroupsMessage alloc] init]; DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncGroupsMessage - buildPlainTextAttachmentDataWithTransaction:transaction]]; - [dataSource setShouldDeleteOnDeallocation]; + buildPlainTextAttachmentDataWithTransaction:transaction] + shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncGroupsMessage @@ -826,8 +826,8 @@ NS_ASSUME_NONNULL_BEGIN if (gThread.groupModel.groupImage) { NSData *data = UIImagePNGRepresentation(gThread.groupModel.groupImage); - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; - [dataSource setShouldDeleteOnDeallocation]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:data fileExtension:@"png" shouldDeleteOnDeallocation:YES]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message diff --git a/SignalServiceKit/src/Util/DataSource.h b/SignalServiceKit/src/Util/DataSource.h index c65d693c8..c2fe276b7 100755 --- a/SignalServiceKit/src/Util/DataSource.h +++ b/SignalServiceKit/src/Util/DataSource.h @@ -29,10 +29,6 @@ NS_ASSUME_NONNULL_BEGIN // Returns YES on success. - (BOOL)writeToPath:(NSString *)dstFilePath; -// If called, this data source will try to delete its on-disk contents -// when it is deallocated. -- (void)setShouldDeleteOnDeallocation; - - (BOOL)isValidImage; @end @@ -41,13 +37,19 @@ NS_ASSUME_NONNULL_BEGIN @interface DataSourceValue : DataSource -+ (nullable DataSource *)dataSourceWithData:(NSData *)data fileExtension:(NSString *)fileExtension; ++ (nullable DataSource *)dataSourceWithData:(NSData *)data + fileExtension:(NSString *)fileExtension + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; -+ (nullable DataSource *)dataSourceWithData:(NSData *)data utiType:(NSString *)utiType; ++ (nullable DataSource *)dataSourceWithData:(NSData *)data + utiType:(NSString *)utiType + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; -+ (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text; ++ (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; -+ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data; ++ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; + (DataSource *)emptyDataSource; @@ -57,9 +59,10 @@ NS_ASSUME_NONNULL_BEGIN @interface DataSourcePath : DataSource -+ (nullable DataSource *)dataSourceWithURL:(NSURL *)fileUrl; ++ (nullable DataSource *)dataSourceWithURL:(NSURL *)fileUrl shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; -+ (nullable DataSource *)dataSourceWithFilePath:(NSString *)filePath; ++ (nullable DataSource *)dataSourceWithFilePath:(NSString *)filePath + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; @end diff --git a/SignalServiceKit/src/Util/DataSource.m b/SignalServiceKit/src/Util/DataSource.m index 82dd3964b..65a08efb0 100755 --- a/SignalServiceKit/src/Util/DataSource.m +++ b/SignalServiceKit/src/Util/DataSource.m @@ -60,11 +60,6 @@ NS_ASSUME_NONNULL_BEGIN return NO; } -- (void)setShouldDeleteOnDeallocation -{ - self.shouldDeleteOnDeallocation = YES; -} - - (BOOL)isValidImage { NSString *_Nullable dataPath = [self dataPathIfOnDisk]; @@ -126,7 +121,9 @@ NS_ASSUME_NONNULL_BEGIN } } -+ (nullable DataSource *)dataSourceWithData:(NSData *)data fileExtension:(NSString *)fileExtension ++ (nullable DataSource *)dataSourceWithData:(NSData *)data + fileExtension:(NSString *)fileExtension + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { OWSAssert(data); @@ -137,35 +134,43 @@ NS_ASSUME_NONNULL_BEGIN DataSourceValue *instance = [DataSourceValue new]; instance.dataValue = data; instance.fileExtension = fileExtension; - // Always try to clean up temp files created by this instance. - [instance setShouldDeleteOnDeallocation]; + instance.shouldDeleteOnDeallocation = shouldDeleteOnDeallocation; return instance; } -+ (nullable DataSource *)dataSourceWithData:(NSData *)data utiType:(NSString *)utiType ++ (nullable DataSource *)dataSourceWithData:(NSData *)data + utiType:(NSString *)utiType + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { NSString *fileExtension = [MIMETypeUtil fileExtensionForUTIType:utiType]; - return [self dataSourceWithData:data fileExtension:fileExtension]; + return [self dataSourceWithData:data + fileExtension:fileExtension + shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; } + (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { if (!text) { return nil; } NSData *data = [text.filterStringForDisplay dataUsingEncoding:NSUTF8StringEncoding]; - return [self dataSourceWithData:data fileExtension:kOversizeTextAttachmentFileExtension]; + return [self dataSourceWithData:data + fileExtension:kOversizeTextAttachmentFileExtension + shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; } -+ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data ++ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { - return [self dataSourceWithData:data fileExtension:kSyncMessageFileExtension]; + return [self dataSourceWithData:data + fileExtension:kSyncMessageFileExtension + shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; } + (DataSource *)emptyDataSource { - return [self dataSourceWithData:[NSData new] fileExtension:@"bin"]; + return [self dataSourceWithData:[NSData new] fileExtension:@"bin" shouldDeleteOnDeallocation:YES]; } - (NSData *)data @@ -269,7 +274,7 @@ NS_ASSUME_NONNULL_BEGIN } } -+ (nullable DataSource *)dataSourceWithURL:(NSURL *)fileUrl ++ (nullable DataSource *)dataSourceWithURL:(NSURL *)fileUrl shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { OWSAssert(fileUrl); @@ -278,10 +283,12 @@ NS_ASSUME_NONNULL_BEGIN } DataSourcePath *instance = [DataSourcePath new]; instance.filePath = fileUrl.path; + instance.shouldDeleteOnDeallocation = shouldDeleteOnDeallocation; return instance; } + (nullable DataSource *)dataSourceWithFilePath:(NSString *)filePath + shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { OWSAssert(filePath); @@ -291,7 +298,7 @@ NS_ASSUME_NONNULL_BEGIN DataSourcePath *instance = [DataSourcePath new]; instance.filePath = filePath; - OWSAssert(!instance.shouldDeleteOnDeallocation); + instance.shouldDeleteOnDeallocation = shouldDeleteOnDeallocation; return instance; } diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift index 12a65e588..7af487377 100644 --- a/SignalShareExtension/ShareViewController.swift +++ b/SignalShareExtension/ShareViewController.swift @@ -634,15 +634,18 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed // NOTE: SharingThreadPickerViewController will try to unpack them // and send them as normal text messages if possible. let urlString = url.absoluteString - return DataSourceValue.dataSource(withOversizeText: urlString) + return DataSourceValue.dataSource(withOversizeText: urlString, + shouldDeleteOnDeallocation: true) } else if UTTypeConformsTo(utiType as CFString, kUTTypeText) { // Share text as oversize text messages. // // NOTE: SharingThreadPickerViewController will try to unpack them // and send them as normal text messages if possible. - return DataSourcePath.dataSource(with: url) + return DataSourcePath.dataSource(with: url, + shouldDeleteOnDeallocation: false) } else { - guard let dataSource = DataSourcePath.dataSource(with: url) else { + guard let dataSource = DataSourcePath.dataSource(with: url, + shouldDeleteOnDeallocation: false) else { return nil } From dfc39b4a1d7cc114345e95b67058b6458dc93ce5 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Mon, 30 Jul 2018 11:00:56 -0400 Subject: [PATCH 4/5] Tidy files. --- ...nversationConfigurationSyncOperation.swift | 4 ++-- .../Jobs/MultiDeviceProfileKeyUpdateJob.swift | 3 +-- .../ConversationViewController.m | 11 ++++------ .../ViewControllers/DebugUI/DebugUIMessages.m | 12 ++++------- .../DebugUI/DebugUISyncMessages.m | 7 +++---- .../ViewControllers/NewGroupViewController.m | 5 ++--- .../attachments/SignalAttachment.swift | 14 ++++++------- SignalMessaging/contacts/OWSContactsSyncing.m | 3 +-- .../src/Messages/OWSMessageManager.m | 9 +++----- SignalServiceKit/src/Util/DataSource.h | 14 ++++--------- SignalServiceKit/src/Util/DataSource.m | 21 ++++++------------- .../ShareViewController.swift | 3 +-- 12 files changed, 38 insertions(+), 68 deletions(-) diff --git a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift index 3565ece84..6c7681073 100644 --- a/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift +++ b/Signal/src/Jobs/ConversationConfigurationSyncOperation.swift @@ -67,7 +67,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { var dataSource: DataSource? = nil self.dbConnection.readWrite { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) - dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData, shouldDeleteOnDeallocation: true) + dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) } guard let attachmentDataSource = dataSource else { @@ -88,7 +88,7 @@ class ConversationConfigurationSyncOperation: OWSOperation { var dataSource: DataSource? = nil self.dbConnection.read { transaction in let messageData: Data = syncMessage.buildPlainTextAttachmentData(with: transaction) - dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData, shouldDeleteOnDeallocation: true) + dataSource = DataSourceValue.dataSource(withSyncMessageData: messageData) } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift index d976bd922..c8f3df93e 100644 --- a/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift +++ b/Signal/src/Jobs/MultiDeviceProfileKeyUpdateJob.swift @@ -50,8 +50,7 @@ import SignalMessaging var dataSource: DataSource? = nil self.editingDatabaseConnection.readWrite { transaction in - dataSource = DataSourceValue.dataSource(withSyncMessageData: syncContactsMessage.buildPlainTextAttachmentData(with: transaction), - shouldDeleteOnDeallocation: true) + dataSource = DataSourceValue.dataSource(withSyncMessageData: syncContactsMessage.buildPlainTextAttachmentData(with: transaction)) } guard let attachmentDataSource = dataSource else { diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 5f6b20ff9..d8df0518c 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -3099,9 +3099,8 @@ typedef enum : NSUInteger { } OWSAssertIsOnMainThread(); - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:imageData - utiType:dataUTI - shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:imageData utiType:dataUTI]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:dataUTI @@ -3940,8 +3939,7 @@ typedef enum : NSUInteger { if (newGroupModel.groupImage) { NSData *data = UIImagePNGRepresentation(newGroupModel.groupImage); - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:data fileExtension:@"png" shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message @@ -4433,8 +4431,7 @@ typedef enum : NSUInteger { TSOutgoingMessage *message; if ([text lengthOfBytesUsingEncoding:NSUTF8StringEncoding] >= kOversizeTextMessageSizeThreshold) { - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithOversizeText:text shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithOversizeText:text]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:kOversizeTextAttachmentUTI]; // TODO we should redundantly send the first n chars in the body field so it can be viewed diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 32d30c8f5..7648b91c8 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -3324,8 +3324,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac { OWSMessageSender *messageSender = [Environment current].messageSender; NSString *message = [self randomOversizeText]; - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithOversizeText:message shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithOversizeText:message]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:kOversizeTextAttachmentUTI]; [ThreadUtil sendMessageWithAttachment:attachment @@ -3355,9 +3354,8 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac + (void)sendRandomAttachment:(TSThread *)thread uti:(NSString *)uti length:(NSUInteger)length { OWSMessageSender *messageSender = [Environment current].messageSender; - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:length] - utiType:uti - shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:length] utiType:uti]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:uti imageQuality:TSImageQualityOriginal]; @@ -4364,9 +4362,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac NSString *utiType = (NSString *)kUTTypeData; const NSUInteger kDataLength = 32; DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:kDataLength] - utiType:utiType - shouldDeleteOnDeallocation:YES]; + [DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:kDataLength] utiType:utiType]; [dataSource setSourceFilename:filename]; SignalAttachment *attachment = [SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType imageQuality:TSImageQualityOriginal]; diff --git a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m index b14e095f8..3cf600cef 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUISyncMessages.m @@ -102,8 +102,8 @@ NS_ASSUME_NONNULL_BEGIN __block DataSource *dataSource; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { dataSource = [DataSourceValue - dataSourceWithSyncMessageData:[syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction] - shouldDeleteOnDeallocation:YES]; + dataSourceWithSyncMessageData:[syncContactsMessage + buildPlainTextAttachmentDataWithTransaction:transaction]]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource @@ -123,8 +123,7 @@ NS_ASSUME_NONNULL_BEGIN __block DataSource *dataSource; [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { dataSource = [DataSourceValue - dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction] - shouldDeleteOnDeallocation:YES]; + dataSourceWithSyncMessageData:[syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction]]; }]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream diff --git a/Signal/src/ViewControllers/NewGroupViewController.m b/Signal/src/ViewControllers/NewGroupViewController.m index 70ccc4845..26d7681f4 100644 --- a/Signal/src/ViewControllers/NewGroupViewController.m +++ b/Signal/src/ViewControllers/NewGroupViewController.m @@ -488,9 +488,8 @@ const NSUInteger kNewGroupViewControllerAvatarWidth = 68; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (model.groupImage) { NSData *data = UIImagePNGRepresentation(model.groupImage); - DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data - fileExtension:@"png" - shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = + [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message diff --git a/SignalMessaging/attachments/SignalAttachment.swift b/SignalMessaging/attachments/SignalAttachment.swift index ba6020853..1382e5385 100644 --- a/SignalMessaging/attachments/SignalAttachment.swift +++ b/SignalMessaging/attachments/SignalAttachment.swift @@ -519,7 +519,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) // Pasted images _SHOULD _NOT_ be resized, if possible. return attachment(dataSource: dataSource, dataUTI: dataUTI, imageQuality: .medium) } @@ -530,7 +530,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) return videoAttachment(dataSource: dataSource, dataUTI: dataUTI) } } @@ -540,7 +540,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) return audioAttachment(dataSource: dataSource, dataUTI: dataUTI) } } @@ -550,7 +550,7 @@ public class SignalAttachment: NSObject { owsFail("\(logTag) Missing expected pasteboard data for UTI: \(dataUTI)") return nil } - let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI, shouldDeleteOnDeallocation: false) + let dataSource = DataSourceValue.dataSource(with: data, utiType: dataUTI) return genericAttachment(dataSource: dataSource, dataUTI: dataUTI) } @@ -723,7 +723,7 @@ public class SignalAttachment: NSObject { return attachment } - guard let dataSource = DataSourceValue.dataSource(with: jpgImageData, fileExtension: "jpg", shouldDeleteOnDeallocation: false) else { + guard let dataSource = DataSourceValue.dataSource(with: jpgImageData, fileExtension: "jpg") else { attachment.error = .couldNotConvertToJpeg return attachment } @@ -892,7 +892,7 @@ public class SignalAttachment: NSObject { } if CGImageDestinationFinalize(destination) { - guard let dataSource = DataSourceValue.dataSource(with: mutableData as Data, utiType: attachment.dataUTI, shouldDeleteOnDeallocation: false) else { + guard let dataSource = DataSourceValue.dataSource(with: mutableData as Data, utiType: attachment.dataUTI) else { attachment.error = .couldNotRemoveMetadata return attachment } @@ -1070,7 +1070,7 @@ public class SignalAttachment: NSObject { // NOTE: The attachment returned by this method may not be valid. // Check the attachment's error property. private class func oversizeTextAttachment(text: String?) -> SignalAttachment { - let dataSource = DataSourceValue.dataSource(withOversizeText: text, shouldDeleteOnDeallocation: false) + let dataSource = DataSourceValue.dataSource(withOversizeText: text) return newAttachment(dataSource: dataSource, dataUTI: kOversizeTextAttachmentUTI, validUTISet: nil, diff --git a/SignalMessaging/contacts/OWSContactsSyncing.m b/SignalMessaging/contacts/OWSContactsSyncing.m index efc9aaca4..dbd5f4fd0 100644 --- a/SignalMessaging/contacts/OWSContactsSyncing.m +++ b/SignalMessaging/contacts/OWSContactsSyncing.m @@ -139,8 +139,7 @@ NSString *const kOWSPrimaryStorageOWSContactsSyncingLastMessageKey self.isRequestInFlight = YES; - DataSource *dataSource = - [DataSourceValue dataSourceWithSyncMessageData:messageData shouldDeleteOnDeallocation:YES]; + DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:messageData]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index d46089d3a..db16d84bd 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -657,8 +657,7 @@ NS_ASSUME_NONNULL_BEGIN profileManager:self.profileManager]; DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncContactsMessage - buildPlainTextAttachmentDataWithTransaction:transaction] - shouldDeleteOnDeallocation:YES]; + buildPlainTextAttachmentDataWithTransaction:transaction]]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncContactsMessage @@ -674,8 +673,7 @@ NS_ASSUME_NONNULL_BEGIN OWSSyncGroupsMessage *syncGroupsMessage = [[OWSSyncGroupsMessage alloc] init]; DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:[syncGroupsMessage - buildPlainTextAttachmentDataWithTransaction:transaction] - shouldDeleteOnDeallocation:YES]; + buildPlainTextAttachmentDataWithTransaction:transaction]]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeApplicationOctetStream inMessage:syncGroupsMessage @@ -826,8 +824,7 @@ NS_ASSUME_NONNULL_BEGIN if (gThread.groupModel.groupImage) { NSData *data = UIImagePNGRepresentation(gThread.groupModel.groupImage); - DataSource *_Nullable dataSource = - [DataSourceValue dataSourceWithData:data fileExtension:@"png" shouldDeleteOnDeallocation:YES]; + DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"]; [self.messageSender enqueueTemporaryAttachment:dataSource contentType:OWSMimeTypeImagePng inMessage:message diff --git a/SignalServiceKit/src/Util/DataSource.h b/SignalServiceKit/src/Util/DataSource.h index c2fe276b7..74ba0fc25 100755 --- a/SignalServiceKit/src/Util/DataSource.h +++ b/SignalServiceKit/src/Util/DataSource.h @@ -37,19 +37,13 @@ NS_ASSUME_NONNULL_BEGIN @interface DataSourceValue : DataSource -+ (nullable DataSource *)dataSourceWithData:(NSData *)data - fileExtension:(NSString *)fileExtension - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; ++ (nullable DataSource *)dataSourceWithData:(NSData *)data fileExtension:(NSString *)fileExtension; -+ (nullable DataSource *)dataSourceWithData:(NSData *)data - utiType:(NSString *)utiType - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; ++ (nullable DataSource *)dataSourceWithData:(NSData *)data utiType:(NSString *)utiType; -+ (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; ++ (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text; -+ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation; ++ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data; + (DataSource *)emptyDataSource; diff --git a/SignalServiceKit/src/Util/DataSource.m b/SignalServiceKit/src/Util/DataSource.m index 65a08efb0..fcebd4511 100755 --- a/SignalServiceKit/src/Util/DataSource.m +++ b/SignalServiceKit/src/Util/DataSource.m @@ -123,7 +123,6 @@ NS_ASSUME_NONNULL_BEGIN + (nullable DataSource *)dataSourceWithData:(NSData *)data fileExtension:(NSString *)fileExtension - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { OWSAssert(data); @@ -134,43 +133,35 @@ NS_ASSUME_NONNULL_BEGIN DataSourceValue *instance = [DataSourceValue new]; instance.dataValue = data; instance.fileExtension = fileExtension; - instance.shouldDeleteOnDeallocation = shouldDeleteOnDeallocation; + instance.shouldDeleteOnDeallocation = YES; return instance; } + (nullable DataSource *)dataSourceWithData:(NSData *)data utiType:(NSString *)utiType - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { NSString *fileExtension = [MIMETypeUtil fileExtensionForUTIType:utiType]; - return [self dataSourceWithData:data - fileExtension:fileExtension - shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; + return [self dataSourceWithData:data fileExtension:fileExtension]; } + (nullable DataSource *)dataSourceWithOversizeText:(NSString *_Nullable)text - shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation { if (!text) { return nil; } NSData *data = [text.filterStringForDisplay dataUsingEncoding:NSUTF8StringEncoding]; - return [self dataSourceWithData:data - fileExtension:kOversizeTextAttachmentFileExtension - shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; + return [self dataSourceWithData:data fileExtension:kOversizeTextAttachmentFileExtension]; } -+ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data shouldDeleteOnDeallocation:(BOOL)shouldDeleteOnDeallocation ++ (DataSource *)dataSourceWithSyncMessageData:(NSData *)data { - return [self dataSourceWithData:data - fileExtension:kSyncMessageFileExtension - shouldDeleteOnDeallocation:shouldDeleteOnDeallocation]; + return [self dataSourceWithData:data fileExtension:kSyncMessageFileExtension]; } + (DataSource *)emptyDataSource { - return [self dataSourceWithData:[NSData new] fileExtension:@"bin" shouldDeleteOnDeallocation:YES]; + return [self dataSourceWithData:[NSData new] fileExtension:@"bin"]; } - (NSData *)data diff --git a/SignalShareExtension/ShareViewController.swift b/SignalShareExtension/ShareViewController.swift index 7af487377..932aa22e3 100644 --- a/SignalShareExtension/ShareViewController.swift +++ b/SignalShareExtension/ShareViewController.swift @@ -634,8 +634,7 @@ public class ShareViewController: UIViewController, ShareViewDelegate, SAEFailed // NOTE: SharingThreadPickerViewController will try to unpack them // and send them as normal text messages if possible. let urlString = url.absoluteString - return DataSourceValue.dataSource(withOversizeText: urlString, - shouldDeleteOnDeallocation: true) + return DataSourceValue.dataSource(withOversizeText: urlString) } else if UTTypeConformsTo(utiType as CFString, kUTTypeText) { // Share text as oversize text messages. // From c15ddf85f86459c36d7974bafc884ffbc7f4bdc7 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 1 Aug 2018 09:47:54 -0400 Subject: [PATCH 5/5] Respond to CR. --- .../src/Messages/Interactions/TSOutgoingMessage.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 53e81b7bf..8f1f96a06 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -356,17 +356,17 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt - (void)dealloc { - [self cleanUpTemporaryAttachmentsIfNecessary]; + [self removeTemporaryAttachments]; } // Each message has the responsibility for eagerly cleaning up its attachments. // Normally this is done in [TSMessage removeWithTransaction], but that doesn't // apply for "transient", unsaved messages (i.e. shouldBeSaved == NO). These // messages should clean up their attachments upon deallocation. -- (void)cleanUpTemporaryAttachmentsIfNecessary +- (void)removeTemporaryAttachments { if (self.shouldBeSaved) { - // Message in not transient; no need to clean up attachments. + // Message is not transient; no need to clean up attachments. return; } NSArray *_Nullable attachmentIds = self.attachmentIds;