Make localSocketClient of FileProviderExtension a lazy var
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Wed, 4 Jan 2023 20:18:21 +0000 (21:18 +0100)
committerClaudio Cambra <claudio.cambra@nextcloud.com>
Fri, 12 May 2023 05:21:09 +0000 (13:21 +0800)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension.swift

index 15455e755da40e166e19d4e4409fc5330f251892..dc0a83f7340754a90e6f48babc0a51ab147769ad 100644 (file)
@@ -21,8 +21,19 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {
     let domain: NSFileProviderDomain
 
     let appGroupIdentifier: String? = Bundle.main.object(forInfoDictionaryKey: "SocketApiPrefix") as? String
-    var socketClient: LocalSocketClient = LocalSocketClient()
-    var ncAccount: FileProviderDomainNextcloudAccountData  = FileProviderDomainNextcloudAccountData()
+    var ncAccount: FileProviderDomainNextcloudAccountData = FileProviderDomainNextcloudAccountData()
+    lazy var socketClient: LocalSocketClient? = {
+        guard let fileProviderSocketApiPrefix = appGroupIdentifier else {
+            NSLog("Could not start file provider socket client properly as SocketApiPrefix is missing")
+            return nil;
+        }
+
+        let containerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: fileProviderSocketApiPrefix)
+        let socketPath = containerUrl?.appendingPathComponent(".fileprovidersocket", conformingTo: .archive)
+        let lineProcessor = FileProviderSocketLineProcessor(delegate: self)
+
+        return LocalSocketClient(socketPath: socketPath?.path, lineProcessor: lineProcessor)
+    }()
 
     let urlSessionIdentifier: String = "com.nextcloud.session.upload.fileproviderext"
     let urlSessionMaximumConnectionsPerHost = 5
@@ -43,7 +54,7 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {
         // The containing application must create a domain using `NSFileProviderManager.add(_:, completionHandler:)`. The system will then launch the application extension process, call `FileProviderExtension.init(domain:)` to instantiate the extension for that domain, and call methods on the instance.
 
         super.init()
-        startLocalSocketClient()
+        self.socketClient?.start()
     }
     
     func invalidate() {
@@ -94,26 +105,11 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension {
     }
 
     // MARK: Nextcloud desktop client communication
-
-    func startLocalSocketClient() {
-        guard let fileProviderSocketApiPrefix = appGroupIdentifier else {
-            NSLog("Could not start file provider socket client properly as SocketApiPrefix is missing")
-            return;
-        }
-
-        let containerUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: fileProviderSocketApiPrefix)
-        let socketPath = containerUrl?.appendingPathComponent(".fileprovidersocket", conformingTo: .archive)
-        let lineProcessor = FileProviderSocketLineProcessor(delegate: self)
-
-        self.socketClient = LocalSocketClient(socketPath: socketPath?.path, lineProcessor: lineProcessor)
-        self.socketClient.start();
-    }
-
     func sendFileProviderDomainIdentifier() {
         let command = "FILE_PROVIDER_DOMAIN_IDENTIFIER_REQUEST_REPLY"
         let argument = domain.identifier.rawValue
         let message = command + ":" + argument + "\n"
-        socketClient.sendMessage(message)
+        socketClient?.sendMessage(message)
     }
 
     func setupDomainAccount(keychainAccount:String) {