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"
41 #define XPROXYSCOPE "proxy"
43 typedef void (^FreshnessResponseBlock
)(bool success
, NSError
*err
);
45 @interface UbiqitousKVSProxy
: NSObject
47 id currentiCloudToken
;
51 @
property (readonly
) NSObject
<CKDStore
>* store
;
52 @
property (readonly
) NSObject
<CKDAccount
>* account
;
55 @
property (retain
, nonatomic
) NSMutableSet
*alwaysKeys
;
56 @
property (retain
, nonatomic
) NSMutableSet
*firstUnlockKeys
;
57 @
property (retain
, nonatomic
) NSMutableSet
*unlockedKeys
;
59 @
property (atomic
) bool unlockedSinceBoot
;
60 @
property (atomic
) bool isLocked
;
61 @
property (atomic
) bool seenKVSStoreChange
;
64 @
property (retain
, nonatomic
) NSMutableSet
*pendingKeys
;
65 @
property (retain
, nonatomic
) NSMutableSet
*shadowPendingKeys
;
67 @
property (retain
, nonatomic
) NSString
*dsid
;
69 @
property (atomic
) bool syncWithPeersPending
;
70 @
property (atomic
) bool shadowSyncWithPeersPending
;
72 @
property (atomic
) bool ensurePeerRegistration
;
73 @
property (atomic
) bool shadowEnsurePeerRegistration
;
75 @
property (atomic
) bool inCallout
;
77 @
property (retain
, nonatomic
) NSMutableArray
<FreshnessResponseBlock
> *freshnessCompletions
;
78 @
property (atomic
) dispatch_time_t nextFreshnessTime
;
80 @
property (atomic
) dispatch_source_t syncTimer
;
81 @
property (atomic
) bool syncTimerScheduled
;
83 @
property (atomic
) dispatch_time_t deadline
;
84 @
property (atomic
) dispatch_time_t lastSyncTime
;
87 @
property (atomic
) dispatch_queue_t calloutQueue
;
89 @
property (atomic
) dispatch_queue_t ckdkvsproxy_queue
;
90 @
property (atomic
) dispatch_source_t penaltyTimer
;
91 @
property (atomic
) bool penaltyTimerScheduled
;
92 @
property (retain
, atomic
) NSMutableDictionary
*monitor
;
93 @
property (retain
, atomic
) NSDictionary
*queuedMessages
;
95 @
property (copy
, atomic
) dispatch_block_t shadowFlushBlock
;
98 + (UbiqitousKVSProxy
*) sharedKVSProxy
;
99 - (NSString
*)description
;
100 - (id
)init NS_UNAVAILABLE
;
101 - (id
)initWithAccount
:(NSObject
<CKDAccount
>*) account
102 store
:(NSObject
<CKDStore
>*) store NS_DESIGNATED_INITIALIZER
;
107 - (void)synchronizeStore
;
108 - (id
) objectForKey
: (NSString
*) key
;
109 - (NSDictionary
<NSString
*, id
>*) copyAsDictionary
;
110 - (void)setObjectsFromDictionary
:(NSDictionary
<NSString
*, NSObject
*> *)otherDictionary
;
111 - (void)waitForSynchronization
:(void (^)(NSDictionary
<NSString
*, NSObject
*> *results
, NSError
*err
))handler
;
114 // Callbacks from stores when things happen
115 - (void)storeKeysChanged
: (NSSet
<NSString
*>*) changedKeys initial
: (bool) initial
;
116 - (void)storeAccountChanged
;
118 - (void)streamEvent
:(xpc_object_t
)notification
;
120 - (void)processAllItems
;
121 - (void)requestSyncWithAllPeers
;
122 - (void)requestEnsurePeerRegistration
;
124 - (void)registerAtTimeKeys
:(NSDictionary
*)keyparms
;
126 - (NSSet
*) keysForCurrentLockState
;
127 - (void) intersectWithCurrentLockState
: (NSMutableSet
*) set
;
129 - (NSMutableSet
*) pendKeysAndGetNewlyPended
: (NSSet
*) keysToPend
;
131 - (NSMutableSet
*) pendingKeysForCurrentLockState
;
132 - (NSMutableSet
*) pendKeysAndGetPendingForCurrentLockState
: (NSSet
*) startingSet
;
134 - (void)processPendingKeysForCurrentLockState
;
136 - (void)registerKeys
: (NSDictionary
*)keys
;
138 - (void)processKeyChangedEvent
:(NSDictionary
*)keysChangedInCloud
;
139 - (NSMutableDictionary
*)copyValues
:(NSSet
*)keysOfInterest
;
141 - (void) doAfterFlush
: (dispatch_block_t
) block
;
142 - (void) calloutWith
: (void(^)(NSSet
*pending
, bool syncWithPeersPending
, bool ensurePeerRegistration
, dispatch_queue_t queue
, void(^done
)(NSSet
*handledKeys
, bool handledSyncWithPeers
, bool handledEnsurePeerRegistration
))) callout
;
143 - (void) sendKeysCallout
: (NSSet
*(^)(NSSet
* pending
, NSError
**error
)) handleKeys
;
145 - (void)recordWriteToKVS
:(NSDictionary
*)values
;
146 - (NSDictionary
*)recordHaltedValuesAndReturnValuesToSafelyWrite
:(NSDictionary
*)values
;