Skip to content

Commit 489f606

Browse files
Merge pull request #170 from nextcloud/ext
new-extension-session
2 parents 87f709d + ffbbb80 commit 489f606

2 files changed

Lines changed: 36 additions & 46 deletions

File tree

Sources/NextcloudKit/NKSession.swift

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,43 @@ public struct NKSession: Sendable {
1717
public let httpMaximumConnectionsPerHostInDownload: Int
1818
public let httpMaximumConnectionsPerHostInUpload: Int
1919
public let dav: String = "remote.php/dav"
20+
public var sharedCookieStorage = ""
2021
public let sessionData: Alamofire.Session
2122
public let sessionDataNoCache: Alamofire.Session
2223
public let sessionDownloadBackground: URLSession
23-
public let sessionDownloadBackgroundExt: URLSession
24+
public let backgroundSessionDelegate: URLSessionDelegate?
2425
public let sessionUploadBackground: URLSession
2526
public let sessionUploadBackgroundWWan: URLSession
26-
public let sessionUploadBackgroundExt: URLSession
27+
28+
public lazy var sessionDownloadBackgroundExt: URLSession = {
29+
let config = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionDownloadBackgroundExt)
30+
config.sharedContainerIdentifier = groupIdentifier
31+
config.sessionSendsLaunchEvents = true
32+
config.isDiscretionary = false
33+
config.allowsCellularAccess = true
34+
config.requestCachePolicy = .useProtocolCachePolicy
35+
config.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHostInDownload
36+
config.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
37+
#if os(iOS) || targetEnvironment(macCatalyst)
38+
config.multipathServiceType = .handover
39+
#endif
40+
return URLSession(configuration: config, delegate: backgroundSessionDelegate, delegateQueue: .main)
41+
}()
42+
43+
public lazy var sessionUploadBackgroundExt: URLSession = {
44+
let config = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionUploadBackgroundExt)
45+
config.sharedContainerIdentifier = groupIdentifier
46+
config.sessionSendsLaunchEvents = true
47+
config.isDiscretionary = false
48+
config.allowsCellularAccess = true
49+
config.requestCachePolicy = .useProtocolCachePolicy
50+
config.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHostInUpload
51+
config.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
52+
#if os(iOS) || targetEnvironment(macCatalyst)
53+
config.multipathServiceType = .handover
54+
#endif
55+
return URLSession(configuration: config, delegate: backgroundSessionDelegate, delegateQueue: .main)
56+
}()
2757

2858
init(nkCommonInstance: NKCommon,
2959
urlBase: String,
@@ -46,10 +76,10 @@ public struct NKSession: Sendable {
4676
self.httpMaximumConnectionsPerHost = httpMaximumConnectionsPerHost
4777
self.httpMaximumConnectionsPerHostInDownload = httpMaximumConnectionsPerHostInDownload
4878
self.httpMaximumConnectionsPerHostInUpload = httpMaximumConnectionsPerHostInUpload
79+
self.backgroundSessionDelegate = NKBackground(nkCommonInstance: nkCommonInstance)
4980

50-
let backgroundSessionDelegate = NKBackground(nkCommonInstance: nkCommonInstance)
5181
// Strange but works ?!?!
52-
let sharedCookieStorage = user + "@" + urlBase
82+
sharedCookieStorage = user + "@" + urlBase
5383

5484
// SessionData Alamofire
5585
let configurationSessionData = URLSessionConfiguration.af.default
@@ -100,26 +130,6 @@ public struct NKSession: Sendable {
100130
configurationDownloadBackground.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
101131
sessionDownloadBackground = URLSession(configuration: configurationDownloadBackground, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
102132

103-
// Session Download Background Extension
104-
let configurationDownloadBackgroundExt = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionDownloadBackgroundExt + UUID().uuidString)
105-
configurationDownloadBackgroundExt.allowsCellularAccess = true
106-
107-
if #available(macOS 11, *) {
108-
configurationDownloadBackgroundExt.sessionSendsLaunchEvents = true
109-
}
110-
111-
configurationDownloadBackgroundExt.isDiscretionary = false
112-
configurationDownloadBackgroundExt.httpMaximumConnectionsPerHost = self.httpMaximumConnectionsPerHostInDownload
113-
configurationDownloadBackgroundExt.requestCachePolicy = .useProtocolCachePolicy
114-
configurationDownloadBackgroundExt.sharedContainerIdentifier = groupIdentifier
115-
116-
#if os(iOS) || targetEnvironment(macCatalyst)
117-
configurationDownloadBackgroundExt.multipathServiceType = .handover
118-
#endif
119-
120-
configurationDownloadBackgroundExt.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
121-
sessionDownloadBackgroundExt = URLSession(configuration: configurationDownloadBackgroundExt, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
122-
123133
// Session Upload Background
124134
let configurationUploadBackground = URLSessionConfiguration.background(withIdentifier: NKCommon().getSessionConfigurationIdentifier(NKCommon().identifierSessionUploadBackground, account: account))
125135
configurationUploadBackground.allowsCellularAccess = true
@@ -152,25 +162,5 @@ public struct NKSession: Sendable {
152162
configurationUploadBackgroundWWan.requestCachePolicy = .useProtocolCachePolicy
153163
configurationUploadBackgroundWWan.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
154164
sessionUploadBackgroundWWan = URLSession(configuration: configurationUploadBackgroundWWan, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
155-
156-
// Session Upload Background Extension
157-
let configurationUploadBackgroundExt = URLSessionConfiguration.background(withIdentifier: NKCommon().identifierSessionUploadBackgroundExt + UUID().uuidString)
158-
configurationUploadBackgroundExt.allowsCellularAccess = true
159-
160-
if #available(macOS 11, *) {
161-
configurationUploadBackgroundExt.sessionSendsLaunchEvents = true
162-
}
163-
164-
configurationUploadBackgroundExt.isDiscretionary = false
165-
configurationUploadBackgroundExt.httpMaximumConnectionsPerHost = self.httpMaximumConnectionsPerHostInUpload
166-
configurationUploadBackgroundExt.requestCachePolicy = .useProtocolCachePolicy
167-
configurationUploadBackgroundExt.sharedContainerIdentifier = groupIdentifier
168-
169-
#if os(iOS) || targetEnvironment(macCatalyst)
170-
configurationUploadBackgroundExt.multipathServiceType = .handover
171-
#endif
172-
173-
configurationUploadBackgroundExt.httpCookieStorage = HTTPCookieStorage.sharedCookieStorage(forGroupContainerIdentifier: sharedCookieStorage)
174-
sessionUploadBackgroundExt = URLSession(configuration: configurationUploadBackgroundExt, delegate: backgroundSessionDelegate, delegateQueue: OperationQueue.main)
175165
}
176166
}

Sources/NextcloudKit/NextcloudKitBackground.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
5353
url = (serverUrlFileName as? String)?.encodedToUrl as? URL
5454
}
5555

56-
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
56+
guard var nkSession = nkCommonInstance.nksessions.session(forAccount: account),
5757
let urlForRequest = url
5858
else {
5959
return (nil, .urlError)
@@ -161,7 +161,7 @@ public final class NKBackground: NSObject, URLSessionTaskDelegate, URLSessionDel
161161
url = (serverUrlFileName as? String)?.encodedToUrl as? URL
162162
}
163163

164-
guard let nkSession = nkCommonInstance.nksessions.session(forAccount: account),
164+
guard var nkSession = nkCommonInstance.nksessions.session(forAccount: account),
165165
let urlForRequest = url
166166
else {
167167
return (nil, .urlError)

0 commit comments

Comments
 (0)