|
|
@@ -13,6 +13,7 @@ import (
|
|
|
"unsafe"
|
|
|
|
|
|
"clusterflow.dev/cfbrowser/api"
|
|
|
+ "clusterflow.dev/cfbrowser/internal/capture"
|
|
|
"clusterflow.dev/cfbrowser/protocol"
|
|
|
"github.com/ebitengine/purego"
|
|
|
)
|
|
|
@@ -22,7 +23,21 @@ type nativePage struct {
|
|
|
loading bool
|
|
|
failed bool
|
|
|
}
|
|
|
+type allocation struct{ X, Y, Width, Height int32 }
|
|
|
+type nativeCapture struct {
|
|
|
+ result chan evalResult
|
|
|
+ page uint64
|
|
|
+ data []byte
|
|
|
+}
|
|
|
type nativeHost struct {
|
|
|
+ snapshots map[uintptr]*nativeCapture
|
|
|
+ snapshot func(uintptr, int32, uint32, uintptr, uintptr, uintptr)
|
|
|
+ snapshotFinish func(uintptr, uintptr, *uintptr) uintptr
|
|
|
+ surfaceDestroy func(uintptr)
|
|
|
+ surfaceWidth, surfaceHeight, surfaceType func(uintptr) int32
|
|
|
+ surfacePNG func(uintptr, uintptr, uintptr) int32
|
|
|
+ sizeAllocate func(uintptr, *allocation)
|
|
|
+ snapshotCallback, pngCallback uintptr
|
|
|
queue chan func()
|
|
|
pages map[uint64]*nativePage
|
|
|
next uint64
|
|
|
@@ -84,7 +99,19 @@ func newHost() (*nativeHost, error) {
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- h := &nativeHost{queue: make(chan func(), 32), pages: map[uint64]*nativePage{}, callbacks: map[uintptr]chan evalResult{}}
|
|
|
+ cairo, err := openLibrary("libcairo.so.2")
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ h := &nativeHost{snapshots: map[uintptr]*nativeCapture{}, queue: make(chan func(), 32), pages: map[uint64]*nativePage{}, callbacks: map[uintptr]chan evalResult{}}
|
|
|
+ purego.RegisterLibFunc(&h.snapshot, wk, "webkit_web_view_get_snapshot")
|
|
|
+ purego.RegisterLibFunc(&h.snapshotFinish, wk, "webkit_web_view_get_snapshot_finish")
|
|
|
+ purego.RegisterLibFunc(&h.surfaceDestroy, cairo, "cairo_surface_destroy")
|
|
|
+ purego.RegisterLibFunc(&h.surfaceWidth, cairo, "cairo_image_surface_get_width")
|
|
|
+ purego.RegisterLibFunc(&h.surfaceHeight, cairo, "cairo_image_surface_get_height")
|
|
|
+ purego.RegisterLibFunc(&h.surfaceType, cairo, "cairo_surface_get_type")
|
|
|
+ purego.RegisterLibFunc(&h.surfacePNG, cairo, "cairo_surface_write_to_png_stream")
|
|
|
+ purego.RegisterLibFunc(&h.sizeAllocate, gtk, "gtk_widget_size_allocate")
|
|
|
purego.RegisterLibFunc(&h.gtkInit, gtk, "gtk_init_check")
|
|
|
purego.RegisterLibFunc(&h.windowNew, gtk, "gtk_window_new")
|
|
|
purego.RegisterLibFunc(&h.windowSize, gtk, "gtk_window_set_default_size")
|
|
|
@@ -144,6 +171,43 @@ func newHost() (*nativeHost, error) {
|
|
|
ch <- evalResult{raw: json.RawMessage(s), err: e}
|
|
|
}
|
|
|
})
|
|
|
+ h.pngCallback = purego.NewCallback(func(key uintptr, ptr *byte, n uint32) int32 {
|
|
|
+ job := h.snapshots[key]
|
|
|
+ if job == nil || int(n) > capture.MaxBytes-len(job.data) {
|
|
|
+ return 11
|
|
|
+ } // CAIRO_STATUS_WRITE_ERROR
|
|
|
+ job.data = append(job.data, unsafe.Slice(ptr, int(n))...)
|
|
|
+ return 0
|
|
|
+ })
|
|
|
+ h.snapshotCallback = purego.NewCallback(func(view, result, key uintptr) {
|
|
|
+ var nativeError uintptr
|
|
|
+ surface := h.snapshotFinish(view, result, &nativeError)
|
|
|
+ job := h.snapshots[key]
|
|
|
+ if surface != 0 {
|
|
|
+ defer h.surfaceDestroy(surface)
|
|
|
+ }
|
|
|
+ if nativeError != 0 {
|
|
|
+ h.errorFree(nativeError)
|
|
|
+ }
|
|
|
+ if job == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer delete(h.snapshots, key)
|
|
|
+ if surface == 0 || nativeError != 0 {
|
|
|
+ job.result <- evalResult{err: api.Err("BROWSER_CAPTURE_FAILED", "WebKit snapshot failed")}
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if h.surfaceType(surface) != 0 || h.surfaceWidth(surface) < 1 || h.surfaceHeight(surface) < 1 || h.surfaceWidth(surface) > capture.MaxDimension || h.surfaceHeight(surface) > capture.MaxDimension {
|
|
|
+ job.result <- evalResult{err: api.Err("BROWSER_RESULT_LIMIT", "invalid capture surface dimensions")}
|
|
|
+ return
|
|
|
+ }
|
|
|
+ status := h.surfacePNG(surface, h.pngCallback, key)
|
|
|
+ if status != 0 {
|
|
|
+ job.result <- evalResult{err: api.Err("BROWSER_RESULT_LIMIT", "PNG encoding failed or exceeds 4 MiB")}
|
|
|
+ return
|
|
|
+ }
|
|
|
+ job.result <- evalResult{raw: job.data}
|
|
|
+ })
|
|
|
h.loadCallback = purego.NewCallback(func(view uintptr, event int32, data uintptr) {
|
|
|
for _, p := range h.pages {
|
|
|
if p.view == view {
|
|
|
@@ -226,16 +290,55 @@ func (h *nativeHost) call(ctx context.Context, f func() (any, error)) (any, erro
|
|
|
|
|
|
func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMessage) (any, error) {
|
|
|
var a struct {
|
|
|
- Page uint64 `json:"page"`
|
|
|
- URL string `json:"url"`
|
|
|
- Op string `json:"op"`
|
|
|
- Script string `json:"script"`
|
|
|
+ Page uint64 `json:"page"`
|
|
|
+ URL string `json:"url"`
|
|
|
+ Op string `json:"op"`
|
|
|
+ Script string `json:"script"`
|
|
|
+ FullPage bool `json:"fullPage"`
|
|
|
}
|
|
|
- if method != "configure" {
|
|
|
+ if method != "configure" && method != "session.features" {
|
|
|
if err := json.Unmarshal(raw, &a); err != nil && string(raw) != "null" {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
+ if method == "page.capture" {
|
|
|
+ ch := make(chan evalResult, 1)
|
|
|
+ var id atomic.Uint64
|
|
|
+ _, err := h.call(ctx, func() (any, error) {
|
|
|
+ p := h.pages[a.Page]
|
|
|
+ if p == nil {
|
|
|
+ return nil, api.Err("BROWSER_PAGE_NOT_FOUND", "native page not found")
|
|
|
+ }
|
|
|
+ if a.FullPage {
|
|
|
+ return nil, api.Err("BROWSER_CAPABILITY_UNSUPPORTED", "WebKitGTK full-page capture is not implemented")
|
|
|
+ }
|
|
|
+ if len(h.snapshots) >= 16 {
|
|
|
+ return nil, api.Err("BROWSER_LIMIT", "too many pending snapshots")
|
|
|
+ }
|
|
|
+ h.evalSeq++
|
|
|
+ key := h.evalSeq
|
|
|
+ id.Store(uint64(key))
|
|
|
+ h.snapshots[key] = &nativeCapture{result: ch, page: a.Page}
|
|
|
+ h.snapshot(p.view, 0, 0, 0, h.snapshotCallback, key)
|
|
|
+ return nil, nil
|
|
|
+ })
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ select {
|
|
|
+ case r := <-ch:
|
|
|
+ if r.err != nil {
|
|
|
+ return nil, r.err
|
|
|
+ }
|
|
|
+ if ctx.Err() != nil {
|
|
|
+ return nil, ctx.Err()
|
|
|
+ }
|
|
|
+ return capture.Save(h.options.DownloadDir, r.raw)
|
|
|
+ case <-ctx.Done():
|
|
|
+ _ = h.dispatch(context.Background(), func() { delete(h.snapshots, uintptr(id.Load())) })
|
|
|
+ return nil, ctx.Err()
|
|
|
+ }
|
|
|
+ }
|
|
|
if method == "page.evaluate" {
|
|
|
ch := make(chan evalResult, 1)
|
|
|
var id atomic.Uint64
|
|
|
@@ -319,6 +422,11 @@ func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMess
|
|
|
}
|
|
|
return h.call(ctx, func() (any, error) {
|
|
|
switch method {
|
|
|
+ case "session.features":
|
|
|
+ if !h.configured {
|
|
|
+ return nil, api.Err("BROWSER_INVALID_CONFIG", "configure worker first")
|
|
|
+ }
|
|
|
+ return map[string]int{"featureVersion": protocol.FeatureVersion}, nil
|
|
|
case "configure":
|
|
|
if h.configured {
|
|
|
return nil, api.Err("BROWSER_INVALID_CONFIG", "worker is already configured")
|
|
|
@@ -326,6 +434,15 @@ func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMess
|
|
|
if err := json.Unmarshal(raw, &h.options); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ if h.options.ViewportWidth == 0 {
|
|
|
+ h.options.ViewportWidth = 1280
|
|
|
+ }
|
|
|
+ if h.options.ViewportHeight == 0 {
|
|
|
+ h.options.ViewportHeight = 800
|
|
|
+ }
|
|
|
+ if h.options.ViewportWidth < 1 || h.options.ViewportWidth > 4096 || h.options.ViewportHeight < 1 || h.options.ViewportHeight > 4096 {
|
|
|
+ return nil, api.Err("BROWSER_INVALID_CONFIG", "invalid viewport dimensions")
|
|
|
+ }
|
|
|
h.configured = true
|
|
|
return true, nil
|
|
|
case "page.new":
|
|
|
@@ -337,7 +454,7 @@ func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMess
|
|
|
}
|
|
|
window := h.windowNew(0)
|
|
|
view := h.viewNew(h.webContext)
|
|
|
- h.windowSize(window, 1280, 800)
|
|
|
+ h.windowSize(window, int32(h.options.ViewportWidth), int32(h.options.ViewportHeight))
|
|
|
h.containerAdd(window, view)
|
|
|
// Retain the named script world across separate evaluation calls.
|
|
|
script := h.scriptNew("void 0;", 1, 0, "cfbrowser-isolated", 0, 0)
|
|
|
@@ -346,6 +463,7 @@ func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMess
|
|
|
// Realize the native surface without mapping a top-level desktop window.
|
|
|
h.show(view)
|
|
|
h.realize(window)
|
|
|
+ h.sizeAllocate(view, &allocation{Width: int32(h.options.ViewportWidth), Height: int32(h.options.ViewportHeight)})
|
|
|
if !h.options.Headless {
|
|
|
h.show(window)
|
|
|
}
|
|
|
@@ -355,6 +473,12 @@ func (h *nativeHost) handle(ctx context.Context, method string, raw json.RawMess
|
|
|
h.signal(view, "load-failed", h.failedCallback, 0, 0, 0)
|
|
|
return h.next, nil
|
|
|
case "page.close":
|
|
|
+ for key, job := range h.snapshots {
|
|
|
+ if job.page == a.Page {
|
|
|
+ job.result <- evalResult{err: api.Err("BROWSER_CLOSED", "page closed during capture")}
|
|
|
+ delete(h.snapshots, key)
|
|
|
+ }
|
|
|
+ }
|
|
|
if p := h.pages[a.Page]; p != nil {
|
|
|
h.destroy(p.window)
|
|
|
delete(h.pages, a.Page)
|