2017-09-13 18:49:19 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "OWSBatchMessageProcessor.h"
|
2017-09-20 17:48:37 +02:00
|
|
|
#import "NSArray+OWS.h"
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "OWSMessageManager.h"
|
2017-09-21 20:36:52 +02:00
|
|
|
#import "OWSQueues.h"
|
2017-09-13 18:49:19 +02:00
|
|
|
#import "OWSSignalServiceProtos.pb.h"
|
|
|
|
#import "TSDatabaseView.h"
|
|
|
|
#import "TSStorageManager.h"
|
|
|
|
#import "TSYapDatabaseObject.h"
|
2017-09-13 21:35:33 +02:00
|
|
|
#import "Threading.h"
|
2017-09-13 18:49:19 +02:00
|
|
|
#import <YapDatabase/YapDatabaseConnection.h>
|
|
|
|
#import <YapDatabase/YapDatabaseTransaction.h>
|
|
|
|
#import <YapDatabase/YapDatabaseView.h>
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
#pragma mark - Persisted data model
|
|
|
|
|
|
|
|
@class OWSSignalServiceProtosEnvelope;
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageContentJob : TSYapDatabaseObject
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSDate *createdAt;
|
|
|
|
|
|
|
|
- (instancetype)initWithEnvelopeData:(NSData *)envelopeData
|
|
|
|
plaintextData:(NSData *_Nullable)plaintextData NS_DESIGNATED_INITIALIZER;
|
|
|
|
- (instancetype)initWithUniqueId:(NSString *)uniqueId NS_UNAVAILABLE;
|
|
|
|
- (OWSSignalServiceProtosEnvelope *)envelopeProto;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageContentJob ()
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSData *envelopeData;
|
|
|
|
@property (nonatomic, readonly, nullable) NSData *plaintextData;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageContentJob
|
|
|
|
|
|
|
|
+ (NSString *)collection
|
|
|
|
{
|
|
|
|
return @"OWSBatchMessageProcessingJob";
|
|
|
|
}
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
- (instancetype)initWithEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
{
|
|
|
|
OWSAssert(envelopeData);
|
|
|
|
|
|
|
|
self = [super initWithUniqueId:[NSUUID new].UUIDString];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_envelopeData = envelopeData;
|
|
|
|
_plaintextData = plaintextData;
|
|
|
|
_createdAt = [NSDate new];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSSignalServiceProtosEnvelope *)envelopeProto
|
|
|
|
{
|
|
|
|
return [OWSSignalServiceProtosEnvelope parseFromData:self.envelopeData];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Finder
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
NSString *const OWSMessageContentJobFinderExtensionName = @"OWSBatchMessageProcessingFinderExtensionName";
|
|
|
|
NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSBatchMessageProcessingFinderExtensionGroup";
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageContentJobFinder : NSObject
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageContentJobFinder ()
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageContentJobFinder
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
- (instancetype)initWithDBConnection:(YapDatabaseConnection *)dbConnection
|
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dbConnection = dbConnection;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
- (NSArray<OWSMessageContentJob *> *)nextJobsForBatchSize:(NSUInteger)maxBatchSize
|
2017-09-13 18:49:19 +02:00
|
|
|
{
|
2017-09-20 17:48:37 +02:00
|
|
|
NSMutableArray<OWSMessageContentJob *> *jobs = [NSMutableArray new];
|
2017-09-13 18:49:19 +02:00
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) {
|
2017-09-20 17:48:37 +02:00
|
|
|
YapDatabaseViewTransaction *viewTransaction = [transaction ext:OWSMessageContentJobFinderExtensionName];
|
2017-09-13 18:49:19 +02:00
|
|
|
OWSAssert(viewTransaction != nil);
|
2017-09-20 17:48:37 +02:00
|
|
|
[viewTransaction enumerateKeysAndObjectsInGroup:OWSMessageContentJobFinderExtensionGroup
|
|
|
|
usingBlock:^(NSString *_Nonnull collection,
|
|
|
|
NSString *_Nonnull key,
|
|
|
|
id _Nonnull object,
|
|
|
|
NSUInteger index,
|
|
|
|
BOOL *_Nonnull stop) {
|
|
|
|
OWSMessageContentJob *job = object;
|
|
|
|
[jobs addObject:job];
|
|
|
|
if (jobs.count >= maxBatchSize) {
|
|
|
|
*stop = YES;
|
|
|
|
}
|
|
|
|
}];
|
2017-09-13 18:49:19 +02:00
|
|
|
}];
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
return [jobs copy];
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addJobWithEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
{
|
2017-09-21 19:46:30 +02:00
|
|
|
// We need to persist the decrypted envelope data ASAP to prevent data loss.
|
2017-09-13 18:49:19 +02:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
2017-09-21 17:55:25 +02:00
|
|
|
OWSMessageContentJob *job =
|
|
|
|
[[OWSMessageContentJob alloc] initWithEnvelopeData:envelopeData plaintextData:plaintextData];
|
|
|
|
[job saveWithTransaction:transaction];
|
2017-09-13 18:49:19 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
- (void)removeJobsWithIds:(NSArray<NSString *> *)uniqueIds
|
2017-09-13 18:49:19 +02:00
|
|
|
{
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
|
2017-09-20 17:48:37 +02:00
|
|
|
[transaction removeObjectsForKeys:uniqueIds inCollection:[OWSMessageContentJob collection]];
|
2017-09-13 18:49:19 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (YapDatabaseView *)databaseExtension
|
|
|
|
{
|
|
|
|
YapDatabaseViewSorting *sorting =
|
|
|
|
[YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(YapDatabaseReadTransaction *transaction,
|
|
|
|
NSString *group,
|
|
|
|
NSString *collection1,
|
|
|
|
NSString *key1,
|
|
|
|
id object1,
|
|
|
|
NSString *collection2,
|
|
|
|
NSString *key2,
|
|
|
|
id object2) {
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object1 isKindOfClass:[OWSMessageContentJob class]]) {
|
2017-09-13 18:49:19 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", [object1 class], collection1);
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageContentJob *job1 = (OWSMessageContentJob *)object1;
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object2 isKindOfClass:[OWSMessageContentJob class]]) {
|
2017-09-13 18:49:19 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", [object2 class], collection2);
|
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageContentJob *job2 = (OWSMessageContentJob *)object2;
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
return [job1.createdAt compare:job2.createdAt];
|
|
|
|
}];
|
|
|
|
|
|
|
|
YapDatabaseViewGrouping *grouping =
|
|
|
|
[YapDatabaseViewGrouping withObjectBlock:^NSString *_Nullable(YapDatabaseReadTransaction *_Nonnull transaction,
|
|
|
|
NSString *_Nonnull collection,
|
|
|
|
NSString *_Nonnull key,
|
|
|
|
id _Nonnull object) {
|
2017-09-20 17:48:37 +02:00
|
|
|
if (![object isKindOfClass:[OWSMessageContentJob class]]) {
|
2017-09-13 18:49:19 +02:00
|
|
|
OWSFail(@"Unexpected object: %@ in collection: %@", object, collection);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arbitrary string - all in the same group. We're only using the view for sorting.
|
2017-09-20 17:48:37 +02:00
|
|
|
return OWSMessageContentJobFinderExtensionGroup;
|
2017-09-13 18:49:19 +02:00
|
|
|
}];
|
|
|
|
|
|
|
|
YapDatabaseViewOptions *options = [YapDatabaseViewOptions new];
|
2017-09-20 17:48:37 +02:00
|
|
|
options.allowedCollections =
|
|
|
|
[[YapWhitelistBlacklist alloc] initWithWhitelist:[NSSet setWithObject:[OWSMessageContentJob collection]]];
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
return [[YapDatabaseView alloc] initWithGrouping:grouping sorting:sorting versionTag:@"1" options:options];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)syncRegisterDatabaseExtension:(YapDatabase *)database
|
|
|
|
{
|
2017-09-20 17:48:37 +02:00
|
|
|
YapDatabaseView *existingView = [database registeredExtension:OWSMessageContentJobFinderExtensionName];
|
2017-09-13 18:49:19 +02:00
|
|
|
if (existingView) {
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSFail(@"%@ was already initialized.", OWSMessageContentJobFinderExtensionName);
|
2017-09-13 18:49:19 +02:00
|
|
|
// already initialized
|
|
|
|
return;
|
|
|
|
}
|
2017-09-20 17:48:37 +02:00
|
|
|
[database registerExtension:[self databaseExtension] withName:OWSMessageContentJobFinderExtensionName];
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 17:55:25 +02:00
|
|
|
#pragma mark Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - Queue Processing
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@interface OWSMessageContentQueue : NSObject
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageManager *messagesManager;
|
2017-09-13 21:35:33 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbReadWriteConnection;
|
2017-09-20 17:48:37 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageContentJobFinder *finder;
|
2017-09-13 18:49:19 +02:00
|
|
|
@property (nonatomic) BOOL isDrainingQueue;
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (instancetype)initWithMessagesManager:(OWSMessageManager *)messagesManager
|
2017-09-13 21:35:33 +02:00
|
|
|
storageManager:(TSStorageManager *)storageManager
|
2017-09-20 17:48:37 +02:00
|
|
|
finder:(OWSMessageContentJobFinder *)finder NS_DESIGNATED_INITIALIZER;
|
2017-09-13 18:49:19 +02:00
|
|
|
- (instancetype)init NS_UNAVAILABLE;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@implementation OWSMessageContentQueue
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (instancetype)initWithMessagesManager:(OWSMessageManager *)messagesManager
|
2017-09-13 21:35:33 +02:00
|
|
|
storageManager:(TSStorageManager *)storageManager
|
2017-09-20 17:48:37 +02:00
|
|
|
finder:(OWSMessageContentJobFinder *)finder
|
2017-09-13 18:49:19 +02:00
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_messagesManager = messagesManager;
|
2017-09-13 21:35:33 +02:00
|
|
|
_dbReadWriteConnection = [storageManager newDatabaseConnection];
|
2017-09-13 18:49:19 +02:00
|
|
|
_finder = finder;
|
|
|
|
_isDrainingQueue = NO;
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(databaseViewRegistrationComplete)
|
|
|
|
name:kNSNotificationName_DatabaseViewRegistrationComplete
|
|
|
|
object:nil];
|
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)databaseViewRegistrationComplete
|
|
|
|
{
|
|
|
|
[self drainQueue];
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
#pragma mark - instance methods
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
- (dispatch_queue_t)serialQueue
|
|
|
|
{
|
|
|
|
static dispatch_queue_t queue = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
queue = dispatch_queue_create("org.whispersystems.message.process", DISPATCH_QUEUE_SERIAL);
|
|
|
|
});
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
- (void)enqueueEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
{
|
|
|
|
OWSAssert(envelopeData);
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
// We need to persist the decrypted envelope data ASAP to prevent data loss.
|
2017-09-13 18:49:19 +02:00
|
|
|
[self.finder addJobWithEnvelopeData:envelopeData plaintextData:plaintextData];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drainQueue
|
|
|
|
{
|
2017-09-21 20:36:52 +02:00
|
|
|
dispatch_async(self.serialQueue, ^{
|
2017-09-13 21:35:33 +02:00
|
|
|
if ([TSDatabaseView hasPendingViewRegistrations]) {
|
|
|
|
// We don't want to process incoming messages until database
|
|
|
|
// view registration is complete.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:49:19 +02:00
|
|
|
if (self.isDrainingQueue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.isDrainingQueue = YES;
|
|
|
|
|
|
|
|
[self drainQueueWorkStep];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drainQueueWorkStep
|
|
|
|
{
|
2017-09-21 20:36:52 +02:00
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
NSArray<OWSMessageContentJob *> *jobs = [self.finder nextJobsForBatchSize:kIncomingMessageBatchSize];
|
2017-09-14 00:03:02 +02:00
|
|
|
OWSAssert(jobs);
|
|
|
|
if (jobs.count < 1) {
|
2017-09-13 18:49:19 +02:00
|
|
|
self.isDrainingQueue = NO;
|
|
|
|
DDLogVerbose(@"%@ Queue is drained", self.tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
[self processJobs:jobs];
|
|
|
|
|
|
|
|
[self.finder removeJobsWithIds:jobs.uniqueIds];
|
|
|
|
|
|
|
|
DDLogVerbose(@"%@ completed %zd jobs. %zd jobs left.",
|
|
|
|
self.tag,
|
|
|
|
jobs.count,
|
|
|
|
[OWSMessageContentJob numberOfKeysInCollection]);
|
|
|
|
|
|
|
|
// Wait a bit in hopes of increasing the batch size.
|
|
|
|
// This delay won't affect the first message to arrive when this queue is idle,
|
|
|
|
// so by definition we're receiving more than one message and can benefit from
|
|
|
|
// batching.
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), self.serialQueue, ^{
|
|
|
|
[self drainQueueWorkStep];
|
|
|
|
});
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 20:36:52 +02:00
|
|
|
- (void)processJobs:(NSArray<OWSMessageContentJob *> *)jobs
|
2017-09-13 18:49:19 +02:00
|
|
|
{
|
2017-09-21 20:36:52 +02:00
|
|
|
AssertOnDispatchQueue(self.serialQueue);
|
|
|
|
|
|
|
|
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
for (OWSMessageContentJob *job in jobs) {
|
|
|
|
[self.messagesManager processEnvelope:job.envelopeProto
|
|
|
|
plaintextData:job.plaintextData
|
|
|
|
transaction:transaction];
|
|
|
|
}
|
|
|
|
}];
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark - OWSBatchMessageProcessor
|
|
|
|
|
|
|
|
@interface OWSBatchMessageProcessor ()
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageContentQueue *processingQueue;
|
2017-09-13 18:49:19 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@implementation OWSBatchMessageProcessor
|
|
|
|
|
|
|
|
- (instancetype)initWithDBConnection:(YapDatabaseConnection *)dbConnection
|
2017-09-14 17:00:30 +02:00
|
|
|
messagesManager:(OWSMessageManager *)messagesManager
|
2017-09-13 21:35:33 +02:00
|
|
|
storageManager:(TSStorageManager *)storageManager
|
2017-09-13 18:49:19 +02:00
|
|
|
{
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
OWSMessageContentJobFinder *finder = [[OWSMessageContentJobFinder alloc] initWithDBConnection:dbConnection];
|
|
|
|
OWSMessageContentQueue *processingQueue = [[OWSMessageContentQueue alloc] initWithMessagesManager:messagesManager
|
|
|
|
storageManager:storageManager
|
|
|
|
finder:finder];
|
2017-09-13 18:49:19 +02:00
|
|
|
|
|
|
|
_processingQueue = processingQueue;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initDefault
|
|
|
|
{
|
|
|
|
// For concurrency coherency we use the same dbConnection to persist and read the unprocessed envelopes
|
|
|
|
YapDatabaseConnection *dbConnection = [[TSStorageManager sharedManager].database newConnection];
|
2017-09-14 17:00:30 +02:00
|
|
|
OWSMessageManager *messagesManager = [OWSMessageManager sharedManager];
|
2017-09-13 21:35:33 +02:00
|
|
|
TSStorageManager *storageManager = [TSStorageManager sharedManager];
|
2017-09-13 18:49:19 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
return [self initWithDBConnection:dbConnection messagesManager:messagesManager storageManager:storageManager];
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
{
|
|
|
|
static OWSBatchMessageProcessor *sharedInstance;
|
|
|
|
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [[self alloc] initDefault];
|
|
|
|
});
|
|
|
|
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - class methods
|
|
|
|
|
|
|
|
+ (void)syncRegisterDatabaseExtension:(YapDatabase *)database
|
|
|
|
{
|
2017-09-20 17:48:37 +02:00
|
|
|
[OWSMessageContentJobFinder syncRegisterDatabaseExtension:database];
|
2017-09-13 18:49:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - instance methods
|
|
|
|
|
|
|
|
- (void)handleAnyUnprocessedEnvelopesAsync
|
|
|
|
{
|
|
|
|
[self.processingQueue drainQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)enqueueEnvelopeData:(NSData *)envelopeData plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
{
|
|
|
|
OWSAssert(envelopeData);
|
|
|
|
|
2017-09-21 19:46:30 +02:00
|
|
|
// We need to persist the decrypted envelope data ASAP to prevent data loss.
|
2017-09-13 18:49:19 +02:00
|
|
|
[self.processingQueue enqueueEnvelopeData:envelopeData plaintextData:plaintextData];
|
|
|
|
[self.processingQueue drainQueue];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|