Skip to content
CefSwift
Esc
↑↓navigate↵open⌘Jpreview
On this page

Chromium's services over Mojo

How CefChromium reaches bookmarks, history, suggestions, tabs and discarding through Chromium's own WebUI handlers in hidden service hosts.

import CefChromium

// Starts the history host (about 0.1 s), which closes 30 s after its last call.
let topSites = try await profile.history.topSites()
print(topSites.count)

Gallery: HistoryDemo.

Internal-page services

CEF has no API for bookmarks, history, most-visited sites, omnibox suggestions or tab discarding, but Chromium’s own WebUI pages reach those services. CefServiceHosts, one per profile, calls them from hidden Chrome-style pages it calls service hosts. The engine is CefKit’s, since native tab discarding uses it; the bookmark, history, most-visited and omnibox hosts and modules are CefChromium’s, declared there and registered with the engine by being called.

CefHistory.topSites() binds a new most-visited page handler for each call. Chromium’s TopSites reads the list from history once, shortly after it loads, and then hourly, and a handler’s updateMostVisitedInfo returns that cached list; creating a handler is what makes TopSites read history again. The handler reports the cached tiles first and the fresh ones when they differ, so the call waits a moment for a second report.

What binds where. The browser process binds a WebUI page’s Mojo interfaces, and grants its extension APIs, by the page’s host, whatever its path. A renderer that asks for an interface its host does not bind is killed (WebUIController binder missing for: …, bad IPC message 221). So each interface has one host, and the hosts cannot share a renderer: Chromium locks a WebUI renderer to its host, and neither --process-per-site nor --renderer-process-limit=1 changes that.

Host Reaches Used for
chrome://bookmarks chrome.bookmarks, chrome.tabs, chrome.windows, chrome://favicon2 CefBookmarks, CefHistory.favicon, CefTabs’s reads of tabs and windows and its changes (no chrome.tabs event listeners: one on this page crashed Chromium later, when an extension was disabled or an incognito window opened)
chrome://tab-strip-internals tab_strip_internals.mojom.PageHandlerFactory CefTabs’s groups (names, colours, collapsing), tab alerts, recently closed, and the updates that wake its reads
chrome://history history.mojom.PageHandler, chrome://favicon2 CefHistory.search, remove
chrome://new-tab-page-third-party most_visited.mojom.MostVisitedPageHandlerFactory CefHistory.topSites
chrome://omnibox-internals mojom.OmniboxInternalsPageHandler CefAutocomplete
chrome://discards discards.mojom.DetailsProvider, discards.mojom.GraphDump Discarding: Chrome-style tabs, and audio and form input of Alloy-style pages; CefChromium’s CefTabLifecycle
chrome://extensions chrome.developerPrivate, chrome.management CefExtensions (not in incognito: Chromium ends the process when an incognito profile opens it)
chrome://settings The WebUI message handlers for site settings (zoom levels), search engines, clearing browsing data, media devices, protocol handlers and fonts, through chrome.send and the cr.webUIResponse and cr.webUIListenerCallback the module defines; chrome.autofillPrivate and chrome.languageSettingsPrivate CefZoomLevels, CefSearchEngines, CefBrowsingData, CefCaptureDevices, CefProtocolHandlers, CefAutofill, CefChromiumSettings (not in incognito)
chrome://password-manager chrome.passwordsPrivate CefPasswords (not in incognito)

chrome://new-tab-page binds most-visited and the search box’s searchbox.mojom.PageHandlerFactory too, and the search box answers queries with Chromium’s autocomplete. It ranks for the new tab page, though: a web search first even when a bookmark matches the typed address, no match on bookmark titles, and no relevance or bookmarked flag in its results. CefChromium keeps chrome://omnibox-internals, whose controller is the address bar’s.

The smallest document. A service host loads the host’s strings.m.js, a few kilobytes of JavaScript that Chromium shows as text, instead of the page’s app, and each service imports only the generated *.mojom-webui.js module it needs, from chrome://resources where Chromium serves it there. Its renderer has the host’s bindings and none of the app’s scripts, styles or elements. A host starts in about a tenth of a second.

Services as modules. A CefServiceHost.Module names the hosts that can serve it and a JavaScript setup that builds a service object once per document (a bound Mojo remote, or an extension API with listeners). A call runs a function, async (service, args, context) => value, against that object in the host’s renderer and decodes its value into the caller’s type; a module that serves from several hosts (favicons) uses whichever of them runs. Hosts attach no DevTools session. Adding a service is declaring a module and calling it.

Calls. The first call in a host document installs a small runtime (CefServiceHostRuntime, window.__cefswiftServices), and the first call of each function defines it there, compiled once; every later call is one line, __cefswiftServices.invoke(handle, args, call), sent as an evaluate message. The runtime encodes the function’s value in the wire format itself, into one ArrayBuffer, with JIT-compiled code; walking the value through the V8 C API (V8Wire) costs about a microsecond a value, which for 5 000 history entries was 98 ms of a 152 ms call (now 31 ms). The reply carries the buffer as one bytes value. The browser process checks its structure with the same validator as any renderer payload, and CefWireDecoder decodes the caller’s type straight from it: no CefJSON tree, no JSON text. A reply of 64 KB or more is decoded off the main actor.

Hop A result under 64 KB 64 KB or more
JavaScript value → wire bytes one pass, into the runtime’s scratch buffer, then one copy into an exact-size ArrayBuffer the same
ArrayBuffer → process message into the payload buffer, then CEF’s cef_binary_value_t once, into shared memory
Browser process read in place; strings copied into the Swift values the mapping read in place, on a background thread

