browser

package
v0.188.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const MaxURL = 8 << 10

MaxURL bounds a URL: a link longer than this is not one a person clicked.

Variables

View Source
var ErrBadURL = errors.New("bad url")

ErrBadURL is returned for anything but an http or https URL Caprock can pass on safely.

View Source
var ErrNoBrowser = errors.New("no such browser")

ErrNoBrowser is returned when the browser asked for is not installed.

Functions

func Build

func Build(goos string, b Browser, u string, env []string) (nativeterm.Plan, error)

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

func CheckURL(raw string) (string, error)

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

func ExeFromCommand(cmd string) string

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

func ExpandExec(words []string, u string) ([]string, error)

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

func IsWebBrowser(b Browser) bool

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.

func SplitExec

func SplitExec(s string) ([]string, error)

SplitExec splits an Exec value into words: spaces separate them, and a word in double quotes keeps its spaces, with \" \` \$ \\ inside meaning the character itself.

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

func DetectDesktop(fsys fs.FS, d XDGDirs, path func(string) string) []Browser

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

func DetectHere(env []string) []Browser

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

func FromAppPaths(paths []string, def string, exists func(string) bool) []Browser

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

func FromClients(cs []Client, defProgID string) []Browser

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

func Sorted(bs []Browser) []Browser

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

func WebBrowsersOnly(bs []Browser) []Browser

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.

func (*Opener) List

func (o *Opener) List() []Browser

List is the installed web browsers (WebBrowsersOnly), the default first.

func (*Opener) Open

func (o *Opener) Open(ctx context.Context, id, u string) (Browser, error)

Open opens u in the browser with this id.

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.

func XDGFrom

func XDGFrom(env []string) XDGDirs

XDGFrom reads the folders from env, with the specification's defaults.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL