2023-11-21 11:29:25 +03:00
/*
* Copyright ( C ) 2023 by Claudio Cambra < claudio . cambra @ nextcloud . com >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* for more details .
*/
import Foundation
import FileProvider
2023-12-05 11:05:25 +03:00
import OSLog
2023-11-21 11:29:25 +03:00
class ClientCommunicationService : NSObject , NSFileProviderServiceSource , NSXPCListenerDelegate , ClientCommunicationProtocol {
let listener = NSXPCListener . anonymous ( )
let serviceName = NSFileProviderServiceName ( " com.nextcloud.desktopclient.ClientCommunicationService " )
let fpExtension : FileProviderExtension
init ( fpExtension : FileProviderExtension ) {
2023-12-11 14:48:40 +03:00
Logger . desktopClientConnection . debug ( " Instantiating client communication service " )
2023-11-21 11:29:25 +03:00
self . fpExtension = fpExtension
super . init ( )
}
func makeListenerEndpoint ( ) throws -> NSXPCListenerEndpoint {
2023-12-11 14:48:40 +03:00
listener . delegate = self
listener . resume ( )
2023-11-21 11:29:25 +03:00
return listener . endpoint
}
2023-12-11 14:48:40 +03:00
func listener ( _ listener : NSXPCListener , shouldAcceptNewConnection newConnection : NSXPCConnection ) -> Bool {
newConnection . exportedInterface = NSXPCInterface ( with : ClientCommunicationProtocol . self )
2023-11-21 12:03:45 +03:00
newConnection . exportedObject = self
newConnection . resume ( )
2023-11-21 11:29:25 +03:00
return true
}
2023-12-11 14:48:40 +03:00
// MARK: - C l i e n t C o m m u n i c a t i o n P r o t o c o l m e t h o d s
2023-12-05 11:05:25 +03:00
func getExtensionAccountId ( completionHandler : @ escaping ( String ? , Error ? ) -> Void ) {
let accountUserId = self . fpExtension . domain . identifier . rawValue
2023-12-12 20:37:13 +03:00
Logger . desktopClientConnection . info ( " Sending extension account ID \( accountUserId , privacy : . public ) " )
2023-12-05 11:05:25 +03:00
completionHandler ( accountUserId , nil )
}
2023-11-21 11:29:25 +03:00
func configureAccount ( withUser user : String ,
serverUrl : String ,
password : String ) {
2023-12-05 11:05:25 +03:00
Logger . desktopClientConnection . info ( " Received configure account information over client communication service " )
2023-11-21 11:29:25 +03:00
self . fpExtension . setupDomainAccount ( user : user ,
serverUrl : serverUrl ,
password : password )
}
func removeAccountConfig ( ) {
self . fpExtension . removeAccountConfig ( )
}
2024-01-02 15:11:05 +03:00
2024-01-03 12:16:00 +03:00
func createDebugLogString ( completionHandler : ( ( String ? , Error ? ) -> Void ) ! ) {
2024-01-02 15:11:05 +03:00
if #available ( macOSApplicationExtension 12.0 , * ) {
2024-01-03 12:16:00 +03:00
let ( logs , error ) = Logger . logEntries ( )
guard error = = nil else {
Logger . logger . error ( " Cannot create debug archive, received error: \( error , privacy : . public ) " )
completionHandler ( nil , error )
return
}
guard let logs = logs else {
Logger . logger . error ( " Canot create debug archive with nil logs. " )
completionHandler ( nil , nil )
return
}
completionHandler ( logs . joined ( separator : " \n " ) , nil )
2024-01-02 15:11:05 +03:00
}
}
2024-02-05 13:42:12 +03:00
func getFastEnumerationState ( completionHandler : @ escaping ( Bool , Bool ) -> Void ) {
let enabled = fpExtension . config . fastEnumerationEnabled
let set = fpExtension . config . fastEnumerationSet
completionHandler ( enabled , set )
}
2024-02-05 13:50:29 +03:00
func setFastEnumerationEnabled ( _ enabled : Bool ) {
fpExtension . config . fastEnumerationEnabled = enabled
2024-02-19 12:01:03 +03:00
Logger . fileProviderExtension . info ( " Fast enumeration setting changed to: \( enabled , privacy : . public ) " )
2024-02-05 13:50:29 +03:00
guard enabled else { return }
// I f e n a b l e d , s t a r t f u l l e n u m e r a t i o n
guard let fpManager = NSFileProviderManager ( for : fpExtension . domain ) else {
let domainName = self . fpExtension . domain . displayName
Logger . fileProviderExtension . error ( " Could not get file provider manager for domain \( domainName , privacy : . public ) , cannot run enumeration after fast enumeration setting change " )
return
}
fpManager . signalEnumerator ( for : . workingSet ) { error in
if error != nil {
Logger . fileProviderExtension . error ( " Error signalling enumerator for working set, received error: \( error ! . localizedDescription , privacy : . public ) " )
}
}
}
2023-11-21 11:29:25 +03:00
}