pEp macOS Desktop adapter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.6 KiB

  1. //
  2. // PEPXPCApiClientService.m
  3. // PEPObjCAdapterXpcApiClient
  4. //
  5. // Created by David Alarcon on 13/1/21.
  6. //
  7. #import "PEPObjCAdapterXPCApiClientService.h"
  8. #import "PEPObjCAdapterXpcApiProtocol.h"
  9. #import "PEPObjCAdapterXpcApiResult.h"
  10. #import "PEPIdentity.h"
  11. #import "PEPMessage.h"
  12. @interface PEPObjCAdapterXPCApiClientService() <NSXPCListenerDelegate>
  13. @property (nonatomic) NSXPCConnection *connection;
  14. @property (nonatomic) NSError *connectionError;
  15. @end
  16. @implementation PEPObjCAdapterXPCApiClientService
  17. // MARK: - Singleton
  18. + (id)shared {
  19. static PEPObjCAdapterXPCApiClientService *sharedPEPObjCAdapterXPCApiClientService = nil;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. sharedPEPObjCAdapterXPCApiClientService = [[self alloc] init];
  23. });
  24. return sharedPEPObjCAdapterXPCApiClientService;
  25. }
  26. // MARK: - Init
  27. - (void)start {
  28. [self connectToXPCService];
  29. }
  30. - (void)stop {
  31. self.connection = nil;
  32. }
  33. // MARK: - Private
  34. - (void)connectToXPCService {
  35. self.connection = [[NSXPCConnection alloc] initWithMachServiceName:daemonLabel
  36. options:0];
  37. NSXPCInterface *remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(PEPObjCAdapterXpcApiProtocol)];
  38. self.connection.remoteObjectInterface = remoteObjectInterface;
  39. // New connections always start in a suspended state
  40. [self.connection resume];
  41. }
  42. // MARK: - PEPSessionProtocol
  43. - (void)mySelf:(nonnull PEPIdentity *)identity
  44. errorCallback:(nonnull void (^)(NSError * _Nonnull))errorCallback
  45. successCallback:(nonnull void (^)(PEPIdentity * _Nonnull))successCallback {
  46. [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
  47. errorCallback(error);
  48. }] myself:identity resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
  49. if (result.result.count) {
  50. PEPIdentity *callbackIdentity = [result.result firstObject];
  51. successCallback(callbackIdentity);
  52. } else {
  53. // Nothing to do
  54. }
  55. }];
  56. }
  57. /// Encrypt a message with explicit encryption format.
  58. - (void)encryptMessage:(PEPMessage *)message
  59. extraKeys:(PEPStringList *_Nullable)extraKeys
  60. encFormat:(PEPEncFormat)encFormat
  61. errorCallback:(void (^)(NSError *error))errorCallback
  62. successCallback:(void (^)(PEPMessage *srcMessage,
  63. PEPMessage *destMessage))successCallback {
  64. [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
  65. errorCallback(error);
  66. }] encryptMessage:message extraKeys:extraKeys encFormat:encFormat
  67. resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
  68. PEPMessage *srcMessage = result.result[0];
  69. PEPMessage *destMessage = result.result[1];
  70. successCallback(srcMessage, destMessage);
  71. }];
  72. }
  73. - (void)encryptMessage:(PEPMessage *)message
  74. forSelf:(PEPIdentity *)ownIdentity
  75. extraKeys:(PEPStringList *_Nullable)extraKeys
  76. errorCallback:(void (^)(NSError *error))errorCallback
  77. successCallback:(void (^)(PEPMessage *srcMessage,
  78. PEPMessage *destMessage))successCallback {
  79. [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
  80. errorCallback(error);
  81. }] encryptMessage:message forSelf:ownIdentity extraKeys:extraKeys
  82. resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
  83. PEPMessage *srcMessage = result.result[0];
  84. PEPMessage *destMessage = result.result[1];
  85. successCallback(srcMessage, destMessage);
  86. }];
  87. }
  88. - (void)outgoingRatingForMessage:(PEPMessage *)theMessage
  89. errorCallback:(void (^)(NSError * _Nonnull))errorCallback
  90. successCallback:(void (^)(PEPRating))successCallback {
  91. [[self.connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
  92. errorCallback(error);
  93. }] outgoingRatingForMessage:theMessage resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
  94. PEPRating pEpRating = [(NSNumber *)[result.result firstObject] intValue];
  95. successCallback(pEpRating);
  96. }];
  97. }
  98. - (PEPColor)colorFromRating:(PEPRating)rating {
  99. __block PEPColor color = PEPColorNoColor;
  100. [[self.connection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
  101. NSLog(@"[PEP4APPLEMAIL] Error: %@", error);
  102. // Nothing to do
  103. }] colorFromRating:rating resultBlock:^(PEPObjCAdapterXpcApiResult * _Nonnull result) {
  104. color = [(NSNumber *)[result.result firstObject] intValue];
  105. }];
  106. return color;
  107. }
  108. @end