4 #import "keychain/categories/NSError+UsefulConstructors.h"
5 #import "keychain/ckks/CKKSLocalResetOperation.h"
6 #import "keychain/ckks/CKKSZoneStateEntry.h"
7 #import "keychain/ckks/CKKSOutgoingQueueEntry.h"
8 #import "keychain/ckks/CKKSIncomingQueueEntry.h"
9 #import "keychain/ckks/CKKSCurrentItemPointer.h"
10 #import "keychain/ckks/CKKSMirrorEntry.h"
11 #import "keychain/ot/OTDefines.h"
13 @implementation CKKSLocalResetOperation
15 @synthesize nextState = _nextState;
16 @synthesize intendedState = _intendedState;
18 - (instancetype)initWithDependencies:(CKKSOperationDependencies*)dependencies
19 intendedState:(OctagonState*)intendedState
20 errorState:(OctagonState*)errorState
22 if(self = [super init]) {
25 _intendedState = intendedState;
26 _nextState = errorState;
28 self.name = @"ckks-local-reset";
34 [self.deps.databaseProvider dispatchSyncWithSQLTransaction:^CKKSDatabaseTransactionResult {
35 [self onqueuePerformLocalReset];
36 return CKKSDatabaseTransactionCommit;
40 - (void)onqueuePerformLocalReset
42 NSError* localerror = nil;
44 CKKSZoneStateEntry* ckse = [CKKSZoneStateEntry state:self.deps.zoneID.zoneName];
45 ckse.ckzonecreated = false;
46 ckse.ckzonesubscribed = false; // I'm actually not sure about this: can you be subscribed to a non-existent zone?
47 ckse.changeToken = NULL;
48 [ckse saveToDatabase:&localerror];
49 if(localerror && self.error == nil) {
50 ckkserror("local-reset", self.deps.zoneID, "couldn't reset zone status: %@", localerror);
51 self.error = localerror;
55 [CKKSMirrorEntry deleteAll:self.deps.zoneID error:&localerror];
56 if(localerror && self.error == nil) {
57 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSMirrorEntry: %@", localerror);
58 self.error = localerror;
62 [CKKSOutgoingQueueEntry deleteAll:self.deps.zoneID error:&localerror];
63 if(localerror && self.error == nil) {
64 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSOutgoingQueueEntry: %@", localerror);
65 self.error = localerror;
69 [CKKSIncomingQueueEntry deleteAll:self.deps.zoneID error:&localerror];
70 if(localerror && self.error == nil) {
71 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSIncomingQueueEntry: %@", localerror);
72 self.error = localerror;
76 [CKKSKey deleteAll:self.deps.zoneID error:&localerror];
77 if(localerror && self.error == nil) {
78 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSKey: %@", localerror);
79 self.error = localerror;
83 [CKKSTLKShareRecord deleteAll:self.deps.zoneID error:&localerror];
84 if(localerror && self.error == nil) {
85 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSTLKShare: %@", localerror);
86 self.error = localerror;
90 [CKKSCurrentKeyPointer deleteAll:self.deps.zoneID error:&localerror];
91 if(localerror && self.error == nil) {
92 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSCurrentKeyPointer: %@", localerror);
93 self.error = localerror;
97 [CKKSCurrentItemPointer deleteAll:self.deps.zoneID error:&localerror];
98 if(localerror && self.error == nil) {
99 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSCurrentItemPointer: %@", localerror);
100 self.error = localerror;
104 [CKKSDeviceStateEntry deleteAll:self.deps.zoneID error:&localerror];
105 if(localerror && self.error == nil) {
106 ckkserror("local-reset", self.deps.zoneID, "couldn't delete all CKKSDeviceStateEntry: %@", localerror);
107 self.error = localerror;
112 ckksnotice("local-reset", self.deps.zoneID, "Successfully deleted all local data");
113 self.nextState = self.intendedState;