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@
26 #include <AssertMacros.h>
28 #import <Foundation/Foundation.h>
30 #import "CKKSKeychainView.h"
32 #include <utilities/SecDb.h>
33 #include <securityd/SecDbItem.h>
34 #include <securityd/SecItemSchema.h>
36 #import <CloudKit/CloudKit.h>
37 #import "CKKSIncomingQueueEntry.h"
38 #import "CKKSItemEncrypter.h"
41 @implementation CKKSIncomingQueueEntry
43 - (NSString*)description {
44 return [NSString stringWithFormat: @"<%@(%@): %@ %@ (%@)>",
45 NSStringFromClass([self class]),
46 self.item.zoneID.zoneName,
52 - (instancetype) initWithCKKSItem:(CKKSItem*) item
53 action:(NSString*) action
54 state:(NSString*) state {
55 if(self = [super init]) {
64 #pragma mark - Property access to underlying CKKSItem
67 return self.item.uuid;
70 -(void)setUuid:(NSString *)uuid {
71 self.item.uuid = uuid;
74 #pragma mark - Database Operations
76 + (instancetype) fromDatabase: (NSString*) uuid zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
77 return [self fromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
80 + (instancetype) tryFromDatabase: (NSString*) uuid zoneID:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
81 return [self tryFromDatabaseWhere: @{@"UUID": CKKSNilToNSNull(uuid), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} error: error];
84 + (NSArray<CKKSIncomingQueueEntry*>*)fetch:(ssize_t)n
85 startingAtUUID:(NSString*)uuid
86 state:(NSString*)state
87 zoneID:(CKRecordZoneID*)zoneID
88 error: (NSError * __autoreleasing *) error {
89 NSMutableDictionary* whereDict = [@{@"state": CKKSNilToNSNull(state), @"ckzone":CKKSNilToNSNull(zoneID.zoneName)} mutableCopy];
91 whereDict[@"UUID"] = [CKKSSQLWhereObject op:@">" stringValue:uuid];
100 #pragma mark - CKKSSQLDatabaseObject methods
102 + (NSString*)sqlTable {
103 return @"incomingqueue";
106 + (NSArray<NSString*>*)sqlColumns {
107 return [[CKKSItem sqlColumns] arrayByAddingObjectsFromArray: @[@"action", @"state"]];
110 - (NSDictionary<NSString*,NSString*>*)whereClauseToFindSelf {
111 return [self.item whereClauseToFindSelf];
114 - (NSDictionary<NSString*,NSString*>*)sqlValues {
115 NSMutableDictionary* values = [[self.item sqlValues] mutableCopy];
116 values[@"action"] = self.action;
117 values[@"state"] = self.state;
122 + (instancetype)fromDatabaseRow: (NSDictionary*) row {
123 return [[CKKSIncomingQueueEntry alloc] initWithCKKSItem: [CKKSItem fromDatabaseRow: row]
124 action: row[@"action"]
125 state: row[@"state"]];
128 + (NSDictionary<NSString*,NSNumber*>*)countsByState:(CKRecordZoneID*)zoneID error: (NSError * __autoreleasing *) error {
129 NSMutableDictionary* results = [[NSMutableDictionary alloc] init];
131 [CKKSSQLDatabaseObject queryDatabaseTable: [[self class] sqlTable]
132 where: @{@"ckzone": CKKSNilToNSNull(zoneID.zoneName)}
133 columns: @[@"state", @"count(rowid)"]
137 processRow: ^(NSDictionary* row) {
138 results[row[@"state"]] = [NSNumber numberWithInteger: [row[@"count(rowid)"] integerValue]];