Browser vuoto: correzioni dopo la revisione (#52)
La revisione di c49df4c ha trovato alcuni punti deboli. Si correggono qui.
- Fuori vista la pagina torna al 100%. Lo zoom calcolato per uno schermo
piccolo restava anche per le sessioni del motore, e le pagine vedevano un
devicePixelRatio diverso (regola 4). Il rapporto si ricalcola solo quando
la finestra viene sullo schermo.
- Pulsante nella barra delle applicazioni. WPF lo cambia nascondendo la
finestra e mostrandola di nuovo (verificato con un aggancio a
WM_WINDOWPOSCHANGED). Ora il pulsante cambia solo mentre la finestra è
fuori vista. Sullo schermo la finestra chiede di ridisegnarsi
(RedrawWindow), senza nulla che la pagina veda. Una prova con WebView2
vero controlla che la finestra non si nasconda mai sullo schermo.
- Ridotta a icona troppo a lungo, la finestra si ripristina coperta (DWM) e
senza prendere il fuoco, e si sposta fuori vista prima di scoprirsi. Prima
ricompariva per un attimo dove stava. Spostare la posizione di ripristino
non basta: Windows la riporta su uno schermo (provato).
- Dopo un riavvio del processo GPU i bordi della pagina cambiano davvero: un
pixel in meno, poi quelli veri. Gli stessi bordi di nuovo potevano non
servire a nulla.
- «Pagina iniziale», sulla barra di Windows e nella vista remota, passa da
BrowserConnectionService.OpenStartPageAsync. Aspetta la fine della
sessione del motore e il turno della pagina, come l'apertura dal riposo,
e agisce solo mentre Alberto guida.
- Vista remota.
- Dopo un riavvio dei fotogrammi la vista aspetta il primo fotogramma per
mezzo secondo prima dell'immagine ferma: Chromium lo manda solo dopo la
risposta allo start, e prima quasi ogni riavvio costava una cattura.
- L'immagine ferma si scarta se nel frattempo arriva un fotogramma vero.
- Il silenzio si conta dall'avvio.
- Un riavvio del browser durante la chiusura non lascia eccezioni non
osservate.
- Un POST vuoto senza Content-Length (a pezzi, HTTP/2) non dà più 400 su
begin, cancel e browser/show (ApiHttp.OptionalBodyAsync).
- La pagina Browser toglie ?accesso=1 dall'indirizzo. Ricaricarla riapriva
la pagina di accesso.
- RemoteViewFramesTests aspetta fino a 20 s invece di 5. Controlla che
l'immagine arrivi, non quanto in fretta; il tempo va nell'uscita della
prova.
Aggiornati PROTOCOLLO.md, la guida (Primo collegamento, Installazione
Windows) e docs/decisioni.md con le righe di #52.
Prove nuove:
- AFrameSoonAfterARestartNeedsNoStillPicture
- PaginaInizialeWaitsForThePageTurnAndActsOnlyWhileAlbertoIsAtTheBrowser
- ABodyWithoutALengthIsReadAndAnEmptyOneIsNoBody
- nella prova della finestra vera: nessun nascondere sullo schermo e pagina
al 100% fuori vista
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c49df4ca7b
commit
654076d280
25 files changed
+505
-86
No files matched your search
@@ -22,7 +22,8 @@ public sealed record ScreencastOptions(int Quality = 70, int MaxWidth = 1280, in
|
||||
/// lost acknowledgement stops the frames for good, and a change of renderer (another site, the rest page, a crash) may
|
||||
/// leave the screencast on the old one. After <see cref="Silence"/> without frames — measured from the start, from the
|
||||
/// last frame or from the last change of document of the main frame — the screencast is stopped and started again (a
|
||||
/// new screencast session), and one still picture of the page is sent when the start brings no frame. Each restart
|
||||
/// new screencast session), and one still picture of the page is sent when no frame came within
|
||||
/// <see cref="FirstFrameWait"/> of the start (Chromium sends the first frame only after answering the start). Each restart
|
||||
/// doubles the wait, up to <see cref="LongestSilence"/>: a page that does not move (Chromium sends nothing then) costs
|
||||
/// one restart every so often, not one every two seconds. Frames that flow again (<see cref="FlowingFrames"/> after a
|
||||
/// restart) or a new document bring the wait back to <see cref="Silence"/>.
|
||||
@@ -38,6 +39,9 @@ public sealed record ScreencastWatchdog(TimeSpan Silence, TimeSpan Tick, TimeSpa
|
||||
/// </summary>
|
||||
public const int FlowingFrames = 3;
|
||||
|
||||
/// <summary>How long a restart waits for its first frame before it sends a still picture instead.</summary>
|
||||
public static readonly TimeSpan FirstFrameWait = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
/// <summary>About two seconds, looked at four times a second, at most half a minute between restarts.</summary>
|
||||
public static ScreencastWatchdog Default { get; } = new(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(250), TimeSpan.FromSeconds(30));
|
||||
}
|
||||
@@ -118,6 +122,7 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
private bool _restartWanted;
|
||||
private bool _silent;
|
||||
private DateTimeOffset _silentSince;
|
||||
private TaskCompletionSource? _nextFrame;
|
||||
|
||||
/// <param name="cdp">The page session.</param>
|
||||
/// <param name="log">Where starts, restarts and silences are noted (never parameters or frames).</param>
|
||||
@@ -338,9 +343,11 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
private void OnReconnected(object? sender, EventArgs e) =>
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
var ct = _lifetime.Token;
|
||||
// Read inside the try: a browser restart that races the dispose finds the source disposed.
|
||||
var ct = CancellationToken.None;
|
||||
try
|
||||
{
|
||||
ct = _lifetime.Token;
|
||||
await _starts.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
@@ -412,10 +419,16 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Stop and start again (a new screencast session), then one still picture when no frame came.</summary>
|
||||
/// <summary>
|
||||
/// Stop and start again (a new screencast session), then one still picture when no frame came within
|
||||
/// <see cref="ScreencastWatchdog.FirstFrameWait"/>. The silence counts from the start; the wait for the next restart
|
||||
/// doubles here, before the start, so that frames flowing meanwhile bring it back to the short one.
|
||||
/// </summary>
|
||||
private async Task RestartAsync(ScreencastWatchdog watchdog, TimeSpan silence, bool afterLoad, CancellationToken ct)
|
||||
{
|
||||
await _starts.WaitAsync(ct).ConfigureAwait(false);
|
||||
var started = false;
|
||||
TaskCompletionSource? nextFrame = null;
|
||||
try
|
||||
{
|
||||
bool first;
|
||||
@@ -427,6 +440,11 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
_silent = true;
|
||||
_silentSince = _clock.Now - silence;
|
||||
}
|
||||
|
||||
_restartWanted = false;
|
||||
_framesSinceRestart = 0;
|
||||
var longer = _silenceLimit + _silenceLimit;
|
||||
_silenceLimit = longer < watchdog.LongestSilence ? longer : watchdog.LongestSilence;
|
||||
}
|
||||
|
||||
var seconds = Math.Max(1, (int)Math.Round(silence.TotalSeconds)).ToString(CultureInfo.InvariantCulture);
|
||||
@@ -443,7 +461,6 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
_log.Debug(line);
|
||||
}
|
||||
|
||||
var before = FramesReceived;
|
||||
try
|
||||
{
|
||||
await _cdp.SendAsync("Page.stopScreencast", null, ct).ConfigureAwait(false);
|
||||
@@ -453,9 +470,22 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
// Nothing was running on the browser side: the start below is what matters.
|
||||
}
|
||||
|
||||
// Ready before the start: its first frame may come as soon as Chromium has answered.
|
||||
nextFrame = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
lock (_watch)
|
||||
{
|
||||
_nextFrame = nextFrame;
|
||||
}
|
||||
|
||||
await SendStartAsync(ct).ConfigureAwait(false);
|
||||
lock (_watch)
|
||||
{
|
||||
_watchFrom = _clock.Now;
|
||||
}
|
||||
|
||||
started = true;
|
||||
Interlocked.Increment(ref _restarts);
|
||||
if (FramesReceived == before)
|
||||
if (!await FrameWithinAsync(nextFrame.Task, ScreencastWatchdog.FirstFrameWait, ct).ConfigureAwait(false))
|
||||
{
|
||||
await SendStillAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
@@ -469,29 +499,65 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
{
|
||||
lock (_watch)
|
||||
{
|
||||
_watchFrom = _clock.Now;
|
||||
_restartWanted = false;
|
||||
_framesSinceRestart = 0;
|
||||
var longer = _silenceLimit + _silenceLimit;
|
||||
_silenceLimit = longer < watchdog.LongestSilence ? longer : watchdog.LongestSilence;
|
||||
if (!started)
|
||||
{
|
||||
_watchFrom = _clock.Now; // a failed restart: the next one after a whole silence, not at the next look
|
||||
}
|
||||
|
||||
if (ReferenceEquals(_nextFrame, nextFrame))
|
||||
{
|
||||
_nextFrame = null;
|
||||
}
|
||||
}
|
||||
|
||||
_starts.Release();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>True when <paramref name="frame"/> completes within <paramref name="wait"/> (on the clock).</summary>
|
||||
private async Task<bool> FrameWithinAsync(Task frame, TimeSpan wait, CancellationToken ct)
|
||||
{
|
||||
if (frame.IsCompleted)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
using var stopWaiting = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
var delay = _clock.Delay(wait, stopWaiting.Token);
|
||||
await Task.WhenAny(frame, delay).ConfigureAwait(false);
|
||||
|
||||
// Asked of the frame itself, not of which task woke the wait: both may be done by now, and a frame is a frame.
|
||||
if (frame.IsCompleted)
|
||||
{
|
||||
// The pause ends here (a clock that moves only when asked keeps no sleeper behind).
|
||||
await stopWaiting.CancelAsync().ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
await delay.ConfigureAwait(false); // cancelled with ct: the restart ends there
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One picture of the page as it is (<c>Page.captureScreenshot</c>, already allowed for the preview): a page that
|
||||
/// does not repaint shows something, and the pointer can be placed on it.
|
||||
/// </summary>
|
||||
private async Task SendStillAsync(CancellationToken ct)
|
||||
{
|
||||
var before = FramesReceived;
|
||||
var jpeg = await PagePreview.CaptureAsync(_cdp, Math.Clamp((_options ?? new ScreencastOptions()).Quality, 0, 100), ct).ConfigureAwait(false);
|
||||
if (jpeg.Length == 0 || Volatile.Read(ref _stopped) == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (FramesReceived != before)
|
||||
{
|
||||
// A real frame came during the capture: it is newer and carries the page's scroll and zoom.
|
||||
_log.Debug("Vista remota: immagine ferma scartata, è arrivato un fotogramma.");
|
||||
return;
|
||||
}
|
||||
|
||||
var last = LastFrame;
|
||||
double width;
|
||||
double height;
|
||||
@@ -577,6 +643,8 @@ public sealed class ScreencastSession : IAsyncDisposable
|
||||
{
|
||||
_lastFrameAt = _clock.Now;
|
||||
_restartWanted = false;
|
||||
_nextFrame?.TrySetResult();
|
||||
_nextFrame = null;
|
||||
if (++_framesSinceRestart >= ScreencastWatchdog.FlowingFrames && _options?.Watchdog is { } watchdog)
|
||||
{
|
||||
// The page moves: frames flow, and a silence from now on is worth a quick restart again.
|
||||
|
||||
Reference in new issue
Block a user