2021-01-31 22:48:26 +01:00
|
|
|
//
|
|
|
|
// SafariWebExtensionHandler.swift
|
|
|
|
// TabFS Extension
|
|
|
|
//
|
|
|
|
// Created by Omar Rizwan on 1/31/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SafariServices
|
|
|
|
import os.log
|
|
|
|
|
|
|
|
class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
|
2021-02-03 08:07:24 +01:00
|
|
|
|
2021-01-31 22:48:26 +01:00
|
|
|
func beginRequest(with context: NSExtensionContext) {
|
2021-02-03 08:07:24 +01:00
|
|
|
|
2021-02-08 06:44:46 +01:00
|
|
|
os_log(.default, "TabFSmsg Received message from browser.runtime.sendNativefffMessage: %@", context as! CVarArg)
|
2021-02-03 08:07:24 +01:00
|
|
|
|
2021-01-31 22:48:26 +01:00
|
|
|
let item = context.inputItems[0] as! NSExtensionItem
|
2021-02-08 06:44:46 +01:00
|
|
|
|
|
|
|
os_log(.default, "TabFSmsg item.userInfo %{public}@", item.userInfo as! CVarArg)
|
2021-02-03 08:07:24 +01:00
|
|
|
guard let message = item.userInfo?["message"] as? [AnyHashable: Any] else { return }
|
|
|
|
|
|
|
|
if message["op"] as! String == "safari_did_connect" {
|
2021-02-08 06:44:46 +01:00
|
|
|
os_log(.default, "TabFSmsg sdc")
|
2021-02-08 11:32:21 +01:00
|
|
|
|
|
|
|
// The XPC service is a subprocess that lives outside the macOS App Sandbox.
|
|
|
|
// It can do forbidden things like spawn tabfs filesystem and set up WebSocket server.
|
|
|
|
|
|
|
|
let connection = NSXPCConnection(serviceName: "com.rsnous.TabFSService")
|
|
|
|
|
|
|
|
connection.remoteObjectInterface = NSXPCInterface(with: TabFSServiceProtocol.self)
|
|
|
|
|
|
|
|
connection.resume()
|
|
|
|
|
|
|
|
let service = connection.remoteObjectProxyWithErrorHandler { error in
|
|
|
|
os_log(.default, "Received error: %{public}@", error as! CVarArg)
|
|
|
|
} as? TabFSServiceProtocol
|
|
|
|
|
|
|
|
// need this one XPC call to actually initialize the service
|
|
|
|
service?.upperCaseString("hello XPC") { response in
|
|
|
|
os_log(.default, "Response from XPC service: %{public}@", response)
|
2021-02-08 13:02:47 +01:00
|
|
|
|
|
|
|
// FIXME: report port back?
|
|
|
|
let response = NSExtensionItem()
|
|
|
|
response.userInfo = [ "message": [ "aResponse to": "moop" ] ]
|
|
|
|
context.completeRequest(returningItems: [response]) { (what) in
|
|
|
|
print(what)
|
|
|
|
}
|
2021-02-08 11:32:21 +01:00
|
|
|
}
|
2021-02-03 08:07:24 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-01-31 22:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|