|
|
- //
- // PEPXPCApiClientService.m
- // PEPObjCAdapterXpcApiClient
- //
- // Created by David Alarcon on 13/1/21.
- //
-
- #import "PEPObjCAdapterXPCApiClientService.h"
-
- #import "PEPObjCAdapterXpcApiProtocol.h"
- #import "PEPObjCAdapterXpcApiResult.h"
- #import "PEPIdentity.h"
- #import "PEPMessage.h"
-
- @interface PEPObjCAdapterXPCApiClientService() <NSXPCListenerDelegate>
- @property (nonatomic) NSXPCConnection *connection;
- @property (nonatomic) NSError *connectionError;
- @end
-
- @implementation PEPObjCAdapterXPCApiClientService
-
- // MARK: - Singleton
-
- + (id)shared {
- static PEPObjCAdapterXPCApiClientService *sharedPEPObjCAdapterXPCApiClientService = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedPEPObjCAdapterXPCApiClientService = [[self alloc] init];
- });
- return sharedPEPObjCAdapterXPCApiClientService;
- }
-
- // MARK: - Init
-
- - (void)start {
- [self connectToXPCService];
- }
-
- - (void)stop {
- self.connection = nil;
- }
-
- // MARK: - Private
-
- - (void)connectToXPCService {
- self.connection = [[NSXPCConnection alloc] initWithMachServiceName:daemonLabel
- options:0];
- NSXPCInterface *remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(PEPObjCAdapterXpcApiProtocol)];
- self.connection.remoteObjectInterface = remoteObjectInterface;
-
- // New connections always start in a suspended state
- [self.connection resume];
- }
-
- // MARK: - PEPSessionProtocol
-
- - (void)mySelf:(nonnull PEPIdentity *)identity
- errorCallback:(nonnull void (^)(NSError * _Nonnull))errorCallback
- successCallback:(nonnull void (^)(PEPIdentity * _Nonnull))successCallback {
-
- [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
- errorCallback(error);
- }] myself:identity resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
- if (result.result.count) {
- PEPIdentity *callbackIdentity = [result.result firstObject];
- successCallback(callbackIdentity);
- } else {
- // Nothing to do
- }
- }];
- }
-
- /// Encrypt a message with explicit encryption format.
- - (void)encryptMessage:(PEPMessage *)message
- extraKeys:(PEPStringList *_Nullable)extraKeys
- encFormat:(PEPEncFormat)encFormat
- errorCallback:(void (^)(NSError *error))errorCallback
- successCallback:(void (^)(PEPMessage *srcMessage,
- PEPMessage *destMessage))successCallback {
- [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
- errorCallback(error);
- }] encryptMessage:message extraKeys:extraKeys encFormat:encFormat
- resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
- PEPMessage *srcMessage = result.result[0];
- PEPMessage *destMessage = result.result[1];
- successCallback(srcMessage, destMessage);
- }];
- }
-
- - (void)encryptMessage:(PEPMessage *)message
- forSelf:(PEPIdentity *)ownIdentity
- extraKeys:(PEPStringList *_Nullable)extraKeys
- errorCallback:(void (^)(NSError *error))errorCallback
- successCallback:(void (^)(PEPMessage *srcMessage,
- PEPMessage *destMessage))successCallback {
-
- [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
- errorCallback(error);
- }] encryptMessage:message forSelf:ownIdentity extraKeys:extraKeys
- resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
- PEPMessage *srcMessage = result.result[0];
- PEPMessage *destMessage = result.result[1];
- successCallback(srcMessage, destMessage);
- }];
- }
-
- - (void)outgoingRatingForMessage:(PEPMessage *)theMessage
- errorCallback:(void (^)(NSError * _Nonnull))errorCallback
- successCallback:(void (^)(PEPRating))successCallback {
-
- [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
- errorCallback(error);
- }] outgoingRatingForMessage:theMessage resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
- PEPRating pEpRating = [(NSNumber *)[result.result firstObject] intValue];
- successCallback(pEpRating);
- }];
- }
-
- - (PEPColor)colorFromRating:(PEPRating)rating {
- __block PEPColor color = PEPColorNoColor;
-
- [[self.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
- NSLog(@"[PEP4APPLEMAIL] Error: %@", error);
- // Nothing to do
- }] colorFromRating:rating resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
- color = [(NSNumber *)[result.result firstObject] intValue];
- }];
-
- return color;
- }
-
- @end
|