|
| 1 | +// |
| 2 | +// DownloadSessionDelegate.swift |
| 3 | +// Storage |
| 4 | +// |
| 5 | +// Created by Guilherme Souza on 04/05/26. |
| 6 | +// |
| 7 | + |
| 8 | +import ConcurrencyExtras |
| 9 | +import Foundation |
| 10 | + |
| 11 | +#if canImport(FoundationNetworking) |
| 12 | + import FoundationNetworking |
| 13 | +#endif |
| 14 | + |
| 15 | +final class DownloadSessionDelegate: NSObject, URLSessionDownloadDelegate, Sendable { |
| 16 | + |
| 17 | + struct DownloadTaskState { |
| 18 | + let eventsContinuation: AsyncStream<TransferEvent<URL>>.Continuation |
| 19 | + let resultContinuation: AsyncStream<Result<URL, any Error>>.Continuation |
| 20 | + } |
| 21 | + |
| 22 | + struct MutableState { |
| 23 | + var tasks: [Int: DownloadTaskState] = [:] |
| 24 | + var backgroundCompletionHandler: (@Sendable () -> Void)? |
| 25 | + } |
| 26 | + |
| 27 | + private let state = LockIsolated(MutableState()) |
| 28 | + |
| 29 | + // MARK: - Task creation |
| 30 | + |
| 31 | + /// Creates a `StorageDownloadTask` backed by this delegate. |
| 32 | + /// |
| 33 | + /// `buildRequest` is called asynchronously before the underlying |
| 34 | + /// `URLSessionDownloadTask` is created, so callers can fetch an auth token |
| 35 | + /// (via `_HTTPClient.createRequest`) without blocking. |
| 36 | + func makeStorageDownloadTask( |
| 37 | + in session: URLSession, |
| 38 | + buildRequest: @escaping @Sendable () async throws -> URLRequest |
| 39 | + ) -> StorageDownloadTask { |
| 40 | + let (eventStream, eventsContinuation) = AsyncStream<TransferEvent<URL>>.makeStream() |
| 41 | + let (resultStream, resultContinuation) = AsyncStream<Result<URL, any Error>>.makeStream( |
| 42 | + bufferingPolicy: .bufferingNewest(1)) |
| 43 | + |
| 44 | + let urlTaskRef = LockIsolated<URLSessionDownloadTask?>(nil) |
| 45 | + |
| 46 | + let resultTask = Task<URL, any Error> { |
| 47 | + for await r in resultStream { return try r.get() } |
| 48 | + throw StorageError.cancelled |
| 49 | + } |
| 50 | + |
| 51 | + // Bootstrap task: fetch the token, build the request, then start the download. |
| 52 | + let bootstrapTask = Task { |
| 53 | + do { |
| 54 | + let request = try await buildRequest() |
| 55 | + let urlTask = session.downloadTask(with: request) |
| 56 | + |
| 57 | + state.withValue { |
| 58 | + $0.tasks[urlTask.taskIdentifier] = DownloadTaskState( |
| 59 | + eventsContinuation: eventsContinuation, |
| 60 | + resultContinuation: resultContinuation |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + urlTaskRef.setValue(urlTask) |
| 65 | + urlTask.resume() |
| 66 | + } catch { |
| 67 | + let storageError = StorageError.from(error) |
| 68 | + eventsContinuation.yield(.failed(storageError)) |
| 69 | + eventsContinuation.finish() |
| 70 | + resultContinuation.yield(.failure(storageError)) |
| 71 | + resultContinuation.finish() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + eventsContinuation.onTermination = { [urlTaskRef] reason in |
| 76 | + guard case .cancelled = reason else { return } |
| 77 | + urlTaskRef.value?.cancel() |
| 78 | + bootstrapTask.cancel() |
| 79 | + } |
| 80 | + |
| 81 | + return StorageDownloadTask( |
| 82 | + events: eventStream, |
| 83 | + resultTask: resultTask, |
| 84 | + pause: { urlTaskRef.value?.suspend() }, |
| 85 | + resume: { urlTaskRef.value?.resume() }, |
| 86 | + cancel: { |
| 87 | + bootstrapTask.cancel() |
| 88 | + urlTaskRef.value?.cancel() |
| 89 | + // Finish continuations in case the bootstrap was cancelled before the |
| 90 | + // URLSessionDownloadTask existed — otherwise observers hang forever. |
| 91 | + eventsContinuation.finish() |
| 92 | + resultContinuation.finish() |
| 93 | + } |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + /// Package-level access for tests to drive delegate callbacks directly. |
| 98 | + package func makeDownloadTask( |
| 99 | + in session: URLSession, |
| 100 | + request: URLRequest |
| 101 | + ) -> ( |
| 102 | + stream: AsyncStream<TransferEvent<URL>>, |
| 103 | + eventsContinuation: AsyncStream<TransferEvent<URL>>.Continuation, |
| 104 | + task: URLSessionDownloadTask |
| 105 | + ) { |
| 106 | + let (eventStream, eventsContinuation) = AsyncStream<TransferEvent<URL>>.makeStream() |
| 107 | + let (resultStream, resultContinuation) = AsyncStream<Result<URL, any Error>>.makeStream( |
| 108 | + bufferingPolicy: .bufferingNewest(1)) |
| 109 | + let urlTask = session.downloadTask(with: request) |
| 110 | + state.withValue { |
| 111 | + $0.tasks[urlTask.taskIdentifier] = DownloadTaskState( |
| 112 | + eventsContinuation: eventsContinuation, |
| 113 | + resultContinuation: resultContinuation |
| 114 | + ) |
| 115 | + } |
| 116 | + _ = resultStream // satisfy unused warning — test uses stream directly via delegate callbacks |
| 117 | + _ = resultContinuation |
| 118 | + return (eventStream, eventsContinuation, urlTask) |
| 119 | + } |
| 120 | + |
| 121 | + func setBackgroundCompletionHandler(_ handler: @escaping @Sendable () -> Void) { |
| 122 | + state.withValue { $0.backgroundCompletionHandler = handler } |
| 123 | + } |
| 124 | + |
| 125 | + // MARK: - URLSessionDownloadDelegate |
| 126 | + |
| 127 | + func urlSession( |
| 128 | + _ session: URLSession, |
| 129 | + downloadTask: URLSessionDownloadTask, |
| 130 | + didWriteData bytesWritten: Int64, |
| 131 | + totalBytesWritten: Int64, |
| 132 | + totalBytesExpectedToWrite: Int64 |
| 133 | + ) { |
| 134 | + state.withValue { |
| 135 | + guard let taskState = $0.tasks[downloadTask.taskIdentifier] else { return } |
| 136 | + taskState.eventsContinuation.yield( |
| 137 | + .progress( |
| 138 | + TransferProgress( |
| 139 | + bytesTransferred: totalBytesWritten, |
| 140 | + totalBytes: totalBytesExpectedToWrite |
| 141 | + ))) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + func urlSession( |
| 146 | + _ session: URLSession, |
| 147 | + downloadTask: URLSessionDownloadTask, |
| 148 | + didFinishDownloadingTo location: URL |
| 149 | + ) { |
| 150 | + state.withValue { |
| 151 | + guard let taskState = $0.tasks[downloadTask.taskIdentifier] else { return } |
| 152 | + |
| 153 | + let destination = FileManager.default.temporaryDirectory |
| 154 | + .appendingPathComponent(UUID().uuidString) |
| 155 | + |
| 156 | + do { |
| 157 | + try FileManager.default.moveItem(at: location, to: destination) |
| 158 | + taskState.eventsContinuation.yield(.completed(destination)) |
| 159 | + taskState.eventsContinuation.finish() |
| 160 | + taskState.resultContinuation.yield(.success(destination)) |
| 161 | + taskState.resultContinuation.finish() |
| 162 | + } catch { |
| 163 | + let storageError = StorageError.fileSystemError(underlying: error) |
| 164 | + taskState.eventsContinuation.yield(.failed(storageError)) |
| 165 | + taskState.eventsContinuation.finish() |
| 166 | + taskState.resultContinuation.yield(.failure(storageError)) |
| 167 | + taskState.resultContinuation.finish() |
| 168 | + } |
| 169 | + $0.tasks.removeValue(forKey: downloadTask.taskIdentifier) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + func urlSession( |
| 174 | + _ session: URLSession, |
| 175 | + task: URLSessionTask, |
| 176 | + didCompleteWithError error: (any Error)? |
| 177 | + ) { |
| 178 | + guard let error else { return } |
| 179 | + |
| 180 | + state.withValue { |
| 181 | + guard let taskState = $0.tasks[task.taskIdentifier] else { return } |
| 182 | + |
| 183 | + let storageError: StorageError |
| 184 | + if (error as? URLError)?.code == .cancelled { |
| 185 | + storageError = .cancelled |
| 186 | + } else { |
| 187 | + storageError = .networkError(underlying: error) |
| 188 | + } |
| 189 | + |
| 190 | + taskState.eventsContinuation.yield(.failed(storageError)) |
| 191 | + taskState.eventsContinuation.finish() |
| 192 | + taskState.resultContinuation.yield(.failure(storageError)) |
| 193 | + taskState.resultContinuation.finish() |
| 194 | + $0.tasks.removeValue(forKey: task.taskIdentifier) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { |
| 199 | + state.withValue { |
| 200 | + $0.backgroundCompletionHandler?() |
| 201 | + $0.backgroundCompletionHandler = nil |
| 202 | + } |
| 203 | + } |
| 204 | +} |
0 commit comments