]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/SOSIntervalEvent.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / SOSIntervalEvent.m
1 //
2 // SOSIntervalEvent.m
3 // Security_ios
4 //
5 // Created by murf on 9/12/19.
6 //
7
8 #import <Foundation/Foundation.h>
9 #import "SOSIntervalEvent.h"
10 #import "keychain/SecureObjectSync/SOSInternal.h"
11
12 /*
13
14 interval setting examples:
15 NSTimeInterval earliestGB = 60*60*24*3; // wait at least 3 days
16 NSTimeInterval latestGB = 60*60*24*7; // wait at most 7 days
17
18 pattern:
19
20 SOSIntervalEvent fooEvent = [[SOSIntervalEvent alloc] initWithDefaults:account.settings dateDescription:@"foocheck" earliest:60*60*24 latest:60*60*36];
21
22 // should we foo?
23 if([fooEvent checkDate]) {
24 WeDoFooToo();
25 // schedule next foo
26 [fooEvent followup];
27 }
28 // "schedule" is only used if you think there's a date upcoming you don't want altered
29 // getDate will return the next schedule event date
30 */
31
32 @implementation SOSIntervalEvent
33
34 - (NSDate *) getDate {
35 return [_defaults valueForKey: _dateDescription];
36 }
37
38 - (bool) checkDate {
39 NSDate *theDate = [self getDate];
40 if(theDate && ([theDate timeIntervalSinceNow] <= 0)) return true;
41 return false;
42 }
43
44 - (void) followup {
45 NSDate *theDate = SOSCreateRandomDateBetweenNowPlus(_earliestDate, _latestDate);
46 [_defaults setValue:theDate forKey: _dateDescription];
47 }
48
49 - (void)schedule {
50 NSDate *theDate = [self getDate];
51 if(!theDate) {
52 [self followup];
53 }
54 }
55
56 -(id)initWithDefaults:(NSUserDefaults*) defaults dateDescription:(NSString *)dateDescription earliest:(NSTimeInterval) earliest latest: (NSTimeInterval) latest {
57 if ((self = [super init])) {
58 _defaults = defaults;
59 if(! _defaults) {
60 _defaults = [[NSUserDefaults alloc] init];
61 }
62 _dateDescription = dateDescription;
63 _earliestDate = earliest;
64 _latestDate = latest;
65 [self schedule];
66 }
67 return self;
68 }
69
70 @end
71