2 * Copyright (c) 2016 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@
24 #import <Foundation/Foundation.h>
27 #import "keychain/ckks/CKKSCKAccountStateTracker.h"
28 #import "keychain/ckks/CKKSReachabilityTracker.h"
29 #import "keychain/ckks/CloudKitDependencies.h"
30 #import "keychain/ckks/CKKSAPSReceiver.h"
31 #import "keychain/ckks/CKKSGroupOperation.h"
33 NS_ASSUME_NONNULL_BEGIN
35 @interface CKKSZone
: NSObject
<CKKSZoneUpdateReceiver
, CKKSAccountStateListener
>
37 CKContainer
* _container
;
38 CKDatabase
* _database
;
42 @
property (readonly
) NSString
* zoneName
;
44 @property CKKSGroupOperation
* zoneSetupOperation
;
45 @
property (nullable
) CKOperationGroup
* zoneSetupOperationGroup
; // set this if you want zone creates to use a different operation group
47 @property
bool zoneCreated
;
48 @property
bool zoneSubscribed
;
49 @
property (nullable
) NSError
* zoneCreatedError
;
50 @
property (nullable
) NSError
* zoneSubscribedError
;
52 // True if this zone object has been halted. Halted zones will never recover.
53 @
property (readonly
) bool halted
;
55 @property CKKSAccountStatus accountStatus
;
57 @
property (readonly
) CKContainer
* container
;
58 @
property (readonly
) CKDatabase
* database
;
60 @
property (weak
) CKKSCKAccountStateTracker
* accountTracker
;
61 @
property (weak
) CKKSReachabilityTracker
* reachabilityTracker
;
63 @
property (readonly
) CKRecordZone
* zone
;
64 @
property (readonly
) CKRecordZoneID
* zoneID
;
66 // Dependencies (for injection)
67 @
property (readonly
) Class
<CKKSFetchRecordZoneChangesOperation
> fetchRecordZoneChangesOperationClass
;
68 @
property (readonly
) Class
<CKKSFetchRecordsOperation
> fetchRecordsOperationClass
;
69 @
property (readonly
) Class
<CKKSQueryOperation
> queryOperationClass
;
70 @
property (readonly
) Class
<CKKSModifySubscriptionsOperation
> modifySubscriptionsOperationClass
;
71 @
property (readonly
) Class
<CKKSModifyRecordZonesOperation
> modifyRecordZonesOperationClass
;
72 @
property (readonly
) Class
<CKKSAPSConnection
> apsConnectionClass
;
74 @property dispatch_queue_t queue
;
76 - (instancetype
)initWithContainer
:(CKContainer
*)container
77 zoneName
:(NSString
*)zoneName
78 accountTracker
:(CKKSCKAccountStateTracker
*)accountTracker
79 reachabilityTracker
:(CKKSReachabilityTracker
*)reachabilityTracker
80 fetchRecordZoneChangesOperationClass
:(Class
<CKKSFetchRecordZoneChangesOperation
>)fetchRecordZoneChangesOperationClass
81 fetchRecordsOperationClass
:(Class
<CKKSFetchRecordsOperation
>)fetchRecordsOperationClass
82 queryOperationClass
:(Class
<CKKSQueryOperation
>)queryOperationClass
83 modifySubscriptionsOperationClass
:(Class
<CKKSModifySubscriptionsOperation
>)modifySubscriptionsOperationClass
84 modifyRecordZonesOperationClass
:(Class
<CKKSModifyRecordZonesOperation
>)modifyRecordZonesOperationClass
85 apsConnectionClass
:(Class
<CKKSAPSConnection
>)apsConnectionClass
;
88 - (CKKSResultOperation
* _Nullable
)deleteCloudKitZoneOperation
:(CKOperationGroup
* _Nullable
)ckoperationGroup
;
90 // Called when CloudKit notifies us that we just logged in.
91 // That is, if we transition from any state to CKAccountStatusAvailable.
92 // This will be called under the protection of dispatchSync.
93 // This is a no-op; you should intercept this call and call handleCKLogin:zoneSubscribed:
94 // with the appropriate state
95 - (void)handleCKLogin
;
97 // Actually start a cloudkit login. Pass in whether you believe this zone has been created and if this device has
98 // subscribed to this zone on the server.
99 - (CKKSResultOperation
* _Nullable
)handleCKLogin
:(bool)zoneCreated zoneSubscribed
:(bool)zoneSubscribed
;
101 // Called when CloudKit notifies us that we just logged out.
102 // i.e. we transition from CKAccountStatusAvailable to any other state.
103 // This will be called under the protection of dispatchSync
104 - (void)handleCKLogout
;
106 // Call this when you're ready for this zone to kick off operations
107 // based on iCloud account status
108 - (void)initializeZone
;
110 // Cancels all operations (no matter what they are).
111 - (void)cancelAllOperations
;
113 // Schedules this operation for execution (if the CloudKit account exists)
114 - (bool)scheduleOperation
:(NSOperation
*)op
;
116 // Use this to schedule an operation handling account status (cleaning up after logout, etc.).
117 - (bool)scheduleAccountStatusOperation
:(NSOperation
*)op
;
119 // Schedules this operation for execution, and doesn't do any dependency magic
120 // This should _only_ be used if you want to run something even if the CloudKit account is logged out
121 - (bool)scheduleOperationWithoutDependencies
:(NSOperation
*)op
;
123 // Use this for testing.
124 - (void)waitUntilAllOperationsAreFinished
;
126 // Use this for testing, to only wait for a certain type of operation to finish.
127 - (void)waitForOperationsOfClass
:(Class
)operationClass
;
129 // If this object wants to do anything that needs synchronization, use this.
130 // If this object has had -halt called, this block will never fire.
131 - (void)dispatchSync
:(bool (^)(void))block
;
133 // Call this to halt everything this zone is doing. This object will never recover. Use for testing.
136 // Call this to reset this object's setup, so you can call createSetupOperation again.
141 NS_ASSUME_NONNULL_END