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/CloudKitDependencies.h"
30 NS_ASSUME_NONNULL_BEGIN
32 @interface CKKSZone
: NSObject
<CKKSZoneUpdateReceiver
, CKKSAccountStateListener
>
34 CKContainer
* _container
;
35 CKDatabase
* _database
;
39 @
property (readonly
) NSString
* zoneName
;
41 @property CKKSGroupOperation
* zoneSetupOperation
;
43 @property
bool zoneCreated
;
44 @property
bool zoneSubscribed
;
45 @
property (nullable
) NSError
* zoneCreatedError
;
46 @
property (nullable
) NSError
* zoneSubscribedError
;
48 // True if this zone object has been halted. Halted zones will never recover.
49 @
property (readonly
) bool halted
;
51 @property CKKSAccountStatus accountStatus
;
53 @
property (readonly
) CKContainer
* container
;
54 @
property (readonly
) CKDatabase
* database
;
56 @
property (weak
) CKKSCKAccountStateTracker
* accountTracker
;
58 @
property (readonly
) CKRecordZone
* zone
;
59 @
property (readonly
) CKRecordZoneID
* zoneID
;
61 // Dependencies (for injection)
62 @
property (readonly
) Class
<CKKSFetchRecordZoneChangesOperation
> fetchRecordZoneChangesOperationClass
;
63 @
property (readonly
) Class
<CKKSFetchRecordsOperation
> fetchRecordsOperationClass
;
64 @
property (readonly
) Class
<CKKSQueryOperation
> queryOperationClass
;
65 @
property (readonly
) Class
<CKKSModifySubscriptionsOperation
> modifySubscriptionsOperationClass
;
66 @
property (readonly
) Class
<CKKSModifyRecordZonesOperation
> modifyRecordZonesOperationClass
;
67 @
property (readonly
) Class
<CKKSAPSConnection
> apsConnectionClass
;
69 @property dispatch_queue_t queue
;
71 - (instancetype
)initWithContainer
:(CKContainer
*)container
72 zoneName
:(NSString
*)zoneName
73 accountTracker
:(CKKSCKAccountStateTracker
*)tracker
74 fetchRecordZoneChangesOperationClass
:(Class
<CKKSFetchRecordZoneChangesOperation
>)fetchRecordZoneChangesOperationClass
75 fetchRecordsOperationClass
:(Class
<CKKSFetchRecordsOperation
>)fetchRecordsOperationClass
76 queryOperationClass
:(Class
<CKKSQueryOperation
>)queryOperationClass
77 modifySubscriptionsOperationClass
:(Class
<CKKSModifySubscriptionsOperation
>)modifySubscriptionsOperationClass
78 modifyRecordZonesOperationClass
:(Class
<CKKSModifyRecordZonesOperation
>)modifyRecordZonesOperationClass
79 apsConnectionClass
:(Class
<CKKSAPSConnection
>)apsConnectionClass
;
82 - (CKKSResultOperation
* _Nullable
)beginResetCloudKitZoneOperation
;
84 // Called when CloudKit notifies us that we just logged in.
85 // That is, if we transition from any state to CKAccountStatusAvailable.
86 // This will be called under the protection of dispatchSync.
87 // This is a no-op; you should intercept this call and call handleCKLogin:zoneSubscribed:
88 // with the appropriate state
89 - (void)handleCKLogin
;
91 // Actually start a cloudkit login. Pass in whether you believe this zone has been created and if this device has
92 // subscribed to this zone on the server.
93 - (NSOperation
* _Nullable
)handleCKLogin
:(bool)zoneCreated zoneSubscribed
:(bool)zoneSubscribed
;
95 // Called when CloudKit notifies us that we just logged out.
96 // i.e. we transition from CKAccountStatusAvailable to any other state.
97 // This will be called under the protection of dispatchSync
98 - (void)handleCKLogout
;
100 // Call this when you're ready for this zone to kick off operations
101 // based on iCloud account status
102 - (void)initializeZone
;
104 // Cancels all operations (no matter what they are).
105 - (void)cancelAllOperations
;
107 - (void)restartCurrentAccountStateOperation
;
109 // Schedules this operation for execution (if the CloudKit account exists)
110 - (bool)scheduleOperation
:(NSOperation
*)op
;
112 // Use this to schedule an operation handling account status (cleaning up after logout, etc.).
113 - (bool)scheduleAccountStatusOperation
:(NSOperation
*)op
;
115 // Schedules this operation for execution, and doesn't do any dependency magic
116 // This should _only_ be used if you want to run something even if the CloudKit account is logged out
117 - (bool)scheduleOperationWithoutDependencies
:(NSOperation
*)op
;
119 // Use this for testing.
120 - (void)waitUntilAllOperationsAreFinished
;
122 // Use this for testing, to only wait for a certain type of operation to finish.
123 - (void)waitForOperationsOfClass
:(Class
)operationClass
;
125 // If this object wants to do anything that needs synchronization, use this.
126 // If this object has had -halt called, this block will never fire.
127 - (void)dispatchSync
:(bool (^)(void))block
;
129 // Call this to halt everything this zone is doing. This object will never recover. Use for testing.
132 // Call this to reset this object's setup, so you can call createSetupOperation again.
137 NS_ASSUME_NONNULL_END