Skip to content

Commit

Permalink
Update documentation and function to emphasize helper tool usage
Browse files Browse the repository at this point in the history
 - Function forBlessedExecutable renamed to forBlessedHelperTool
 - README now includes how to use this convenience function
 - README and framework documentation

Additionally:
 - XPCMachServer: renames connection to client internally to make it clear this is actually representing an XPC connection - it doesn't necessarily correspond to just one client as XPC connections can be passed between processes. This has no externally visible change to the public API.
  • Loading branch information
Josh Kaplan committed Oct 26, 2021
1 parent 422a3e3 commit 81f098d
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 75 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
SecureXPC provides an easy way to perform secure XPC Mach Services communication.
[`Codable`](https://developer.apple.com/documentation/swift/codable) conforming types are used to send messages and
receive replies. This framework is ideal for communicating with privileged executables installed via
receive replies. This framework is ideal for communicating with helper tools installed via
[`SMJobBless`](https://developer.apple.com/documentation/servicemanagement/1431078-smjobbless).

# Usage
The envisioned pattern when using this framework is to define routes in a shared file, create a server in one executable
(or app) and register these routes, and then from another app (or executable) create a client and call these routes.

**Routes**
The envisioned pattern when using this framework is to define routes in a shared file, create a server in one program
(such as a helper tool) and register these routes, then from another program (such as an app) create a client and call
these routes.

## Routes
In a file shared by the client and server define one or more routes:
```swift
let route = XPCRouteWithMessageWithReply("bezaddle",
messageType: String.self,
replyType: Bool.self)
```

**Server**

In one executable (or app) create a server, register those routes, and then start the server:
## Server
In one program create a server, register those routes, and then start the server:
```swift
...
let server = XPCMachServer(machServiceName: "com.example.service",
Expand All @@ -32,9 +31,14 @@ private func bedazzle(message: String) throws -> Bool {
}
```

**Client**
If this program is a helper tool installed by `SMJobBless`, then in many cases it can be initialized automatically with
`XPCMachServer.forBlessedHelperTool()`:
```swift
let server = XPCMachServer.forBlessedHelperTool()
```

In another app (or executable) create a client, then call one of those routes:
## Client
In another program create a client, then call one of those routes:
```swift
let client = XPCMachClient(machServiceName: "com.example.service")
try client.sendMessage("Get Schwifty", route: route, withReply: { result in
Expand Down
29 changes: 17 additions & 12 deletions Sources/SecureXPC/SecureXPC.docc/SecureXPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ A **secure** high-level framework designed specifically for XPC Mach Services.

SecureXPC provides an easy way to perform secure XPC Mach Services communication.
[`Codable`](https://developer.apple.com/documentation/swift/codable) conforming types are used to send messages and
receive replies. This framework is ideal for communicating with privileged executables installed via
receive replies. This framework is ideal for communicating with helper tools installed via
[`SMJobBless`](https://developer.apple.com/documentation/servicemanagement/1431078-smjobbless).

#### Usage
The envisioned pattern when using this framework is to define routes in a shared file, create a server in one executable
(or app) and register these routes, and then from another app (or executable) create a client and call these routes.
## Usage
The envisioned pattern when using this framework is to define routes in a shared file, create a server in one program
(such as a helper tool) and register these routes, then from another program (such as an app) create a client and call
these routes.

**Routes**
#### Routes

In a file shared by the client and server define one or more routes:
```swift
Expand All @@ -23,9 +24,9 @@ let route = XPCRouteWithMessageWithReply("bezaddle",
```
There are four different types of routes; learn more about <doc:/Routes>.

**Server**
#### Server

In one executable (or app) create a server, register those routes, and then start the server:
In one program create a server, register those routes, and then start the server:
```swift
...
let server = XPCMachServer(machServiceName: "com.example.service",
Expand All @@ -38,11 +39,18 @@ private func bedazzle(message: String) throws -> Bool {
<# implementation here #>
}
```

If this program is a helper tool installed by `SMJobBless`, then in many cases it can be initialized automatically with
``XPCMachServer/forBlessedHelperTool()``:
```swift
let server = XPCMachServer.forBlessedHelperTool()
```

See ``XPCMachServer`` for details on how to create, configure, and start a server.

**Client**
#### Client

In another app (or executable) create a client, then call one of those routes:
In another program create a client, then call one of those routes:
```swift
let client = XPCMachClient(machServiceName: "com.example.service")
try client.sendMessage("Get Schwifty", route: route, withReply: { result in
Expand All @@ -57,9 +65,7 @@ try client.sendMessage("Get Schwifty", route: route, withReply: { result in
See ``XPCMachClient`` for more on how to send with a client.

## Topics

### Client and Server

- ``XPCMachClient``
- ``XPCMachServer``

Expand All @@ -71,5 +77,4 @@ See ``XPCMachClient`` for more on how to send with a client.
- ``XPCRouteWithMessageWithReply``

### Errors

- ``XPCError``
4 changes: 2 additions & 2 deletions Sources/SecureXPC/XPCCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public enum XPCError: Error {
case decodingError(DecodingError)
/// The route associated with the incoming XPC message is not registed with the server.
case routeNotRegistered(String)
/// The calling program's property list configuration is not compatible with ``XPCMachServer/forBlessedExecutable()``.
case misconfiguredBlessedExecutable(String)
/// The calling program's property list configuration is not compatible with ``XPCMachServer/forBlessedHelperTool()``.
case misconfiguredBlessedHelperTool(String)
/// An underlying error occurred which was not anticipated; the associated value is this error.
case other(Error)
/// Unknown error occurred.
Expand Down
3 changes: 1 addition & 2 deletions Sources/SecureXPC/XPCMachClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Foundation
/// ```swift
/// let client = XPCMachClient(machServiceName: "com.example.service")
/// let updateConfigRoute = XPCRouteWithMessageWithReply("update", "config",
/// messageType:Config.self,
/// messageType: Config.self,
/// replyType: Config.self)
/// let config = <# create Config instance #>
/// client.sendMessage(config, route: updateConfigRoute, withReply: {
Expand All @@ -61,7 +61,6 @@ import Foundation
/// - ``send(route:withReply:)``
/// - ``sendMessage(_:route:)``
/// - ``sendMessage(_:route:withReply:)``
///
/// ### Receiving Replies
/// - ``XPCReplyHandler``
public class XPCMachClient {
Expand Down
Loading

0 comments on commit 81f098d

Please sign in to comment.