| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- // Package native controls a native WebView worker through private stdio pipes.
- package native
- import (
- "context"
- "encoding/json"
- "io"
- "os"
- "os/exec"
- "runtime"
- "sync"
- "time"
- "clusterflow.dev/cfbrowser/api"
- "clusterflow.dev/cfbrowser/internal/capture"
- "clusterflow.dev/cfbrowser/protocol"
- )
- type Driver struct {
- WorkerPath string
- Diagnostics io.Writer
- }
- func (*Driver) Probe(ctx context.Context) (api.Capabilities, error) {
- if err := ctx.Err(); err != nil {
- return api.Capabilities{}, err
- }
- features := map[string]api.Capability{}
- status := "unsupported"
- if nativeSupported() {
- status = "supported"
- }
- for _, n := range []string{"backgroundNavigation", "evalResult", "isolatedWorld", "domSnapshot", "openShadowDOM", "screenshotViewport"} {
- features[n] = api.Capability{Status: status, Reason: "native adapter implemented; worker startup verifies runtime availability"}
- }
- for _, n := range []string{"displayless", "trustedInput", "axSnapshot", "crossOriginFrames", "screenshotFullPage", "downloads", "fileUpload", "persistentProfile", "userHandoff", "proxy"} {
- features[n] = api.Capability{Status: "unsupported", Reason: "not implemented in native prototype"}
- }
- if runtime.GOOS == "windows" {
- features["trustedInput"] = api.Capability{Status: status, Reason: "WebView2 internal Input domain; select/check/scroll remain synthetic"}
- features["screenshotFullPage"] = api.Capability{Status: status, Reason: "bounded to 4096 x 4096 pixels and 4 MiB"}
- }
- features["syntheticInput"] = api.Capability{Status: status}
- features["networkPolicy"] = api.Capability{Status: "degraded", Reason: "top-level navigation validation only"}
- return api.Capabilities{Engine: nativeEngine(), Platform: runtime.GOOS + "/" + runtime.GOARCH, Features: features}, nil
- }
- type session struct {
- downloadDir string
- cmd *exec.Cmd
- stdin io.WriteCloser
- stdout io.ReadCloser
- client *protocol.Client
- done chan error
- once sync.Once
- }
- type enginePage struct {
- s *session
- id uint64
- once sync.Once
- }
- func (d *Driver) Open(ctx context.Context, o api.EngineOptions) (api.EngineSession, error) {
- if err := ctx.Err(); err != nil {
- return nil, err
- }
- if !nativeSupported() {
- return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "native worker requires Linux or 64-bit Windows/macOS")
- }
- path, err := exec.LookPath(d.WorkerPath)
- if err != nil || d.WorkerPath == "" {
- return nil, api.Err("BROWSER_ENGINE_NOT_FOUND", "configure cfbrowser-worker path")
- }
- cmd := exec.Command(path)
- cmd.Stderr = d.Diagnostics
- configureCommand(cmd)
- if o.NoSandbox && runtime.GOOS == "linux" {
- cmd.Env = append(os.Environ(), "WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1")
- }
- stdin, err := cmd.StdinPipe()
- if err != nil {
- return nil, err
- }
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- stdin.Close()
- return nil, err
- }
- if err = cmd.Start(); err != nil {
- stdin.Close()
- stdout.Close()
- return nil, err
- }
- s := &session{downloadDir: o.DownloadDir, cmd: cmd, stdin: stdin, stdout: stdout, done: make(chan error, 1)}
- s.client = protocol.NewClient(stdout, stdin)
- go func() { s.done <- cmd.Wait() }()
- var ok bool
- if err = s.client.Call(ctx, "configure", o, &ok); err != nil {
- _ = s.Close()
- return nil, err
- }
- if !ok {
- _ = s.Close()
- return nil, api.Err("BROWSER_RUNTIME_UNAVAILABLE", "worker rejected engine configuration")
- }
- var features struct {
- Version int `json:"featureVersion"`
- }
- err = s.client.Call(ctx, "session.features", nil, &features)
- if err != nil || features.Version != protocol.FeatureVersion {
- _ = s.Close()
- if ctx.Err() != nil {
- return nil, ctx.Err()
- }
- return nil, api.Err("BROWSER_WORKER_OUTDATED", "rebuild and package the native worker from the same cfbrowser source as the SDK")
- }
- return s, nil
- }
- func (s *session) NewPage(ctx context.Context) (api.EnginePage, error) {
- var id uint64
- err := s.client.Call(ctx, "page.new", nil, &id)
- if err != nil {
- return nil, err
- }
- return &enginePage{s: s, id: id}, nil
- }
- func (s *session) Downloads() []api.Download { return []api.Download{} }
- func (s *session) Close() error {
- s.once.Do(func() {
- s.stdin.Close()
- s.client.Close()
- select {
- case <-s.done:
- case <-time.After(2 * time.Second):
- _ = s.cmd.Process.Kill()
- <-s.done
- }
- s.stdout.Close()
- })
- return nil
- }
- func (p *enginePage) Navigate(ctx context.Context, url, op string) (string, error) {
- var out string
- err := p.s.client.Call(ctx, "page.navigate", map[string]any{"page": p.id, "url": url, "op": op}, &out)
- return out, err
- }
- func (p *enginePage) Evaluate(ctx context.Context, script string) (json.RawMessage, error) {
- var out json.RawMessage
- err := p.s.client.Call(ctx, "page.evaluate", map[string]any{"page": p.id, "script": script}, &out)
- return out, err
- }
- func (p *enginePage) Input(ctx context.Context, action, value string, x, y float64) error {
- return p.s.client.Call(ctx, "page.input", map[string]any{"page": p.id, "action": action, "value": value, "x": x, "y": y}, nil)
- }
- func (p *enginePage) Capture(ctx context.Context) ([]byte, error) { return p.capture(ctx, false) }
- func (p *enginePage) CaptureFullPage(ctx context.Context) ([]byte, error) {
- return p.capture(ctx, true)
- }
- func (p *enginePage) capture(ctx context.Context, full bool) ([]byte, error) {
- var name string
- if err := p.s.client.Call(ctx, "page.capture", map[string]any{"page": p.id, "fullPage": full}, &name); err != nil {
- return nil, err
- }
- return capture.Load(p.s.downloadDir, name)
- }
- func (p *enginePage) Close() error {
- var err error
- p.once.Do(func() {
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- err = p.s.client.Call(ctx, "page.close", map[string]any{"page": p.id}, nil)
- })
- return err
- }
- func nativeSupported() bool {
- return runtime.GOOS == "linux" || (runtime.GOOS == "windows" || runtime.GOOS == "darwin") && (runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64")
- }
- func nativeEngine() string {
- switch runtime.GOOS {
- case "windows":
- return "native-webview2"
- case "darwin":
- return "native-wkwebview"
- case "linux":
- return "native-webkitgtk"
- }
- return "native-unsupported"
- }
|