From 34b6da7b2ff2fd70168ee1ccb82e8048ef0792f2 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Sat, 23 Jan 2021 23:10:54 -0300 Subject: [PATCH] Fix build warnings; fix crash around url session errors. --- ...vancedPinSettingsTableViewController.swift | 8 +- .../StorageServiceManager.swift | 4 +- SignalMessaging/categories/UIView+OWS.swift | 2 +- .../src/Messages/MessageFetcherJob.swift | 4 +- .../src/Network/OWSURLSession.swift | 89 ++++++++++++++++--- .../src/Protos/Generated/SSKProto.swift | 4 +- .../src/Util/ModelReadCache.swift | 2 +- .../src/Util/StorageService.swift | 4 +- 8 files changed, 92 insertions(+), 25 deletions(-) diff --git a/Signal/src/ViewControllers/AppSettings/AdvancedPinSettingsTableViewController.swift b/Signal/src/ViewControllers/AppSettings/AdvancedPinSettingsTableViewController.swift index 1bbb3bc22f..785f99d102 100644 --- a/Signal/src/ViewControllers/AppSettings/AdvancedPinSettingsTableViewController.swift +++ b/Signal/src/ViewControllers/AppSettings/AdvancedPinSettingsTableViewController.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -42,8 +42,12 @@ class AdvancedPinSettingsTableViewController: OWSTableViewController { } self.navigationController?.pushViewController(vc, animated: true) } else { - PinSetupViewController.disablePinWithConfirmation(fromViewController: self).done { [weak self] _ in + firstly(on: .main) { + PinSetupViewController.disablePinWithConfirmation(fromViewController: self) + }.done { [weak self] _ in self?.updateTableContents() + }.catch { error in + owsFailDebug("Error: \(error)") } } })) diff --git a/SignalMessaging/Storage Service/StorageServiceManager.swift b/SignalMessaging/Storage Service/StorageServiceManager.swift index b2d6ba9042..f06006bb10 100644 --- a/SignalMessaging/Storage Service/StorageServiceManager.swift +++ b/SignalMessaging/Storage Service/StorageServiceManager.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -1209,7 +1209,7 @@ class StorageServiceOperation: OWSOperation { private func cleanUpUnknownIdentifiers(transaction: SDSAnyWriteTransaction) { // We may have learned of new record types; if so we should // cull them from the unknownIdentifiersTypeMap on launch. - var knownTypes: [StorageServiceProtoManifestRecordKeyType] = [ + let knownTypes: [StorageServiceProtoManifestRecordKeyType] = [ .contact, .groupv1, .groupv2, diff --git a/SignalMessaging/categories/UIView+OWS.swift b/SignalMessaging/categories/UIView+OWS.swift index 024272b5df..69c5bc88c3 100644 --- a/SignalMessaging/categories/UIView+OWS.swift +++ b/SignalMessaging/categories/UIView+OWS.swift @@ -29,7 +29,7 @@ public extension UIEdgeInsets { return plus(-inset) } - public var asSize: CGSize { + var asSize: CGSize { CGSize(width: left + right, height: top + bottom) } diff --git a/SignalServiceKit/src/Messages/MessageFetcherJob.swift b/SignalServiceKit/src/Messages/MessageFetcherJob.swift index d55c5e5b94..40b7f1169a 100644 --- a/SignalServiceKit/src/Messages/MessageFetcherJob.swift +++ b/SignalServiceKit/src/Messages/MessageFetcherJob.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -101,7 +101,7 @@ public class MessageFetcherJob: NSObject { completionQueue.async { self.operationQueue.waitUntilAllOperationsAreFinished() - _ = self.serialQueue.sync { + self.serialQueue.sync { self.activeFetchCycles.remove(fetchCycle.uuid) self.completedFetchCyclesCounter += 1 } diff --git a/SignalServiceKit/src/Network/OWSURLSession.swift b/SignalServiceKit/src/Network/OWSURLSession.swift index d6b24f753b..0ed5fe1ffc 100644 --- a/SignalServiceKit/src/Network/OWSURLSession.swift +++ b/SignalServiceKit/src/Network/OWSURLSession.swift @@ -963,13 +963,12 @@ private class UploadTaskState: TaskState { // i.e. OWSURLSession --(session)--> URLSession --(delegate)--> URLSessionDelegateBox // x-----(weakDelegate)-----| // -private typealias BoxedType = (URLSessionDelegate & URLSessionTaskDelegate & URLSessionDownloadDelegate) -private class URLSessionDelegateBox: NSObject, BoxedType { +private class URLSessionDelegateBox: NSObject { - private weak var weakDelegate: BoxedType? - private var strongReference: BoxedType? + private weak var weakDelegate: OWSURLSession? + private var strongReference: OWSURLSession? - init(delegate: BoxedType) { + init(delegate: OWSURLSession) { self.weakDelegate = delegate } @@ -981,6 +980,11 @@ private class URLSessionDelegateBox: NSObject, BoxedType { strongReference = newValue ? weakDelegate : nil } } +} + +// MARK: - + + extension URLSessionDelegateBox: URLSessionDelegate, URLSessionTaskDelegate, URLSessionDownloadDelegate { // Any of the optional methods will be forwarded using objc selector forwarding // If all goes according to plan, weakDelegate will only go nil once everything is being dealloced @@ -989,16 +993,75 @@ private class URLSessionDelegateBox: NSObject, BoxedType { weakDelegate?.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: location) } - override func responds(to aSelector: Selector!) -> Bool { - return super.responds(to: aSelector) || (weakDelegate?.responds(to: aSelector) == true) + func urlSession(_ session: URLSession, + downloadTask: URLSessionDownloadTask, + didWriteData bytesWritten: Int64, + totalBytesWritten: Int64, + totalBytesExpectedToWrite: Int64) { + weakDelegate?.urlSession(session, + downloadTask: downloadTask, + didWriteData: bytesWritten, + totalBytesWritten: totalBytesWritten, + totalBytesExpectedToWrite: totalBytesExpectedToWrite) } - override func forwardingTarget(for aSelector: Selector!) -> Any? { - if weakDelegate?.responds(to: aSelector) == true { - return weakDelegate - } else { - return nil - } + func urlSession(_ session: URLSession, + downloadTask: URLSessionDownloadTask, + didResumeAtOffset fileOffset: Int64, + expectedTotalBytes: Int64) { + weakDelegate?.urlSession(session, + downloadTask: downloadTask, + didResumeAtOffset: fileOffset, + expectedTotalBytes: expectedTotalBytes) } + public typealias URLAuthenticationChallengeCompletion = OWSURLSession.URLAuthenticationChallengeCompletion + + func urlSession(_ session: URLSession, + task: URLSessionTask, + didReceive challenge: URLAuthenticationChallenge, + completionHandler: @escaping URLAuthenticationChallengeCompletion) { + weakDelegate?.urlSession(session, + task: task, + didReceive: challenge, + completionHandler: completionHandler) + } + + func urlSession(_ session: URLSession, + task: URLSessionTask, + didSendBodyData bytesSent: Int64, + totalBytesSent: Int64, + totalBytesExpectedToSend: Int64) { + weakDelegate?.urlSession(session, + task: task, + didSendBodyData: bytesSent, + totalBytesSent: totalBytesSent, + totalBytesExpectedToSend: totalBytesExpectedToSend) + } + + func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { + weakDelegate?.urlSession(session, + task: task, + didCompleteWithError: error) + } + + func urlSession(_ session: URLSession, + didReceive challenge: URLAuthenticationChallenge, + completionHandler: @escaping URLAuthenticationChallengeCompletion) { + weakDelegate?.urlSession(session, + didReceive: challenge, + completionHandler: completionHandler) + } + + func urlSession(_ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest: URLRequest, + completionHandler: @escaping (URLRequest?) -> Void) { + weakDelegate?.urlSession(session, + task: task, + willPerformHTTPRedirection: response, + newRequest: newRequest, + completionHandler: completionHandler) + } } diff --git a/SignalServiceKit/src/Protos/Generated/SSKProto.swift b/SignalServiceKit/src/Protos/Generated/SSKProto.swift index 51b835b0fc..494a0aa7cf 100644 --- a/SignalServiceKit/src/Protos/Generated/SSKProto.swift +++ b/SignalServiceKit/src/Protos/Generated/SSKProto.swift @@ -1,5 +1,5 @@ // -// Copyright (c) 2020 Open Whisper Systems. All rights reserved. +// Copyright (c) 2021 Open Whisper Systems. All rights reserved. // import Foundation @@ -1526,7 +1526,7 @@ public class SSKProtoCallMessageIceUpdate: NSObject, Codable { fileprivate convenience init(_ proto: SignalServiceProtos_CallMessage.IceUpdate) throws { guard proto.hasID else { - throw SSKProtoError.invalidProtobuf(description: "\(Self.logTag) missing required field: id") + throw SSKProtoError.invalidProtobuf(description: "\(Self.logTag()) missing required field: id") } let id = proto.id diff --git a/SignalServiceKit/src/Util/ModelReadCache.swift b/SignalServiceKit/src/Util/ModelReadCache.swift index 881e6df5c6..a072fd34a4 100644 --- a/SignalServiceKit/src/Util/ModelReadCache.swift +++ b/SignalServiceKit/src/Util/ModelReadCache.swift @@ -376,7 +376,7 @@ private class ModelReadCache StorageServiceProtoManifestRecordKey { - var builder = StorageServiceProtoManifestRecordKey.builder(data: data, type: type) + let builder = StorageServiceProtoManifestRecordKey.builder(data: data, type: type) return try builder.build() }