session-ios/Signal/src/util/Backup/OWSBackupAPI.swift

803 lines
36 KiB
Swift
Raw Normal View History

2018-03-06 14:29:25 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import CloudKit
2018-03-20 22:22:19 +01:00
// We don't worry about atomic writes. Each backup export
// will diff against last successful backup.
//
// Note that all of our CloudKit records are immutable.
// "Persistent" records are only uploaded once.
// "Ephemeral" records are always uploaded to a new record name.
2018-03-06 14:29:25 +01:00
@objc public class OWSBackupAPI: NSObject {
// If we change the record types, we need to ensure indices
// are configured properly in the CloudKit dashboard.
//
// TODO: Change the record types when we ship to production.
static let signalBackupRecordType = "signalBackup"
static let manifestRecordNameSuffix = "manifest"
static let payloadKey = "payload"
2018-03-13 18:39:00 +01:00
static let maxRetries = 5
2018-03-13 18:05:51 +01:00
private class func recordIdForTest() -> String {
2018-03-06 14:29:25 +01:00
return "test-\(NSUUID().uuidString)"
}
2018-03-13 18:05:51 +01:00
private class func database() -> CKDatabase {
let myContainer = CKContainer.default()
let privateDatabase = myContainer.privateCloudDatabase
return privateDatabase
}
2018-03-13 18:39:00 +01:00
private class func invalidServiceResponseError() -> Error {
return OWSErrorWithCodeDescription(.backupFailure,
NSLocalizedString("BACKUP_EXPORT_ERROR_INVALID_CLOUDKIT_RESPONSE",
comment: "Error indicating that the app received an invalid response from CloudKit."))
}
2018-03-13 18:05:51 +01:00
// MARK: - Upload
2018-03-06 14:29:25 +01:00
@objc
public class func saveTestFileToCloud(recipientId: String,
fileUrl: URL,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
let recordName = "\(recordNamePrefix(forRecipientId: recipientId))test-\(NSUUID().uuidString)"
2018-03-06 14:29:25 +01:00
saveFileToCloud(fileUrl: fileUrl,
recordName: recordName,
recordType: signalBackupRecordType,
success: success,
failure: failure)
}
2018-03-06 19:58:06 +01:00
// "Ephemeral" files are specific to this backup export and will always need to
// be saved. For example, a complete image of the database is exported each time.
// We wouldn't want to overwrite previous images until the entire backup export is
// complete.
@objc
public class func saveEphemeralDatabaseFileToCloud(recipientId: String,
fileUrl: URL,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
let recordName = "\(recordNamePrefix(forRecipientId: recipientId))ephemeralFile-\(NSUUID().uuidString)"
saveFileToCloud(fileUrl: fileUrl,
recordName: recordName,
2018-03-13 18:05:51 +01:00
recordType: signalBackupRecordType,
success: success,
failure: failure)
2018-03-06 19:58:06 +01:00
}
2018-03-17 21:29:57 +01:00
// "Persistent" files may be shared between backup export; they should only be saved
// once. For example, attachment files should only be uploaded once. Subsequent
// backups can reuse the same record.
@objc
public class func recordNameForPersistentFile(recipientId: String,
fileId: String) -> String {
return "\(recordNamePrefix(forRecipientId: recipientId))persistentFile-\(fileId)"
2018-03-17 21:29:57 +01:00
}
2018-03-06 19:58:06 +01:00
// "Persistent" files may be shared between backup export; they should only be saved
// once. For example, attachment files should only be uploaded once. Subsequent
// backups can reuse the same record.
2018-03-06 19:58:06 +01:00
@objc
public class func recordNameForManifest(recipientId: String) -> String {
return "\(recordNamePrefix(forRecipientId: recipientId))\(manifestRecordNameSuffix)"
}
private class func isManifest(recordName: String) -> Bool {
return recordName.hasSuffix(manifestRecordNameSuffix)
}
private class func recordNamePrefix(forRecipientId recipientId: String) -> String {
return "\(recipientId)-"
}
private class func recipientId(forRecordName recordName: String) -> String? {
let recipientIds = self.recipientIds(forRecordNames: [recordName])
guard let recipientId = recipientIds.first else {
return nil
}
return recipientId
}
private class func recipientIds(forRecordNames recordNames: [String]) -> [String] {
let regex: NSRegularExpression
do {
regex = try NSRegularExpression(pattern: "(\\+[0-9]+)\\-")
} catch {
Logger.error("couldn't compile regex: \(error)")
return []
}
var recipientIds = [String]()
for recordName in recordNames {
guard let match = regex.firstMatch(in: recordName, options: [], range: NSRange(location: 0, length: recordName.count)) else {
continue
}
guard match.range.location == 0 else {
// Match must be at start of string.
continue
}
let recipientId = (recordName as NSString).substring(with: match.range) as String
recipientIds.append(recipientId)
}
return recipientIds
}
// "Persistent" files may be shared between backup export; they should only be saved
// once. For example, attachment files should only be uploaded once. Subsequent
// backups can reuse the same record.
@objc
public class func savePersistentFileOnceToCloud(recipientId: String,
fileId: String,
2018-05-25 21:21:43 +02:00
fileUrlBlock: @escaping () -> URL?,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
let recordName = recordNameForPersistentFile(recipientId: recipientId, fileId: fileId)
saveFileOnceToCloud(recordName: recordName,
recordType: signalBackupRecordType,
fileUrlBlock: fileUrlBlock,
success: success,
failure: failure)
2018-03-06 14:29:25 +01:00
}
@objc
public class func upsertManifestFileToCloud(recipientId: String,
fileUrl: URL,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
// We want to use a well-known record id and type for manifest files.
let recordName = recordNameForManifest(recipientId: recipientId)
upsertFileToCloud(fileUrl: fileUrl,
recordName: recordName,
recordType: signalBackupRecordType,
2018-03-06 19:58:06 +01:00
success: success,
failure: failure)
}
2018-03-06 14:29:25 +01:00
@objc
2018-03-06 14:36:05 +01:00
public class func saveFileToCloud(fileUrl: URL,
recordName: String,
2018-03-06 14:36:05 +01:00
recordType: String,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
let recordID = CKRecordID(recordName: recordName)
2018-03-06 14:29:25 +01:00
let record = CKRecord(recordType: recordType, recordID: recordID)
2018-03-06 14:36:05 +01:00
let asset = CKAsset(fileURL: fileUrl)
record[payloadKey] = asset
2018-03-06 14:36:05 +01:00
saveRecordToCloud(record: record,
success: success,
failure: failure)
2018-03-06 14:36:05 +01:00
}
@objc
public class func saveRecordToCloud(record: CKRecord,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
2018-03-13 18:05:51 +01:00
saveRecordToCloud(record: record,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-13 18:05:51 +01:00
success: success,
failure: failure)
2018-03-06 14:29:25 +01:00
}
2018-03-13 18:05:51 +01:00
private class func saveRecordToCloud(record: CKRecord,
remainingRetries: Int,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
let saveOperation = CKModifyRecordsOperation(recordsToSave: [record ], recordIDsToDelete: nil)
saveOperation.modifyRecordsCompletionBlock = { (records, recordIds, error) in
2018-03-14 14:12:20 +01:00
let outcome = outcomeForCloudKitError(error: error,
2018-03-13 18:05:51 +01:00
remainingRetries: remainingRetries,
label: "Save Record")
2018-03-14 14:12:20 +01:00
switch outcome {
2018-03-13 18:05:51 +01:00
case .success:
let recordName = record.recordID.recordName
success(recordName)
2018-03-14 14:12:20 +01:00
case .failureDoNotRetry(let outcomeError):
failure(outcomeError)
2018-03-13 18:05:51 +01:00
case .failureRetryAfterDelay(let retryDelay):
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + retryDelay, execute: {
saveRecordToCloud(record: record,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
})
case .failureRetryWithoutDelay:
DispatchQueue.global().async {
saveRecordToCloud(record: record,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
}
2018-03-13 18:39:00 +01:00
case .unknownItem:
2018-08-27 16:27:48 +02:00
owsFailDebug("unexpected CloudKit response.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
}
}
saveOperation.isAtomic = false
// These APIs are only available in iOS 9.3 and later.
if #available(iOS 9.3, *) {
saveOperation.isLongLived = true
saveOperation.qualityOfService = .background
}
database().add(saveOperation)
}
2018-03-08 14:31:35 +01:00
// Compare:
// * An "upsert" creates a new record if none exists and
// or updates if there is an existing record.
// * A "save once" creates a new record if none exists and
// does nothing if there is an existing record.
@objc
public class func upsertFileToCloud(fileUrl: URL,
recordName: String,
recordType: String,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
2018-03-08 14:31:35 +01:00
checkForFileInCloud(recordName: recordName,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-08 14:31:35 +01:00
success: { (record) in
if let record = record {
// Record found, updating existing record.
let asset = CKAsset(fileURL: fileUrl)
record[payloadKey] = asset
saveRecordToCloud(record: record,
success: success,
failure: failure)
} else {
// No record found, saving new record.
saveFileToCloud(fileUrl: fileUrl,
recordName: recordName,
recordType: recordType,
success: success,
failure: failure)
}
},
failure: failure)
}
// Compare:
// * An "upsert" creates a new record if none exists and
// or updates if there is an existing record.
// * A "save once" creates a new record if none exists and
// does nothing if there is an existing record.
@objc
public class func saveFileOnceToCloud(recordName: String,
recordType: String,
2018-05-25 21:21:43 +02:00
fileUrlBlock: @escaping () -> URL?,
2018-03-14 14:12:20 +01:00
success: @escaping (String) -> Void,
failure: @escaping (Error) -> Void) {
2018-03-08 14:31:35 +01:00
checkForFileInCloud(recordName: recordName,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-08 14:31:35 +01:00
success: { (record) in
if record != nil {
// Record found, skipping save.
success(recordName)
} else {
// No record found, saving new record.
2018-05-25 21:21:43 +02:00
guard let fileUrl = fileUrlBlock() else {
2018-08-23 16:37:34 +02:00
Logger.error("error preparing file for upload.")
2018-03-08 14:31:35 +01:00
failure(OWSErrorWithCodeDescription(.exportBackupError,
NSLocalizedString("BACKUP_EXPORT_ERROR_SAVE_FILE_TO_CLOUD_FAILED",
comment: "Error indicating the backup export failed to save a file to the cloud.")))
2018-03-08 14:31:35 +01:00
return
}
saveFileToCloud(fileUrl: fileUrl,
recordName: recordName,
recordType: recordType,
success: success,
failure: failure)
}
},
failure: failure)
}
2018-03-13 18:05:51 +01:00
// MARK: - Delete
@objc
public class func deleteRecordsFromCloud(recordNames: [String],
2018-05-25 21:21:43 +02:00
success: @escaping () -> Void,
2018-03-14 14:12:20 +01:00
failure: @escaping (Error) -> Void) {
deleteRecordsFromCloud(recordNames: recordNames,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-13 18:05:51 +01:00
success: success,
failure: failure)
}
private class func deleteRecordsFromCloud(recordNames: [String],
2018-03-13 18:05:51 +01:00
remainingRetries: Int,
2018-05-25 21:21:43 +02:00
success: @escaping () -> Void,
2018-03-14 14:12:20 +01:00
failure: @escaping (Error) -> Void) {
2018-03-13 18:05:51 +01:00
2018-03-20 16:23:45 +01:00
let recordIDs = recordNames.map { CKRecordID(recordName: $0) }
let deleteOperation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: recordIDs)
deleteOperation.modifyRecordsCompletionBlock = { (records, recordIds, error) in
2018-03-13 18:05:51 +01:00
2018-03-14 14:12:20 +01:00
let outcome = outcomeForCloudKitError(error: error,
remainingRetries: remainingRetries,
label: "Delete Records")
2018-03-14 14:12:20 +01:00
switch outcome {
2018-03-13 18:05:51 +01:00
case .success:
2018-05-25 21:21:43 +02:00
success()
2018-03-14 14:12:20 +01:00
case .failureDoNotRetry(let outcomeError):
failure(outcomeError)
2018-03-13 18:05:51 +01:00
case .failureRetryAfterDelay(let retryDelay):
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + retryDelay, execute: {
deleteRecordsFromCloud(recordNames: recordNames,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
2018-03-13 18:05:51 +01:00
})
case .failureRetryWithoutDelay:
DispatchQueue.global().async {
deleteRecordsFromCloud(recordNames: recordNames,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
2018-03-13 18:05:51 +01:00
}
2018-03-13 18:39:00 +01:00
case .unknownItem:
2018-08-27 16:27:48 +02:00
owsFailDebug("unexpected CloudKit response.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
2018-03-13 18:05:51 +01:00
}
}
database().add(deleteOperation)
2018-03-13 18:05:51 +01:00
}
// MARK: - Exists?
2018-03-08 14:31:35 +01:00
private class func checkForFileInCloud(recordName: String,
2018-03-13 18:39:00 +01:00
remainingRetries: Int,
2018-03-14 14:12:20 +01:00
success: @escaping (CKRecord?) -> Void,
failure: @escaping (Error) -> Void) {
let recordId = CKRecordID(recordName: recordName)
let fetchOperation = CKFetchRecordsOperation(recordIDs: [recordId ])
// Don't download the file; we're just using the fetch to check whether or
// not this record already exists.
fetchOperation.desiredKeys = []
fetchOperation.perRecordCompletionBlock = { (record, recordId, error) in
2018-03-13 18:39:00 +01:00
2018-03-14 14:12:20 +01:00
let outcome = outcomeForCloudKitError(error: error,
2018-03-13 18:39:00 +01:00
remainingRetries: remainingRetries,
label: "Check for Record")
2018-03-14 14:12:20 +01:00
switch outcome {
2018-03-13 18:39:00 +01:00
case .success:
guard let record = record else {
2018-08-27 16:27:48 +02:00
owsFailDebug("missing fetching record.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
return
}
2018-03-13 18:39:00 +01:00
// Record found.
success(record)
2018-03-14 14:12:20 +01:00
case .failureDoNotRetry(let outcomeError):
failure(outcomeError)
2018-03-13 18:39:00 +01:00
case .failureRetryAfterDelay(let retryDelay):
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + retryDelay, execute: {
checkForFileInCloud(recordName: recordName,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
})
case .failureRetryWithoutDelay:
DispatchQueue.global().async {
checkForFileInCloud(recordName: recordName,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
}
case .unknownItem:
// Record not found.
success(nil)
}
2018-03-06 19:58:06 +01:00
}
2018-03-13 18:05:51 +01:00
database().add(fetchOperation)
2018-03-06 19:58:06 +01:00
}
@objc
public class func checkForManifestInCloud(recipientId: String,
success: @escaping (Bool) -> Void,
2018-03-14 14:12:20 +01:00
failure: @escaping (Error) -> Void) {
2018-03-08 14:31:35 +01:00
let recordName = recordNameForManifest(recipientId: recipientId)
checkForFileInCloud(recordName: recordName,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-08 14:31:35 +01:00
success: { (record) in
success(record != nil)
},
failure: failure)
}
@objc
public class func allRecipientIdsWithManifestsInCloud(success: @escaping ([String]) -> Void,
failure: @escaping (Error) -> Void) {
let processResults = { (recordNames: [String]) in
DispatchQueue.global().async {
let manifestRecordNames = recordNames.filter({ (recordName) -> Bool in
self.isManifest(recordName: recordName)
})
let recipientIds = self.recipientIds(forRecordNames: manifestRecordNames)
success(recipientIds)
}
}
let query = CKQuery(recordType: signalBackupRecordType, predicate: NSPredicate(value: true))
// Fetch the first page of results for this query.
fetchAllRecordNamesStep(recipientId: nil,
query: query,
previousRecordNames: [String](),
cursor: nil,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
success: processResults,
failure: failure)
}
@objc
public class func fetchAllRecordNames(recipientId: String,
success: @escaping ([String]) -> Void,
failure: @escaping (Error) -> Void) {
let query = CKQuery(recordType: signalBackupRecordType, predicate: NSPredicate(value: true))
// Fetch the first page of results for this query.
fetchAllRecordNamesStep(recipientId: recipientId,
query: query,
previousRecordNames: [String](),
cursor: nil,
remainingRetries: maxRetries,
success: success,
failure: failure)
}
// @objc
// public class func fetchAllBackupRecipientIds(success: @escaping ([String]) -> Void,
// failure: @escaping (Error) -> Void) {
//
// let processResults = { (recordNames: [String]) in
// DispatchQueue.global().async {
// let recipientIds = self.recipientIds(forRecordNames: recordNames)
// success(recipientIds)
// }
// }
//
// let query = CKQuery(recordType: signalBackupRecordType, predicate: NSPredicate(value: true))
// // Fetch the first page of results for this query.
// fetchAllRecordNamesStep(recipientId: nil,
// query: query,
// previousRecordNames: [String](),
// cursor: nil,
// remainingRetries: maxRetries,
// success: processResults,
// failure: failure)
// }
private class func fetchAllRecordNamesStep(recipientId: String?,
query: CKQuery,
previousRecordNames: [String],
cursor: CKQueryCursor?,
2018-03-13 18:39:00 +01:00
remainingRetries: Int,
2018-03-14 14:12:20 +01:00
success: @escaping ([String]) -> Void,
failure: @escaping (Error) -> Void) {
var allRecordNames = previousRecordNames
2018-03-13 18:05:51 +01:00
let queryOperation = CKQueryOperation(query: query)
// If this isn't the first page of results for this query, resume
// where we left off.
queryOperation.cursor = cursor
// Don't download the file; we're just using the query to get a list of record names.
queryOperation.desiredKeys = []
queryOperation.recordFetchedBlock = { (record) in
assert(record.recordID.recordName.count > 0)
let recordName = record.recordID.recordName
if let recipientId = recipientId {
let prefix = recordNamePrefix(forRecipientId: recipientId)
guard recordName.hasPrefix(prefix) else {
Logger.info("Ignoring record: \(recordName)")
return
}
}
allRecordNames.append(recordName)
}
queryOperation.queryCompletionBlock = { (cursor, error) in
2018-03-13 18:39:00 +01:00
2018-03-14 14:12:20 +01:00
let outcome = outcomeForCloudKitError(error: error,
2018-03-13 18:39:00 +01:00
remainingRetries: remainingRetries,
label: "Fetch All Records")
2018-03-14 14:12:20 +01:00
switch outcome {
2018-03-13 18:39:00 +01:00
case .success:
if let cursor = cursor {
2018-08-23 16:37:34 +02:00
Logger.verbose("fetching more record names \(allRecordNames.count).")
2018-03-13 18:39:00 +01:00
// There are more pages of results, continue fetching.
fetchAllRecordNamesStep(recipientId: recipientId,
query: query,
2018-03-13 18:39:00 +01:00
previousRecordNames: allRecordNames,
cursor: cursor,
remainingRetries: maxRetries,
success: success,
failure: failure)
return
}
2018-08-23 16:37:34 +02:00
Logger.info("fetched \(allRecordNames.count) record names.")
2018-03-13 18:39:00 +01:00
success(allRecordNames)
2018-03-14 14:12:20 +01:00
case .failureDoNotRetry(let outcomeError):
failure(outcomeError)
2018-03-13 18:39:00 +01:00
case .failureRetryAfterDelay(let retryDelay):
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + retryDelay, execute: {
fetchAllRecordNamesStep(recipientId: recipientId,
query: query,
2018-03-13 18:39:00 +01:00
previousRecordNames: allRecordNames,
cursor: cursor,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
})
case .failureRetryWithoutDelay:
DispatchQueue.global().async {
fetchAllRecordNamesStep(recipientId: recipientId,
query: query,
2018-03-13 18:39:00 +01:00
previousRecordNames: allRecordNames,
cursor: cursor,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
}
case .unknownItem:
2018-08-27 16:27:48 +02:00
owsFailDebug("unexpected CloudKit response.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
}
}
2018-03-13 18:05:51 +01:00
database().add(queryOperation)
}
2018-03-13 18:05:51 +01:00
// MARK: - Download
2018-03-08 19:38:42 +01:00
@objc
public class func downloadManifestFromCloud(recipientId: String,
success: @escaping (Data) -> Void,
failure: @escaping (Error) -> Void) {
let recordName = recordNameForManifest(recipientId: recipientId)
downloadDataFromCloud(recordName: recordName,
2018-03-13 18:05:51 +01:00
success: success,
failure: failure)
2018-03-08 19:38:42 +01:00
}
@objc
public class func downloadDataFromCloud(recordName: String,
2018-03-14 14:12:20 +01:00
success: @escaping (Data) -> Void,
failure: @escaping (Error) -> Void) {
2018-03-08 19:38:42 +01:00
downloadFromCloud(recordName: recordName,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-08 19:38:42 +01:00
success: { (asset) in
DispatchQueue.global().async {
do {
let data = try Data(contentsOf: asset.fileURL)
success(data)
} catch {
2018-08-23 16:37:34 +02:00
Logger.error("couldn't load asset file: \(error).")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
2018-03-08 19:38:42 +01:00
}
}
},
failure: failure)
}
@objc
public class func downloadFileFromCloud(recordName: String,
toFileUrl: URL,
2018-05-25 21:21:43 +02:00
success: @escaping () -> Void,
2018-03-14 14:12:20 +01:00
failure: @escaping (Error) -> Void) {
2018-03-08 19:38:42 +01:00
downloadFromCloud(recordName: recordName,
2018-03-13 18:39:00 +01:00
remainingRetries: maxRetries,
2018-03-08 19:38:42 +01:00
success: { (asset) in
DispatchQueue.global().async {
do {
2018-03-12 20:10:37 +01:00
try FileManager.default.copyItem(at: asset.fileURL, to: toFileUrl)
2018-05-25 21:21:43 +02:00
success()
2018-03-08 19:38:42 +01:00
} catch {
2018-08-23 16:37:34 +02:00
Logger.error("couldn't copy asset file: \(error).")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
2018-03-08 19:38:42 +01:00
}
}
},
failure: failure)
}
2018-03-13 18:39:00 +01:00
// We return the CKAsset and not its fileUrl because
// CloudKit offers no guarantees around how long it'll
// keep around the underlying file. Presumably we can
// defer cleanup by maintaining a strong reference to
// the asset.
2018-03-08 19:38:42 +01:00
private class func downloadFromCloud(recordName: String,
2018-03-13 18:39:00 +01:00
remainingRetries: Int,
2018-03-14 14:12:20 +01:00
success: @escaping (CKAsset) -> Void,
failure: @escaping (Error) -> Void) {
2018-03-08 19:38:42 +01:00
let recordId = CKRecordID(recordName: recordName)
let fetchOperation = CKFetchRecordsOperation(recordIDs: [recordId ])
// Download all keys for this record.
fetchOperation.perRecordCompletionBlock = { (record, recordId, error) in
2018-03-13 18:39:00 +01:00
2018-03-14 14:12:20 +01:00
let outcome = outcomeForCloudKitError(error: error,
2018-03-13 18:39:00 +01:00
remainingRetries: remainingRetries,
label: "Download Record")
2018-03-14 14:12:20 +01:00
switch outcome {
2018-03-13 18:39:00 +01:00
case .success:
guard let record = record else {
2018-08-23 16:37:34 +02:00
Logger.error("missing fetching record.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
return
}
guard let asset = record[payloadKey] as? CKAsset else {
2018-08-23 16:37:34 +02:00
Logger.error("record missing payload.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
return
}
success(asset)
2018-03-14 14:12:20 +01:00
case .failureDoNotRetry(let outcomeError):
failure(outcomeError)
2018-03-13 18:39:00 +01:00
case .failureRetryAfterDelay(let retryDelay):
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + retryDelay, execute: {
downloadFromCloud(recordName: recordName,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
})
case .failureRetryWithoutDelay:
DispatchQueue.global().async {
downloadFromCloud(recordName: recordName,
remainingRetries: remainingRetries - 1,
success: success,
failure: failure)
}
case .unknownItem:
2018-08-23 16:37:34 +02:00
Logger.error("missing fetching record.")
2018-03-13 18:39:00 +01:00
failure(invalidServiceResponseError())
2018-03-08 19:38:42 +01:00
}
}
2018-03-13 18:05:51 +01:00
database().add(fetchOperation)
2018-03-08 19:38:42 +01:00
}
2018-03-13 18:05:51 +01:00
// MARK: - Access
2018-03-06 14:29:25 +01:00
@objc
2018-03-14 14:12:20 +01:00
public class func checkCloudKitAccess(completion: @escaping (Bool) -> Void) {
2018-03-06 14:29:25 +01:00
CKContainer.default().accountStatus(completionHandler: { (accountStatus, error) in
DispatchQueue.main.async {
2018-03-06 14:36:05 +01:00
switch accountStatus {
case .couldNotDetermine:
2018-08-23 16:37:34 +02:00
Logger.error("could not determine CloudKit account status:\(String(describing: error)).")
2018-03-12 21:49:57 +01:00
OWSAlerts.showErrorAlert(message: NSLocalizedString("CLOUDKIT_STATUS_COULD_NOT_DETERMINE", comment: "Error indicating that the app could not determine that user's CloudKit account status"))
2018-03-06 14:36:05 +01:00
completion(false)
case .noAccount:
2018-08-23 16:37:34 +02:00
Logger.error("no CloudKit account.")
2018-03-12 21:49:57 +01:00
OWSAlerts.showErrorAlert(message: NSLocalizedString("CLOUDKIT_STATUS_NO_ACCOUNT", comment: "Error indicating that user does not have an iCloud account."))
2018-03-06 14:36:05 +01:00
completion(false)
case .restricted:
2018-08-23 16:37:34 +02:00
Logger.error("restricted CloudKit account.")
2018-03-12 21:49:57 +01:00
OWSAlerts.showErrorAlert(message: NSLocalizedString("CLOUDKIT_STATUS_RESTRICTED", comment: "Error indicating that the app was prevented from accessing the user's CloudKit account."))
2018-03-06 14:36:05 +01:00
completion(false)
case .available:
completion(true)
}
2018-03-06 14:29:25 +01:00
}
})
}
2018-03-13 18:05:51 +01:00
// MARK: - Retry
2018-03-17 12:50:09 +01:00
private enum APIOutcome {
2018-03-13 18:05:51 +01:00
case success
case failureDoNotRetry(error:Error)
2018-03-17 12:50:09 +01:00
case failureRetryAfterDelay(retryDelay: TimeInterval)
2018-03-13 18:05:51 +01:00
case failureRetryWithoutDelay
2018-03-13 18:39:00 +01:00
// This only applies to fetches.
case unknownItem
2018-03-13 18:05:51 +01:00
}
2018-03-14 14:12:20 +01:00
private class func outcomeForCloudKitError(error: Error?,
2018-03-13 18:05:51 +01:00
remainingRetries: Int,
2018-03-17 12:50:09 +01:00
label: String) -> APIOutcome {
2018-03-13 18:05:51 +01:00
if let error = error as? CKError {
2018-03-13 18:39:00 +01:00
if error.code == CKError.unknownItem {
// This is not always an error for our purposes.
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) unknown item.")
2018-03-13 18:39:00 +01:00
return .unknownItem
}
2018-08-23 16:37:34 +02:00
Logger.error("\(label) failed: \(error)")
2018-03-13 18:39:00 +01:00
2018-03-13 18:05:51 +01:00
if remainingRetries < 1 {
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) no more retries.")
2018-03-13 18:05:51 +01:00
return .failureDoNotRetry(error:error)
}
if #available(iOS 11, *) {
if error.code == CKError.serverResponseLost {
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) retry without delay.")
2018-03-13 18:05:51 +01:00
return .failureRetryWithoutDelay
}
}
switch error {
case CKError.requestRateLimited, CKError.serviceUnavailable, CKError.zoneBusy:
let retryDelay = error.retryAfterSeconds ?? 3.0
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) retry with delay: \(retryDelay).")
2018-03-13 18:05:51 +01:00
return .failureRetryAfterDelay(retryDelay:retryDelay)
case CKError.networkFailure:
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) retry without delay.")
2018-03-13 18:05:51 +01:00
return .failureRetryWithoutDelay
default:
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) unknown CKError.")
2018-03-13 18:05:51 +01:00
return .failureDoNotRetry(error:error)
}
} else if let error = error {
2018-08-23 16:37:34 +02:00
Logger.error("\(label) failed: \(error)")
2018-03-13 18:05:51 +01:00
if remainingRetries < 1 {
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) no more retries.")
2018-03-13 18:05:51 +01:00
return .failureDoNotRetry(error:error)
}
2018-08-23 16:37:34 +02:00
Logger.verbose("\(label) unknown error.")
2018-03-13 18:05:51 +01:00
return .failureDoNotRetry(error:error)
} else {
2018-08-23 16:37:34 +02:00
Logger.info("\(label) succeeded.")
2018-03-13 18:05:51 +01:00
return .success
}
}
// MARK: -
@objc
public class func setup() {
cancelAllLongLivedOperations()
}
private class func cancelAllLongLivedOperations() {
// These APIs are only available in iOS 9.3 and later.
guard #available(iOS 9.3, *) else {
return
}
let container = CKContainer.default()
container.fetchAllLongLivedOperationIDs { (operationIds, error) in
if let error = error {
Logger.error("Could not get all long lived operations: \(error)")
return
}
guard let operationIds = operationIds else {
Logger.error("No operation ids.")
return
}
for operationId in operationIds {
container.fetchLongLivedOperation(withID: operationId, completionHandler: { (operation, error) in
if let error = error {
Logger.error("Could not get long lived operation [\(operationId)]: \(error)")
return
}
guard let operation = operation else {
Logger.error("No operation.")
return
}
operation.cancel()
})
}
}
}
2018-03-06 14:29:25 +01:00
}