IOSAD-172 Re-order cache methods IOSAD-172
authorDirk Zimmermann <dz@pep.security>
Mon, 29 Jun 2020 16:39:43 +0200
branchIOSAD-172
changeset 1548bd20f1a6c3cc
parent 1547 516474ecf260
child 1549 cf7b3b9c0e96
IOSAD-172 Re-order cache methods
pEpObjCAdapter/PEPPassphraseCache.m
     1.1 --- a/pEpObjCAdapter/PEPPassphraseCache.m	Mon Jun 29 16:39:43 2020 +0200
     1.2 +++ b/pEpObjCAdapter/PEPPassphraseCache.m	Mon Jun 29 16:39:43 2020 +0200
     1.3 @@ -16,6 +16,7 @@
     1.4  static NSTimeInterval s_defaultTimeoutInSeconds = 10 * 60;
     1.5  static NSTimeInterval s_defaultCheckExpiryInterval = 60;
     1.6  
     1.7 +/// Extension for internals
     1.8  @interface PEPPassphraseCache ()
     1.9  
    1.10  /// Timeout of passwords in seconds.
    1.11 @@ -29,48 +30,13 @@
    1.12  
    1.13  @implementation PEPPassphraseCache
    1.14  
    1.15 -static PEPPassphraseCache *s_sharedInstance;
    1.16 -
    1.17 -+ (void)initialize
    1.18 -{
    1.19 -    static BOOL initialized = NO;
    1.20 -    if (!initialized) {
    1.21 -        initialized = YES;
    1.22 -        s_sharedInstance = [[PEPPassphraseCache alloc] init];
    1.23 -    }
    1.24 -}
    1.25 +#pragma mark - API
    1.26  
    1.27  + (instancetype)sharedInstance
    1.28  {
    1.29      return s_sharedInstance;
    1.30  }
    1.31  
    1.32 -/// Internal constructor (for now).
    1.33 -- (instancetype)initWithPassphraseTimeout:(NSTimeInterval)timeout
    1.34 -                      checkExpiryInterval:(NSTimeInterval)checkExpiryInterval
    1.35 -{
    1.36 -    self = [super init];
    1.37 -    if (self) {
    1.38 -        _timeout = timeout;
    1.39 -        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
    1.40 -        _mutablePassphraseEntries = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
    1.41 -
    1.42 -        // we have a strong reference to the timer, but the timer doesn't have one to us
    1.43 -        typeof(self) __weak weakSelf = self;
    1.44 -
    1.45 -        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
    1.46 -        dispatch_source_set_timer(_timer,
    1.47 -                                  DISPATCH_TIME_NOW,
    1.48 -                                  checkExpiryInterval * NSEC_PER_SEC,
    1.49 -                                  checkExpiryInterval / 10 * NSEC_PER_SEC);
    1.50 -        dispatch_source_set_event_handler(_timer, ^{
    1.51 -            [weakSelf removeStaleEntries];
    1.52 -        });
    1.53 -        dispatch_resume(_timer);
    1.54 -    }
    1.55 -    return self;
    1.56 -}
    1.57 -
    1.58  /// Public constructor with default values.
    1.59  - (instancetype)init
    1.60  {
    1.61 @@ -105,23 +71,6 @@
    1.62      return [NSArray arrayWithArray:resultingPassphrases];
    1.63  }
    1.64  
    1.65 -/// Remove password entries that have timed out.
    1.66 -/// - Note: Assumes it gets called on `queue`.
    1.67 -- (void)removeStaleEntries
    1.68 -{
    1.69 -    NSMutableArray *resultingPassphrases = [NSMutableArray
    1.70 -                                            arrayWithCapacity:s_maxNumberOfPassphrases];
    1.71 -
    1.72 -    for (PEPPassphraseCacheEntry *entry in self.mutablePassphraseEntries) {
    1.73 -        if (![self isExpiredPassphraseEntry:entry]) {
    1.74 -            [resultingPassphrases addObject:entry];
    1.75 -        }
    1.76 -    }
    1.77 -
    1.78 -    [self.mutablePassphraseEntries removeAllObjects];
    1.79 -    [self.mutablePassphraseEntries addObjectsFromArray:resultingPassphrases];
    1.80 -}
    1.81 -
    1.82  - (void)resetTimeoutForPassphrase:(NSString *)passphrase
    1.83  {
    1.84      if ([passphrase isEqualToString:@""]) {
    1.85 @@ -145,6 +94,64 @@
    1.86      });
    1.87  }
    1.88  
    1.89 +#pragma mark - Internals
    1.90 +
    1.91 +static PEPPassphraseCache *s_sharedInstance;
    1.92 +
    1.93 ++ (void)initialize
    1.94 +{
    1.95 +    static BOOL initialized = NO;
    1.96 +    if (!initialized) {
    1.97 +        initialized = YES;
    1.98 +        s_sharedInstance = [[PEPPassphraseCache alloc] init];
    1.99 +    }
   1.100 +}
   1.101 +
   1.102 +/// Internal constructor (for now).
   1.103 +- (instancetype)initWithPassphraseTimeout:(NSTimeInterval)timeout
   1.104 +                      checkExpiryInterval:(NSTimeInterval)checkExpiryInterval
   1.105 +{
   1.106 +    self = [super init];
   1.107 +    if (self) {
   1.108 +        _timeout = timeout;
   1.109 +        _queue = dispatch_queue_create("PEPPassphraseCache Queue", DISPATCH_QUEUE_SERIAL);
   1.110 +        _mutablePassphraseEntries = [NSMutableArray arrayWithCapacity:s_maxNumberOfPassphrases];
   1.111 +
   1.112 +        // we have a strong reference to the timer, but the timer doesn't have one to us
   1.113 +        typeof(self) __weak weakSelf = self;
   1.114 +
   1.115 +        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);
   1.116 +        dispatch_source_set_timer(_timer,
   1.117 +                                  DISPATCH_TIME_NOW,
   1.118 +                                  checkExpiryInterval * NSEC_PER_SEC,
   1.119 +                                  checkExpiryInterval / 10 * NSEC_PER_SEC);
   1.120 +        dispatch_source_set_event_handler(_timer, ^{
   1.121 +            [weakSelf removeStaleEntries];
   1.122 +        });
   1.123 +        dispatch_resume(_timer);
   1.124 +    }
   1.125 +    return self;
   1.126 +}
   1.127 +
   1.128 +/// Remove password entries that have timed out.
   1.129 +/// - Note: Assumes it gets called on `queue`.
   1.130 +- (void)removeStaleEntries
   1.131 +{
   1.132 +    NSMutableArray *resultingPassphrases = [NSMutableArray
   1.133 +                                            arrayWithCapacity:s_maxNumberOfPassphrases];
   1.134 +
   1.135 +    for (PEPPassphraseCacheEntry *entry in self.mutablePassphraseEntries) {
   1.136 +        if (![self isExpiredPassphraseEntry:entry]) {
   1.137 +            [resultingPassphrases addObject:entry];
   1.138 +        }
   1.139 +    }
   1.140 +
   1.141 +    [self.mutablePassphraseEntries removeAllObjects];
   1.142 +    [self.mutablePassphraseEntries addObjectsFromArray:resultingPassphrases];
   1.143 +}
   1.144 +
   1.145 +#pragma mark - Helpers
   1.146 +
   1.147  - (BOOL)isExpiredPassphraseEntry:(PEPPassphraseCacheEntry *)passphraseEntry
   1.148  {
   1.149      NSDate *now = [NSDate date];