«Pagina iniziale» solo finché Alberto guida, e la vista che non si blocca (#52)
Una verifica indipendente di 654076d ha trovato tre punti deboli.
- «Pagina iniziale» guardava il permesso di guida una volta sola, prima di
aspettare fino a 15 s la fine della sessione e 15 s il turno della pagina
(che poi prendeva comunque). Se nel frattempo Alberto chiudeva la finestra,
Febe navigava senza permesso e fuori dal Governatore (regole 7 e 8). Ora la
pressione vale finché dura il permesso di chi l'ha premuta: la finestra di
Windows passa un gettone che si annulla con il suo permesso, la vista remota
quello della propria vita. Con il turno della pagina in mano la connessione
guarda di nuovo che Alberto guidi ancora; senza turno rinuncia invece di
prendere la pagina a un passo della ricerca. L'attesa del turno è
BrowserConnectionService.PageTurnWait (15 s, le prove la accorciano).
- Nella vista remota «Pagina iniziale» aspettava dentro il ciclo di ricezione:
la vista restava ferma fino a mezzo minuto e poi riversava clic e tasti sulla
pagina nuova. Ora la pressione corre a parte, una per volta, e intanto clic,
tasti, «Indietro» e «Ricarica» si scartano.
- Repaint chiedeva un ridisegno sincrono (RDW_UPDATENOW) a finestre figlie del
processo del browser di WebView2: uno bloccato avrebbe fermato il thread
dell'interfaccia. Ora si chiede solo di ridisegnare.
Prove nuove, che senza le correzioni non passano: la pressione dimenticata se
Alberto lascia il browser, quella che finisce con il permesso di chi l'ha
premuta, la pagina mai tolta a un passo della ricerca, la vista che non si
blocca e scarta l'input; su Windows con WebView2 vero, il gettone che si
annulla quando la finestra si chiude. PROTOCOLLO.md, la guida e decisioni.md
dicono il comportamento nuovo.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
654076d280
commit
2bbad2e9b7
12 files changed
+340
-24
No files matched your search
@@ -128,6 +128,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// </summary>
|
||||
public TimeSpan DrivingGrace { get; set; } = TimeSpan.FromMinutes(2);
|
||||
|
||||
/// <summary>
|
||||
/// How long Alberto's page waits for its turn while a step of the profile search holds it (real time). Read at every
|
||||
/// wait; tests shorten it.
|
||||
/// </summary>
|
||||
public TimeSpan PageTurnWait { get; set; } = TimeSpan.FromSeconds(15);
|
||||
|
||||
/// <summary>The engine, once it exists (it is built after the browser).</summary>
|
||||
public void AttachEngine(EngineHost engine) => _engine = engine ?? throw new ArgumentNullException(nameof(engine));
|
||||
|
||||
@@ -212,6 +218,17 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
/// with the page's turn, so that no step of a session or of the profile search has its page swapped under it. Only
|
||||
/// while he is at the browser: otherwise the page is the engine's and nothing happens.
|
||||
/// </summary>
|
||||
/// <param name="ct">
|
||||
/// Cancelled when the lease of whoever pressed the button ends (the window closed, the remote view closed): the press
|
||||
/// is then forgotten, whatever it was waiting for.
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// The waits can last up to <see cref="PageTurnWait"/> and more: Alberto may have let go of the browser meanwhile, and
|
||||
/// then the page is the engine's again (rules 7 and 8: no navigation without his lease, outside the Governor). So
|
||||
/// right before navigating, with the page's turn in hand, it looks again: he still drives and the caller's lease still
|
||||
/// holds. Without the page's turn (a step of the search kept it) nothing happens either: the page is not taken from
|
||||
/// under the step.
|
||||
/// </remarks>
|
||||
public async Task OpenStartPageAsync(CancellationToken ct)
|
||||
{
|
||||
if (!_browser.IsAvailable)
|
||||
@@ -219,7 +236,15 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
return;
|
||||
}
|
||||
|
||||
await _commands.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await _commands.WaitAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!_browser.UserIsDriving)
|
||||
@@ -229,6 +254,20 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
|
||||
await WaitForSessionEndAsync(ct).ConfigureAwait(false);
|
||||
using var turn = await TakePageAsync(ct).ConfigureAwait(false);
|
||||
if (_engine is not null && turn is null)
|
||||
{
|
||||
_log.Info("collegamento", "«Pagina iniziale» non aperta: la pagina è ancora occupata dalla ricerca dei profili. Riprova fra poco.");
|
||||
return;
|
||||
}
|
||||
|
||||
// The last look, with the page's turn in hand (no recipe and no search step can open a page until it is given
|
||||
// back): Alberto still drives and the caller's lease still holds.
|
||||
if (ct.IsCancellationRequested || !_browser.UserIsDriving)
|
||||
{
|
||||
_log.Info("collegamento", "«Pagina iniziale» non aperta: nel frattempo hai lasciato il browser.");
|
||||
return;
|
||||
}
|
||||
|
||||
var target = StartPageUrl;
|
||||
var what = target == _loginUrl ? "pagina di accesso" : "pagina iniziale del sito";
|
||||
var result = await _browser.Cdp.NavigateAsync(target, ct).ConfigureAwait(false);
|
||||
@@ -236,6 +275,10 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
? $"«Pagina iniziale»: {what} aperta."
|
||||
: $"«Pagina iniziale»: {what} non aperta (errore di rete del browser).");
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
_log.Info("collegamento", "«Pagina iniziale» non aperta: nel frattempo hai lasciato il browser.");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_commands.Release();
|
||||
@@ -686,8 +729,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The page turn for Alberto's login page, so that no step of the profile search runs under it; after 15 seconds
|
||||
/// the page is taken anyway (Alberto asked for it), and null comes back.
|
||||
/// The page turn for Alberto's own navigation, so that no step of the profile search runs under it; null after
|
||||
/// <see cref="PageTurnWait"/> (and always without an engine). The login and the start page from the rest page then
|
||||
/// take the page anyway (Alberto asked for it, under his lease); «Pagina iniziale» gives up.
|
||||
/// </summary>
|
||||
private async Task<PageTurn?> TakePageAsync(CancellationToken ct)
|
||||
{
|
||||
@@ -697,7 +741,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
|
||||
using var limit = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
limit.CancelAfter(TimeSpan.FromSeconds(15));
|
||||
limit.CancelAfter(PageTurnWait);
|
||||
try
|
||||
{
|
||||
return await engine.Services.PageTurns.TakeAsync(PageUser.Engine, limit.Token).ConfigureAwait(false);
|
||||
|
||||
@@ -95,6 +95,7 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
private CoreWebView2DevTools? _devTools;
|
||||
private TaskCompletionSource? _exited;
|
||||
private IDisposable? _lease;
|
||||
private CancellationTokenSource? _leaseEnded;
|
||||
private int _generation;
|
||||
private bool _starting;
|
||||
private bool _crashedWhileStarting;
|
||||
@@ -595,14 +596,21 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
}
|
||||
|
||||
TakeLease();
|
||||
_ = OpenStartPageQuietlyAsync(open);
|
||||
|
||||
// The press lasts as long as this lease: closed meanwhile, the window gives the page back to the engine and the
|
||||
// connection forgets the press (rules 7 and 8).
|
||||
_ = OpenStartPageQuietlyAsync(open, _leaseEnded!.Token);
|
||||
}
|
||||
|
||||
private async Task OpenStartPageQuietlyAsync(Func<CancellationToken, Task> open)
|
||||
private async Task OpenStartPageQuietlyAsync(Func<CancellationToken, Task> open, CancellationToken whileLeased)
|
||||
{
|
||||
try
|
||||
{
|
||||
await open(CancellationToken.None).ConfigureAwait(true);
|
||||
await open(whileLeased).ConfigureAwait(true);
|
||||
}
|
||||
catch (OperationCanceledException) when (whileLeased.IsCancellationRequested)
|
||||
{
|
||||
// The window went out of view before the start page could open: nothing to say.
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
@@ -646,7 +654,9 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
{
|
||||
if (_window is { } window)
|
||||
{
|
||||
Win32.RedrawWindow(window.Handle, IntPtr.Zero, IntPtr.Zero, Win32.RdwInvalidate | Win32.RdwAllChildren | Win32.RdwUpdateNow);
|
||||
// Only invalidated, never painted at once (RDW_UPDATENOW): the children belong to WebView2's browser process,
|
||||
// and a hung one would hold the UI thread in a synchronous paint.
|
||||
Win32.RedrawWindow(window.Handle, IntPtr.Zero, IntPtr.Zero, Win32.RdwInvalidate | Win32.RdwAllChildren);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -688,6 +698,7 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
if (_lease is null)
|
||||
{
|
||||
_lease = UserDriving.Begin();
|
||||
_leaseEnded = new CancellationTokenSource();
|
||||
_log.Info("browser", "Alberto usa il browser di Febe: il motore resta fermo.");
|
||||
}
|
||||
|
||||
@@ -701,6 +712,15 @@ internal sealed class WebView2Host : IBrowserHost
|
||||
_lease = null;
|
||||
_settings.MarkUsed();
|
||||
lease.Dispose();
|
||||
|
||||
// Off the UI thread: whatever waited on this lease (a «Pagina iniziale» in progress) gives up there. The source
|
||||
// has no timer and is never disposed, so a late look at its token is still safe.
|
||||
if (_leaseEnded is { } ended)
|
||||
{
|
||||
_leaseEnded = null;
|
||||
_ = ended.CancelAsync();
|
||||
}
|
||||
|
||||
_log.Info("browser", "Browser di Febe fuori vista.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,6 @@ internal static class Win32
|
||||
|
||||
public const uint RdwInvalidate = 0x0001;
|
||||
public const uint RdwAllChildren = 0x0080;
|
||||
public const uint RdwUpdateNow = 0x0100;
|
||||
|
||||
/// <summary>DWMWA_CLOAK: the window stays shown for Windows but DWM does not put it on screen.</summary>
|
||||
public const int DwmCloak = 13;
|
||||
|
||||
@@ -182,6 +182,7 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
private readonly RemoteViewSession _session;
|
||||
private readonly Func<CancellationToken, Task>? _openStartPage;
|
||||
private readonly ScreencastWatchdog? _watchdog;
|
||||
private readonly IBrowserLog _log;
|
||||
private readonly Lock _gate = new();
|
||||
private readonly Queue<RemoteViewMeta> _meta = new();
|
||||
private readonly SemaphoreSlim _signal = new(0, int.MaxValue);
|
||||
@@ -198,6 +199,8 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
private int _height;
|
||||
private int _disposed;
|
||||
private bool _used;
|
||||
private int _startPagePending;
|
||||
private Task _startPage = Task.CompletedTask;
|
||||
|
||||
public BrowserRemoteViewConnection(BrowserRemoteViewHost owner, IFebeBrowser browser, BrowserUseTracker tracker, IClock clock, IClock browserClock, IBrowserLog log, Func<CancellationToken, Task>? openStartPage, ScreencastWatchdog? watchdog)
|
||||
{
|
||||
@@ -207,6 +210,7 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
_clock = clock;
|
||||
_openStartPage = openStartPage;
|
||||
_watchdog = watchdog;
|
||||
_log = log;
|
||||
|
||||
// A real log (issue #52): the starts, restarts and silences of the frames reach the registro.
|
||||
_session = new RemoteViewSession(browser.Cdp, browser.UserDriving, log, browserClock);
|
||||
@@ -216,6 +220,21 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
|
||||
private ICdpSession Cdp => _browser.Cdp;
|
||||
|
||||
/// <summary>True while a «Pagina iniziale» waits or loads: the input of the view is dropped meanwhile.</summary>
|
||||
internal bool StartPagePending => Volatile.Read(ref _startPagePending) != 0;
|
||||
|
||||
/// <summary>The last «Pagina iniziale» of this view (a completed task before the first): tests wait on it.</summary>
|
||||
internal Task StartPageOpening
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
return _startPage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartAsync(int width, int height, CancellationToken ct)
|
||||
{
|
||||
_width = width;
|
||||
@@ -281,9 +300,11 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
public Task MouseAsync(RemoteViewMouse input, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input);
|
||||
if (_session.Screencast.LastFrame is null)
|
||||
if (_session.Screencast.LastFrame is null || StartPagePending)
|
||||
{
|
||||
return Task.CompletedTask; // no frame yet: nothing on screen to point at
|
||||
// No frame yet: nothing on screen to point at. «Pagina iniziale» in progress: a click would land on the page
|
||||
// about to go, or on the start page while it loads.
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
MarkUsed();
|
||||
@@ -307,6 +328,11 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
public Task KeyAsync(RemoteViewKey input, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(input);
|
||||
if (StartPagePending)
|
||||
{
|
||||
return Task.CompletedTask; // as for the mouse: not typed into a page about to change
|
||||
}
|
||||
|
||||
MarkUsed();
|
||||
if (input.Action == RemoteViewKeyAction.Char)
|
||||
{
|
||||
@@ -321,16 +347,15 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
MarkUsed();
|
||||
if (action == RemoteViewNavigation.Home)
|
||||
{
|
||||
// Alberto's own navigation (rule 7), done by the connection: like the start page opened from the rest page,
|
||||
// it waits for the engine's session to end and takes the page's turn first.
|
||||
if (_openStartPage is { } open)
|
||||
{
|
||||
await open(ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
OpenStartPage();
|
||||
return;
|
||||
}
|
||||
|
||||
if (StartPagePending)
|
||||
{
|
||||
return; // the start page is about to replace the page anyway
|
||||
}
|
||||
|
||||
string? target;
|
||||
lock (_gate)
|
||||
{
|
||||
@@ -366,6 +391,46 @@ internal sealed class BrowserRemoteViewConnection : IRemoteViewConnection
|
||||
await _session.NavigateAsync(target, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «Pagina iniziale»: Alberto's own navigation (rule 7), done by the connection — like the start page opened from the
|
||||
/// rest page, it waits for the engine's session to end and takes the page's turn first, up to half a minute. So it runs
|
||||
/// beside the view, not in its receive loop (the view would freeze meanwhile), for as long as the view and its lease
|
||||
/// last; one at a time, and until it is done the view's input is dropped.
|
||||
/// </summary>
|
||||
private void OpenStartPage()
|
||||
{
|
||||
if (_openStartPage is not { } open || Interlocked.CompareExchange(ref _startPagePending, 1, 0) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var task = Task.Run(() => OpenStartPageAsync(open), CancellationToken.None);
|
||||
lock (_gate)
|
||||
{
|
||||
_startPage = task;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OpenStartPageAsync(Func<CancellationToken, Task> open)
|
||||
{
|
||||
try
|
||||
{
|
||||
await open(_ended.Token).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (_ended.IsCancellationRequested)
|
||||
{
|
||||
// The view closed first: its lease is gone, the page is the engine's.
|
||||
}
|
||||
catch (Exception ex) when (ex is not OutOfMemoryException)
|
||||
{
|
||||
_log.Write(BrowserLogLevel.Warning, $"«Pagina iniziale» non aperta ({ex.GetType().Name}).");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Volatile.Write(ref _startPagePending, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Ends the view from the server side (another tab, shutdown): the page is told why.</summary>
|
||||
public async Task EndAsync(RemoteViewEnd reason)
|
||||
{
|
||||
|
||||
@@ -18,7 +18,11 @@ Lo usano due parti scritte in parallelo: non cambiarlo senza aggiornare entrambe
|
||||
pagina di accesso. Aprirla, dal riposo o con il pulsante «Pagina iniziale» (barra della finestra di Windows, vista
|
||||
remota), è un'azione di Alberto, sotto il suo permesso di guida (`UserIsDriving`), non del motore: Febe aspetta che la
|
||||
sessione del motore finisca, prende il turno della pagina (un passo della ricerca dei profili finisce prima) e solo
|
||||
dopo naviga. «Indietro», «Ricarica» e i clic nella pagina invece agiscono subito, come nel browser. La striscia rossa
|
||||
dopo naviga. La pressione del pulsante vale finché resta aperto chi l'ha premuta (la finestra sullo schermo, la vista
|
||||
remota): se nel frattempo Alberto l'ha chiusa, o non guida più, o il turno della pagina non arriva entro 15 s, non
|
||||
succede nulla. Nella vista remota l'attesa non ferma la vista: una pressione per volta, e finché non è finita i clic,
|
||||
i tasti, «Indietro» e «Ricarica» si scartano (finirebbero sulla pagina che sta per cambiare). Fuori da quell'attesa
|
||||
«Indietro», «Ricarica» e i clic nella pagina agiscono subito, come nel browser. La striscia rossa
|
||||
di una disconnessione apre il browser con `openLogin: true` (in Docker la pagina Browser con `?accesso=1`).
|
||||
|
||||
In modalità dimostrativa la scena `scollegato` dà `notConnected`, la scena `fermo` dà `verificationRequired`.
|
||||
|
||||
Reference in new issue
Block a user