Bookmarks and history
Chromium's own bookmark, history and top-sites stores, read and changed from Swift.
import CefChromium
_ = try await profile.bookmarks.add(url, title: "Example")
let recent = try await profile.bookmarks.recent(limit: 12)
let visits = try await profile.history.search("swift", limit: 20)
let topSites = try await profile.history.topSites()
print(recent.count, visits.count, topSites.count)
Gallery: BookmarksDemo, HistoryDemo.
Browser example: Features/Bookmarks adds the star and Bookmarks menu; Features/History the History menu; Features/StartPage a start page of top sites.
Bookmarks, history and suggestions
With the optional CefChromium library
(.product(name: "CefChromium", package: "CefSwift")), a profile gives you
the data behind Chrome’s bookmarks, history and address bar:
import CefChromium
let bookmark = try await profile.bookmarks.add(url, title: "CefSwift")
let roots = try await profile.bookmarks.tree()
let recent = try await profile.bookmarks.recent(limit: 12)
for await _ in profile.bookmarks.changes { /* refresh your UI */ }
let visits = try await profile.history.search("swift", limit: 20)
let topSites = try await profile.history.topSites()
try await profile.history.remove(visits)
for try await page in profile.history.entries(matching: "", pageSize: 200) { print(page.count) }
let icons = try await profile.history.favicons(for: visits.map(\.url))
for try await result in profile.autocomplete.results(for: "cef") {
print(result.suggestions.map(\.text), result.isComplete)
}
bookmarksalso hasbookmark(_:),children(of:),search(_:),bookmarks(for:),addFolder(_:in:at:),update(_:title:url:),move(_:to:at:)andremove(_:).changesyields for edits made through this API and in Chrome’s UI; edits made while the consumer is busy are reported once, as one signal to read again.historygroups visits by day, aschrome://historydoes. It records only visits made in Chrome-style pages.topSites()includes visits made up to the call and takes up to a second.history.entries(matching:pageSize:)reads the same history a page at a time, each page when the loop asks for it, so a long history never sits in memory whole.history.favicon(for:size:)returns the icon Chromium keeps for a page that is not open, such as a history entry, bookmark or most-visited site, ornilwhen it has none;favicons(for:size:)asks for a whole list in one call. Icons arrive as their pixels, which become the image’s bitmap without being encoded on the way. For an open page, usepage.favicon.- An incognito profile keeps no history: its
historymethods throw. It shares the default profile’s bookmarks, as Chrome’s incognito windows do, soincognito.bookmarksisCefProfile.default.bookmarks. autocomplete.results(for:currentURL:allowsInlineCompletion:)streams what Chrome’s address bar would show, ranked, each suggestion with akind(history, bookmark, open tab, search, …),detail(a page’s title),relevance,inlineCompletionandisBookmarked. Results come as Chromium’s providers answer (local matches within a few milliseconds, the search engine’s when the network answers), and the last oneisComplete.suggestions(for:)returns only that last one. Search-engine suggestions come from the network; filter onkindto keep only local results.- The latest query wins. Starting one ends the profile’s query still
running (its stream finishes,
suggestions(for:)throwsCancellationError), and Chromium stops working on it, so query on every keystroke without debouncing. Ending the consuming task does the same.
Suggestions as you type, in SwiftUI: .task(id:) ends the stream for the
text it replaces.
import CefChromium
import SwiftUI
struct AddressField: View {
let profile: CefProfile
@State private var text = ""
@State private var suggestions: [CefAutocomplete.Suggestion] = []
var body: some View {
VStack(alignment: .leading) {
TextField("Search or enter address", text: $text)
ForEach(suggestions) { suggestion in
Text(suggestion.text) + Text(suggestion.inlineCompletion).foregroundStyle(.secondary)
}
}
.task(id: text) {
do {
for try await result in profile.autocomplete.results(for: text) {
suggestions = result.suggestions
}
} catch {
suggestions = []
}
}
}
}
In AppKit, hand the field’s text to results(following:) as a stream:
let (texts, typed) = AsyncStream.makeStream(of: String.self, bufferingPolicy: .bufferingNewest(1))
typed.yield("swi") // in controlTextDidChange(_:), the field's text
for try await result in profile.autocomplete.results(following: texts) {
print(result.text, result.suggestions.prefix(8).map(\.text))
}
CEF has no API for any of this. CefChromium uses the services Chromium’s own
pages use (chrome.bookmarks, and the Mojo handlers of the history, new
tab and chrome://omnibox-internals pages) from hidden service hosts, one renderer
process each. A host starts on the first call that needs it, in about a
tenth of a second, and closes 30 seconds after its last call, when macOS
runs low on memory, and when its profile’s last page closes, which lets
an incognito profile and its data go. Its window stays invisible, even when
Chromium brings its windows forward. While the bookmarks host is closed,
tree(), children(of:), bookmark(_:) and recent(limit:) read
Chromium’s Bookmarks file, and changes watches it, so following
bookmarks costs no process; an edit made in Chrome’s own UI reaches them
about 2.5 seconds later, when Chromium saves the file. Chromium does not
save while the app shows no page in a visible window; until it has, and
in a new profile that has no file yet, those reads start the host
(architecture).
These interfaces are internal to Chromium and more likely than CefKit to
change with a Chromium update, which is why they live in the optional
CefChromium library, bound to the pinned CEF; chrome://omnibox-internals is a
debugging page, so autocomplete is experimental. SelfTest exercises all
of them, so a CEF update that breaks one fails CI.
A call costs a renderer round trip, about a tenth of a millisecond, plus Chromium’s own work: about 0.5 ms for a favicon, a bookmark search or a small tree, 1 ms for a page of history, 2 to 3 ms for local suggestions. Cancelling a call’s task resumes it at once and stops the work in the host; results of 64 KB or more decode off the main actor (design notes).
What each host costs while it runs, measured with
Scripts/measure-services.py on the
machine described under Footprint: memory is
the growth of the whole app’s physical footprint when the host starts,
latency the median of five calls.
| Service | Host | Memory | First call | Later calls |
|---|---|---|---|---|
bookmarks reads, changes while the host is closed |
none (Chromium’s file) | 0 | – | – |
bookmarks changes and searches |
chrome://bookmarks |
21 MB | 137 ms | 1 ms |
history.search, remove |
chrome://history |
27 MB | 175 ms | 2 ms |
history.topSites() |
chrome://new-tab-page-third-party |
21 MB | 127 ms | 1 ms |
history.favicon(for:size:) |
chrome://bookmarks or chrome://history, whichever runs |
0 when one runs | 16 ms | 2 ms |
autocomplete |
chrome://omnibox-internals |
23 MB | 234 ms | 9 ms |
| Discarding | chrome://discards |
22 MB | 122 ms | 1 ms |
The Browser example uses them for address-field suggestions
(Features/AddressBar), a bookmark star and a Bookmarks menu
(Features/Bookmarks), a History menu (Features/History), and a start
page whose tiles come from topSites() and bookmarks.recent() through a
CefBridge (Features/StartPage). It
reads history when the menu bar opens, so at launch it starts no host.