diff --git a/src/Febe.Browser/Cdp/CdpCommandPolicy.cs b/src/Febe.Browser/Cdp/CdpCommandPolicy.cs
index bd543f2..2565107 100644
--- a/src/Febe.Browser/Cdp/CdpCommandPolicy.cs
+++ b/src/Febe.Browser/Cdp/CdpCommandPolicy.cs
@@ -92,11 +92,31 @@ public sealed class CdpCommandNotAllowedException(string method, string message)
public sealed class CdpCommandException(string method, int code, string browserMessage)
: Exception($"Il comando CDP «{method}» è fallito: {browserMessage} (codice {code}).")
{
+ /// Chromium's generic «server error» code.
+ public const int ServerErrorCode = -32000;
+
+ ///
+ /// What Chromium answers to a browser-side page command (Page.startScreencast, Page.captureScreenshot)
+ /// while the page's current document is not «active»: from ReadyToCommitNavigation of a navigation that uses
+ /// a new RenderFrameHost until that navigation commits, DevTools already points at the new, still uncommitted frame
+ /// (content/browser/devtools/protocol/page_handler.cc, AssureTopLevelActiveFrame;
+ /// render_frame_devtools_agent_host.cc, ReadyToCommitNavigation). A new RenderFrameHost comes with a process
+ /// swap, a cross-site or back-forward-cache swap and, with RenderDocument (on by default in current Chromium), with
+ /// every navigation of the page to a new document — the rest page included.
+ ///
+ public const string InactivePageMessage = "Not attached to an active page";
+
public string Method { get; } = method;
public int Code { get; } = code;
public string BrowserMessage { get; } = browserMessage;
+
+ ///
+ /// The page is between two documents (see ): the same command a moment later finds
+ /// the new document committed. A browser that is gone answers differently (closed connection, no answer).
+ ///
+ public bool IsInactivePage => Code == ServerErrorCode && string.Equals(BrowserMessage, InactivePageMessage, StringComparison.Ordinal);
}
/// The connection to the browser is closed (browser exited, crashed or is restarting).
diff --git a/src/Febe.Browser/RemoteView/RemoteViewSession.cs b/src/Febe.Browser/RemoteView/RemoteViewSession.cs
index a96a8f8..9ab7dd1 100644
--- a/src/Febe.Browser/RemoteView/RemoteViewSession.cs
+++ b/src/Febe.Browser/RemoteView/RemoteViewSession.cs
@@ -64,12 +64,16 @@ public sealed class RemoteViewSession : IAsyncDisposable
private IDisposable? _lease;
private int _buttons;
- public RemoteViewSession(ICdpSession cdp, UserDrivingGate gate, IBrowserLog? log = null)
+ /// The page session.
+ /// Where the «Alberto is driving» lease is taken.
+ /// Where opening and closing are noted (never keys).
+ /// Wall-clock time for the screencast's start attempts; null: the system clock.
+ public RemoteViewSession(ICdpSession cdp, UserDrivingGate gate, IBrowserLog? log = null, IClock? clock = null)
{
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
_gate = gate ?? throw new ArgumentNullException(nameof(gate));
_log = log ?? NullBrowserLog.Instance;
- Screencast = new ScreencastSession(cdp, _log);
+ Screencast = new ScreencastSession(cdp, _log, clock);
}
public ScreencastSession Screencast { get; }
diff --git a/src/Febe.Browser/RemoteView/ScreencastSession.cs b/src/Febe.Browser/RemoteView/ScreencastSession.cs
index 530f9a0..5b3e299 100644
--- a/src/Febe.Browser/RemoteView/ScreencastSession.cs
+++ b/src/Febe.Browser/RemoteView/ScreencastSession.cs
@@ -39,10 +39,22 @@ public sealed record ScreencastFrame(
/// Page.screencastFrameAck, stopped with Page.stopScreencast. Frames are delivered through
/// (only the latest ones are kept when the reader is slow) and .
///
+///
+/// A view opened while the page changes document (the rest page after a session, the login page opened together with
+/// the view) can meet the moment in which Chromium refuses the start with «Not attached to an active page»: the start
+/// is sent again, a few times, for at most . Any other failure is reported at once.
+///
public sealed class ScreencastSession : IAsyncDisposable
{
+ /// How long a start refused because the page is between two documents is tried again.
+ public static readonly TimeSpan InactivePageBudget = TimeSpan.FromSeconds(5);
+
+ private static readonly TimeSpan FirstPause = TimeSpan.FromMilliseconds(100);
+ private static readonly TimeSpan LongestPause = TimeSpan.FromMilliseconds(500);
+
private readonly ICdpSession _cdp;
private readonly IBrowserLog _log;
+ private readonly IClock _clock;
private readonly Channel _frames = Channel.CreateBounded(
new BoundedChannelOptions(2) { FullMode = BoundedChannelFullMode.DropOldest, SingleReader = true });
@@ -54,10 +66,14 @@ public sealed class ScreencastSession : IAsyncDisposable
private int _stopped;
private long _framesReceived;
- public ScreencastSession(ICdpSession cdp, IBrowserLog? log = null)
+ /// The page session.
+ /// Where the start and stop are noted (never parameters or frames).
+ /// Wall-clock time for the pauses between start attempts; null: the system clock.
+ public ScreencastSession(ICdpSession cdp, IBrowserLog? log = null, IClock? clock = null)
{
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
_log = log ?? NullBrowserLog.Instance;
+ _clock = clock ?? new SystemClock();
}
/// Raised on the browser event thread for every frame: handlers must be quick.
@@ -144,20 +160,47 @@ public sealed class ScreencastSession : IAsyncDisposable
_lifetime.Dispose();
}
- private Task SendStartAsync(CancellationToken ct)
+ private async Task SendStartAsync(CancellationToken ct)
{
var o = _options ?? new ScreencastOptions();
- return _cdp.SendAsync(
- "Page.startScreencast",
- new JsonObject
+ var deadline = _clock.Now + InactivePageBudget;
+ var pause = FirstPause;
+ for (var attempt = 1; ; attempt++)
+ {
+ try
{
- ["format"] = "jpeg",
- ["quality"] = Math.Clamp(o.Quality, 0, 100),
- ["maxWidth"] = o.MaxWidth,
- ["maxHeight"] = o.MaxHeight,
- ["everyNthFrame"] = Math.Max(1, o.EveryNthFrame),
- },
- ct);
+ await _cdp.SendAsync(
+ "Page.startScreencast",
+ new JsonObject
+ {
+ ["format"] = "jpeg",
+ ["quality"] = Math.Clamp(o.Quality, 0, 100),
+ ["maxWidth"] = o.MaxWidth,
+ ["maxHeight"] = o.MaxHeight,
+ ["everyNthFrame"] = Math.Max(1, o.EveryNthFrame),
+ },
+ ct).ConfigureAwait(false);
+ if (attempt > 1)
+ {
+ _log.Debug($"Vista remota: fotogrammi avviati al tentativo {attempt.ToString(System.Globalization.CultureInfo.InvariantCulture)}, dopo un cambio di pagina.");
+ }
+
+ return;
+ }
+ catch (CdpCommandException ex) when (ex.IsInactivePage && _clock.Now + pause <= deadline)
+ {
+ // A navigation is committing in a new frame of the page: nothing is started on the browser side, the
+ // same start a moment later finds the new document. Past the budget the error goes up as it is.
+ }
+
+ await _clock.Delay(pause, ct).ConfigureAwait(false);
+ if (Volatile.Read(ref _stopped) == 1)
+ {
+ return; // stopped meanwhile (a restart's start in the background): nothing left to start
+ }
+
+ pause = pause * 2 < LongestPause ? pause * 2 : LongestPause;
+ }
}
private void OnReconnected(object? sender, EventArgs e) =>