Custom schemes and protocols
Serve your app's own content from a scheme of your own, from files or generated, streamed or large.
@main
struct MyApp: CefSwiftApp {
static var runtimeConfiguration: CefRuntime.Configuration {
var configuration = CefRuntime.Configuration.embedded
configuration.customSchemes = [CefCustomScheme(name: "app")]
return configuration
}
init() {
let webRoot = Bundle.main.resourceURL!.appending(path: "web")
CefRuntime.shared.registerSchemeHandler(scheme: "app", handler: CefDirectorySchemeHandler(directory: webRoot))
}
var body: some Scene {
WindowGroup { CefWebView(url: URL(string: "app://local/index.html")!) }
}
}
Gallery: CustomSchemeDemo.
Serving your own content
Declare the scheme in the configuration, then register a handler once
Chromium is running, before any page loads from it. In a CefSwiftApp, the
app’s init() is the place:
@main
struct MyApp: CefSwiftApp {
static var runtimeConfiguration: CefRuntime.Configuration {
var configuration = CefRuntime.Configuration.embedded
configuration.customSchemes = [CefCustomScheme(name: "app")]
return configuration
}
init() {
let webRoot = Bundle.main.resourceURL!.appending(path: "web")
CefRuntime.shared.registerSchemeHandler(
scheme: "app", handler: CefDirectorySchemeHandler(directory: webRoot))
}
var body: some Scene {
WindowGroup { CefWebView(url: URL(string: "app://local/index.html")!) }
}
}
CefDirectorySchemeHandler serves files from a folder, with MIME types from
file extensions, and never outside that folder: a symbolic link is followed
only when its target lies inside the folder. For generated content,
implement CefSchemeHandler:
struct Pages: CefSchemeHandler {
func response(for request: CefSchemeRequest) async -> CefSchemeResponse {
CefSchemeResponse(mimeType: "text/html", body: Data("<h1>\(request.urlString)</h1>".utf8))
}
}
A handler registered with the runtime serves every profile, present and
future, incognito and persistent ones included. A profile can serve a
scheme its own way: profile.registerSchemeHandler(scheme:domain:handler:)
takes the place of the runtime’s handler for that scheme and domain in
that profile alone, and profile.removeSchemeHandler(scheme:domain:) brings
the runtime’s back. CefRuntime.shared.removeSchemeHandler(scheme:domain:)
removes the runtime’s from every profile without one of its own.
let preview = CefProfile.incognito()
preview.registerSchemeHandler(scheme: "app", handler: CefDirectorySchemeHandler(directory: URL(filePath: "/tmp/draft")))
The default CefCustomScheme options make the scheme
standard, secure, CORS-enabled and fetchable; pass options: to change
them.
Where a handler runs
Chromium asks for responses on its IO thread and never waits there for
the handler: response(for:) runs with the handler’s own isolation.
| Handler | Runs on | For |
|---|---|---|
A Sendable type, as above |
Swift’s concurrent executor, many requests at once | Computing or looking up a response |
Isolated to CefIOActor |
Chromium’s IO thread, without a hop | Quick answers from state kept there, such as a cache; never blocking |
Isolated to @MainActor |
The main thread | Reading your app’s model |
@MainActor
final class LibraryPages: CefSchemeHandler {
let library: Library
init(library: Library) { self.library = library }
func response(for request: CefSchemeRequest) async -> CefSchemeResponse {
CefSchemeResponse(mimeType: "text/html", body: Data(library.html.utf8))
}
}
Large and streamed bodies
A body is .data(_:), .file(_:) or .stream(_:length:). Chromium reads
a file or a stream as the page consumes it, never more than about a
megabyte ahead, so a 200 MB response costs about 12 MB of memory instead
of 200 MB. A file is read straight into Chromium’s buffer as the page
asks for more, on Chromium’s thread pool. A stream is any
AsyncSequence of Data; give its length when you know it.
struct Media: CefSchemeHandler {
let library: MediaLibrary
func response(for request: CefSchemeRequest) async -> CefSchemeResponse {
guard let file = library.file(for: request.url) else { return .notFound() }
return CefSchemeResponse(mimeType: "video/mp4", body: .file(file))
}
}
struct Generated: CefSchemeHandler {
func response(for request: CefSchemeRequest) async -> CefSchemeResponse {
let chunks = AsyncThrowingStream<Data, any Error> { continuation in
Task {
for part in 0..<100 { continuation.yield(Data("part \(part)\n".utf8)) }
continuation.finish()
}
}
return CefSchemeResponse(mimeType: "text/plain", body: .stream(chunks))
}
}
A request with a single Range header, as <video> and <audio> send
when they seek, gets 206 Partial Content with only those bytes, or 416
past the end, for any body whose length is known; a file skips to the
range without reading what comes before. Responses with a known length
carry Accept-Ranges: bytes. A handler that sets a status other than 200
answers ranges itself.