Code generate Swift wrappers for protocol buffers.

This commit is contained in:
Matthew Chen 2018-08-01 10:45:21 -04:00
parent e45a6d5bed
commit f814157a9e
19 changed files with 1811 additions and 1948 deletions

View file

@ -109,7 +109,7 @@ class BaseContext(object):
def derive_swift_name(self):
names = self.inherited_proto_names()
return self.args.wrapper_prefix + '_'.join(names)
return self.args.wrapper_prefix + ''.join(names)
def derive_wrapped_swift_name(self):
names = self.inherited_proto_names()
@ -275,6 +275,11 @@ class FileContext(BaseContext):
import Foundation
''')
writer.extend('''
// WARNING: This code is generated. Only edit within the markers.
'''.strip())
writer.newline()
writer.invalid_protobuf_error_name = '%sError' % self.args.wrapper_prefix
writer.extend(('''
public enum %s: Error {
@ -329,6 +334,9 @@ class MessageContext(BaseContext):
child.prepare()
def generate(self, writer):
for child in self.messages:
child.generate(writer)
writer.add('// MARK: - %s' % self.swift_name)
writer.newline()
@ -337,7 +345,7 @@ class MessageContext(BaseContext):
writer.push_context(self.proto_name, self.swift_name)
for child in self.children():
for child in self.enums:
child.generate(writer)
# Prepare fields
@ -431,10 +439,11 @@ public func serializedData() throws -> Data {
writer.newline()
# Preserve existing validation logic.
validation_block = args.validation_map[self.swift_name]
if validation_block:
writer.add_raw(validation_block)
writer.newline()
if self.swift_name in args.validation_map:
validation_block = args.validation_map[self.swift_name]
if validation_block:
writer.add_raw(validation_block)
writer.newline()
writer.add('// MARK: - End Validation Logic for %s -' % self.swift_name)
writer.newline()
@ -577,10 +586,8 @@ class EnumContext(BaseContext):
writer.add('private class func %sWrap(_ value: %s) -> %s {' % ( self.swift_name, wrapped_swift_name, self.swift_name, ) )
writer.push_indent()
writer.add('switch value {')
writer.push_indent()
for case_name, case_index in self.case_pairs():
writer.add('case .%s: return .%s' % (case_name, case_name,))
writer.pop_indent()
writer.add('}')
writer.pop_indent()
writer.add('}')
@ -588,10 +595,8 @@ class EnumContext(BaseContext):
writer.add('private class func %sUnwrap(_ value: %s) -> %s {' % ( self.swift_name, self.swift_name, wrapped_swift_name, ) )
writer.push_indent()
writer.add('switch value {')
writer.push_indent()
for case_name, case_index in self.case_pairs():
writer.add('case .%s: return .%s' % (case_name, case_name,))
writer.pop_indent()
writer.add('}')
writer.pop_indent()
writer.add('}')

View file

@ -38,7 +38,7 @@ public class MessageFetcherJob: NSObject {
Logger.info("\(self.logTag) fetching messages via REST.")
let promise = self.fetchUndeliveredMessages().then { (envelopes: [SSKEnvelope], more: Bool) -> Promise<Void> in
let promise = self.fetchUndeliveredMessages().then { (envelopes: [SSKProtoEnvelope], more: Bool) -> Promise<Void> in
for envelope in envelopes {
Logger.info("\(self.logTag) received envelope.")
do {
@ -85,7 +85,7 @@ public class MessageFetcherJob: NSObject {
timer = nil
}
private func parseMessagesResponse(responseObject: Any?) -> (envelopes: [SSKEnvelope], more: Bool)? {
private func parseMessagesResponse(responseObject: Any?) -> (envelopes: [SSKProtoEnvelope], more: Bool)? {
guard let responseObject = responseObject else {
Logger.error("\(self.logTag) response object was surpringly nil")
return nil
@ -110,7 +110,7 @@ public class MessageFetcherJob: NSObject {
}
}()
let envelopes: [SSKEnvelope] = messageDicts.compactMap { buildEnvelope(messageDict: $0) }
let envelopes: [SSKProtoEnvelope] = messageDicts.compactMap { buildEnvelope(messageDict: $0) }
return (
envelopes: envelopes,
@ -118,12 +118,12 @@ public class MessageFetcherJob: NSObject {
)
}
private func buildEnvelope(messageDict: [String: Any]) -> SSKEnvelope? {
private func buildEnvelope(messageDict: [String: Any]) -> SSKProtoEnvelope? {
do {
let params = ParamParser(dictionary: messageDict)
let typeInt: Int32 = try params.required(key: "type")
guard let type: SSKEnvelope.SSKEnvelopeType = SSKEnvelope.SSKEnvelopeType(rawValue: typeInt) else {
guard let type: SSKProtoEnvelope.SSKProtoEnvelopeType = SSKProtoEnvelope.SSKProtoEnvelopeType(rawValue: typeInt) else {
Logger.error("\(self.logTag) `typeInt` was invalid: \(typeInt)")
throw ParamParser.ParseError.invalidFormat("type")
}
@ -134,14 +134,14 @@ public class MessageFetcherJob: NSObject {
let legacyMessage: Data? = try params.optionalBase64EncodedData(key: "message")
let content: Data? = try params.optionalBase64EncodedData(key: "content")
return SSKEnvelope(timestamp: UInt64(timestamp), source: source, sourceDevice: sourceDevice, type: type, content: content, legacyMessage: legacyMessage)
return SSKProtoEnvelope(type: type, relay: nil, source: source, timestamp: UInt64(timestamp), sourceDevice: sourceDevice, legacyMessage: legacyMessage, content: content)
} catch {
owsFail("\(self.logTag) in \(#function) error building envelope: \(error)")
return nil
}
}
private func fetchUndeliveredMessages() -> Promise<(envelopes: [SSKEnvelope], more: Bool)> {
private func fetchUndeliveredMessages() -> Promise<(envelopes: [SSKProtoEnvelope], more: Bool)> {
return Promise { fulfill, reject in
let request = OWSRequestFactory.getMessagesRequest()
self.networkManager.makeRequest(
@ -165,8 +165,12 @@ public class MessageFetcherJob: NSObject {
}
}
private func acknowledgeDelivery(envelope: SSKEnvelope) {
let request = OWSRequestFactory.acknowledgeMessageDeliveryRequest(withSource: envelope.source, timestamp: envelope.timestamp)
private func acknowledgeDelivery(envelope: SSKProtoEnvelope) {
guard let source = envelope.source else {
Logger.error("\(self.logTag) envelope missing source.")
return
}
let request = OWSRequestFactory.acknowledgeMessageDeliveryRequest(withSource: source, timestamp: envelope.timestamp)
self.networkManager.makeRequest(request,
success: { (_: URLSessionDataTask?, _: Any?) -> Void in
Logger.debug("\(self.logTag) acknowledged delivery for message at timestamp: \(envelope.timestamp)")

View file

@ -3372,7 +3372,7 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
completion:nil];
}
+ (SSKEnvelope *)createEnvelopeForThread:(TSThread *)thread
+ (SSKProtoEnvelope *)createEnvelopeForThread:(TSThread *)thread
{
OWSAssert(thread);
@ -3390,13 +3390,14 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
}
}();
SSKEnvelope *envelope = [[SSKEnvelope alloc] initWithTimestamp:timestamp
source:source
sourceDevice:1
type:SSKEnvelopeTypeCiphertext
content:nil
legacyMessage:nil];
SSKProtoEnvelope *envelope = [[SSKProtoEnvelope alloc] initWithType:SSKProtoEnvelopeTypeCiphertext
relay:nil
source:source
timestamp:timestamp
sourceDevice:1
legacyMessage:nil
content:nil];
return envelope;
}
@ -3886,15 +3887,16 @@ typedef OWSContact * (^OWSContactBlock)(YapDatabaseReadWriteTransaction *transac
uint64_t timestamp = [NSDate ows_millisecondTimeStamp];
NSString *source = recipientId;
uint32_t sourceDevice = 1;
SSKEnvelopeType envelopeType = SSKEnvelopeTypeCiphertext;
SSKProtoEnvelopeType envelopeType = SSKProtoEnvelopeTypeCiphertext;
NSData *content = plaintextData;
SSKEnvelope *envelope = [[SSKEnvelope alloc] initWithTimestamp:timestamp
source:source
sourceDevice:sourceDevice
type:envelopeType
content:content
legacyMessage:nil];
SSKProtoEnvelope *envelope = [[SSKProtoEnvelope alloc] initWithType:envelopeType
relay:nil
source:source
timestamp:timestamp
sourceDevice:sourceDevice
legacyMessage:nil
content:content];
NSError *error;
NSData *_Nullable envelopeData = [envelope serializedDataAndReturnError:&error];

View file

@ -7,7 +7,7 @@ import XCTest
import SignalServiceKit
import SwiftProtobuf
class SSKEnvelopeTest: XCTestCase {
class SSKProtoEnvelopeTest: XCTestCase {
override func setUp() {
super.setUp()
@ -21,12 +21,12 @@ class SSKEnvelopeTest: XCTestCase {
func testParse_EmptyData() {
let data = Data()
XCTAssertThrowsError(try SSKEnvelope(serializedData: data))
XCTAssertThrowsError(try SSKProtoEnvelope(serializedData: data))
}
func testParse_UnparseableData() {
let data = "asdf".data(using: .utf8)!
XCTAssertThrowsError(try SSKEnvelope(serializedData: data)) { error in
XCTAssertThrowsError(try SSKProtoEnvelope(serializedData: data)) { error in
XCTAssert(error is SwiftProtobuf.BinaryDecodingError)
}
}
@ -42,7 +42,7 @@ class SSKEnvelopeTest: XCTestCase {
let encodedData = "CAESDCsxNTU1MTIzMTIzNCjKm4WazSw4AQ=="
let data = Data(base64Encoded: encodedData)!
XCTAssertNoThrow(try SSKEnvelope(serializedData: data))
XCTAssertNoThrow(try SSKProtoEnvelope(serializedData: data))
}
func testParse_invalidData() {
@ -56,9 +56,9 @@ class SSKEnvelopeTest: XCTestCase {
let encodedData = "EgwrMTU1NTEyMzEyMzQojdmOms0sOAE="
let data = Data(base64Encoded: encodedData)!
XCTAssertThrowsError(try SSKEnvelope(serializedData: data)) { (error) -> Void in
XCTAssertThrowsError(try SSKProtoEnvelope(serializedData: data)) { (error) -> Void in
switch error {
case SSKEnvelope.EnvelopeError.invalidProtobuf:
case SSKProtoEnvelope.EnvelopeError.invalidProtobuf:
break
default:
XCTFail("unexpected error: \(error)")

View file

@ -7,7 +7,7 @@
NS_ASSUME_NONNULL_BEGIN
@class SSKEnvelope;
@class SSKProtoEnvelope;
typedef NS_ENUM(int32_t, TSErrorMessageType) {
TSErrorMessageNoSession,
@ -55,18 +55,18 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) {
inThread:(nullable TSThread *)thread
failedMessageType:(TSErrorMessageType)errorMessageType;
+ (instancetype)corruptedMessageWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)corruptedMessageWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (instancetype)corruptedMessageInUnknownThread;
+ (instancetype)invalidVersionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)invalidVersionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (instancetype)invalidKeyExceptionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)invalidKeyExceptionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (instancetype)missingSessionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)missingSessionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (instancetype)nonblockingIdentityChangeInThread:(TSThread *)thread recipientId:(NSString *)recipientId;

View file

@ -84,7 +84,7 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
return [self initWithTimestamp:timestamp inThread:thread failedMessageType:errorMessageType recipientId:nil];
}
- (instancetype)initWithEnvelope:(SSKEnvelope *)envelope
- (instancetype)initWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
failedMessageType:(TSErrorMessageType)errorMessageType
{
@ -146,7 +146,7 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
}
}
+ (instancetype)corruptedMessageWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)corruptedMessageWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
return [[self alloc] initWithEnvelope:envelope
@ -159,7 +159,7 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
return [[self alloc] initWithFailedMessageType:TSErrorMessageInvalidMessage];
}
+ (instancetype)invalidVersionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)invalidVersionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
return [[self alloc] initWithEnvelope:envelope
@ -167,7 +167,7 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
failedMessageType:TSErrorMessageInvalidVersion];
}
+ (instancetype)invalidKeyExceptionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)invalidKeyExceptionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
return [[self alloc] initWithEnvelope:envelope
@ -175,7 +175,7 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
failedMessageType:TSErrorMessageInvalidKeyException];
}
+ (instancetype)missingSessionWithEnvelope:(SSKEnvelope *)envelope
+ (instancetype)missingSessionWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
return

View file

@ -6,13 +6,13 @@
NS_ASSUME_NONNULL_BEGIN
@class SSKEnvelope;
@class SSKProtoEnvelope;
// DEPRECATED - we no longer create new instances of this class (as of mid-2017); However, existing instances may
// exist, so we should keep this class around to honor their old behavior.
@interface TSInvalidIdentityKeyReceivingErrorMessage : TSInvalidIdentityKeyErrorMessage
+ (nullable instancetype)untrustedKeyWithEnvelope:(SSKEnvelope *)envelope
+ (nullable instancetype)untrustedKeyWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
@end

View file

@ -28,13 +28,13 @@ NS_ASSUME_NONNULL_BEGIN
@implementation TSInvalidIdentityKeyReceivingErrorMessage {
// Not using a property declaration in order to exclude from DB serialization
SSKEnvelope *_Nullable _envelope;
SSKProtoEnvelope *_Nullable _envelope;
}
@synthesize envelopeData = _envelopeData;
+ (nullable instancetype)untrustedKeyWithEnvelope:(SSKEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
+ (nullable instancetype)untrustedKeyWithEnvelope:(SSKProtoEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{
TSContactThread *contactThread =
[TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
@ -46,8 +46,8 @@ NS_ASSUME_NONNULL_BEGIN
}
- (nullable instancetype)initForUnknownIdentityKeyWithTimestamp:(uint64_t)timestamp
inThread:(TSThread *)thread
incomingEnvelope:(SSKEnvelope *)envelope
inThread:(TSThread *)thread
incomingEnvelope:(SSKProtoEnvelope *)envelope
{
self = [self initWithTimestamp:timestamp inThread:thread failedMessageType:TSErrorMessageWrongTrustedIdentityKey];
if (!self) {
@ -66,11 +66,11 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
- (nullable SSKEnvelope *)envelope
- (nullable SSKProtoEnvelope *)envelope
{
if (!_envelope) {
NSError *error;
SSKEnvelope *_Nullable envelope = [[SSKEnvelope alloc] initWithSerializedData:self.envelopeData error:&error];
SSKProtoEnvelope *_Nullable envelope = [SSKProtoEnvelope parseData:self.envelopeData error:&error];
if (error || envelope == nil) {
OWSProdLogAndFail(@"%@ Could not parse proto: %@", self.logTag, error);
} else {
@ -118,7 +118,7 @@ NS_ASSUME_NONNULL_BEGIN
return nil;
}
if (self.envelope.type != SSKEnvelopeTypePrekeyBundle) {
if (self.envelope.type != SSKProtoEnvelopeTypePrekeyBundle) {
DDLogError(@"Refusing to attempt key extraction from an envelope which isn't a prekey bundle");
return nil;
}

View file

@ -5,7 +5,7 @@
NS_ASSUME_NONNULL_BEGIN
@class OWSStorage;
@class SSKEnvelope;
@class SSKProtoEnvelope;
@class YapDatabaseReadWriteTransaction;
// This class is used to write incoming (decrypted, unprocessed)

View file

@ -40,7 +40,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithUniqueId:(NSString *_Nullable)uniqueId NS_UNAVAILABLE;
@property (nonatomic, readonly, nullable) SSKEnvelope *envelope;
@property (nonatomic, readonly, nullable) SSKProtoEnvelope *envelope;
@end
@ -74,13 +74,13 @@ NS_ASSUME_NONNULL_BEGIN
return [super initWithCoder:coder];
}
- (nullable SSKEnvelope *)envelope
- (nullable SSKProtoEnvelope *)envelope
{
NSError *error;
SSKEnvelope *_Nullable result = [[SSKEnvelope alloc] initWithSerializedData:self.envelopeData error:&error];
SSKProtoEnvelope *_Nullable result = [SSKProtoEnvelope parseData:self.envelopeData error:&error];
if (error) {
OWSProdLogAndFail(@"%@ paring SSKEnvelope failed with error: %@", self.logTag, error);
OWSProdLogAndFail(@"%@ paring SSKProtoEnvelope failed with error: %@", self.logTag, error);
return nil;
}
@ -396,7 +396,7 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
};
@try {
SSKEnvelope *_Nullable envelope = job.envelope;
SSKProtoEnvelope *_Nullable envelope = job.envelope;
if (!envelope) {
reportFailure(transaction);
} else {

View file

@ -6,7 +6,7 @@
NS_ASSUME_NONNULL_BEGIN
@class SSKEnvelope;
@class SSKProtoEnvelope;
@class YapDatabaseReadWriteTransaction;
typedef void (^DecryptSuccessBlock)(NSData *_Nullable plaintextData, YapDatabaseReadWriteTransaction *transaction);
@ -22,7 +22,7 @@ typedef void (^DecryptFailureBlock)(void);
//
// Exactly one of successBlock & failureBlock will be called,
// once.
- (void)decryptEnvelope:(SSKEnvelope *)envelope
- (void)decryptEnvelope:(SSKProtoEnvelope *)envelope
successBlock:(DecryptSuccessBlock)successBlock
failureBlock:(DecryptFailureBlock)failureBlock;

View file

@ -81,7 +81,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Blocking
- (BOOL)isEnvelopeBlocked:(SSKEnvelope *)envelope
- (BOOL)isEnvelopeBlocked:(SSKProtoEnvelope *)envelope
{
OWSAssert(envelope);
@ -90,7 +90,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Decryption
- (void)decryptEnvelope:(SSKEnvelope *)envelope
- (void)decryptEnvelope:(SSKProtoEnvelope *)envelope
successBlock:(DecryptSuccessBlock)successBlockParameter
failureBlock:(DecryptFailureBlock)failureBlockParameter
{
@ -129,7 +129,7 @@ NS_ASSUME_NONNULL_BEGIN
}
switch (envelope.type) {
case SSKEnvelopeTypeCiphertext: {
case SSKProtoEnvelopeTypeCiphertext: {
[self decryptSecureMessage:envelope
successBlock:^(NSData *_Nullable plaintextData, YapDatabaseReadWriteTransaction *transaction) {
DDLogDebug(@"%@ decrypted secure message.", self.logTag);
@ -146,7 +146,7 @@ NS_ASSUME_NONNULL_BEGIN
// Return to avoid double-acknowledging.
return;
}
case SSKEnvelopeTypePrekeyBundle: {
case SSKProtoEnvelopeTypePrekeyBundle: {
[self decryptPreKeyBundle:envelope
successBlock:^(NSData *_Nullable plaintextData, YapDatabaseReadWriteTransaction *transaction) {
DDLogDebug(@"%@ decrypted pre-key whisper message", self.logTag);
@ -165,9 +165,9 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
// These message types don't have a payload to decrypt.
case SSKEnvelopeTypeReceipt:
case SSKEnvelopeTypeKeyExchange:
case SSKEnvelopeTypeUnknown: {
case SSKProtoEnvelopeTypeReceipt:
case SSKProtoEnvelopeTypeKeyExchange:
case SSKProtoEnvelopeTypeUnknown: {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
successBlock(nil, transaction);
}];
@ -193,7 +193,7 @@ NS_ASSUME_NONNULL_BEGIN
failureBlock();
}
- (void)decryptSecureMessage:(SSKEnvelope *)envelope
- (void)decryptSecureMessage:(SSKProtoEnvelope *)envelope
successBlock:(DecryptSuccessBlock)successBlock
failureBlock:(void (^)(NSError *_Nullable error))failureBlock
{
@ -210,7 +210,7 @@ NS_ASSUME_NONNULL_BEGIN
failureBlock:failureBlock];
}
- (void)decryptPreKeyBundle:(SSKEnvelope *)envelope
- (void)decryptPreKeyBundle:(SSKProtoEnvelope *)envelope
successBlock:(DecryptSuccessBlock)successBlock
failureBlock:(void (^)(NSError *_Nullable error))failureBlock
{
@ -230,7 +230,7 @@ NS_ASSUME_NONNULL_BEGIN
failureBlock:failureBlock];
}
- (void)decryptEnvelope:(SSKEnvelope *)envelope
- (void)decryptEnvelope:(SSKProtoEnvelope *)envelope
cipherTypeName:(NSString *)cipherTypeName
cipherMessageBlock:(id<CipherMessage> (^_Nonnull)(NSData *))cipherMessageBlock
successBlock:(DecryptSuccessBlock)successBlock
@ -279,7 +279,7 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
- (void)processException:(NSException *)exception envelope:(SSKEnvelope *)envelope
- (void)processException:(NSException *)exception envelope:(SSKProtoEnvelope *)envelope
{
DDLogError(@"%@ Got exception: %@ of type: %@ with reason: %@",
self.logTag,
@ -326,7 +326,7 @@ NS_ASSUME_NONNULL_BEGIN
}
- (void)notifyUserForErrorMessage:(TSErrorMessage *)errorMessage
envelope:(SSKEnvelope *)envelope
envelope:(SSKProtoEnvelope *)envelope
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];

View file

@ -6,14 +6,14 @@ NS_ASSUME_NONNULL_BEGIN
@class OWSSignalServiceProtosContent;
@class OWSSignalServiceProtosDataMessage;
@class SSKEnvelope;
@class SSKProtoEnvelope;
NSString *envelopeAddress(SSKEnvelope *envelope);
NSString *envelopeAddress(SSKProtoEnvelope *envelope);
@interface OWSMessageHandler : NSObject
- (NSString *)descriptionForEnvelopeType:(SSKEnvelope *)envelope;
- (NSString *)descriptionForEnvelope:(SSKEnvelope *)envelope;
- (NSString *)descriptionForEnvelopeType:(SSKProtoEnvelope *)envelope;
- (NSString *)descriptionForEnvelope:(SSKProtoEnvelope *)envelope;
- (NSString *)descriptionForContent:(OWSSignalServiceProtosContent *)content;
- (NSString *)descriptionForDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage;

View file

@ -9,31 +9,31 @@
NS_ASSUME_NONNULL_BEGIN
// used in log formatting
NSString *envelopeAddress(SSKEnvelope *envelope)
NSString *envelopeAddress(SSKProtoEnvelope *envelope)
{
return [NSString stringWithFormat:@"%@.%d", envelope.source, (unsigned int)envelope.sourceDevice];
}
@implementation OWSMessageHandler
- (NSString *)descriptionForEnvelopeType:(SSKEnvelope *)envelope
- (NSString *)descriptionForEnvelopeType:(SSKProtoEnvelope *)envelope
{
OWSAssert(envelope != nil);
switch (envelope.type) {
case SSKEnvelopeTypeReceipt:
case SSKProtoEnvelopeTypeReceipt:
return @"DeliveryReceipt";
case SSKEnvelopeTypeUnknown:
case SSKProtoEnvelopeTypeUnknown:
// Shouldn't happen
OWSProdFail([OWSAnalyticsEvents messageManagerErrorEnvelopeTypeUnknown]);
return @"Unknown";
case SSKEnvelopeTypeCiphertext:
case SSKProtoEnvelopeTypeCiphertext:
return @"SignalEncryptedMessage";
case SSKEnvelopeTypeKeyExchange:
case SSKProtoEnvelopeTypeKeyExchange:
// Unsupported
OWSProdFail([OWSAnalyticsEvents messageManagerErrorEnvelopeTypeKeyExchange]);
return @"KeyExchange";
case SSKEnvelopeTypePrekeyBundle:
case SSKProtoEnvelopeTypePrekeyBundle:
return @"PreKeyEncryptedMessage";
default:
// Shouldn't happen
@ -42,7 +42,7 @@ NSString *envelopeAddress(SSKEnvelope *envelope)
}
}
- (NSString *)descriptionForEnvelope:(SSKEnvelope *)envelope
- (NSString *)descriptionForEnvelope:(SSKProtoEnvelope *)envelope
{
OWSAssert(envelope != nil);

View file

@ -6,7 +6,7 @@
NS_ASSUME_NONNULL_BEGIN
@class SSKEnvelope;
@class SSKProtoEnvelope;
@class TSThread;
@class YapDatabaseReadWriteTransaction;
@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)sharedManager;
// processEnvelope: can be called from any thread.
- (void)processEnvelope:(SSKEnvelope *)envelope
- (void)processEnvelope:(SSKProtoEnvelope *)envelope
plaintextData:(NSData *_Nullable)plaintextData
transaction:(YapDatabaseReadWriteTransaction *)transaction;

View file

@ -158,7 +158,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Blocking
- (BOOL)isEnvelopeBlocked:(SSKEnvelope *)envelope
- (BOOL)isEnvelopeBlocked:(SSKProtoEnvelope *)envelope
{
OWSAssert(envelope);
@ -167,7 +167,7 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - message handling
- (void)processEnvelope:(SSKEnvelope *)envelope
- (void)processEnvelope:(SSKProtoEnvelope *)envelope
plaintextData:(NSData *_Nullable)plaintextData
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -189,8 +189,8 @@ NS_ASSUME_NONNULL_BEGIN
OWSAssert(![self isEnvelopeBlocked:envelope]);
switch (envelope.type) {
case SSKEnvelopeTypeCiphertext:
case SSKEnvelopeTypePrekeyBundle:
case SSKProtoEnvelopeTypeCiphertext:
case SSKProtoEnvelopeTypePrekeyBundle:
if (plaintextData) {
[self handleEnvelope:envelope plaintextData:plaintextData transaction:transaction];
} else {
@ -198,15 +198,15 @@ NS_ASSUME_NONNULL_BEGIN
@"%@ missing decrypted data for envelope: %@", self.logTag, [self descriptionForEnvelope:envelope]);
}
break;
case SSKEnvelopeTypeReceipt:
case SSKProtoEnvelopeTypeReceipt:
OWSAssert(!plaintextData);
[self handleDeliveryReceipt:envelope transaction:transaction];
break;
// Other messages are just dismissed for now.
case SSKEnvelopeTypeKeyExchange:
case SSKProtoEnvelopeTypeKeyExchange:
DDLogWarn(@"Received Key Exchange Message, not supported");
break;
case SSKEnvelopeTypeUnknown:
case SSKProtoEnvelopeTypeUnknown:
DDLogWarn(@"Received an unknown message type");
break;
default:
@ -215,8 +215,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleDeliveryReceipt:(SSKEnvelope *)envelope
transaction:(YapDatabaseReadWriteTransaction *)transaction
- (void)handleDeliveryReceipt:(SSKProtoEnvelope *)envelope transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(envelope);
OWSAssert(transaction);
@ -272,7 +271,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleEnvelope:(SSKEnvelope *)envelope
- (void)handleEnvelope:(SSKProtoEnvelope *)envelope
plaintextData:(NSData *)plaintextData
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -326,7 +325,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleIncomingEnvelope:(SSKEnvelope *)envelope
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -395,7 +394,7 @@ NS_ASSUME_NONNULL_BEGIN
}
- (void)sendGroupInfoRequest:(NSData *)groupId
envelope:(SSKEnvelope *)envelope
envelope:(SSKProtoEnvelope *)envelope
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(groupId.length > 0);
@ -429,7 +428,7 @@ NS_ASSUME_NONNULL_BEGIN
return [TextSecureKitEnv sharedEnv].profileManager;
}
- (void)handleIncomingEnvelope:(SSKEnvelope *)envelope
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
withReceiptMessage:(OWSSignalServiceProtosReceiptMessage *)receiptMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -464,7 +463,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleIncomingEnvelope:(SSKEnvelope *)envelope
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
withCallMessage:(OWSSignalServiceProtosCallMessage *)callMessage
{
OWSAssert(envelope);
@ -499,7 +498,7 @@ NS_ASSUME_NONNULL_BEGIN
});
}
- (void)handleReceivedGroupAvatarUpdateWithEnvelope:(SSKEnvelope *)envelope
- (void)handleReceivedGroupAvatarUpdateWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -537,7 +536,7 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
- (void)handleReceivedMediaWithEnvelope:(SSKEnvelope *)envelope
- (void)handleReceivedMediaWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -585,7 +584,7 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
- (void)handleIncomingEnvelope:(SSKEnvelope *)envelope
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
withSyncMessage:(OWSSignalServiceProtosSyncMessage *)syncMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -720,7 +719,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleEndSessionMessageWithEnvelope:(SSKEnvelope *)envelope
- (void)handleEndSessionMessageWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -737,7 +736,7 @@ NS_ASSUME_NONNULL_BEGIN
[self.primaryStorage deleteAllSessionsForContact:envelope.source protocolContext:transaction];
}
- (void)handleExpirationTimerUpdateMessageWithEnvelope:(SSKEnvelope *)envelope
- (void)handleExpirationTimerUpdateMessageWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -780,7 +779,7 @@ NS_ASSUME_NONNULL_BEGIN
[message saveWithTransaction:transaction];
}
- (void)handleProfileKeyMessageWithEnvelope:(SSKEnvelope *)envelope
- (void)handleProfileKeyMessageWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
OWSAssert(envelope);
@ -805,7 +804,7 @@ NS_ASSUME_NONNULL_BEGIN
[profileManager setProfileKeyData:profileKey forRecipientId:recipientId];
}
- (void)handleReceivedTextMessageWithEnvelope:(SSKEnvelope *)envelope
- (void)handleReceivedTextMessageWithEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -845,7 +844,7 @@ NS_ASSUME_NONNULL_BEGIN
}
}
- (void)handleGroupInfoRequest:(SSKEnvelope *)envelope
- (void)handleGroupInfoRequest:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
@ -901,7 +900,7 @@ NS_ASSUME_NONNULL_BEGIN
[self sendGroupUpdateForThread:gThread message:message];
}
- (TSIncomingMessage *_Nullable)handleReceivedEnvelope:(SSKEnvelope *)envelope
- (TSIncomingMessage *_Nullable)handleReceivedEnvelope:(SSKProtoEnvelope *)envelope
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
attachmentIds:(NSArray<NSString *> *)attachmentIds
transaction:(YapDatabaseReadWriteTransaction *)transaction
@ -1083,7 +1082,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)finalizeIncomingMessage:(TSIncomingMessage *)incomingMessage
thread:(TSThread *)thread
envelope:(SSKEnvelope *)envelope
envelope:(SSKProtoEnvelope *)envelope
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(thread);
@ -1203,7 +1202,7 @@ NS_ASSUME_NONNULL_BEGIN
* Group or Contact thread for message, creating a new contact thread if necessary,
* but never creating a new group thread.
*/
- (nullable TSThread *)threadForEnvelope:(SSKEnvelope *)envelope
- (nullable TSThread *)threadForEnvelope:(SSKProtoEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
transaction:(YapDatabaseReadWriteTransaction *)transaction
{

View file

@ -31,7 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly) NSDate *createdAt;
@property (nonatomic, readonly) NSData *envelopeData;
@property (nonatomic, readonly, nullable) SSKEnvelope *envelopeProto;
@property (nonatomic, readonly, nullable) SSKProtoEnvelope *envelopeProto;
- (instancetype)initWithEnvelopeData:(NSData *)envelopeData NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
@ -68,10 +68,10 @@ NS_ASSUME_NONNULL_BEGIN
return [super initWithCoder:coder];
}
- (nullable SSKEnvelope *)envelopeProto
- (nullable SSKProtoEnvelope *)envelopeProto
{
NSError *error;
SSKEnvelope *_Nullable envelope = [[SSKEnvelope alloc] initWithSerializedData:self.envelopeData error:&error];
SSKProtoEnvelope *_Nullable envelope = [SSKProtoEnvelope parseData:self.envelopeData error:&error];
if (error || envelope == nil) {
OWSFail(@"%@ failed to parase envelope with error: %@", self.logTag, error);
return nil;
@ -327,7 +327,7 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
AssertOnDispatchQueue(self.serialQueue);
OWSAssert(job);
SSKEnvelope *_Nullable envelope = nil;
SSKProtoEnvelope *_Nullable envelope = nil;
@try {
envelope = job.envelopeProto;
} @catch (NSException *exception) {

File diff suppressed because it is too large Load diff

View file

@ -1,149 +0,0 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
public class SSKEnvelope: NSObject {
public enum EnvelopeError: Error {
case invalidProtobuf(description: String)
}
@objc
public enum SSKEnvelopeType: Int32 {
case unknown = 0
case ciphertext = 1
case keyExchange = 2
case prekeyBundle = 3
case receipt = 5
}
@objc
public let timestamp: UInt64
@objc
public let source: String
@objc
public let sourceDevice: UInt32
@objc
public let type: SSKEnvelopeType
@objc
public let relay: String?
@objc
public let content: Data?
@objc
public let legacyMessage: Data?
@objc
public init(timestamp: UInt64, source: String, sourceDevice: UInt32, type: SSKEnvelopeType, content: Data?, legacyMessage: Data?) {
self.source = source
self.type = type
self.timestamp = timestamp
self.sourceDevice = sourceDevice
self.relay = nil
self.content = content
self.legacyMessage = legacyMessage
}
@objc
public init(serializedData: Data) throws {
let proto: SignalServiceProtos_Envelope = try SignalServiceProtos_Envelope(serializedData: serializedData)
guard proto.hasSource else {
throw EnvelopeError.invalidProtobuf(description: "missing required field: source")
}
self.source = proto.source
guard proto.hasType else {
throw EnvelopeError.invalidProtobuf(description: "missing required field: type")
}
self.type = {
switch proto.type {
case .unknown:
return .unknown
case .ciphertext:
return .ciphertext
case .keyExchange:
return .keyExchange
case .prekeyBundle:
return .prekeyBundle
case .receipt:
return .receipt
}
}()
guard proto.hasTimestamp else {
throw EnvelopeError.invalidProtobuf(description: "missing required field: timestamp")
}
self.timestamp = proto.timestamp
guard proto.hasSourceDevice else {
throw EnvelopeError.invalidProtobuf(description: "missing required field: sourceDevice")
}
self.sourceDevice = proto.sourceDevice
if proto.hasContent {
self.content = proto.content
} else {
self.content = nil
}
if proto.hasLegacyMessage {
self.legacyMessage = proto.legacyMessage
} else {
self.legacyMessage = nil
}
if proto.relay.count > 0 {
self.relay = proto.relay
} else {
relay = nil
}
}
@objc
public func serializedData() throws -> Data {
return try self.asProtobuf.serializedData()
}
private var asProtobuf: SignalServiceProtos_Envelope {
let proto = SignalServiceProtos_Envelope.with { (builder) in
builder.source = self.source
builder.type = {
switch self.type {
case .unknown:
return .unknown
case .ciphertext:
return .ciphertext
case .keyExchange:
return .keyExchange
case .prekeyBundle:
return .prekeyBundle
case .receipt:
return .receipt
}
}()
builder.timestamp = self.timestamp
builder.sourceDevice = self.sourceDevice
if let relay = self.relay {
builder.relay = relay
}
if let content = self.content {
builder.content = content
}
}
return proto
}
}