]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OctagonFlags.m
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / ot / OctagonFlags.m
1
2 #if OCTAGON
3
4 #import "keychain/ot/OctagonFlags.h"
5 #import "keychain/ot/OTStates.h"
6 #import "keychain/ckks/CKKSCondition.h"
7
8 @interface OctagonFlags ()
9 @property dispatch_queue_t queue;
10 @property NSMutableSet<OctagonFlag*>* flags;
11 @property (readonly) NSSet* allowableFlags;
12 @end
13
14 @implementation OctagonFlags
15
16 - (instancetype)initWithQueue:(dispatch_queue_t)queue
17 flags:(NSSet<OctagonFlag*>*)possibleFlags
18 {
19 if((self = [super init])) {
20 _queue = queue;
21 _flags = [NSMutableSet set];
22 _flagConditions = [[NSMutableDictionary alloc] init];
23 _allowableFlags = possibleFlags;
24
25 for(OctagonFlag* flag in possibleFlags) {
26 self.flagConditions[flag] = [[CKKSCondition alloc] init];
27 }
28 }
29 return self;
30 }
31
32 - (NSString*)description
33 {
34 return [NSString stringWithFormat:@"<OctagonFlags: %@>", [self contentsAsString]];
35 }
36
37 - (NSString*)contentsAsString
38 {
39 if(self.flags.count == 0) {
40 return @"none";
41 }
42 return [[self.flags allObjects] componentsJoinedByString:@","];
43 }
44
45 - (NSArray<NSString*>*)dumpFlags
46 {
47 return [self.flags allObjects];
48 }
49
50 - (BOOL)_onqueueContains:(nonnull OctagonFlag *)flag {
51 dispatch_assert_queue(self.queue);
52 return [self.flags containsObject:flag];
53 }
54
55 - (void)_onqueueSetFlag:(nonnull OctagonFlag *)flag {
56 dispatch_assert_queue(self.queue);
57 [self.flags addObject:flag];
58 }
59
60 - (CKKSCondition*)conditionForFlag:(OctagonFlag*)flag {
61 return self.flagConditions[flag];
62 }
63
64 - (void)setFlag:(nonnull OctagonFlag *)flag {
65 dispatch_sync(self.queue, ^{
66 [self _onqueueSetFlag:flag];
67 });
68 }
69
70 - (void)_onqueueRemoveFlag:(nonnull OctagonFlag *)flag {
71 dispatch_assert_queue(self.queue);
72
73 NSAssert([self.allowableFlags containsObject:flag], @"state machine tried to handle unknown flag %@", flag);
74
75 [self.flags removeObject:flag];
76 [self.flagConditions[flag] fulfill];
77 self.flagConditions[flag] = [[CKKSCondition alloc]init];
78 }
79
80 @end
81
82 #endif