Files
HackBrowserData/internal/app/discover.go
T
Roger 0ed920f846 docs: trim code comments to the why (#676)
Comments that restated names or narrated the code are gone, and multi-line ones are cut to a line of why.
Only comments change: each edited Go file has the same token stream as before. winapi and abe come later.
2026-11-02 00:03:03 +08:00

98 lines
2.7 KiB
Go

package app
import (
"fmt"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/chromium"
"github.com/moond4rk/hackbrowserdata/internal/browser/firefox"
"github.com/moond4rk/hackbrowserdata/internal/browser/safari"
)
// Discovery holds an error result per browser whose data could not be read, so it is reported, not taken as absence.
type Discovery struct {
Dirs []browser.UserDataDir
Failed []Result
}
// Discover applies -p only when one browser is named.
func Discover(req Request) Discovery {
var d Discovery
for _, def := range browser.Supported() {
if req.Browser != allBrowsers && def.ID != req.Browser {
continue
}
opts := browser.DiscoverOptions{Home: req.Home}
if req.Browser != allBrowsers {
opts.ProfilePath = req.ProfilePath
}
dirs, err := discover(def, opts)
d.Dirs = append(d.Dirs, withData(dirs)...)
if err != nil {
d.Failed = append(d.Failed, dirFailure(browser.DirID(def.ID), err))
}
}
return d
}
// withData drops profiles holding no file any category reads, such as a bare Preferences, but keeps unreadable ones.
func withData(dirs []browser.UserDataDir) []browser.UserDataDir {
extractors := extractorsWith(nil)
var out []browser.UserDataDir
for _, dir := range dirs {
var profiles []browser.Profile
for _, p := range dir.Profiles {
if hasData(dir, p, extractors(dir, p)) {
profiles = append(profiles, p)
}
}
if len(profiles) > 0 {
dir.Profiles = profiles
out = append(out, dir)
}
}
return out
}
func hasData(dir browser.UserDataDir, p browser.Profile, ex browser.Extractors) bool {
for _, x := range ex {
if len(x.Sources) == 0 {
continue
}
if _, ok, err := locate(dir, p, x.Sources); ok || err != nil {
return true
}
}
return false
}
// The base packages import browser, so the dispatch on Base lives here rather than in browser.
func discover(def browser.Def, opts browser.DiscoverOptions) ([]browser.UserDataDir, error) {
switch def.Base {
case browser.Chromium:
return chromium.Discover(def, opts)
case browser.Firefox:
return firefox.Discover(def, opts)
case browser.Safari:
return safari.Discover(def, opts)
default:
return nil, fmt.Errorf("%s: no base package", def.ID)
}
}
// extractorsWith hands Safari the run's login keychain, which its default profile's password extractor reads.
func extractorsWith(kc safari.LoginKeychain) extractorsFunc {
return func(dir browser.UserDataDir, p browser.Profile) browser.Extractors {
switch dir.Def.Base {
case browser.Chromium:
return chromium.Extractors(dir, p)
case browser.Firefox:
return firefox.Extractors(dir, p)
case browser.Safari:
return safari.Extractors(dir, p, kc)
default:
return nil
}
}
}