2 * Copyright (c) 2020 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 <TrustedPeers/TrustedPeers.h>
27 #import "keychain/ckks/CKKSAnalytics.h"
28 #import "keychain/ot/categories/OTAccountMetadataClassC+KeychainSupport.h"
29 #import "keychain/ot/OTModifyUserControllableViewStatusOperation.h"
30 #import "keychain/ot/OTStates.h"
31 #import "keychain/ot/ObjCImprovements.h"
32 #import "keychain/TrustedPeersHelper/TrustedPeersHelperProtocol.h"
34 @interface OTModifyUserControllableViewStatusOperation ()
35 @property OTOperationDependencies* deps;
37 @property OctagonState* peerMissingState;
39 @property TPPBPeerStableInfo_UserControllableViewStatus intendedViewStatus;
42 @implementation OTModifyUserControllableViewStatusOperation
43 @synthesize intendedState = _intendedState;
44 @synthesize nextState = _nextState;
46 - (instancetype)initWithDependencies:(OTOperationDependencies*)dependencies
47 intendedViewStatus:(TPPBPeerStableInfo_UserControllableViewStatus)intendedViewStatus
48 intendedState:(OctagonState*)intendedState
49 peerMissingState:(OctagonState*)peerMissingState
50 errorState:(OctagonState*)errorState
52 if ((self = [super init])) {
55 _intendedViewStatus = intendedViewStatus;
57 _intendedState = intendedState;
58 _peerMissingState = peerMissingState;
59 _nextState = errorState;
67 if(self.intendedViewStatus == TPPBPeerStableInfo_UserControllableViewStatus_FOLLOWING) {
68 #if TARGET_OS_WATCH || TARGET_OS_TV
69 // Watches and TVs want to be able to set the FOLLOWING state
70 [self performWithStatus:self.intendedViewStatus];
72 // For other platforms, we want to determine the actual state by asking
75 // Should we ask SOS? Or Octagon?
76 if(self.deps.sosAdapter.sosEnabled) {
78 BOOL safariViewEnabled = [self.deps.sosAdapter safariViewSyncingEnabled:&error];
81 secerror("octagon-ckks: Unable to fetch SOS Safari view status: %@", error);
86 secnotice("octagon-ckks", "Currently SOS believes the safari view is '%@'", safariViewEnabled ? @"enabled" : @"disabled");
88 TPPBPeerStableInfo_UserControllableViewStatus status = safariViewEnabled ?
89 TPPBPeerStableInfo_UserControllableViewStatus_ENABLED :
90 TPPBPeerStableInfo_UserControllableViewStatus_DISABLED;
92 [self performWithStatus:status];
96 secnotice("octagon-ckks", "Determining peers' user-controllable views policy");
98 [self.deps.cuttlefishXPCWrapper fetchCurrentPolicyWithContainer:self.deps.containerName
99 context:self.deps.contextID
101 reply:^(TPSyncingPolicy* _Nullable syncingPolicy,
102 TPPBPeerStableInfo_UserControllableViewStatus userControllableViewStatusOfPeers,
103 NSError* _Nullable error) {
107 secnotice("octagon-ckks", "Determining peers' user-controllable views policy failed: %@", error);
112 secnotice("octagon-ckks", "Retrieved peers' user-controllable views policy as: %@",
113 TPPBPeerStableInfo_UserControllableViewStatusAsString(userControllableViewStatusOfPeers));
115 [self performWithStatus:userControllableViewStatusOfPeers];
121 [self performWithStatus:self.intendedViewStatus];
125 - (void)performWithStatus:(TPPBPeerStableInfo_UserControllableViewStatus)intendedViewStatus
129 secnotice("octagon-ckks", "Setting user-controllable views to %@", TPPBPeerStableInfo_UserControllableViewStatusAsString(self.intendedViewStatus));
131 [self.deps.cuttlefishXPCWrapper updateWithContainer:self.deps.containerName
132 context:self.deps.contextID
138 syncUserControllableViews:[NSNumber numberWithInt:intendedViewStatus]
139 reply:^(TrustedPeersHelperPeerState* peerState, TPSyncingPolicy* syncingPolicy, NSError* error) {
141 if(error || !syncingPolicy) {
142 secerror("octagon-ckks: setting user-controllable views status errored: %@", error);
145 if([self.deps.lockStateTracker isLockedError:self.error]) {
146 secnotice("octagon-ckks", "Updating user-controllable view status failed because of lock state, will retry once unlocked: %@", self.error);
147 OctagonPendingFlag* pendingFlag = [[OctagonPendingFlag alloc] initWithFlag:OctagonFlagAttemptUserControllableViewStatusUpgrade
148 conditions:OctagonPendingConditionsDeviceUnlocked];
150 [self.deps.flagHandler handlePendingFlag:pendingFlag];
152 if(peerState.peerStatus & (TPPeerStatusExcluded | TPPeerStatusUnknown)) {
153 secnotice("octagon-ckks", "Updating user-controllable view status failed because our self peer is excluded or missing");
154 self.nextState = self.peerMissingState;
159 secnotice("octagon-ckks", "Received syncing policy %@ with view list: %@", syncingPolicy, syncingPolicy.viewList);
161 NSError* stateError = nil;
162 [self.deps.stateHolder persistAccountChanges:^OTAccountMetadataClassC * _Nullable(OTAccountMetadataClassC * _Nonnull metadata) {
163 [metadata setTPSyncingPolicy:syncingPolicy];
165 } error:&stateError];
168 secerror("octagon: failed to save policy+views: %@", stateError);
169 self.error = stateError;
173 [self.deps.viewManager setCurrentSyncingPolicy:syncingPolicy];
175 self.nextState = self.intendedState;