2 * Copyright (c) 2012-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@
28 #import <Foundation/Foundation.h>
29 #import <dispatch/queue.h>
33 #import <utilities/debugging.h>
35 #import "SOSCloudKeychainConstants.h"
36 #import "SOSCloudKeychainClient.h"
39 #import "CKDAccount.h"
40 #import "CKDLockMonitor.h"
41 #import "XPCNotificationDispatcher.h"
43 #define XPROXYSCOPE "proxy"
45 typedef void (^FreshnessResponseBlock
)(bool success
, NSError
*err
);
47 @interface UbiqitousKVSProxy
: NSObject
<XPCNotificationListener
, CKDLockListener
>
49 id currentiCloudToken
;
53 @
property (readonly
) NSObject
<CKDStore
>* store
;
54 @
property (readonly
) NSObject
<CKDAccount
>* account
;
55 @
property (readonly
) NSObject
<CKDLockMonitor
>* lockMonitor
;
57 @
property (readonly
) NSURL
* persistenceURL
;
59 @
property (retain
, nonatomic
) NSMutableSet
*alwaysKeys
;
60 @
property (retain
, nonatomic
) NSMutableSet
*firstUnlockKeys
;
61 @
property (retain
, nonatomic
) NSMutableSet
*unlockedKeys
;
63 @
property (atomic
) bool seenKVSStoreChange
;
66 @
property (retain
, nonatomic
) NSMutableSet
*pendingKeys
;
67 @
property (retain
, nonatomic
) NSMutableSet
*shadowPendingKeys
;
69 @
property (retain
, nonatomic
) NSString
*dsid
;
70 @
property (retain
, nonatomic
) NSString
*accountUUID
;
72 @
property (retain
, nonatomic
) NSMutableSet
<NSString
*>* pendingSyncPeerIDs
;
73 @
property (retain
, nonatomic
) NSMutableSet
<NSString
*>* shadowPendingSyncPeerIDs
;
75 @
property (retain
, nonatomic
) NSMutableSet
<NSString
*>* pendingSyncBackupPeerIDs
;
76 @
property (retain
, nonatomic
) NSMutableSet
<NSString
*>* shadowPendingSyncBackupPeerIDs
;
78 @
property (atomic
) bool ensurePeerRegistration
;
79 @
property (atomic
) bool ensurePeerRegistrationEnqueuedButNotStarted
;
81 // Another version of ensurePeerRegistration due to legacy code structure
82 @
property (atomic
) bool shadowEnsurePeerRegistration
;
84 @
property (atomic
) bool inCallout
;
86 @
property (retain
, nonatomic
) NSMutableArray
<FreshnessResponseBlock
> *freshnessCompletions
;
87 @
property (atomic
) dispatch_time_t nextFreshnessTime
;
89 @
property (atomic
) dispatch_queue_t calloutQueue
;
91 @
property (atomic
) dispatch_queue_t ckdkvsproxy_queue
;
93 @
property (copy
, atomic
) dispatch_block_t shadowFlushBlock
;
96 - (NSString
*)description
;
97 - (instancetype
)init NS_UNAVAILABLE
;
99 + (instancetype
)withAccount
:(NSObject
<CKDAccount
>*) account
100 store
:(NSObject
<CKDStore
>*) store
101 lockMonitor
:(NSObject
<CKDLockMonitor
>*) lockMonitor
102 persistence
:(NSURL
*) localPersistence
;
104 - (instancetype
)initWithAccount
:(NSObject
<CKDAccount
>*) account
105 store
:(NSObject
<CKDStore
>*) store
106 lockMonitor
:(NSObject
<CKDLockMonitor
>*) lockMonitor
107 persistence
:(NSURL
*) localPersistence NS_DESIGNATED_INITIALIZER
;
112 - (void)synchronizeStore
;
113 - (id
) objectForKey
: (NSString
*) key
;
114 - (NSDictionary
<NSString
*, id
>*) copyAsDictionary
;
115 - (void)setObjectsFromDictionary
:(NSDictionary
<NSString
*, NSObject
*> *)otherDictionary
;
116 - (void)waitForSynchronization
:(void (^)(NSDictionary
<NSString
*, NSObject
*> *results
, NSError
*err
))handler
;
119 // Callbacks from stores when things happen
120 - (void)storeKeysChanged
: (NSSet
<NSString
*>*) changedKeys initial
: (bool) initial
;
121 - (void)storeAccountChanged
;
123 - (void)requestEnsurePeerRegistration
;
125 - (void)requestSyncWithPeerIDs
: (NSArray
<NSString
*>*) peerIDs backupPeerIDs
: (NSArray
<NSString
*>*) backupPeerIDs
;
126 - (BOOL
)hasSyncPendingFor
: (NSString
*) peerID
;
127 - (BOOL
)hasPendingKey
: (NSString
*) keyName
;
129 - (void)registerAtTimeKeys
:(NSDictionary
*)keyparms
;
131 - (NSSet
*) keysForCurrentLockState
;
132 - (void) intersectWithCurrentLockState
: (NSMutableSet
*) set
;
134 - (NSMutableSet
*) pendKeysAndGetNewlyPended
: (NSSet
*) keysToPend
;
136 - (NSMutableSet
*) pendingKeysForCurrentLockState
;
137 - (NSMutableSet
*) pendKeysAndGetPendingForCurrentLockState
: (NSSet
*) startingSet
;
139 - (void)processPendingKeysForCurrentLockState
;
141 - (void)registerKeys
: (NSDictionary
*)keys forAccount
: (NSString
*) accountUUID
;
142 - (void)removeKeys
: (NSArray
*)keys forAccount
: (NSString
*) accountUUID
;
144 - (void)processKeyChangedEvent
:(NSDictionary
*)keysChangedInCloud
;
145 - (NSMutableDictionary
*)copyValues
:(NSSet
*)keysOfInterest
;
147 - (void) doAfterFlush
: (dispatch_block_t
) block
;
148 - (void) calloutWith
: (void(^)(NSSet
*pending
, NSSet
* pendingSyncIDs
, NSSet
* pendingBackupSyncIDs
, bool ensurePeerRegistration
, dispatch_queue_t queue
, void(^done
)(NSSet
*handledKeys
, NSSet
*handledSyncs
, bool handledEnsurePeerRegistration
, NSError
* error
))) callout
;
149 - (void) sendKeysCallout
: (NSSet
*(^)(NSSet
* pending
, NSError
**error
)) handleKeys
;
151 - (void)perfCounters
:(void(^)(NSDictionary
*counters
))callback
;