Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: update handlers to ensure Chrome initialization (Phase 2 - Part 2)

Marioa4ec3eed813c
diff --git a/internal/bridge/api.go b/internal/bridge/api.go
index edeac44e..c4ca99c8 100644
--- a/internal/bridge/api.go
+++ b/internal/bridge/api.go
@@ -6,6 +6,7 @@ import (
"time"
"github.com/chromedp/cdproto/target"
+ "github.com/pinchtab/pinchtab/internal/config"
)
// BridgeAPI abstracts browser tab operations for handler testing.
@@ -26,6 +27,8 @@ type BridgeAPI interface {
TabLockInfo(tabID string) *LockInfo
Lock(tabID, owner string, ttl time.Duration) error
Unlock(tabID, owner string) error
+
+ EnsureChrome(cfg *config.RuntimeConfig) error
}
type LockInfo struct {
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index 99b2c02a..be601285 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -13,6 +13,12 @@ import (
)
func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
var req bridge.ActionRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
web.Error(w, 400, fmt.Errorf("decode: %w", err))
@@ -82,6 +88,12 @@ type actionResult struct {
}
func (h *Handlers) HandleActions(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
var req actionsRequest
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, maxBodySize)).Decode(&req); err != nil {
web.Error(w, 400, fmt.Errorf("decode: %w", err))
diff --git a/internal/handlers/handler_actions_test.go b/internal/handlers/handler_actions_test.go
index 664b0fab..8b17b9d1 100644
--- a/internal/handlers/handler_actions_test.go
+++ b/internal/handlers/handler_actions_test.go
@@ -25,6 +25,10 @@ func (m *failMockBridge) ListTargets() ([]*target.Info, error) {
return nil, fmt.Errorf("list targets failed")
}
+func (m *failMockBridge) EnsureChrome(cfg *config.RuntimeConfig) error {
+ return nil
+}
+
func TestHandleActions_EmptyArray(t *testing.T) {
h := New(&mockBridge{}, &config.RuntimeConfig{}, nil, nil, nil)
req := httptest.NewRequest("POST", "/actions", bytes.NewReader([]byte(`{"actions": []}`)))
diff --git a/internal/handlers/handler_test.go b/internal/handlers/handler_test.go
index b44e8f86..21846d96 100644
--- a/internal/handlers/handler_test.go
+++ b/internal/handlers/handler_test.go
@@ -53,6 +53,11 @@ func (m *mockBridge) CloseTab(tabID string) error {
return nil
}
+func (m *mockBridge) EnsureChrome(cfg *config.RuntimeConfig) error {
+ // Mock implementation - just return nil
+ return nil
+}
+
func (m *mockBridge) DeleteRefCache(tabID string) {}
func (m *mockBridge) TabLockInfo(tabID string) *bridge.LockInfo { return nil }
diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go
index bc26c397..0a98ec62 100644
--- a/internal/handlers/handlers.go
+++ b/internal/handlers/handlers.go
@@ -27,6 +27,11 @@ func New(b bridge.BridgeAPI, cfg *config.RuntimeConfig, p bridge.ProfileService,
}
}
+// ensureChrome ensures Chrome is initialized before handling requests that need it
+func (h *Handlers) ensureChrome() error {
+ return h.Bridge.EnsureChrome(h.Config)
+}
+
func (h *Handlers) RegisterRoutes(mux *http.ServeMux, doShutdown func()) {
mux.HandleFunc("GET /health", h.HandleHealth)
mux.HandleFunc("GET /tabs", h.HandleTabs)
diff --git a/internal/handlers/integration_test.go b/internal/handlers/integration_test.go
index 839c9ab9..491cde45 100644
--- a/internal/handlers/integration_test.go
+++ b/internal/handlers/integration_test.go
@@ -37,6 +37,10 @@ func (m *integrationMockBridge) TabLockInfo(tabID string) *bridge.LockInfo {
return nil
}
+func (m *integrationMockBridge) EnsureChrome(cfg *config.RuntimeConfig) error {
+ return nil
+}
+
func TestIntegration_RoutesRegistration(t *testing.T) {
b := &integrationMockBridge{tabs: make(map[string]context.Context)}
cfg := &config.RuntimeConfig{}
diff --git a/internal/handlers/media.go b/internal/handlers/media.go
index f1284812..93048d4e 100644
--- a/internal/handlers/media.go
+++ b/internal/handlers/media.go
@@ -19,6 +19,12 @@ import (
)
func (h *Handlers) HandleScreenshot(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
tabID := r.URL.Query().Get("tabId")
output := r.URL.Query().Get("output")
reqNoAnim := r.URL.Query().Get("noAnimations") == "true"
@@ -99,6 +105,12 @@ func (h *Handlers) HandleScreenshot(w http.ResponseWriter, r *http.Request) {
}
func (h *Handlers) HandlePDF(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
tabID := r.URL.Query().Get("tabId")
output := r.URL.Query().Get("output")
@@ -259,6 +271,12 @@ func (h *Handlers) HandlePDF(w http.ResponseWriter, r *http.Request) {
}
func (h *Handlers) HandleText(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
tabID := r.URL.Query().Get("tabId")
mode := r.URL.Query().Get("mode")
diff --git a/internal/handlers/navigation.go b/internal/handlers/navigation.go
index 9c4267ca..4683f007 100644
--- a/internal/handlers/navigation.go
+++ b/internal/handlers/navigation.go
@@ -16,6 +16,12 @@ import (
const maxBodySize = 1 << 20
func (h *Handlers) HandleNavigate(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
var req struct {
TabID string `json:"tabId"`
URL string `json:"url"`
diff --git a/internal/handlers/snapshot.go b/internal/handlers/snapshot.go
index 767e2446..50f903c8 100644
--- a/internal/handlers/snapshot.go
+++ b/internal/handlers/snapshot.go
@@ -17,6 +17,12 @@ import (
)
func (h *Handlers) HandleSnapshot(w http.ResponseWriter, r *http.Request) {
+ // Ensure Chrome is initialized
+ if err := h.ensureChrome(); err != nil {
+ web.Error(w, 500, fmt.Errorf("chrome initialization: %w", err))
+ return
+ }
+
tabID := r.URL.Query().Get("tabId")
filter := r.URL.Query().Get("filter")
doDiff := r.URL.Query().Get("diff") == "true"