safari: clean up code a bit

This commit is contained in:
Omar Rizwan 2021-02-08 04:21:58 -08:00
parent b19f68cc8e
commit 78f67aa873
4 changed files with 26 additions and 77 deletions

View file

@ -12,43 +12,35 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
func beginRequest(with context: NSExtensionContext) { func beginRequest(with context: NSExtensionContext) {
os_log(.default, "TabFSmsg Received message from browser.runtime.sendNativefffMessage: %@", context as! CVarArg)
let item = context.inputItems[0] as! NSExtensionItem let item = context.inputItems[0] as! NSExtensionItem
os_log(.default, "TabFSmsg item.userInfo %{public}@", item.userInfo as! CVarArg)
guard let message = item.userInfo?["message"] as? [AnyHashable: Any] else { return } guard let message = item.userInfo?["message"] as? [AnyHashable: Any] else { return }
guard message["op"] as! String == "safari_did_connect" else { return }
if message["op"] as! String == "safari_did_connect" { // The XPC service is a subprocess that lives outside the macOS App Sandbox.
os_log(.default, "TabFSmsg sdc") // 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)
// The XPC service is a subprocess that lives outside the macOS App Sandbox. // FIXME: report port back?
// It can do forbidden things like spawn tabfs filesystem and set up WebSocket server. let response = NSExtensionItem()
response.userInfo = [ "message": [ "aResponse to": "moop" ] ]
let connection = NSXPCConnection(serviceName: "com.rsnous.TabFSService") context.completeRequest(returningItems: [response]) { (what) in
print(what)
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)
// FIXME: report port back?
let response = NSExtensionItem()
response.userInfo = [ "message": [ "aResponse to": "moop" ] ]
context.completeRequest(returningItems: [response]) { (what) in
print(what)
}
} }
return
} }
return
} }
} }

View file

@ -14,8 +14,8 @@
filePath = "TabFS Extension/SafariWebExtensionHandler.swift" filePath = "TabFS Extension/SafariWebExtensionHandler.swift"
startingColumnNumber = "9223372036854775807" startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807"
startingLineNumber = "17" startingLineNumber = "15"
endingLineNumber = "17" endingLineNumber = "15"
landmarkName = "beginRequest(with:)" landmarkName = "beginRequest(with:)"
landmarkType = "7"> landmarkType = "7">
</BreakpointContent> </BreakpointContent>
@ -36,37 +36,5 @@
landmarkType = "7"> landmarkType = "7">
</BreakpointContent> </BreakpointContent>
</BreakpointProxy> </BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C3177652-3D89-4E01-8BF5-03C666008BDB"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "TabFS Extension/SafariWebExtensionHandler.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "20"
endingLineNumber = "20"
landmarkName = "beginRequest(with:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "111CBF7A-E3E9-4303-9961-9812F8781A39"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "TabFS Extension/SafariWebExtensionHandler.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "15"
endingLineNumber = "15"
landmarkName = "beginRequest(with:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints> </Breakpoints>
</Bucket> </Bucket>

View file

@ -27,9 +27,7 @@ class TabFSService: NSObject, TabFSServiceProtocol {
fsInput = inputPipe.fileHandleForWriting fsInput = inputPipe.fileHandleForWriting
fsOutput = outputPipe.fileHandleForReading fsOutput = outputPipe.fileHandleForReading
os_log(.default, "TabFSmsg tfs service: willrun")
try! fs.run() try! fs.run()
os_log(.default, "TabFSmsg tfs service: ran")
} }
var ws: NWListener! var ws: NWListener!
@ -61,7 +59,7 @@ class TabFSService: NSObject, TabFSServiceProtocol {
let context = NWConnection.ContentContext(identifier: "context", metadata: [metaData]) let context = NWConnection.ContentContext(identifier: "context", metadata: [metaData])
conn.send(content: req, contentContext: context, completion: .contentProcessed({ err in conn.send(content: req, contentContext: context, completion: .contentProcessed({ err in
if err != nil { if err != nil {
os_log(.default, "%{public}@ error: %{public}@", String(data: req, encoding: .utf8) as! CVarArg, err!.debugDescription as CVarArg) os_log(.default, "req %{public}@ error: %{public}@", String(data: req, encoding: .utf8) as! CVarArg, err!.debugDescription as CVarArg)
// FIXME: ERROR // FIXME: ERROR
} }
})) }))
@ -75,7 +73,6 @@ class TabFSService: NSObject, TabFSServiceProtocol {
return return
} }
os_log(.default, "resp %{public}@", String(data: resp, encoding: .utf8) as! CVarArg)
self.fsInput.write(withUnsafeBytes(of: UInt32(resp.count)) { Data($0) }) self.fsInput.write(withUnsafeBytes(of: UInt32(resp.count)) { Data($0) })
self.fsInput.write(resp) self.fsInput.write(resp)
read() read()
@ -89,7 +86,6 @@ class TabFSService: NSObject, TabFSServiceProtocol {
while true { while true {
// read from them // read from them
let length = self.fsOutput.readData(ofLength: 4).withUnsafeBytes { $0.load(as: UInt32.self) } let length = self.fsOutput.readData(ofLength: 4).withUnsafeBytes { $0.load(as: UInt32.self) }
os_log(.default, "TabFSmsg tfs service: read %{public}d", length)
let req = self.fsOutput.readData(ofLength: Int(length)) let req = self.fsOutput.readData(ofLength: Int(length))
// send to other side of WEBSOCKET // send to other side of WEBSOCKET
@ -107,17 +103,10 @@ class TabFSService: NSObject, TabFSServiceProtocol {
let response = string.uppercased() let response = string.uppercased()
reply(response) reply(response)
} }
//
// func response(_ resp: Data) {
// fsInput.write(withUnsafeBytes(of: UInt32(resp.count)) { Data($0) })
// fsInput.write(resp)
// }
} }
class TabFSServiceDelegate: NSObject, NSXPCListenerDelegate { class TabFSServiceDelegate: NSObject, NSXPCListenerDelegate {
func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool { func listener(_ listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
os_log(.default, "TabFSmsg tfs service: starting delegate")
let exportedObject = TabFSService() let exportedObject = TabFSService()
newConnection.exportedInterface = NSXPCInterface(with: TabFSServiceProtocol.self) newConnection.exportedInterface = NSXPCInterface(with: TabFSServiceProtocol.self)
newConnection.exportedObject = exportedObject newConnection.exportedObject = exportedObject