pEpObjCAdapter/PEPObjCAdapter.m
author buff <andreas@pep-project.org>
Wed, 11 Oct 2017 12:40:46 +0200
branchIOSAD-50
changeset 272 c456123edfa7
parent 269 5a187b6cf883
child 276 3a09f74f0c11
permissions -rw-r--r--
IOSAD-50 moves internal stuff to PEPInternalSession
dirk@187
     1
//
edouard@191
     2
//  pEpObjCAdapter.m
edouard@191
     3
//  pEpObjCAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Volker Birk on 28.04.15.
dirk@187
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
@import Foundation;
dirk@187
    10
dirk@187
    11
#import "PEPObjCAdapter.h"
dirk@187
    12
#import "PEPObjCAdapter+Internal.h"
dirk@187
    13
#import "PEPMessage.h"
andreas@262
    14
#import "PEPSessionProvider.h"
dirk@187
    15
#include "keymanagement.h"
andreas@262
    16
#import "PEPCopyableThread.h"
dirk@187
    17
dirk@187
    18
///////////////////////////////////////////////////////////////////////////////
dirk@187
    19
//  Keyserver and Identity lookup - C part
dirk@187
    20
dirk@187
    21
dirk@187
    22
int examine_identity(pEp_identity *ident, void *management)
dirk@187
    23
{
dirk@187
    24
    //PEPQueue *q = (__bridge PEPQueue *)management;
dirk@187
    25
    PEPQueue *q = [PEPObjCAdapter getLookupQueue];
dirk@187
    26
    
dirk@187
    27
    NSDictionary *identity = PEP_identityDictFromStruct(ident);
dirk@187
    28
    
dirk@187
    29
    [q enqueue:identity];
dirk@187
    30
    return 0;
dirk@187
    31
}
dirk@187
    32
dirk@187
    33
static pEp_identity *retrieve_next_identity(void *management)
dirk@187
    34
{
dirk@187
    35
    //PEPQueue *q = (__bridge PEPQueue *)management;
dirk@187
    36
    PEPQueue *q = [PEPObjCAdapter getLookupQueue];
dirk@187
    37
    
dirk@187
    38
    // Dequeue is a blocking operation
dirk@187
    39
    // that returns nil when queue is killed
dirk@187
    40
    NSDictionary *ident = [q dequeue];
dirk@187
    41
    
dirk@187
    42
    if (ident)
dirk@187
    43
        return PEP_identityDictToStruct(ident);
dirk@187
    44
    else
dirk@187
    45
        return NULL;
dirk@187
    46
}
dirk@187
    47
dirk@187
    48
///////////////////////////////////////////////////////////////////////////////
dirk@187
    49
// Sync - C part
dirk@187
    50
dirk@187
    51
// Called by sync thread only
dirk@187
    52
