diff options
Diffstat (limited to 'extension/safari/TabFS/TabFSService')
4 files changed, 128 insertions, 0 deletions
diff --git a/extension/safari/TabFS/TabFSService/Info.plist b/extension/safari/TabFS/TabFSService/Info.plist new file mode 100644 index 0000000..8b1c3aa --- /dev/null +++ b/extension/safari/TabFS/TabFSService/Info.plist @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>CFBundleDevelopmentRegion</key> + <string>$(DEVELOPMENT_LANGUAGE)</string> + <key>CFBundleDisplayName</key> + <string>TabFSService</string> + <key>CFBundleExecutable</key> + <string>$(EXECUTABLE_NAME)</string> + <key>CFBundleIdentifier</key> + <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundleName</key> + <string>$(PRODUCT_NAME)</string> + <key>CFBundlePackageType</key> + <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + <key>XPCService</key> + <dict> + <key>ServiceType</key> + <string>Application</string> + </dict> +</dict> +</plist> diff --git a/extension/safari/TabFS/TabFSService/TabFSService.swift b/extension/safari/TabFS/TabFSService/TabFSService.swift new file mode 100644 index 0000000..f91a4b4 --- /dev/null +++ b/extension/safari/TabFS/TabFSService/TabFSService.swift @@ -0,0 +1,69 @@ +// +// TabFSService.swift +// TabFSService +// +// Created by Omar Rizwan on 2/7/21. +// + +import Foundation + +class TabFSService: NSObject, TabFSServiceProtocol { + var fs: Process! + var fsInput: FileHandle! + var fsOutput: FileHandle! + + init(app: TabFSServiceConsumerProtocol) { + super.init() + + fs = Process() + fs.executableURL = URL(fileURLWithPath: "/Users/osnr/Code/tabfs/fs/tabfs") + fs.currentDirectoryURL = fs.executableURL?.deletingLastPathComponent() + + fs.arguments = [] + + let inputPipe = Pipe(), outputPipe = Pipe() + fs.standardInput = inputPipe + fs.standardOutput = outputPipe + + fsInput = inputPipe.fileHandleForWriting + fsOutput = outputPipe.fileHandleForReading + + try! fs.run() + + // split new thread + DispatchQueue.global(qos: .default).async { + while true { + // read from them + let length = self.fsOutput.readData(ofLength: 4).withUnsafeBytes { $0.load(as: UInt32.self) } + let req = self.fsOutput.readData(ofLength: Int(length)) + // send to other side of XPC conn + app.request(req) + } + } + + // FIXME: disable auto termination + } + + func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) { + let response = string.uppercased() + reply(response) + } + + func response(_ resp: Data) { + fsInput.write(withUnsafeBytes(of: UInt32(resp.count)) { Data($0) }) + fsInput.write(resp) + } +} + +class TabFSServiceDelegate: NSObject, NSXPCListenerDelegate { + func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { + newConnection.remoteObjectInterface = NSXPCInterface(with: TabFSServiceConsumerProtocol.self) + + let exportedObject = TabFSService(app: newConnection.remoteObjectProxy as! TabFSServiceConsumerProtocol) + newConnection.exportedInterface = NSXPCInterface(with: TabFSServiceProtocol.self) + newConnection.exportedObject = exportedObject + + newConnection.resume() + return true + } +} diff --git a/extension/safari/TabFS/TabFSService/TabFSServiceProtocols.swift b/extension/safari/TabFS/TabFSService/TabFSServiceProtocols.swift new file mode 100644 index 0000000..298015a --- /dev/null +++ b/extension/safari/TabFS/TabFSService/TabFSServiceProtocols.swift @@ -0,0 +1,17 @@ +// +// TabFSServiceProtocols.swift +// app-sandbox-xpc-test +// +// Created by Omar Rizwan on 2/7/21. +// + +import Foundation + +@objc public protocol TabFSServiceConsumerProtocol { + func request(_ req: Data) +} +@objc public protocol TabFSServiceProtocol { + func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) + + func response(_ resp: Data) +} diff --git a/extension/safari/TabFS/TabFSService/main.swift b/extension/safari/TabFS/TabFSService/main.swift new file mode 100644 index 0000000..39c5a75 --- /dev/null +++ b/extension/safari/TabFS/TabFSService/main.swift @@ -0,0 +1,13 @@ +// +// main.m +// TabFSService +// +// Created by Omar Rizwan on 2/7/21. +// + +import Foundation + +let delegate = TabFSServiceDelegate() +let listener = NSXPCListener.service() +listener.delegate = delegate +listener.resume() |