2021-01-31 22:48:26 +01:00
|
|
|
//
|
|
|
|
// SafariWebExtensionHandler.swift
|
|
|
|
// TabFS Extension
|
|
|
|
//
|
|
|
|
// Created by Omar Rizwan on 1/31/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SafariServices
|
2021-02-03 08:07:24 +01:00
|
|
|
import SafariServices.SFSafariApplication
|
2021-01-31 22:48:26 +01:00
|
|
|
import os.log
|
|
|
|
|
2021-02-08 06:44:46 +01:00
|
|
|
class TabFSServiceManager: TabFSServiceConsumerProtocol {
|
|
|
|
static let shared = TabFSServiceManager()
|
|
|
|
|
|
|
|
var service: TabFSServiceProtocol!
|
|
|
|
|
|
|
|
func connect() {
|
|
|
|
let connection = NSXPCConnection(serviceName: "com.rsnous.TabFSService")
|
|
|
|
|
|
|
|
connection.remoteObjectInterface = NSXPCInterface(with: TabFSServiceProtocol.self)
|
|
|
|
|
|
|
|
connection.exportedInterface = NSXPCInterface(with: TabFSServiceConsumerProtocol.self)
|
|
|
|
connection.exportedObject = self
|
|
|
|
|
|
|
|
connection.resume()
|
|
|
|
|
|
|
|
service = connection.remoteObjectProxyWithErrorHandler { error in
|
|
|
|
os_log(.default, "Received error: %{public}@", error as! CVarArg)
|
|
|
|
} as? TabFSServiceProtocol
|
|
|
|
|
|
|
|
service?.upperCaseString("hello XPC") { response in
|
|
|
|
os_log(.default, "Response from XPC service: %{public}@", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func request(_ req: Data) {
|
|
|
|
SFSafariApplication.dispatchMessage(
|
|
|
|
withName: "ToSafari",
|
|
|
|
toExtensionWithIdentifier: "com.rsnous.TabFS-Extension",
|
|
|
|
userInfo: try! JSONSerialization.jsonObject(with: req, options: []) as! [String : Any]
|
|
|
|
) { error in
|
|
|
|
debugPrint("Message attempted. Error info: \(String.init(describing: error))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func response(_ resp: [AnyHashable: Any]) {
|
|
|
|
try! service.response(JSONSerialization.data(withJSONObject: resp, options: []))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:48:26 +01:00
|
|
|
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")
|
|
|
|
TabFSServiceManager.shared.connect()
|
2021-02-07 11:46:07 +01:00
|
|
|
//
|
|
|
|
// let response = NSExtensionItem()
|
|
|
|
// response.userInfo = [ "message": [ "aResponse to": "moop" ] ]
|
|
|
|
// context.completeRequest(returningItems: [response], completionHandler: nil)
|
2021-02-03 08:07:24 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-02-08 06:44:46 +01:00
|
|
|
|
|
|
|
TabFSServiceManager.shared.response(message)
|
2021-02-03 08:07:24 +01:00
|
|
|
//
|
|
|
|
// os_log(.default, "Received message from browser.runtime.sendNativeMessage: %@", op as! CVarArg)
|
|
|
|
|
|
|
|
// let response = NSExtensionItem()
|
|
|
|
// response.userInfo = [ "message": [ "Response to": op ] ]
|
|
|
|
//
|
|
|
|
// // How do I get too the app????
|
|
|
|
//
|
|
|
|
// context.completeRequest(returningItems: [response], completionHandler: nil)
|
2021-01-31 22:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|