2 * Copyright (c) 2018 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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"
32 OctagonState* const OctagonStateMachineNotStarted = (OctagonState*) @"not_started";
33 OctagonState* const OctagonStateMachineHalted = (OctagonState*) @"halted";
35 @implementation OctagonStateTransitionOperation : CKKSResultOperation
36 - (instancetype)initIntending:(OctagonState*)intendedState
37 errorState:(OctagonState*)errorState
39 if((self = [super init])) {
40 _nextState = errorState;
41 _intendedState = intendedState;
46 - (NSString*)description
48 return [NSString stringWithFormat:@"<OctagonStateTransitionOperation(%@): intended:%@ actual:%@>", self.name, self.intendedState, self.nextState];
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
57 OctagonStateTransitionOperation* op = [[self alloc] initIntending:intendedState
58 errorState:errorState];
60 [op addExecutionBlock:^{
69 + (instancetype)named:(NSString*)name
70 entering:(OctagonState*)intendedState
72 OctagonStateTransitionOperation* op = [[self alloc] initIntending:intendedState
73 errorState:intendedState];
79 @interface OctagonStateTransitionRequest ()
80 @property dispatch_queue_t queue;
81 @property bool timeoutCanOccur;
84 @implementation OctagonStateTransitionRequest
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
92 if((self = [super init])) {
94 _sourceStates = sourceStates;
97 _timeoutCanOccur = true;
98 _transitionOperation = transitionOp;
101 [self timeout:timeout];
106 - (NSString*)description
108 return [NSString stringWithFormat:@"<OctagonStateTransitionRequest: %@ %@ %@>", self.name, self.transitionOperation, self.sourceStates];
111 - (CKKSResultOperation<OctagonStateTransitionOperationProtocol>* _Nullable)_onqueueStart
113 dispatch_assert_queue(self.queue);
115 if(self.timeoutCanOccur) {
116 self.timeoutCanOccur = false;
117 return self.transitionOperation;
123 - (instancetype)timeout:(dispatch_time_t)timeout
126 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), self.queue, ^{
128 if(self.timeoutCanOccur) {
129 self.timeoutCanOccur = false;
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];