Documentation
¶
Overview ¶
Package browser lists the web browsers installed on this machine and opens a link in the one the user picks: the chooser a ⇧⌘-click (Ctrl+Shift+click off macOS) on a link in a terminal opens (.ai/04-ui.md § Session Detail, .ai/21-app.md).
Each OS says which apps handle web links in its own way, and the list is read from there; then only the web browsers known here are kept, with the default always (known.go), since a chat app or a terminal registers for https too:
- macOS: LaunchServices, the apps registered for `https` (LSCopyAllHandlersForURLScheme) and the default one, each resolved to its bundle (browser_darwin.go).
- Windows: the registry's StartMenuInternet clients, HKCU then HKLM, with the default from the https UserChoice (browser_windows.go).
- Linux: the .desktop entries whose MimeType names x-scheme-handler/http or https, with the default from mimeapps.list (desktop.go).
Opening is a process Caprock starts (rule 7 is about processes it did not start): an argv handed to exec, never a shell string, and the URL is its own word. Only http and https are opened; a URL starts with its scheme, so it can never be read as a flag.
Nothing here decides who may ask; the API refuses anything that is not a request from this machine (internal/api/browser.go).
Index ¶
- Constants
- Variables
- func Build(goos string, b Browser, u string, env []string) (nativeterm.Plan, error)
- func CheckURL(raw string) (string, error)
- func ExeFromCommand(cmd string) string
- func ExpandExec(words []string, u string) ([]string, error)
- func IsWebBrowser(b Browser) bool
- func SplitExec(s string) ([]string, error)
- type Browser
- func DetectDesktop(fsys fs.FS, d XDGDirs, path func(string) string) []Browser
- func DetectHere(env []string) []Browser
- func FromAppPaths(paths []string, def string, exists func(string) bool) []Browser
- func FromClients(cs []Client, defProgID string) []Browser
- func Sorted(bs []Browser) []Browser
- func WebBrowsersOnly(bs []Browser) []Browser
- type Client
- type Opener
- type XDGDirs
Constants ¶
const MaxURL = 8 << 10
MaxURL bounds a URL: a link longer than this is not one a person clicked.
Variables ¶
var ErrBadURL = errors.New("bad url")
ErrBadURL is returned for anything but an http or https URL Caprock can pass on safely.
var ErrNoBrowser = errors.New("no such browser")
ErrNoBrowser is returned when the browser asked for is not installed.
Functions ¶
func Build ¶
Build turns a request into the process that opens u in b, on goos. u has already passed CheckURL; env is the user's environment, for PATH (Linux).
func CheckURL ¶
CheckURL validates what a browser is asked to open: http or https, with a host, no control characters or spaces, at most MaxURL bytes. It returns the URL as it will be passed on.
func ExeFromCommand ¶
ExeFromCommand is the executable of a registry command line: the quoted first word, or the text up to ".exe" when it is not quoted. "" when there is none.
func ExpandExec ¶
ExpandExec turns an entry's Exec words into the argv that opens u: %u %U %f %F become the URL (once; with none, the URL goes last), %% is a percent sign, and the other field codes (%i %c %k and the deprecated ones) drop.
func IsWebBrowser ¶ added in v0.133.0
IsWebBrowser says whether b is a browser the chooser knows: its macOS bundle id, its app or display name, its Windows executable or key, or its Linux desktop file id is on the lists above.
Types ¶
type Browser ¶
type Browser struct {
// ID is stable for this machine: the bundle id on macOS
// ("com.google.chrome"), the StartMenuInternet key on Windows, the
// desktop file's name on Linux ("firefox.desktop").
ID string `json:"id"`
// Name is what the chooser says ("Google Chrome").
Name string `json:"name"`
// Default is the system's default browser.
Default bool `json:"default"`
// contains filtered or unexported fields
}
Browser is one browser found on this machine.
func DetectDesktop ¶
DetectDesktop lists the desktop entries that handle http or https links, the default one marked. An entry in an earlier data dir hides one with the same id in a later dir, as the specification says.
func DetectHere ¶
DetectHere lists the browsers installed: on Linux the desktop entries that handle web links (desktop.go), read from the real root and the user's XDG dirs; elsewhere none.
func FromAppPaths ¶
FromAppPaths turns the app bundles LaunchServices names for web links into the macOS list: def is the default one's bundle. Only a bundle in an Applications folder is kept (/Applications, ~/Applications, the system's, Safari's cryptex), so a test browser a tool downloaded into a cache or a hidden folder is not offered; nor is a bundle inside another (a helper), one in the Trash, or one stat says is gone. Of two bundles with the same name (an old copy) the first wins. Every app that registers for https is returned, as macOS's own Default web browser menu lists it — a terminal or a chat app among them — and the Opener keeps only the web browsers (known.go). Kept free of build tags so its tests run on every OS.
func FromClients ¶
FromClients turns the clients into the list: one per key (the first seen, so HKCU before HKLM), with an executable, the one whose https ProgId is the user's choice marked default.
func Sorted ¶
Sorted puts the default browser first and the rest by name, which is the order the chooser shows. It drops a second entry with an id already seen.
func WebBrowsersOnly ¶ added in v0.133.0
WebBrowsersOnly keeps the browsers IsWebBrowser knows, and the default whatever it is.
type Client ¶
type Client struct {
// Key is the subkey's name ("Google Chrome", "FIREFOX.EXE").
Key string
// Name is its display name: the key's default value or its
// Capabilities\ApplicationName.
Name string
// Command is shell\open\command's default value.
Command string
// HTTPSProgID is Capabilities\URLAssociations' https value.
HTTPSProgID string
}
Client is one key under Clients\StartMenuInternet.
type Opener ¶
type Opener struct {
// Env is the user's login-shell environment (internal/userenv).
Env func() []string
// Detect finds the browsers; DetectHere when nil. Swapped by tests.
Detect func(env []string) []Browser
// Run carries a plan out; nativeterm.Run when nil. Swapped by tests, so
// no test ever opens a browser on someone's screen.
Run func(ctx context.Context, p nativeterm.Plan, env []string) error
// contains filtered or unexported fields
}
Opener is what the daemon hands the API: it finds the browsers once per daemon run, against the user's own environment, and opens a link in one.
type XDGDirs ¶
type XDGDirs struct {
// Data are the data dirs, the user's first ($XDG_DATA_HOME, then
// $XDG_DATA_DIRS), plus Flatpak's and Snap's exports; each holds an
// applications/ folder.
Data []string
// Config are the config dirs, the user's first ($XDG_CONFIG_HOME, then
// $XDG_CONFIG_DIRS), for mimeapps.list.
Config []string
// Desktops are $XDG_CURRENT_DESKTOP's names, lowercased, for the
// desktop-specific `<desktop>-mimeapps.list`.
Desktops []string
}
XDGDirs are the folders the Linux side reads, from the user's environment.