pEpObjCAdapter/PEPSessionProvider.m
author Dirk Zimmermann <dirk@pep-project.org>
Wed, 13 Jun 2018 11:19:35 +0200
changeset 627 2f9d21f8b1a6
parent 625 6da48501887b
child 636 11d6508b7c62
permissions -rw-r--r--
IOSAD-94 make sure main session is always configured again
andreas@262
     1
//
andreas@262
     2
//  PEPSessionProvider.m
andreas@262
     3
//  pEpObjCAdapter
andreas@262
     4
//
andreas@262
     5
//  Created by Andreas Buff on 09.10.17.
andreas@262
     6
//  Copyright © 2017 p≡p. All rights reserved.
andreas@262
     7
//
andreas@262
     8
andreas@262
     9
#import "PEPSessionProvider.h"
andreas@262
    10
andreas@416
    11
#import "PEPObjCAdapter+Internal.h"
andreas@269
    12
#import "PEPInternalSession.h"
andreas@262
    13
#import "PEPCopyableThread.h"
andreas@262
    14
andreas@262
    15
@implementation PEPSessionProvider
andreas@262
    16
andreas@262
    17
static NSLock *s_sessionForThreadLock = nil;
andreas@269
    18
static NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *s_sessionForThreadDict;
andreas@262
    19
andreas@290
    20
/** We have to conform to the Engines rule: "The first session has to be created on the main thread and kept
andreas@290
    21
 alive until all sessiopns created afterwards have been teared down."
andreas@290
    22
 Here we hold it.
andreas@290
    23
 */
andreas@290
    24
static PEPInternalSession *s_sessionForMainThread = nil;
andreas@290
    25
andreas@262
    26
#pragma mark - Public API
andreas@262
    27
andreas@269
    28
+ (PEPInternalSession * _Nonnull)session
andreas@262
    29
{
andreas@262
    30
    [[self sessionForThreadLock] lock];
andreas@273
    31
andreas@290
    32
    // Assure a session for the main thread exists and is kept alive before anyother session is created.
andreas@290
    33
    [self assureSessionForMainThreadExists];
andreas@290
    34
andreas@290
    35
    if ([NSThread isMainThread]) {
andreas@292
    36
        [[self sessionForThreadLock] unlock];
andreas@290
    37
        return s_sessionForMainThread;
andreas@290
    38
    }
andreas@290
    39
andreas@288
    40
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
    41
    PEPCopyableThread *currentThread = [[PEPCopyableThread alloc] initWithThread:[NSThread currentThread]];
dirk@550
    42
    PEPInternalSession * __strong newOrExistingSession = dict[currentThread];
dirk@550
    43
    if (!newOrExistingSession) {
dirk@550
    44
        newOrExistingSession = [PEPInternalSession new];
dirk@550
    45
        dict[currentThread] = newOrExistingSession;
andreas@262
    46
    }
dirk@625
    47
dirk@625
    48
    // configuration
dirk@601
    49
    [self setConfigUnEncryptedSubjectOnSession:newOrExistingSession];
dirk@625
    50
    [self setPassiveModeOnSession:newOrExistingSession];
dirk@625
    51
andreas@272
    52
    [self nullifySessionsOfFinishedThreads];
andreas@273
    53
andreas@262
    54
    [[self sessionForThreadLock] unlock];
andreas@273
    55
dirk@550
    56
    return newOrExistingSession;
andreas@262
    57
}
andreas@262
    58
andreas@262
    59
+ (void)cleanup
andreas@262
    60
{
andreas@262
    61
    [[self sessionForThreadLock] lock];
andreas@273
    62
andreas@273
    63
    [self cleanupInternal];
andreas@273
    64
andreas@262
    65
    [[self sessionForThreadLock] unlock];
andreas@262
    66
}
andreas@262
    67
andreas@262
    68
#pragma mark - Life Cycle
andreas@262
    69
andreas@262
    70
+ (void)initialize
andreas@262
    71
{
andreas@262
    72
    s_sessionForThreadLock = [NSLock new];
andreas@262
    73
    s_sessionForThreadDict = [NSMutableDictionary new];
andreas@262
    74
}
andreas@262
    75
andreas@262
    76
#pragma mark - Lock
andreas@262
    77
andreas@262
    78
+ (NSLock *)sessionForThreadLock
andreas@262
    79
{
andreas@262
    80
    return s_sessionForThreadLock;
andreas@262
    81
}
andreas@262
    82
andreas@262
    83
