]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2013-2014 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 | ||
25 | #import "KDSecCircle.h" | |
26 | #import "KDCirclePeer.h" | |
27 | #include <notify.h> | |
28 | #include <dispatch/dispatch.h> | |
29 | ||
30 | #import <Security/SecureObjectSync/SOSCloudCircle.h> | |
31 | #import <Security/SecureObjectSync/SOSPeerInfo.h> | |
32 | ||
33 | #import <CloudServices/SecureBackup.h> | |
34 | ||
35 | #include <utilities/debugging.h> | |
36 | ||
37 | @interface KDSecCircle () | |
38 | @property (retain) NSMutableArray *callbacks; | |
39 | ||
40 | @property (readwrite) unsigned long long changeCount; | |
41 | ||
42 | @property (readwrite) SOSCCStatus rawStatus; | |
43 | ||
44 | @property (readwrite) NSString *status; | |
45 | @property (readwrite) NSError *error; | |
46 | ||
47 | @property (readwrite) NSArray *peers; | |
48 | @property (readwrite) NSArray *applicants; | |
49 | ||
50 | @property (readwrite) dispatch_queue_t queue_; | |
51 | ||
52 | @end | |
53 | ||
54 | @implementation KDSecCircle | |
55 | ||
56 | -(void)updateCheck | |
57 | { | |
58 | // XXX: assert not on main_queue | |
59 | CFErrorRef err = NULL; | |
60 | ||
61 | SOSCCValidateUserPublic(NULL); // requires the account queue - makes the rest of this wait for fresh info. This used to happen in SOSCCThisDeviceIsInCircle(below) before we made it use cached info. | |
62 | SOSCCStatus newRawStatus = SOSCCThisDeviceIsInCircle(&err); | |
63 | NSArray *peerInfos = (__bridge NSArray *) SOSCCCopyApplicantPeerInfo(&err); | |
64 | NSMutableArray *newApplicants = [[NSMutableArray alloc] initWithCapacity:peerInfos.count]; | |
65 | [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
66 | [newApplicants addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]]; | |
67 | }]; | |
68 | ||
69 | peerInfos = (__bridge NSArray *) SOSCCCopyPeerPeerInfo(&err); | |
70 | NSMutableArray *newPeers = [[NSMutableArray alloc] initWithCapacity:peerInfos.count]; | |
71 | [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
72 | [newPeers addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]]; | |
73 | }]; | |
74 | ||
75 | secdebug("kcn", "rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err); | |
76 | ||
77 | dispatch_async(dispatch_get_main_queue(), ^{ | |
78 | self.rawStatus = newRawStatus; | |
79 | ||
80 | switch (newRawStatus) { | |
81 | case kSOSCCInCircle: | |
82 | self.status = @"In Circle"; | |
83 | break; | |
84 | ||
85 | case kSOSCCNotInCircle: | |
86 | self.status = @"Not In Circle"; | |
87 | break; | |
88 | ||
89 | case kSOSCCRequestPending: | |
90 | self.status = @"Request Pending"; | |
91 | break; | |
92 | ||
93 | case kSOSCCCircleAbsent: | |
94 | self.status = @"Circle Absent"; | |
95 | break; | |
96 | ||
97 | case kSOSCCError: | |
98 | self.status = [NSString stringWithFormat:@"Error: %@", err]; | |
99 | break; | |
100 | ||
101 | default: | |
102 | self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus]; | |
103 | break; | |
104 | } | |
105 | ||
106 | self.applicants = [newApplicants copy]; | |
107 | self.peers = [newPeers copy]; | |
108 | self.error = (__bridge NSError *)(err); | |
109 | ||
110 | self.changeCount++; | |
111 | for (dispatch_block_t callback in self.callbacks) { | |
112 | callback(); | |
113 | } | |
114 | }); | |
115 | } | |
116 | ||
117 | // XXX It's a botch to use the "name" and not applicant, but | |
118 | // it is hard to get anythign else to survive a serialastion | |
119 | // trip thoguth NSUserNotificationCenter. | |
120 | // | |
121 | // Er, now that I look more closely maybe SOSPeerInfoGetPeerID... | |
122 | ||
123 | typedef void (^applicantBlock)(id applicant); | |
124 | ||
125 | -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock | |
126 | { | |
127 | dispatch_async(self.queue_, ^{ | |
128 | for (KDCirclePeer *applicant in self.applicants) { | |
129 | if ([applicantId isEqualToString:applicantId]) { | |
130 | applicantBlock(applicant.peerObject); | |
131 | break; | |
132 | } | |
133 | } | |
134 | }); | |
135 | } | |
136 | ||
137 | // Tell clang that these bools are okay, even if NSAssert doesn't use them | |
138 | #pragma clang diagnostic push | |
139 | #pragma clang diagnostic ignored "-Wunused-variable" | |
140 | ||
141 | -(void)acceptApplicantId:(NSString*)applicantId | |
142 | { | |
143 | [self forApplicantId:applicantId run:^void(id applicant) { | |
144 | CFErrorRef err = NULL; | |
145 | bool ok = SOSCCAcceptApplicants((__bridge CFArrayRef)(@[applicant]), &err); | |
146 | NSAssert(ok, @"Error %@ while accepting %@ (%@)", err, applicantId, applicant); | |
147 | }]; | |
148 | } | |
149 | ||
150 | -(void)rejectApplicantId:(NSString*)applicantId | |
151 | { | |
152 | [self forApplicantId:applicantId run:^void(id applicant) { | |
153 | CFErrorRef err = NULL; | |
154 | bool ok = SOSCCRejectApplicants((__bridge CFArrayRef)(@[applicant]), &err); | |
155 | NSAssert(ok, @"Error %@ while rejecting %@ (%@)", err, applicantId, applicant); | |
156 | }]; | |
157 | } | |
158 | ||
159 | #pragma clang diagnostic pop | |
160 | ||
161 | -(id)init | |
162 | { | |
163 | if ((self = [super init])) { | |
164 | int token; | |
165 | ||
166 | self->_queue_ = dispatch_queue_create([[NSString stringWithFormat:@"KDSecCircle@%p", self] UTF8String], NULL); | |
167 | self->_callbacks = [NSMutableArray new]; | |
168 | notify_register_dispatch(kSOSCCCircleChangedNotification, &token, self.queue_, ^(int token1) { | |
169 | [self updateCheck]; | |
170 | }); | |
171 | } | |
172 | return self; | |
173 | } | |
174 | ||
175 | -(void)addChangeCallback:(dispatch_block_t)callback | |
176 | { | |
177 | [self.callbacks addObject:callback]; | |
178 | if (self.changeCount) { | |
179 | dispatch_async(dispatch_get_main_queue(), callback); | |
180 | } else if (self.callbacks.count == 1) { | |
181 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
182 | [self updateCheck]; | |
183 | }); | |
184 | } | |
185 | } | |
186 | ||
187 | -(BOOL)isInCircle | |
188 | { | |
189 | return (self.rawStatus == kSOSCCInCircle); | |
190 | } | |
191 | ||
192 | -(BOOL)isOutOfCircle | |
193 | { | |
194 | return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent); | |
195 | } | |
196 | ||
197 | -(void)enableSync | |
198 | { | |
199 | CFErrorRef err = NULL; | |
200 | if (self.rawStatus == kSOSCCCircleAbsent) { | |
201 | SOSCCResetToOffering(&err); | |
202 | } else { | |
203 | SOSCCRequestToJoinCircle(&err); | |
204 | } | |
205 | ||
206 | CFMutableSetRef viewsToEnable = CFSetCreateMutable(NULL, 0, NULL); | |
207 | CFMutableSetRef viewsToDisable = CFSetCreateMutable(NULL, 0, NULL); | |
208 | CFSetAddValue(viewsToEnable, (void*)kSOSViewWiFi); | |
209 | CFSetAddValue(viewsToEnable, (void*)kSOSViewAutofillPasswords); | |
210 | CFSetAddValue(viewsToEnable, (void*)kSOSViewSafariCreditCards); | |
211 | CFSetAddValue(viewsToEnable, (void*)kSOSViewOtherSyncable); | |
212 | ||
213 | SOSCCViewSet(viewsToEnable, viewsToDisable); | |
214 | CFRelease(viewsToEnable); | |
215 | CFRelease(viewsToDisable); | |
216 | } | |
217 | ||
218 | -(void)disableSync | |
219 | { | |
220 | CFErrorRef err = NULL; | |
221 | SOSCCRemoveThisDeviceFromCircle(&err); | |
222 | } | |
223 | ||
224 | @end |