runtime_activity.go RawHistoryFolder
1package cef
2
3import (
4 "sync"
5
6 "github.com/bnema/dumber/internal/application/port"
7)
8
9// downloadOwner identifies one download by its owning browser and the CEF
10// download ID. Filename and destination are never identity: they can collide
11// or change mid-download.
12type downloadOwner struct {
13 browserID int32
14 downloadID uint32
15}
16
17// RuntimeActivityTracker implements port.RuntimeActivity for the CEF engine.
18// It counts accepted creations through OnAfterCreated/failure, registered
19// live views, outstanding GTK cleanup, and unterminated downloads. All
20// methods are safe for concurrent use from CEF callbacks. Delivery is
21// serialized under the state lock, so the initial snapshot and every later
22// transition arrive in order; subscribers must not reenter the tracker.
23// A nil tracker is a safe no-op sink.
24type RuntimeActivityTracker struct {
25 mu sync.Mutex
26 pending int
27 active int
28 cleanup int
29 downloads map[downloadOwner]struct{}
30 subscribers map[uint64]func(port.RuntimeActivitySnapshot)
31 nextSub uint64
32}
33
34// NewRuntimeActivityTracker returns an idle tracker.
35func NewRuntimeActivityTracker() *RuntimeActivityTracker {
36 return &RuntimeActivityTracker{
37 downloads: make(map[downloadOwner]struct{}),
38 subscribers: make(map[uint64]func(port.RuntimeActivitySnapshot)),
39 }
40}
41
42// Snapshot returns the current outstanding-work counts.
43func (t *RuntimeActivityTracker) Snapshot() port.RuntimeActivitySnapshot {
44 if t == nil {
45 return port.RuntimeActivitySnapshot{}
46 }
47 t.mu.Lock()
48 defer t.mu.Unlock()
49 return t.snapshotLocked()
50}
51
52// Subscribe delivers the current snapshot synchronously, then every later
53// transition in subscription order. Delivery holds the state lock, so
54// callbacks must not call back into the tracker. The returned function
55// unsubscribes.
56func (t *RuntimeActivityTracker) Subscribe(fn func(port.RuntimeActivitySnapshot)) (unsubscribe func()) {
57 if t == nil || fn == nil {
58 return func() {}
59 }
60 t.mu.Lock()
61 defer t.mu.Unlock()
62 id := t.nextSub
63 t.nextSub++
64 t.subscribers[id] = fn
65 fn(t.snapshotLocked())
66 return func() {
67 t.mu.Lock()
68 defer t.mu.Unlock()
69 delete(t.subscribers, id)
70 }
71}
72
73// NoteCreationAccepted records one accepted browser creation.
74func (t *RuntimeActivityTracker) NoteCreationAccepted() {
75 if t == nil {
76 return
77 }
78 t.mu.Lock()
79 defer t.mu.Unlock()
80 t.pending++
81 t.notifyLocked()
82}
83
84// NoteCreationResolved records one creation resolved through OnAfterCreated
85// (registered) or a definitive failure. Over-resolution is clamped at zero.
86func (t *RuntimeActivityTracker) NoteCreationResolved() {
87 if t == nil {
88 return
89 }
90 t.mu.Lock()
91 defer t.mu.Unlock()
92 if t.pending > 0 {
93 t.pending--
94 }
95 t.notifyLocked()
96}
97
98// NoteViewRegistered records one live view registration.
99func (t *RuntimeActivityTracker) NoteViewRegistered() {
100 if t == nil {
101 return
102 }
103 t.mu.Lock()
104 defer t.mu.Unlock()
105 t.active++
106 t.notifyLocked()
107}
108
109// NoteViewCloseStarted records one native close: the view leaves the active
110// set and enters GTK-teardown outstanding.
111func (t *RuntimeActivityTracker) NoteViewCloseStarted() {
112 if t == nil {
113 return
114 }
115 t.mu.Lock()
116 defer t.mu.Unlock()
117 if t.active > 0 {
118 t.active--
119 }
120 t.cleanup++
121 t.notifyLocked()
122}
123
124// NoteCleanupCompleted records one finished GTK bridge/popup cleanup.
125func (t *RuntimeActivityTracker) NoteCleanupCompleted() {
126 if t == nil {
127 return
128 }
129 t.mu.Lock()
130 defer t.mu.Unlock()
131 if t.cleanup > 0 {
132 t.cleanup--
133 }
134 t.notifyLocked()
135}
136
137// NoteDownloadStarted records one download start keyed by owning browser and
138// CEF download ID.
139func (t *RuntimeActivityTracker) NoteDownloadStarted(browserID int32, downloadID uint32) {
140 if t == nil {
141 return
142 }
143 t.mu.Lock()
144 defer t.mu.Unlock()
145 t.downloads[downloadOwner{browserID: browserID, downloadID: downloadID}] = struct{}{}
146 t.notifyLocked()
147}
148
149// NoteDownloadProgress is observed without effect: only start and terminal
150// events change activity. Progress without a start never creates work.
151func (t *RuntimeActivityTracker) NoteDownloadProgress(_ int32, _ uint32) {}
152
153// NoteDownloadTerminal records one terminal download event (complete,
154// cancel, or failure) for the exact owning browser. Duplicate terminals are
155// idempotent; terminals for unknown owners change nothing, so one browser
156// completing a download ID never clears another browser's same ID.
157func (t *RuntimeActivityTracker) NoteDownloadTerminal(browserID int32, downloadID uint32) {
158 if t == nil {
159 return
160 }
161 t.mu.Lock()
162 defer t.mu.Unlock()
163 delete(t.downloads, downloadOwner{browserID: browserID, downloadID: downloadID})
164 t.notifyLocked()
165}
166
167// DropBrowser reconciles owner destruction through the native lifecycle:
168// entries owned by a destroyed browser can never receive terminal events.
169// It is never driven by window removal alone.
170func (t *RuntimeActivityTracker) DropBrowser(browserID int32) {
171 if t == nil {
172 return
173 }
174 t.mu.Lock()
175 defer t.mu.Unlock()
176 changed := false
177 for owner := range t.downloads {
178 if owner.browserID == browserID {
179 delete(t.downloads, owner)
180 changed = true
181 }
182 }
183 if changed {
184 t.notifyLocked()
185 }
186}
187
188func (t *RuntimeActivityTracker) snapshotLocked() port.RuntimeActivitySnapshot {
189 return port.RuntimeActivitySnapshot{
190 PendingCreations: t.pending,
191 ActiveViews: t.active,
192 PendingCleanup: t.cleanup,
193 ActiveDownloads: len(t.downloads),
194 }
195}
196
197// notifyLocked delivers the current snapshot to subscribers in subscription
198// order. The caller must hold t.mu and must not reenter the tracker from a
199// callback.
200func (t *RuntimeActivityTracker) notifyLocked() {
201 snapshot := t.snapshotLocked()
202 for id := uint64(0); id < t.nextSub; id++ {
203 if fn, ok := t.subscribers[id]; ok {
204 fn(snapshot)
205 }
206 }
207}