mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-10 15:23:27 +01:00
* [Shared] Revises ALTLocalizedError protocol * Refactors errors to conform to revised ALTLocalizedError protocol * [Missing Commit] Remaining changes for ALTLocalizedError * [AltServer] Refactors errors to conform to revised ALTLocalizedError protocol * [Missing Commit] Declares ALTLocalizedTitleErrorKey + ALTLocalizedDescriptionKey * Updates Objective-C errors to match revised ALTLocalizedError * [Missing Commit] Unnecessary ALTLocalizedDescription logic * [Shared] Refactors NSError.withLocalizedFailure to properly support ALTLocalizedError * [Shared] Supports adding localized titles to errors via NSError.withLocalizedTitle() * Revises ErrorResponse logic to support arbitrary errors and user info values * [Missed Commit] Renames CodableServerError to CodableError * Merges ConnectionError into OperationError * [Missed Commit] Doesn’t check ALTWrappedError’s userInfo for localizedDescription * [Missed] Fixes incorrect errorDomain for ALTErrorEnums * [Missed] Removes nonexistent ALTWrappedError.h * Includes source file and line number in OperationError.unknown failureReason * Adds localizedTitle to AppManager operation errors * Fixes adding localizedTitle + localizedFailure to ALTWrappedError * Updates ToastView to use error’s localizedTitle as title * [Shared] Adds NSError.formattedDetailedDescription(with:) Returns formatted NSAttributedString containing all user info values intended for displaying to the user. * [Shared] Updates Error.localizedErrorCode to say “code” instead of “error” * Conforms ALTLocalizedError to CustomStringConvertible * Adds “View More Details” option to Error Log context menu to view detailed error description * [Shared] Fixes NSError.formattedDetailedDescription appearing black in dark mode * [AltServer] Updates error alert to match revised error logic Uses error’s localizedTitle as alert title. * [AltServer] Adds “View More Details” button to error alert to view detailed error info * [AltServer] Renames InstallError to OperationError and conforms to ALTErrorEnum * [Shared] Removes CodableError support for Date user info values Not currently used, and we don’t want to accidentally parse a non-Date as a Date in the meantime. * [Shared] Includes dynamic UserInfoValueProvider values in NSError.formattedDetailedDescription() * [Shared] Includes source file + line in NSError.formattedDetailedDescription() Automatically captures source file + line when throwing ALTErrorEnums. * [Shared] Captures source file + line for unknown errors * Removes sourceFunction from OperationError * Adds localizedTitle to AuthenticationViewController errors * [Shared] Moves nested ALTWrappedError logic to ALTWrappedError initializer * [AltServer] Removes now-redundant localized failure from JIT errors All JIT errors now have a localizedTitle which effectively says the same thing. * Makes OperationError.Code start at 1000 “Connection errors” subsection starts at 1200. * [Shared] Updates Error domains to revised [Source].[ErrorType] format * Updates ALTWrappedError.localizedDescription to prioritize using wrapped NSLocalizedDescription as failure reason * Makes ALTAppleAPIError codes start at 3000 * [AltServer] Adds relevant localizedFailures to ALTDeviceManager.installApplication() errors * Revises OperationError failureReasons and recovery suggestions All failure reasons now read correctly when preceded by a failure reason and “because”. * Revises ALTServerError error messages All failure reasons now read correctly when preceded by a failure reason and “because”. * Most failure reasons now read correctly when preceded by a failure reason and “because”. * ALTServerErrorUnderlyingError forwards all user info provider calls to underlying error. * Revises error messages for ALTAppleAPIErrorIncorrectCredentials * [Missed] Removes NSError+AltStore.swift from AltStore target * [Shared] Updates AltServerErrorDomain to revised [Source].[ErrorType] format * [Shared] Removes “code” from Error.localizedErrorCode * [Shared] Makes ALTServerError codes (appear to) start at 2000 We can’t change the actual error codes without breaking backwards compatibility, so instead we just add 2000 whenever we display ALTServerError codes to the user. * Moves VerificationError.errorFailure to VerifyAppOperation * Supports custom failure reason for OperationError.unknown * [Shared] Changes AltServerErrorDomain to “AltServer.ServerError” * [Shared] Converts ALTWrappedError to Objective-C class NSError subclasses must be written in ObjC for Swift.Error <-> NSError bridging to work correctly. # Conflicts: # AltStore.xcodeproj/project.pbxproj * Fixes decoding CodableError nested user info values
273 lines
11 KiB
Swift
273 lines
11 KiB
Swift
//
|
|
// BackgroundRefreshAppsOperation.swift
|
|
// AltStore
|
|
//
|
|
// Created by Riley Testut on 7/6/20.
|
|
// Copyright © 2020 Riley Testut. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import CoreData
|
|
|
|
import AltStoreCore
|
|
|
|
typealias RefreshError = RefreshErrorCode.Error
|
|
enum RefreshErrorCode: Int, ALTErrorEnum, CaseIterable
|
|
{
|
|
case noInstalledApps
|
|
|
|
var errorFailureReason: String {
|
|
switch self
|
|
{
|
|
case .noInstalledApps: return NSLocalizedString("No active apps require refreshing.", comment: "")
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension CFNotificationName
|
|
{
|
|
static let requestAppState = CFNotificationName("com.altstore.RequestAppState" as CFString)
|
|
static let appIsRunning = CFNotificationName("com.altstore.AppState.Running" as CFString)
|
|
|
|
static func requestAppState(for appID: String) -> CFNotificationName
|
|
{
|
|
let name = String(CFNotificationName.requestAppState.rawValue) + "." + appID
|
|
return CFNotificationName(name as CFString)
|
|
}
|
|
|
|
static func appIsRunning(for appID: String) -> CFNotificationName
|
|
{
|
|
let name = String(CFNotificationName.appIsRunning.rawValue) + "." + appID
|
|
return CFNotificationName(name as CFString)
|
|
}
|
|
}
|
|
|
|
private let ReceivedApplicationState: @convention(c) (CFNotificationCenter?, UnsafeMutableRawPointer?, CFNotificationName?, UnsafeRawPointer?, CFDictionary?) -> Void =
|
|
{ (center, observer, name, object, userInfo) in
|
|
guard let name = name, let observer = observer else { return }
|
|
|
|
let operation = unsafeBitCast(observer, to: BackgroundRefreshAppsOperation.self)
|
|
operation.receivedApplicationState(notification: name)
|
|
}
|
|
|
|
@objc(BackgroundRefreshAppsOperation)
|
|
class BackgroundRefreshAppsOperation: ResultOperation<[String: Result<InstalledApp, Error>]>
|
|
{
|
|
let installedApps: [InstalledApp]
|
|
private let managedObjectContext: NSManagedObjectContext
|
|
|
|
var presentsFinishedNotification: Bool = true
|
|
|
|
private let refreshIdentifier: String = UUID().uuidString
|
|
private var runningApplications: Set<String> = []
|
|
|
|
init(installedApps: [InstalledApp])
|
|
{
|
|
self.installedApps = installedApps
|
|
self.managedObjectContext = installedApps.compactMap({ $0.managedObjectContext }).first ?? DatabaseManager.shared.persistentContainer.newBackgroundContext()
|
|
|
|
super.init()
|
|
}
|
|
|
|
override func finish(_ result: Result<[String: Result<InstalledApp, Error>], Error>)
|
|
{
|
|
super.finish(result)
|
|
|
|
self.scheduleFinishedRefreshingNotification(for: result, delay: 0)
|
|
|
|
self.managedObjectContext.perform {
|
|
self.stopListeningForRunningApps()
|
|
}
|
|
|
|
DispatchQueue.main.async {
|
|
if UIApplication.shared.applicationState == .background
|
|
{
|
|
ServerManager.shared.stopDiscovering()
|
|
}
|
|
}
|
|
}
|
|
|
|
override func main()
|
|
{
|
|
super.main()
|
|
|
|
guard !self.installedApps.isEmpty else {
|
|
self.finish(.failure(RefreshError(.noInstalledApps)))
|
|
return
|
|
}
|
|
|
|
if !ServerManager.shared.isDiscovering
|
|
{
|
|
ServerManager.shared.startDiscovering()
|
|
}
|
|
|
|
self.managedObjectContext.perform {
|
|
print("Apps to refresh:", self.installedApps.map(\.bundleIdentifier))
|
|
|
|
self.startListeningForRunningApps()
|
|
|
|
// Wait for 3 seconds (2 now, 1 later in FindServerOperation) to:
|
|
// a) give us time to discover AltServers
|
|
// b) give other processes a chance to respond to requestAppState notification
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
|
|
self.managedObjectContext.perform {
|
|
|
|
let filteredApps = self.installedApps.filter { !self.runningApplications.contains($0.bundleIdentifier) }
|
|
print("Filtered Apps to Refresh:", filteredApps.map { $0.bundleIdentifier })
|
|
|
|
let group = AppManager.shared.refresh(filteredApps, presentingViewController: nil)
|
|
group.beginInstallationHandler = { (installedApp) in
|
|
guard installedApp.bundleIdentifier == StoreApp.altstoreAppID else { return }
|
|
|
|
// We're starting to install AltStore, which means the app is about to quit.
|
|
// So, we schedule a "refresh successful" local notification to be displayed after a delay,
|
|
// but if the app is still running, we cancel the notification.
|
|
// Then, we schedule another notification and repeat the process.
|
|
|
|
// Also since AltServer has already received the app, it can finish installing even if we're no longer running in background.
|
|
|
|
if let error = group.context.error
|
|
{
|
|
self.scheduleFinishedRefreshingNotification(for: .failure(error))
|
|
}
|
|
else
|
|
{
|
|
var results = group.results
|
|
results[installedApp.bundleIdentifier] = .success(installedApp)
|
|
|
|
self.scheduleFinishedRefreshingNotification(for: .success(results))
|
|
}
|
|
}
|
|
group.completionHandler = { (results) in
|
|
self.finish(.success(results))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension BackgroundRefreshAppsOperation
|
|
{
|
|
func startListeningForRunningApps()
|
|
{
|
|
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
|
|
let observer = Unmanaged.passUnretained(self).toOpaque()
|
|
|
|
for installedApp in self.installedApps
|
|
{
|
|
let appIsRunningNotification = CFNotificationName.appIsRunning(for: installedApp.bundleIdentifier)
|
|
CFNotificationCenterAddObserver(notificationCenter, observer, ReceivedApplicationState, appIsRunningNotification.rawValue, nil, .deliverImmediately)
|
|
|
|
let requestAppStateNotification = CFNotificationName.requestAppState(for: installedApp.bundleIdentifier)
|
|
CFNotificationCenterPostNotification(notificationCenter, requestAppStateNotification, nil, nil, true)
|
|
}
|
|
}
|
|
|
|
func stopListeningForRunningApps()
|
|
{
|
|
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
|
|
let observer = Unmanaged.passUnretained(self).toOpaque()
|
|
|
|
for installedApp in self.installedApps
|
|
{
|
|
let appIsRunningNotification = CFNotificationName.appIsRunning(for: installedApp.bundleIdentifier)
|
|
CFNotificationCenterRemoveObserver(notificationCenter, observer, appIsRunningNotification, nil)
|
|
}
|
|
}
|
|
|
|
func receivedApplicationState(notification: CFNotificationName)
|
|
{
|
|
let baseName = String(CFNotificationName.appIsRunning.rawValue)
|
|
|
|
let appID = String(notification.rawValue).replacingOccurrences(of: baseName + ".", with: "")
|
|
self.runningApplications.insert(appID)
|
|
}
|
|
|
|
func scheduleFinishedRefreshingNotification(for result: Result<[String: Result<InstalledApp, Error>], Error>, delay: TimeInterval = 5)
|
|
{
|
|
func scheduleFinishedRefreshingNotification()
|
|
{
|
|
self.cancelFinishedRefreshingNotification()
|
|
|
|
let content = UNMutableNotificationContent()
|
|
|
|
var shouldPresentAlert = true
|
|
|
|
do
|
|
{
|
|
let results = try result.get()
|
|
shouldPresentAlert = !results.isEmpty
|
|
|
|
for (_, result) in results
|
|
{
|
|
guard case let .failure(error) = result else { continue }
|
|
throw error
|
|
}
|
|
|
|
content.title = NSLocalizedString("Refreshed Apps", comment: "")
|
|
content.body = NSLocalizedString("All apps have been refreshed.", comment: "")
|
|
}
|
|
catch ~OperationError.Code.serverNotFound
|
|
{
|
|
shouldPresentAlert = false
|
|
}
|
|
catch ~RefreshErrorCode.noInstalledApps
|
|
{
|
|
shouldPresentAlert = false
|
|
}
|
|
catch
|
|
{
|
|
print("Failed to refresh apps in background.", error)
|
|
|
|
content.title = NSLocalizedString("Failed to Refresh Apps", comment: "")
|
|
content.body = error.localizedDescription
|
|
|
|
shouldPresentAlert = true
|
|
}
|
|
|
|
if shouldPresentAlert
|
|
{
|
|
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: delay + 1, repeats: false)
|
|
|
|
let request = UNNotificationRequest(identifier: self.refreshIdentifier, content: content, trigger: trigger)
|
|
UNUserNotificationCenter.current().add(request)
|
|
|
|
if delay > 0
|
|
{
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + delay) {
|
|
UNUserNotificationCenter.current().getPendingNotificationRequests() { (requests) in
|
|
// If app is still running at this point, we schedule another notification with same identifier.
|
|
// This prevents the currently scheduled notification from displaying, and starts another countdown timer.
|
|
// First though, make sure there _is_ still a pending request, otherwise it's been cancelled
|
|
// and we should stop polling.
|
|
guard requests.contains(where: { $0.identifier == self.refreshIdentifier }) else { return }
|
|
|
|
scheduleFinishedRefreshingNotification()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if self.presentsFinishedNotification
|
|
{
|
|
scheduleFinishedRefreshingNotification()
|
|
}
|
|
|
|
// Perform synchronously to ensure app doesn't quit before we've finishing saving to disk.
|
|
let context = DatabaseManager.shared.persistentContainer.newBackgroundContext()
|
|
context.performAndWait {
|
|
_ = RefreshAttempt(identifier: self.refreshIdentifier, result: result, context: context)
|
|
|
|
do { try context.save() }
|
|
catch { print("Failed to save refresh attempt.", error) }
|
|
}
|
|
}
|
|
|
|
func cancelFinishedRefreshingNotification()
|
|
{
|
|
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [self.refreshIdentifier])
|
|
}
|
|
}
|