feat: add browser launcher registry

This commit is contained in:
QiuSW
2026-07-22 14:57:27 +08:00
parent cf828aec21
commit 23be05210b
5 changed files with 350 additions and 3 deletions
+190
View File
@@ -0,0 +1,190 @@
package browser
import (
"context"
"errors"
"fmt"
"os/exec"
"sync"
"chub/internal/domain"
)
var ErrInvalidProcess = errors.New("browser process handle is invalid")
type CommandRunner interface {
Start(executable string, args []string) (RunnerProcess, error)
}
type RunnerProcess interface {
PID() int
Wait() (int, error)
}
type ProfileUse struct {
Occupied bool
PID int
Source string
}
type Launcher struct {
runner CommandRunner
mu sync.Mutex
profiles map[string]*trackedProcess
}
func NewLauncher(runner CommandRunner) *Launcher {
return &Launcher{runner: runner, profiles: make(map[string]*trackedProcess)}
}
func NewOSLauncher() *Launcher { return NewLauncher(osRunner{}) }
func (l *Launcher) Start(ctx context.Context, spec domain.LaunchSpec) (ProcessHandle, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if l == nil || l.runner == nil {
return nil, fmt.Errorf("%w: command runner is required", domain.ErrInvalidLaunchSpec)
}
normalized, err := spec.Normalize()
if err != nil {
return nil, err
}
if normalized.Executable == "" {
return nil, fmt.Errorf("%w: executable is required", domain.ErrInvalidLaunchSpec)
}
args, err := BuildArgs(normalized)
if err != nil {
return nil, err
}
l.mu.Lock()
profile := normalized.UserDataDir
if existing := l.profiles[profile]; existing != nil {
l.mu.Unlock()
return nil, &ProfileOccupiedError{UserDataDir: profile, PID: existing.PID()}
}
pending := newPendingProcess()
l.profiles[profile] = pending
l.mu.Unlock()
raw, err := l.runner.Start(normalized.Executable, args)
if err != nil {
l.release(profile, pending)
return nil, err
}
if raw == nil || raw.PID() <= 0 {
l.release(profile, pending)
return nil, ErrInvalidProcess
}
process := newTrackedProcess(raw)
l.mu.Lock()
if l.profiles[profile] != pending {
l.mu.Unlock()
return nil, fmt.Errorf("%w: profile reservation changed", ErrInvalidProcess)
}
l.profiles[profile] = process
l.mu.Unlock()
go func() {
_, _ = process.Wait(context.Background())
l.release(profile, process)
}()
return process, nil
}
func (l *Launcher) InspectProfile(ctx context.Context, userDataDir string) (ProfileUse, error) {
if err := ctx.Err(); err != nil {
return ProfileUse{}, err
}
normalized, err := (domain.LaunchSpec{Kind: domain.BrowserChrome, UserDataDir: userDataDir, TargetURL: "https://example.invalid"}).Normalize()
if err != nil {
return ProfileUse{}, err
}
l.mu.Lock()
defer l.mu.Unlock()
process := l.profiles[normalized.UserDataDir]
if process == nil {
return ProfileUse{}, nil
}
return ProfileUse{Occupied: true, PID: process.PID(), Source: "chub_registry"}, nil
}
func (l *Launcher) release(profile string, process *trackedProcess) {
l.mu.Lock()
defer l.mu.Unlock()
if l.profiles[profile] == process {
delete(l.profiles, profile)
}
}
type ProfileOccupiedError struct {
UserDataDir string
PID int
}
func (e *ProfileOccupiedError) Error() string {
if e.PID <= 0 {
return fmt.Sprintf("%s: %s", domain.ErrProfileOccupied, e.UserDataDir)
}
return fmt.Sprintf("%s: %s (pid %d)", domain.ErrProfileOccupied, e.UserDataDir, e.PID)
}
func (e *ProfileOccupiedError) Unwrap() error { return domain.ErrProfileOccupied }
type ProcessHandle interface {
PID() int
Wait(context.Context) (int, error)
}
type trackedProcess struct {
pid int
raw RunnerProcess
once sync.Once
done chan struct{}
code int
err error
}
func newPendingProcess() *trackedProcess { return &trackedProcess{done: make(chan struct{})} }
func newTrackedProcess(raw RunnerProcess) *trackedProcess {
return &trackedProcess{pid: raw.PID(), raw: raw, done: make(chan struct{})}
}
func (p *trackedProcess) PID() int { return p.pid }
func (p *trackedProcess) Wait(ctx context.Context) (int, error) {
if p.raw == nil {
return 0, errors.New("browser process is still starting")
}
p.once.Do(func() {
go func() {
p.code, p.err = p.raw.Wait()
close(p.done)
}()
})
select {
case <-p.done:
return p.code, p.err
case <-ctx.Done():
return 0, ctx.Err()
}
}
type osRunner struct{}
func (osRunner) Start(executable string, args []string) (RunnerProcess, error) {
command := exec.Command(executable, args...)
if err := command.Start(); err != nil {
return nil, err
}
return &osProcess{command: command}, nil
}
type osProcess struct{ command *exec.Cmd }
func (p *osProcess) PID() int { return p.command.Process.Pid }
func (p *osProcess) Wait() (int, error) {
err := p.command.Wait()
if p.command.ProcessState == nil {
return 0, err
}
return p.command.ProcessState.ExitCode(), err
}
+120
View File
@@ -0,0 +1,120 @@
package browser
import (
"context"
"errors"
"path/filepath"
"sync"
"testing"
"chub/internal/domain"
)
func TestLauncherSeparatesProfilesBlocksDuplicatesAndReleasesAfterExit(t *testing.T) {
runner := &fakeRunner{}
launcher := NewLauncher(runner)
firstProfile := filepath.Join(t.TempDir(), "one")
secondProfile := filepath.Join(t.TempDir(), "two")
first, err := launcher.Start(context.Background(), launchSpec(firstProfile))
if err != nil {
t.Fatalf("first Start() error = %v", err)
}
second, err := launcher.Start(context.Background(), launchSpec(secondProfile))
if err != nil {
t.Fatalf("second Start() error = %v", err)
}
if first.PID() == second.PID() {
t.Fatalf("separate profiles shared PID %d", first.PID())
}
if _, err := launcher.Start(context.Background(), launchSpec(firstProfile)); !errors.Is(err, domain.ErrProfileOccupied) {
t.Fatalf("duplicate Start() error = %v", err)
}
use, err := launcher.InspectProfile(context.Background(), firstProfile)
if err != nil || !use.Occupied || use.PID != first.PID() || use.Source != "chub_registry" {
t.Fatalf("profile use = %+v, error = %v", use, err)
}
runner.processAt(0).finish(0, nil)
if code, err := first.Wait(context.Background()); err != nil || code != 0 {
t.Fatalf("Wait() = %d, %v", code, err)
}
for i := 0; i < 50; i++ {
use, err = launcher.InspectProfile(context.Background(), firstProfile)
if err == nil && !use.Occupied {
break
}
}
if use.Occupied {
t.Fatalf("profile remained occupied after exit: %+v", use)
}
runner.processAt(1).finish(0, nil)
}
func TestProcessWaitCancellationDoesNotTerminateBrowser(t *testing.T) {
runner := &fakeRunner{}
launcher := NewLauncher(runner)
handle, err := launcher.Start(context.Background(), launchSpec(filepath.Join(t.TempDir(), "profile")))
if err != nil {
t.Fatalf("Start() error = %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := handle.Wait(ctx); !errors.Is(err, context.Canceled) {
t.Fatalf("cancelled Wait() error = %v", err)
}
if runner.processAt(0).wasFinished() {
t.Fatal("cancelled Wait terminated browser")
}
runner.processAt(0).finish(23, nil)
if code, err := handle.Wait(context.Background()); err != nil || code != 23 {
t.Fatalf("completed Wait() = %d, %v", code, err)
}
}
func launchSpec(profile string) domain.LaunchSpec {
return domain.LaunchSpec{Kind: domain.BrowserChrome, Executable: `C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe`, UserDataDir: profile, TargetURL: "https://example.com"}
}
type fakeRunner struct {
mu sync.Mutex
processes []*fakeProcess
}
func (r *fakeRunner) Start(_ string, _ []string) (RunnerProcess, error) {
r.mu.Lock()
defer r.mu.Unlock()
p := newFakeProcess(4100 + len(r.processes))
r.processes = append(r.processes, p)
return p, nil
}
func (r *fakeRunner) processAt(index int) *fakeProcess {
r.mu.Lock()
defer r.mu.Unlock()
return r.processes[index]
}
type fakeProcess struct {
pid int
done chan processResult
mu sync.Mutex
finished bool
}
type processResult struct {
code int
err error
}
func newFakeProcess(pid int) *fakeProcess {
return &fakeProcess{pid: pid, done: make(chan processResult, 1)}
}
func (p *fakeProcess) PID() int { return p.pid }
func (p *fakeProcess) Wait() (int, error) { result := <-p.done; return result.code, result.err }
func (p *fakeProcess) finish(code int, err error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.finished {
return
}
p.finished = true
p.done <- processResult{code: code, err: err}
}
func (p *fakeProcess) wasFinished() bool { p.mu.Lock(); defer p.mu.Unlock(); return p.finished }