File: FPUIExtensionServiceSource.swift

package info (click to toggle)
nextcloud-desktop 4.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,404 kB
  • sloc: cpp: 118,401; objc: 752; python: 606; sh: 395; ansic: 391; ruby: 174; makefile: 44; javascript: 32; xml: 6
file content (101 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  FPUIExtensionCommunicationService.swift
//  FileProviderExt
//
//  SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
//  SPDX-License-Identifier: GPL-2.0-or-later
//

import FileProvider
import Foundation
import NextcloudKit
import NextcloudFileProviderKit
import OSLog

class FPUIExtensionServiceSource: NSObject, NSFileProviderServiceSource, NSXPCListenerDelegate, FPUIExtensionService {
    let keychain: Keychain
    let listener = NSXPCListener.anonymous()
    let logger: FileProviderLogger
    let serviceName = fpUiExtensionServiceName
    let fpExtension: FileProviderExtension

    init(fpExtension: FileProviderExtension) {
        keychain = Keychain(log: fpExtension.log)
        logger = FileProviderLogger(category: "FPUIExtensionServiceSource", log: fpExtension.log)
        logger.debug("Instantiating FPUIExtensionService service")
        self.fpExtension = fpExtension
        super.init()
    }

    func makeListenerEndpoint() throws -> NSXPCListenerEndpoint {
        listener.delegate = self
        listener.resume()
        return listener.endpoint
    }

    func listener(
        _ listener: NSXPCListener,
        shouldAcceptNewConnection newConnection: NSXPCConnection
    ) -> Bool {
        newConnection.exportedInterface = NSXPCInterface(with: FPUIExtensionService.self)
        newConnection.exportedObject = self
        newConnection.resume()
        return true
    }

    //MARK: - FPUIExtensionService protocol methods

    func authenticate() async -> NSError? {
        logger.info("Authenticating...")

        guard let user = fpExtension.config.user, let userId = fpExtension.config.userId, let serverUrl = fpExtension.config.serverUrl, let password = keychain.getPassword(for: user, on: serverUrl) else {
            logger.error("Missing account information, cannot authenticate!")
            return NSError(.missingAccountInformation)
        }

        return await withCheckedContinuation { continuation in
            fpExtension.setupDomainAccount(user: user, userId: userId, serverUrl: serverUrl, password: password) { error in
                continuation.resume(returning: error)
            }
        }
    }

    func userAgent() async -> NSString? {
        guard let account = fpExtension.ncAccount?.ncKitAccount else {
            return nil
        }

        let nkSession = fpExtension.ncKit.nkCommonInstance.nksessions.session(forAccount: account)
        return nkSession?.userAgent as NSString?
    }

    func credentials() async -> NSDictionary {
        return (fpExtension.ncAccount?.dictionary() ?? [:]) as NSDictionary
    }

    func itemServerPath(identifier: NSFileProviderItemIdentifier) async -> NSString? {
        let rawIdentifier = identifier.rawValue
        logger.info("Fetching shares for item \(rawIdentifier)")

        guard let baseUrl = fpExtension.ncAccount?.davFilesUrl else {
            logger.error("Could not fetch shares as ncAccount on parent extension is nil")
            return nil
        }

        guard let account = fpExtension.ncAccount?.ncKitAccount else {
            logger.error("Could not fetch ncKitAccount on parent extension")
            return nil
        }
        guard let dbManager = fpExtension.dbManager else {
            logger.error("Could not get db manager for \(account)")
            return nil
        }
        guard let item = dbManager.itemMetadataFromFileProviderItemIdentifier(identifier) else {
            logger.error("No item \(rawIdentifier) in db, no shares.")
            return nil
        }

        let completePath = item.serverUrl + "/" + item.fileName
        return completePath.replacingOccurrences(of: baseUrl, with: "") as NSString
    }
}