Repository navigation
Expand file tree
/
Copy pathcontext.go
More file actions
133 lines (116 loc) · 4.36 KB
/
Copy pathcontext.go
File metadata and controls
133 lines (116 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package devbrowser
import (
"context"
"fmt"
"path/filepath"
"strings"
"webtyp.com/devbrowser/chromedp"
"webtyp.com/lang"
)
// FlagSPKIList trusts the listed SubjectPublicKeyInfo hashes and nothing else.
// It is NOT --ignore-certificate-errors: verification stays on everywhere else.
const FlagSPKIList = "ignore-certificate-errors-spki-list"
// flagTestType hides Chrome's "You are using an unsupported command-line flag"
// infobar, which FlagSPKIList would otherwise trigger in every dev window. It
// changes UI only: certificate verification and every other check stay on.
const flagTestType = "test-type"
func (h *DevBrowser) buildAllocatorOptions() []chromedp.ExecAllocatorOption {
opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Flag("headless", h.Headless),
// chromedp defaults enable-automation=true, which shows the headed
// infobar "Chrome is being controlled by automated test software".
// false omits the switch (map overwrite); CDP still works via
// remote-debugging-port.
chromedp.Flag("enable-automation", false),
chromedp.Flag("use-fake-ui-for-media-stream", true),
// Force the X11 ozone backend. On Linux/Wayland this routes Chrome
// through XWayland, which is the ONLY way the window position is
// readable/settable via CDP (native Wayland never exposes absolute
// window coordinates, so GetWindowForTarget always reports 0,0).
// Ignored as a no-op on Windows and macOS, so it stays cross-platform.
chromedp.Flag("ozone-platform", "x11"),
chromedp.Flag("window-position", h.Position),
chromedp.WindowSize(h.Width, h.Height),
// Chrome 113+: never show the "Restore pages?" bubble. Third layer only:
// older builds or distro Chromium may not know it, and Chrome ignores
// unknown switches.
chromedp.Flag("hide-crash-restore-bubble", true),
)
// Always enable DevTools for non-headless sessions
h.DevToolsReserved = true
if !h.Headless {
opts = append(opts, chromedp.Flag("auto-open-devtools-for-tabs", true))
}
// Disable cache by default unless explicitly enabled
// Note: disk-cache-size and media-cache-size flags cause "invalid exec pool flag" errors
// Use --disable-cache instead
if !h.CacheEnabled {
opts = append(opts,
chromedp.Flag("disable-cache", true),
chromedp.Flag("disable-gpu-shader-disk-cache", true),
)
}
spki := h.TrustDevCertSPKI
if h.ResolveTrustDevCertSPKI != nil {
if live, err := h.ResolveTrustDevCertSPKI(); err == nil && live != "" {
spki = live
}
}
if spki != "" {
opts = append(opts,
chromedp.Flag(FlagSPKIList, spki),
chromedp.Flag(flagTestType, true),
)
}
if h.ProfileDir != "" {
if profileLocked(h.ProfileDir) {
h.profileFallbackReason = "locked by another process"
} else {
if err := markProfileExitedCleanly(h.ProfileDir); err != nil {
h.Logger(fmt.Sprintf("browser profile: could not mark clean exit: %v", err))
}
opts = append(opts, chromedp.UserDataDir(h.ProfileDir))
}
}
// Resolve the Chrome executable path
chromePath := ResolveChromeExecPath()
opts = append(opts, chromedp.ExecPath(chromePath))
return opts
}
// profileLocked reports whether a running Chrome holds the profile at dir. A lock whose owner
// cannot be read counts as held.
func profileLocked(dir string) bool {
pid, hasLock, readable := lockOwner(filepath.Join(dir, singletonLockName))
if !hasLock {
return false
}
return !readable || processAlive(pid)
}
func (h *DevBrowser) CreateBrowserContext() error {
h.shutdownChrome()
opts := h.buildAllocatorOptions()
if h.profileFallbackReason != "" {
h.Logger(lang.Translate("browser", "profile", "unavailable,", "using", "a", "temporary", "one:", h.profileFallbackReason).String())
// Clear it so we only log it once per session failure
h.profileFallbackReason = ""
}
allocCtx, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel := chromedp.NewContext(allocCtx,
chromedp.WithErrorf(func(format string, args ...any) {
// Chrome sends new CDP enum values before cdproto is updated.
// These unmarshal errors are harmless — suppress them to avoid
// corrupting the TUI via stderr.
if strings.HasPrefix(format, "could not unmarshal event") {
return
}
h.Log("ERROR:", fmt.Sprintf(format, args...))
}),
)
h.rootCtx = ctx
h.Ctx = ctx
h.Cancel = cancel
h.mainTab = &browserTab{ctx: ctx}
h.active.Store(h.mainTab)
h.AllocCancel = allocCancel
return nil
}