PEP_STATUS notify_handshake(void *unused_object, pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
dirk@187
    53
{
dirk@187
    54
    id <PEPSyncDelegate> syncDelegate = [PEPObjCAdapter getSyncDelegate];
dirk@187
    55
    if ( syncDelegate )
dirk@187
    56
        return [syncDelegate
dirk@187
    57
                notifyHandshakeWithSignal:signal
dirk@187
    58
                me:PEP_identityDictFromStruct(me)
dirk@187
    59
                partner:PEP_identityDictFromStruct(partner)];
dirk@187
    60
    else
dirk@187
    61
        return PEP_SYNC_NO_NOTIFY_CALLBACK;
dirk@187
    62
}
dirk@187
    63
dirk@187
    64
// Called by sync thread only
dirk@187
    65
PEP_STATUS message_to_send(void *unused_object, message *msg)
dirk@187
    66
{
dirk@187
    67
    id <PEPSyncDelegate> syncDelegate = [PEPObjCAdapter getSyncDelegate];
dirk@187
    68
    if ( syncDelegate )
dirk@187
    69
        return [syncDelegate sendMessage:PEP_messageDictFromStruct(msg)];
dirk@187
    70
    else
dirk@187
    71
        return PEP_SEND_FUNCTION_NOT_REGISTERED;
dirk@187
    72
}
dirk@187
    73
dirk@187
    74
// called indirectly by decrypt message - any thread/session
dirk@187
    75
int inject_sync_msg(void *msg, void *unused_management)
dirk@187
    76
{
dirk@187
    77
    PEPQueue *q = [PEPObjCAdapter getSyncQueue];
dirk@187
    78
    
dirk@187
    79
    [q enqueue:[NSValue valueWithPointer:msg]];
dirk@187
    80
    
dirk@187
    81
    return 0;
dirk@187
    82
}
dirk@187
    83
dirk@187
    84
// Called by sync thread only
dirk@187
    85
void *retrieve_next_sync_msg(void *unused_mamagement, time_t *timeout)
dirk@187
    86
{
edouard@192
    87
    bool needs_fastpoll = (*timeout != 0);
edouard@192
    88
    
edouard@192
    89
    id <PEPSyncDelegate> syncDelegate = [PEPObjCAdapter getSyncDelegate];
edouard@192
    90
    if ( syncDelegate && needs_fastpoll )
edouard@192
    91
        [syncDelegate fastPolling:true];
edouard@192
    92
    
dirk@187
    93
    PEPQueue *q = [PEPObjCAdapter getSyncQueue];
andreas@268
    94
    
edouard@193
    95
    void* result = (void*)[[q timedDequeue:timeout] pointerValue];
andreas@268
    96
    
edouard@192
    97
    if ( syncDelegate && needs_fastpoll )
edouard@192
    98
        [syncDelegate fastPolling:false];
dirk@187
    99
    
edouard@192
   100
    return result;
andreas@268
   101
    
dirk@187
   102
}
dirk@187
   103
dirk@187
   104
///////////////////////////////////////////////////////////////////////////////
dirk@187
   105
// DB and paths
dirk@187
   106
dirk@187
   107
const char* _Nullable SystemDB = NULL;
dirk@187
   108
NSURL *s_homeURL;
dirk@254
   109
static NSLock *s_initLock;
dirk@187
   110
dirk@187
   111
@implementation PEPObjCAdapter
dirk@187
   112
dirk@187
   113
+ (void)initialize
dirk@187
   114
{
dirk@187
   115
    s_homeURL = [self createApplicationDirectory];
dirk@254
   116
    s_initLock = [[NSLock alloc] init];
dirk@254
   117
}
dirk@254
   118
dirk@254
   119
+ (NSLock *)initLock
dirk@254
   120
{
dirk@254
   121
    return s_initLock;
dirk@187
   122
}
dirk@187
   123
dirk@187
   124
+ (NSURL *)homeURL
dirk@187
   125
{
dirk@187
   126
    return s_homeURL;
dirk@187
   127
}
dirk@187
   128
dirk@187
   129
+ (NSURL *)createApplicationDirectory
dirk@187
   130
{
dirk@187
   131
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
   132
    if (!bundleID) {
dirk@187
   133
        // This can happen in unit tests
dirk@187
   134
        bundleID = @"test";
dirk@187
   135
    }
dirk@187
   136
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
   137
    NSURL *dirPath = nil;
andreas@268
   138
    
dirk@187
   139
    // Find the application support directory in the home directory.
dirk@187
   140
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
   141
                                        inDomains:NSUserDomainMask];
dirk@187
   142
    if ([appSupportDir count] > 0)
dirk@187
   143
    {
dirk@187
   144
        // Append the bundle ID to the URL for the
dirk@187
   145
        // Application Support directory.
dirk@187
   146
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
   147
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
   148
        
dirk@187
   149
        // If the directory does not exist, this method creates it.
dirk@187
   150
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
   151
        NSError *theError = nil;
dirk@187
   152
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
   153
                           attributes:nil error:&theError])
dirk@187
   154
        {
dirk@187
   155
            // Handle the error.
dirk@187
   156
            return nil;
dirk@187
   157
        }
dirk@187
   158
    }
andreas@268
   159
    
dirk@187
   160
    return dirPath;
dirk@187
   161
}
dirk@187
   162
dirk@187
   163