+ (NSMutableDictionary *)sessionForThreadDict
andreas@262
    84
{
andreas@262
    85
    return s_sessionForThreadDict;
andreas@262
    86
}
andreas@262
    87
dirk@625
    88
#pragma mark - configuration
andreas@416
    89
dirk@601
    90
+ (void)setConfigUnEncryptedSubjectOnSession:(PEPInternalSession *)session
andreas@416
    91
{
dirk@601
    92
    BOOL unEncryptedSubjectEnabled = [PEPObjCAdapter unEncryptedSubjectEnabled];
dirk@601
    93
    [session configUnEncryptedSubjectEnabled:unEncryptedSubjectEnabled];
andreas@416
    94
}
andreas@416
    95
dirk@625
    96
+ (void)setPassiveModeOnSession:(PEPInternalSession *)session
dirk@625
    97
{
dirk@625
    98
    BOOL passiveModeEnabled = [PEPObjCAdapter passiveModeEnabled];
dirk@625
    99
    [session configurePassiveModeEnabled:passiveModeEnabled];
dirk@625
   100
}
dirk@625
   101
dirk@625
   102
#pragma mark -
dirk@625
   103
andreas@290
   104
/**
andreas@290
   105
 Assures a session for the main thread is set.
andreas@290
   106
 */
andreas@290
   107
+ (void)assureSessionForMainThreadExists
andreas@290
   108
{
dirk@627
   109
    // shared code to set global configuration every time
dirk@627
   110
    void (^configurationBlock)(void) = ^{
dirk@627
   111
        [self setConfigUnEncryptedSubjectOnSession:s_sessionForMainThread];
dirk@627
   112
        [self setPassiveModeOnSession:s_sessionForMainThread];
dirk@627
   113
    };
dirk@627
   114
dirk@549
   115
    if (s_sessionForMainThread) {
dirk@627
   116
        configurationBlock();
dirk@549
   117
        return;
dirk@549
   118
    }
dirk@549
   119
dirk@627
   120
    // shared code that is executed in any case, either on the main thread or in the background
dirk@548
   121
    void (^creationBlock)(void) = ^{
dirk@548
   122
        if (s_sessionForMainThread) {
dirk@548
   123
            return;
dirk@548
   124
        }
dirk@548
   125
        s_sessionForMainThread = [PEPInternalSession new];
dirk@627
   126
        configurationBlock();
dirk@548
   127
    };
dirk@548
   128
andreas@290
   129
andreas@290
   130
    if ([NSThread isMainThread]) {
dirk@548
   131
        creationBlock();
andreas@290
   132
    } else {
dirk@548
   133
        dispatch_sync(dispatch_get_main_queue(), creationBlock);
andreas@290
   134
    }
andreas@290
   135
}
andreas@262
   136
andreas@273
   137
+ (void)cleanupInternal
andreas@273
   138
{
andreas@273
   139
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@273
   140
andreas@273
   141
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@275
   142
        [self nullifySessionForThread:thread];
andreas@273
   143
    }
andreas@290
   144
    s_sessionForMainThread = nil;
andreas@288
   145
    [dict removeAllObjects];
andreas@273
   146
}
andreas@273
   147
andreas@273
   148
/**
andreas@273
   149
 Tears down all sessions that belong to a thread that has finish executing forever.
andreas@273
   150
 */
andreas@272
   151
+ (void)nullifySessionsOfFinishedThreads
andreas@262
   152
{
andreas@269
   153
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
   154
    for (PEPCopyableThread *thread in dict.allKeys) {
andreas@262
   155
        if (thread.isFinished) {
andreas@273
   156
            // The session has been created to run on the one and only thread.
andreas@273
   157
            // As this thread did finish executing forever, we can be sure the session can not be
andreas@273
   158
            // accessed anymore, thus it is OK to nullify it on another thread as the one it has been
andreas@273
   159
            // created for.
andreas@262
   160
            [self nullifySessionForThread:thread];
andreas@262
   161
        }
andreas@262
   162
    }
andreas@262
   163
}
andreas@262
   164
andreas@262
   165
+ (void)nullifySessionForThread:(PEPCopyableThread *)thread
andreas@262
   166
{
andreas@269
   167
    NSMutableDictionary<PEPCopyableThread*,PEPInternalSession*> *dict = [self sessionForThreadDict];
andreas@262
   168
    dict[thread] = nil;
andreas@262
   169
}
andreas@262
   170
andreas@262
   171
@end