]> git.saurik.com Git - apple/security.git/blob - keychain/ot/OctagonStateMachineHelpers.m
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / ot / OctagonStateMachineHelpers.m
1 /*
2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if OCTAGON
25
26 #import "keychain/ot/OTStates.h"
27 #import "keychain/ot/ObjCImprovements.h"
28 #import "keychain/ot/OTDefines.h"
29 #import "keychain/ot/OTConstants.h"
30 #import "keychain/categories/NSError+UsefulConstructors.h"
31
32 OctagonState* const OctagonStateMachineNotStarted = (OctagonState*) @"not_started";
33 OctagonState* const OctagonStateMachineHalted = (OctagonState*) @"halted";
34
35 @implementation OctagonStateTransitionOperation : CKKSResultOperation
36 - (instancetype)initIntending:(OctagonState*)intendedState
37 errorState:(OctagonState*)errorState
38 {
39 if((self = [super init])) {
40 _nextState = errorState;
41 _intendedState = intendedState;
42 }
43 return self;
44 }
45
46 - (NSString*)description
47 {
48 return [NSString stringWithFormat:@"<OctagonStateTransitionOperation(%@): intended:%@ actual:%@>", self.name, self.intendedState, self.nextState];
49 }
50
51 + (instancetype)named:(NSString*)name
52 intending:(OctagonState*)intendedState
53 errorState:(OctagonState*)errorState
54 timeout:(dispatch_time_t)timeout
55 withBlockTakingSelf:(void(^)(OctagonStateTransitionOperation* op))block
56 {
57 OctagonStateTransitionOperation* op = [[self alloc] initIntending:intendedState
58 errorState:errorState];
59 WEAKIFY(op);
60 [op addExecutionBlock:^{
61 STRONGIFY(op);
62 block(op);
63 }];
64 op.name = name;
65 [op timeout:timeout];
66 return op;
67 }
68
69 + (instancetype)named:(NSString*)name
70 entering:(OctagonState*)intendedState
71 {
72 OctagonStateTransitionOperation* op = [[self alloc] initIntending:intendedState
73 errorState:intendedState];
74 op.name = name;
75 return op;
76 }
77 @end
78
79 @interface OctagonStateTransitionRequest ()
80 @property dispatch_queue_t queue;
81 @property bool timeoutCanOccur;
82 @end
83
84 @implementation OctagonStateTransitionRequest
85
86 - (instancetype)init:(NSString*)name
87 sourceStates:(NSSet<OctagonState*>*)sourceStates
88 serialQueue:(dispatch_queue_t)queue
89 timeout:(dispatch_time_t)timeout
90 transitionOp:(CKKSResultOperation<OctagonStateTransitionOperationProtocol>*)transitionOp
91 {
92 if((self = [super init])) {
93 _name = name;
94 _sourceStates = sourceStates;
95 _queue = queue;
96
97 _timeoutCanOccur = true;
98 _transitionOperation = transitionOp;
99 }
100
101 [self timeout:timeout];
102
103 return self;
104 }
105
106 - (NSString*)description
107 {
108 return [NSString stringWithFormat:@"<OctagonStateTransitionRequest: %@ %@ %@>", self.name, self.transitionOperation, self.sourceStates];
109 }
110
111 - (CKKSResultOperation<OctagonStateTransitionOperationProtocol>* _Nullable)_onqueueStart
112 {
113 dispatch_assert_queue(self.queue);
114
115 if(self.timeoutCanOccur) {
116 self.timeoutCanOccur = false;
117 return self.transitionOperation;
118 } else {
119 return nil;
120 }
121 }
122
123 - (instancetype)timeout:(dispatch_time_t)timeout
124 {
125 WEAKIFY(self);
126 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), self.queue, ^{
127 STRONGIFY(self);
128 if(self.timeoutCanOccur) {
129 self.timeoutCanOccur = false;
130
131 // The operation will only realize it's finished once added to any operation queue. Fake one up.
132 [self.transitionOperation timeout:0*NSEC_PER_SEC];
133 [[[NSOperationQueue alloc] init] addOperation:self.transitionOperation];
134 }
135 });
136
137 return self;
138 }
139
140 @end
141
142 #endif // OCTAGON