+ (NSURL *)createAndSetHomeDirectory
dirk@187
   164
{
dirk@187
   165
    // create and set home directory
dirk@187
   166
    setenv("HOME", [[s_homeURL path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
andreas@268
   167
    
dirk@187
   168
    // create and set temp directory
dirk@187
   169
    NSURL *tmpDirUrl = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
dirk@187
   170
    setenv("TEMP", [[tmpDirUrl path] cStringUsingEncoding:NSUTF8StringEncoding], 1);
andreas@268
   171
    
dirk@187
   172
    return s_homeURL;
dirk@187
   173
}
dirk@187
   174
dirk@187
   175
+ (NSString *) getBundlePathFor: (NSString *) filename
dirk@187
   176
{
dirk@187
   177
    return nil;
dirk@187
   178
}
dirk@187
   179
dirk@187
   180
+ (NSString *) copyAssetIntoDocumentsDirectory:(NSBundle *)rootBundle
andreas@262
   181
                                              :(NSString *)bundleName
andreas@262
   182
                                              :(NSString *)fileName{
andreas@268
   183
    
dirk@187
   184
    NSURL *homeUrl = [PEPObjCAdapter createAndSetHomeDirectory];
dirk@187
   185
    NSString *documentsDirectory = [homeUrl path];
dirk@187
   186
    
dirk@187
   187
    if(!(documentsDirectory && bundleName && fileName))
dirk@187
   188
        return nil;
dirk@187
   189
    
dirk@187
   190
    // Check if the database file exists in the documents directory.
dirk@187
   191
    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName];
dirk@187
   192
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   193
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   194
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   195
                               [[rootBundle resourcePath]
dirk@187
   196
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   197
        
dirk@187
   198
        if (!bundleObj)
dirk@187
   199
            return nil;
dirk@187
   200
        
dirk@187
   201
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   202
        
dirk@187
   203
        NSError *error;
dirk@187
   204
        [[NSFileManager defaultManager]
dirk@187
   205
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   206
        
dirk@187
   207
        // Check if any error occurred during copying and display it.
dirk@187
   208
        if (error != nil) {
dirk@187
   209
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   210
            return nil;
dirk@187
   211
        }
dirk@187
   212
    }
dirk@187
   213
    NSLog(@"Asset %@ copied into %@", fileName, destinationPath);
dirk@187
   214
    return destinationPath;
dirk@187
   215
}
dirk@187
   216
dirk@187
   217
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle{
dirk@187
   218
    NSString *systemDBPath = [PEPObjCAdapter copyAssetIntoDocumentsDirectory:rootBundle
andreas@262
   219
                                                                            :@"pEpTrustWords.bundle"
andreas@262
   220
                                                                            :@"system.db"];
dirk@187
   221
    if (SystemDB) {
dirk@187
   222
        free((void *) SystemDB);
dirk@187
   223
    }
dirk@187
   224
    SystemDB = strdup(systemDBPath.UTF8String);
dirk@187
   225
}
dirk@187
   226
dirk@187
   227
+ (void)setupTrustWordsDB
dirk@187
   228
{
dirk@187
   229
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   230
}
dirk@187
   231
dirk@187
   232
static NSMutableArray* boundSessions = nil;
dirk@187
   233
dirk@187
   234
+ (NSMutableArray*)boundSessions
dirk@187
   235
{
dirk@187
   236
    static dispatch_once_t once;
dirk@187
   237
    dispatch_once(&once, ^{
dirk@187
   238
        boundSessions =  [[NSMutableArray alloc] init];
dirk@187
   239
    });
dirk@187
   240
    return boundSessions;
dirk@187
   241
}
dirk@187
   242
dirk@187
   243
///////////////////////////////////////////////////////////////////////////////
dirk@187
   244
//  Keyserver and Identity lookup - ObjC part
dirk@187
   245
dirk@187
   246
static PEPQueue *lookupQueue = nil;
dirk@187
   247
static NSThread *lookupThread = nil;
dirk@187
   248
static NSConditionLock *lookupThreadJoinCond = nil;
dirk@187
   249
dirk@187
   250
+ (void)lookupThreadRoutine:(id)object
dirk@187
   251
{
dirk@187
   252
    [lookupThreadJoinCond lock];
andreas@268
   253
    
dirk@187
   254
    // FIXME: do_KeyManagement asserts if management is null.
dirk@187
   255
    do_keymanagement(retrieve_next_identity, "NOTNULL" /* (__bridge void *)queue */);
dirk@187
   256
    
dirk@187
   257
    // Set and signal join()
dirk@187
   258
    [lookupThreadJoinCond unlockWithCondition:YES];
dirk@187
   259
}
dirk@187
   260
dirk@187
   261
+ (void)startKeyserverLookup
dirk@187
   262
{
dirk@187
   263
    if (!lookupQueue)
dirk@187
   264
    {
dirk@187
   265
        lookupQueue = [[PEPQueue alloc]init];
dirk@187
   266
        
dirk@187
   267
        // There is no join() call on NSThreads.
dirk@187
   268
        lookupThreadJoinCond = [[NSConditionLock alloc] initWithCondition:NO];
dirk@187
   269
        
dirk@187
   270
        lookupThread = [[NSThread alloc] initWithTarget:self selector:@selector(lookupThreadRoutine:) object:nil];
dirk@187
   271
        [lookupThread start];
dirk@187
   272
    }
dirk@187
   273
}
dirk@187
   274
dirk@187
   275
+ (void)stopKeyserverLookup
dirk@187
   276
{
dirk@187
   277
    
dirk@187
   278
    if (lookupQueue)
dirk@187
   279
    {
dirk@187
   280
        // Flush queue and kick the consumer
dirk@187
   281
        [lookupQueue kill];
dirk@187
   282
        
dirk@187
   283
        // Thread then bailout. Wait for that.
dirk@187
   284
        [lookupThreadJoinCond lockWhenCondition:YES];
dirk@187
   285
        [lookupThreadJoinCond unlock];
dirk@187
   286
        
dirk@187
   287
        lookupThread = nil;
dirk@187
   288
        lookupQueue = nil;
dirk@187
   289
        lookupThreadJoinCond = nil;
dirk@187
   290
    }
dirk@187
   291
}
dirk@187
   292
dirk@187
   293
+ (void)registerExamineFunction:(PEP_SESSION)session
dirk@187
   294
{
dirk@187
   295
    register_examine_function(session, examine_identity, NULL /* (__bridge void *)queue */);
dirk@187
   296
}
dirk@187
   297
dirk@187
   298
+ (PEPQueue*)getLookupQueue
dirk@187
   299
{
dirk@187
   300
    return lookupQueue;
dirk@187
   301
}
dirk@187
   302
dirk@187
   303
///////////////////////////////////////////////////////////////////////////////
dirk@187
   304
// Sync - ObjC part
dirk@187
   305
dirk@187
   306
static PEPQueue *syncQueue = nil;
dirk@187
   307
static NSThread *syncThread = nil;
dirk@187
   308
static NSConditionLock *syncThreadJoinCond = nil;
dirk@187
   309
static PEP_SESSION sync_session = NULL;
dirk@187
   310
static id <PEPSyncDelegate> syncDelegate = nil;
dirk@187
   311
dirk@187
   312
dirk@187
   313
+ (void)syncThreadRoutine:(id)object
dirk@187
   314
{
dirk@187
   315
    [syncThreadJoinCond lock];
dirk@187
   316
    
andreas@268
   317
    
edouard@195
   318
    PEP_STATUS status;
andreas@268
   319
    
dirk@187
   320
    status = do_sync_protocol(sync_session,
dirk@187
   321
                              /* "object" : notifying, sending (unused) */
dirk@187
   322
                              "NOTNULL");
dirk@187
   323
    
edouard@195
   324
    // TODO : log something if status not as expected
dirk@187
   325
    
dirk@187
   326
    
dirk@187
   327
    [syncThreadJoinCond unlockWithCondition:YES];
dirk@187
   328
}
dirk@187
   329
dirk@187
   330
+ (void)attachSyncSession:(PEP_SESSION)session
dirk@187
   331
{
dirk@187
   332
    if(sync_session)
dirk@187
   333
        attach_sync_session(session, sync_session);
dirk@187
   334
}
dirk@187
   335
dirk@187
   336
+ (void)detachSyncSession:(PEP_SESSION)session
dirk@187
   337
{
dirk@187
   338
    detach_sync_session(session);
dirk@187
   339
}
dirk@187
   340
dirk@187
   341
+ (void)startSync:(id <PEPSyncDelegate>)delegate;
dirk@187
   342
{
dirk@187
   343
    syncDelegate = delegate;
dirk@187
   344
    
dirk@187
   345
    if (!syncQueue)
dirk@187
   346
    {
dirk@187
   347
        syncQueue = [[PEPQueue alloc]init];
dirk@187
   348
        
dirk@187
   349
        syncThreadJoinCond = [[NSConditionLock alloc] initWithCondition:NO];
andreas@268
   350
        
dirk@254
   351
        [[PEPObjCAdapter initLock] lock];
edouard@195
   352
        PEP_STATUS status = init(&sync_session);
dirk@254
   353
        [[PEPObjCAdapter initLock] unlock];
edouard@195
   354
        if (status != PEP_STATUS_OK) {
edouard@195
   355
            return;
edouard@195
   356
        }
edouard@195
   357
        
edouard@195
   358
        register_sync_callbacks(sync_session,
edouard@195
   359
                                /* "management" : queuing (unused) */
edouard@195
   360
                                "NOTNULL",
edouard@195
   361
                                message_to_send,
edouard@195
   362
                                notify_handshake,
edouard@195
   363
                                inject_sync_msg,
edouard@195
   364
                                retrieve_next_sync_msg);
dirk@187
   365
        
dirk@187
   366
        syncThread = [[NSThread alloc]
dirk@187
   367
                      initWithTarget:self
dirk@187
   368
                      selector:@selector(syncThreadRoutine:)
dirk@187
   369
                      object:nil];
andreas@268
   370
        
dirk@187
   371
        [syncThread start];
dirk@187
   372
    }
andreas@268
   373
    
dirk@187
   374
    NSMutableArray* sessionList = [PEPObjCAdapter boundSessions];
edouard@191
   375
    NSValue* v;
andreas@269
   376
    PEPInternalSession* session;
dirk@187
   377
    @synchronized (sessionList) {
edouard@191
   378
        for (v in sessionList) {
edouard@191
   379
            session = [v nonretainedObjectValue];
dirk@187
   380
            [PEPObjCAdapter attachSyncSession:[session session]];
dirk@187
   381
        }
dirk@187
   382
    }
dirk@187
   383
}
dirk@187
   384
dirk@187
   385
+ (void)stopSync
dirk@187
   386
{
dirk@187
   387
    NSMutableArray* sessionList = [PEPObjCAdapter boundSessions];
edouard@191
   388
    NSValue* v;
andreas@269
   389
    PEPInternalSession* session;
dirk@187
   390
    @synchronized (sessionList) {
edouard@191
   391
        for (v in sessionList) {
edouard@191
   392
            session = [v nonretainedObjectValue];
dirk@187
   393
            [PEPObjCAdapter detachSyncSession:[session session]];
dirk@187
   394
        }
dirk@187
   395
    }
dirk@187
   396
    
dirk@187
   397
    syncDelegate = nil;
dirk@187
   398
    
dirk@187
   399
    if (syncQueue)
dirk@187
   400
    {
edouard@194
   401
        [syncQueue purge:^(id item){
edouard@194
   402
            sync_msg_t *msg = [item pointerValue];
edouard@194
   403
            free_sync_msg(msg);
edouard@194
   404
        }];
dirk@187
   405
        
dirk@187
   406
        [syncThreadJoinCond lockWhenCondition:YES];
dirk@187
   407
        [syncThreadJoinCond unlock];
andreas@268
   408
        
dirk@254
   409
        [[PEPObjCAdapter initLock] lock];
edouard@195
   410
        release(sync_session);
dirk@254
   411
        [[PEPObjCAdapter initLock] unlock];
dirk@187
   412
        
edouard@195
   413
        sync_session = NULL;
dirk@187
   414
        syncThread = nil;
dirk@187
   415
        syncQueue = nil;
dirk@187
   416
        syncThreadJoinCond = nil;
dirk@187
   417
    }
dirk@187
   418
}
dirk@187
   419
dirk@187
   420
+ (PEPQueue*)getSyncQueue
dirk@187
   421
{
dirk@187
   422
    return syncQueue;
dirk@187
   423
}
dirk@187
   424
dirk@187
   425
+ (id <PEPSyncDelegate>)getSyncDelegate
dirk@187
   426
{
dirk@187
   427
    return syncDelegate;
dirk@187
   428
}
dirk@187
   429
andreas@269
   430
+ (void)bindSession:(PEPInternalSession*)session
dirk@187
   431
{
dirk@187
   432
    NSMutableArray* sessionList = [PEPObjCAdapter boundSessions];
dirk@187
   433
    @synchronized (sessionList) {
edouard@191
   434
        [sessionList addObject:[NSValue valueWithNonretainedObject:session]];
dirk@187
   435
    }
andreas@268
   436
    
dirk@187
   437
    [PEPObjCAdapter registerExamineFunction:[session session]];
dirk@187
   438
    [PEPObjCAdapter attachSyncSession:[session session]];
dirk@187
   439
}
dirk@187
   440
andreas@269
   441
+ (void)unbindSession:(PEPInternalSession*)session
dirk@187
   442
{
dirk@187
   443
    [PEPObjCAdapter detachSyncSession:[session session]];
dirk@187
   444
    
dirk@187
   445
    NSMutableArray* sessionList = [PEPObjCAdapter boundSessions];
dirk@187
   446
    @synchronized (sessionList) {
edouard@191
   447
        [sessionList removeObject:[NSValue valueWithNonretainedObject:session]];
dirk@187
   448
    }
andreas@268
   449
    
andreas@268
   450
}
dirk@187
   451
dirk@187
   452
@end