Cancellation. Each call has an AbortController in the host, whose signal the function gets as context.signal. Cancelling the calling task resumes it at once with CancellationError (CefPendingOperations and withDeadline complete a waiter synchronously when cancelled on the main thread), then sends cancel(call) as an evaluation the renderer does not answer (request 0). The signal aborts; the omnibox module stops Chromium’s controller (below), and a Mojo call that cannot be aborted, such as a history query, runs out with its result dropped. A reply that arrives after its caller has gone finds no waiter and is released undecoded; the runtime forgets the call when its promise settles.

Progress and events. context.progress(value) sends an intermediate value, encoded the same way, through the host’s CefBridge (__cefswiftServiceEvents.progress({call, data})). One is in flight per call at a time: a value produced while the one before is on its way replaces the one waiting, so a slow consumer bounds memory and sees the newest, and none is delivered after the call’s result. emit(name, payload) coalesces events of one name the same way, which turns a burst of 10 000 bookmark changes into about 600 deliveries. Autocomplete streams each response of Chromium’s controller as progress, so local matches arrive before the search engine’s.

Autocomplete. mojom.OmniboxInternalsPageHandler drives one controller per handler, and chrome://omnibox-internals binds one handler per page, so a profile has one query at a time: CefAutocomplete lets the latest win, ending the stream before it, and the module settles a query superseded in the host. The handler has no stop call; an aborted query starts a query for no text, since AutocompleteController::Start stops every provider (their network requests included) before it runs, and no provider answers empty input.

Host failures. A call whose host stops before it answers (the renderer crashed, or the host was closed) throws "… stopped responding", unless the call is idempotent (reads), which is then made once more on a new host. A call that times out is not repeated: it closes the host, and waiting as long again would double the delay.

Lifetime. A host starts on the first call that needs it and closes 30 seconds after its last one, when macOS reports memory pressure, when its profile’s last page closes (except the default profile’s), and when the app quits. A call that times out, or a renderer that dies, closes the host; the call throws and the next one starts the host again. Service pages are left out of CefRuntime.pages and CefWindow.all. Their windows are 1 × 1 point, since a hidden window’s compositor keeps buffers the size of the window, about 11 MB of GPU memory for a default-sized one, and they are transparent, ignore the mouse and are ordered out whenever Chromium shows them, as it does when it brings its windows forward. Chromium saves every Chrome-style window in its session, these included, and reopens them after a crash; their addresses carry a marker fragment, so CefRuntime recognizes and closes them. Alloy-style and offscreen pages do not get WebUI bindings for these hosts, so a service host has to be a Chrome-style window.

Reads without a renderer. Chromium saves a profile’s bookmarks to the JSON file Bookmarks about 2.5 seconds after each change, replacing it atomically. It holds the save back while the app shows no page in a visible window (measured with SelfTest’s groups run alone: over a minute with only hidden service hosts open, 2.5 seconds once a window shows a page), and a new profile has no file until its first save. While the bookmarks host is not running, and the file has been written since the last change made through it, CefBookmarks reads the tree, a folder’s contents, a bookmark and the recent bookmarks from that file, and changes watches it with a DispatchSource. A file in another format version, or a profile with managed or account bookmarks, which the file does not hold, falls back to the host. Searches and lookups by address, whose matching is Chromium’s, and every change go through the host. History, favicons and top sites live in SQLite databases that Chromium opens with exclusive locking: a read-only open fails with SQLITE_BUSY once Chromium has written, and an immutable open reads pages Chromium may be writing, so CefChromium reads them only through the hosts. The DevTools Protocol of Chromium 156 has no domain for any of these services.

chrome://history cannot open in an incognito profile, and an incognito profile keeps no history, so CefHistory throws there instead of starting a host. An incognito profile’s bookmarks are the default profile’s CefBookmarks, since Chromium shares the original profile’s bookmarks with its incognito profile.

Service hosts: Mojo handlers, not the WebUI apps

CefBookmarks, CefHistory, CefAutocomplete and discarding call the Mojo handlers of Chromium’s internal pages from hidden hosts. An earlier design opened each page’s WebUI app in its host instead. Those hosts cost 38 to 83 MB each, and their first call took as long as with the handlers. The Browser example, measured with measure-services.py page (median of three) on a build without the passkey and Chromium-panel code:

Browser example Handlers: processes Memory WebUI apps: processes Memory
At launch 6 143 MB 8 264 MB
After typing in the address field, bookmarking the page and opening the menu bar 9 217 MB 10 305 MB
40 s later, once the hosts have closed 6 157 MB 10 290 MB

Chromium’s own features: a module, not CefKit

Bookmarks, history and autocomplete, and the presentation of Chromium’s bubbles, find bar and status bubble, used to be CefKit API. They drive interfaces Chromium keeps private: the Mojo handlers and extension APIs of its WebUI pages, the format of its Bookmarks file, the classes of its AppKit windows and the view names its accessibility reports. Each CEF update can break them, and a browser product needs them where an app that embeds pages does not, so they moved to the optional CefChromium module, bound to the pinned CEF. What CefKit had to keep is general: the service host engine, which native tab discarding needs too, per-window module state, a Chrome-command interceptor, and an overlay slot over the page. Without the module, Chromium shows its interface as it draws it.

Was this page helpful?