@@ -56,7 +56,7 @@ final class FileDownloadManager: FileDownloadManagerProtocol {
56
56
57
57
weak var delegate : FileDownloadManagerDelegate ?
58
58
59
- init ( preferences: DownloadsPreferences = . init ( ) ) {
59
+ init ( preferences: DownloadsPreferences = . shared ) {
60
60
self . preferences = preferences
61
61
}
62
62
@@ -102,7 +102,17 @@ final class FileDownloadManager: FileDownloadManagerProtocol {
102
102
tempURL: location. tempURL,
103
103
isBurner: fromBurnerWindow)
104
104
105
- self . downloadTaskDelegates [ task] = { [ weak delegate] in delegate }
105
+ let shouldCancelDownloadIfDelegateIsGone = delegate != nil
106
+ self . downloadTaskDelegates [ task] = { [ weak delegate] in
107
+ if let delegate {
108
+ return delegate
109
+ }
110
+ // if the delegate was originally provided but deallocated since then – the download task should be cancelled
111
+ if shouldCancelDownloadIfDelegateIsGone {
112
+ return CancelledDownloadTaskDelegate ( )
113
+ }
114
+ return nil
115
+ }
106
116
107
117
downloads. insert ( task)
108
118
downloadAddedSubject. send ( task)
@@ -205,7 +215,7 @@ extension FileDownloadManager: WebKitDownloadTaskDelegate {
205
215
fileTypes. append ( fileType)
206
216
}
207
217
208
- delegate. chooseDestination ( suggestedFilename: suggestedFilename, directoryURL : downloadLocation , fileTypes: fileTypes) { [ weak self] url, fileType in
218
+ delegate. chooseDestination ( suggestedFilename: suggestedFilename, fileTypes: fileTypes) { [ weak self] url, fileType in
209
219
guard let self, let url else {
210
220
completion ( nil , nil )
211
221
return
@@ -259,8 +269,21 @@ extension FileDownloadManager: WebKitDownloadTaskDelegate {
259
269
protocol DownloadTaskDelegate : AnyObject {
260
270
261
271
@MainActor
262
- func chooseDestination( suggestedFilename: String ? , directoryURL : URL ? , fileTypes: [ UTType ] , callback: @escaping @MainActor ( URL ? , UTType ? ) -> Void )
272
+ func chooseDestination( suggestedFilename: String ? , fileTypes: [ UTType ] , callback: @escaping @MainActor ( URL ? , UTType ? ) -> Void )
263
273
@MainActor
264
274
func fileIconFlyAnimationOriginalRect( for downloadTask: WebKitDownloadTask ) -> NSRect ?
265
275
266
276
}
277
+
278
+ // if the original Download Task delegate is gone, this one is used to cancel the download
279
+ final class CancelledDownloadTaskDelegate : DownloadTaskDelegate {
280
+
281
+ func chooseDestination( suggestedFilename: String ? , fileTypes: [ UTType ] , callback: @escaping @MainActor ( URL ? , UTType ? ) -> Void ) {
282
+ callback ( nil , nil )
283
+ }
284
+
285
+ func fileIconFlyAnimationOriginalRect( for downloadTask: WebKitDownloadTask ) -> NSRect ? {
286
+ nil
287
+ }
288
+
289
+ }
0 commit comments