2 * Copyright (c) 2013-2014 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@
25 #import "KDSecCircle.h"
26 #import "KDCirclePeer.h"
28 #include <dispatch/dispatch.h>
29 #import "SecureObjectSync/SOSCloudCircle.h"
30 #include "SecureObjectSync/SOSPeerInfo.h"
31 #import <CloudServices/SecureBackup.h>
32 #include "../utilities/utilities/debugging.h"
34 @interface KDSecCircle ()
35 @property (retain) NSMutableArray *callbacks;
37 @property (readwrite) unsigned long long changeCount;
39 @property (readwrite) SOSCCStatus rawStatus;
41 @property (readwrite) NSString *status;
42 @property (readwrite) NSError *error;
44 @property (readwrite) NSArray *peers;
45 @property (readwrite) NSArray *applicants;
47 @property (readwrite) dispatch_queue_t queue_;
51 @implementation KDSecCircle
55 // XXX: assert not on main_queue
56 CFErrorRef err = NULL;
57 SOSCCStatus newRawStatus = SOSCCThisDeviceIsInCircle(&err);
58 NSArray *peerInfos = (__bridge NSArray *) SOSCCCopyApplicantPeerInfo(&err);
59 NSMutableArray *newApplicants = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
60 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
61 [newApplicants addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
64 peerInfos = (__bridge NSArray *) SOSCCCopyPeerPeerInfo(&err);
65 NSMutableArray *newPeers = [[NSMutableArray alloc] initWithCapacity:peerInfos.count];
66 [peerInfos enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
67 [newPeers addObject:[[KDCirclePeer alloc] initWithPeerObject:obj]];
70 NSLog(@"rawStatus %d, #applicants %lu, #peers %lu, err=%@", newRawStatus, (unsigned long)[newApplicants count], (unsigned long)[newPeers count], err);
72 dispatch_async(dispatch_get_main_queue(), ^{
73 self.rawStatus = newRawStatus;
75 switch (newRawStatus) {
77 self.status = @"In Circle";
80 case kSOSCCNotInCircle:
81 self.status = @"Not In Circle";
84 case kSOSCCRequestPending:
85 self.status = @"Request Pending";
88 case kSOSCCCircleAbsent:
89 self.status = @"Circle Absent";
93 self.status = [NSString stringWithFormat:@"Error: %@", err];
97 self.status = [NSString stringWithFormat:@"Unknown status code %d", self.rawStatus];
101 self.applicants = [newApplicants copy];
102 self.peers = [newPeers copy];
103 self.error = (__bridge NSError *)(err);
106 for (dispatch_block_t callback in self.callbacks) {
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.
116 // Er, now that I look more closely maybe SOSPeerInfoGetPeerID...
118 typedef void (^applicantBlock)(id applicant);
120 -(void)forApplicantId:(NSString*)applicantId run:(applicantBlock)applicantBlock
122 dispatch_async(self.queue_, ^{
123 for (KDCirclePeer *applicant in self.applicants) {
124 if ([applicantId isEqualToString:applicantId]) {
125 applicantBlock(applicant.peerObject);
132 -(void)acceptApplicantId:(NSString*)applicantId
134 [self forApplicantId:applicantId run:^void(id applicant) {
135 CFErrorRef err = NULL;
136 bool ok = SOSCCAcceptApplicants((__bridge CFArrayRef)(@[applicant]), &err);
137 NSAssert(ok, @"Error %@ while accepting %@ (%@)", err, applicantId, applicant);
141 -(void)rejectApplicantId:(NSString*)applicantId
143 [self forApplicantId:applicantId run:^void(id applicant) {
144 CFErrorRef err = NULL;
145 bool ok = SOSCCRejectApplicants((__bridge CFArrayRef)(@[applicant]), &err);
146 NSAssert(ok, @"Error %@ while rejecting %@ (%@)", err, applicantId, applicant);
155 self->_queue_ = dispatch_queue_create([[NSString stringWithFormat:@"KDSecCircle@%p", self] UTF8String], NULL);
156 self->_callbacks = [NSMutableArray new];
157 notify_register_dispatch(kSOSCCCircleChangedNotification, &token, self.queue_, ^(int token){
164 -(void)addChangeCallback:(dispatch_block_t)callback
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), ^{
178 return (self.rawStatus == kSOSCCInCircle);
183 return (self.rawStatus == kSOSCCNotInCircle || self.rawStatus == kSOSCCCircleAbsent);
188 CFErrorRef err = NULL;
189 if (self.rawStatus == kSOSCCCircleAbsent) {
190 SOSCCResetToOffering(&err);
192 SOSCCRequestToJoinCircle(&err);
198 CFErrorRef err = NULL;
199 SOSCCRemoveThisDeviceFromCircle(&err);