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