Integrazione 0.1.1: Febe per Windows si collega, VS Code, prove su Windows, storico e vista remota #4
No files matched your search
@@ -92,11 +92,31 @@ public sealed class CdpCommandNotAllowedException(string method, string message)
|
|||||||
public sealed class CdpCommandException(string method, int code, string browserMessage)
|
public sealed class CdpCommandException(string method, int code, string browserMessage)
|
||||||
: Exception($"Il comando CDP «{method}» è fallito: {browserMessage} (codice {code}).")
|
: Exception($"Il comando CDP «{method}» è fallito: {browserMessage} (codice {code}).")
|
||||||
{
|
{
|
||||||
|
/// <summary>Chromium's generic «server error» code.</summary>
|
||||||
|
public const int ServerErrorCode = -32000;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// What Chromium answers to a browser-side page command (<c>Page.startScreencast</c>, <c>Page.captureScreenshot</c>)
|
||||||
|
/// while the page's current document is not «active»: from <c>ReadyToCommitNavigation</c> 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, <c>AssureTopLevelActiveFrame</c>;
|
||||||
|
/// render_frame_devtools_agent_host.cc, <c>ReadyToCommitNavigation</c>). 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.
|
||||||
|
/// </summary>
|
||||||
|
public const string InactivePageMessage = "Not attached to an active page";
|
||||||
|
|
||||||
public string Method { get; } = method;
|
public string Method { get; } = method;
|
||||||
|
|
||||||
public int Code { get; } = code;
|
public int Code { get; } = code;
|
||||||
|
|
||||||
public string BrowserMessage { get; } = browserMessage;
|
public string BrowserMessage { get; } = browserMessage;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The page is between two documents (see <see cref="InactivePageMessage"/>): the same command a moment later finds
|
||||||
|
/// the new document committed. A browser that is gone answers differently (closed connection, no answer).
|
||||||
|
/// </summary>
|
||||||
|
public bool IsInactivePage => Code == ServerErrorCode && string.Equals(BrowserMessage, InactivePageMessage, StringComparison.Ordinal);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The connection to the browser is closed (browser exited, crashed or is restarting).</summary>
|
/// <summary>The connection to the browser is closed (browser exited, crashed or is restarting).</summary>
|
||||||
|
|||||||
@@ -64,12 +64,16 @@ public sealed class RemoteViewSession : IAsyncDisposable
|
|||||||
private IDisposable? _lease;
|
private IDisposable? _lease;
|
||||||
private int _buttons;
|
private int _buttons;
|
||||||
|
|
||||||
public RemoteViewSession(ICdpSession cdp, UserDrivingGate gate, IBrowserLog? log = null)
|
/// <param name="cdp">The page session.</param>
|
||||||
|
/// <param name="gate">Where the «Alberto is driving» lease is taken.</param>
|
||||||
|
/// <param name="log">Where opening and closing are noted (never keys).</param>
|
||||||
|
/// <param name="clock">Wall-clock time for the screencast's start attempts; null: the system clock.</param>
|
||||||
|
public RemoteViewSession(ICdpSession cdp, UserDrivingGate gate, IBrowserLog? log = null, IClock? clock = null)
|
||||||
{
|
{
|
||||||
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
|
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
|
||||||
_gate = gate ?? throw new ArgumentNullException(nameof(gate));
|
_gate = gate ?? throw new ArgumentNullException(nameof(gate));
|
||||||
_log = log ?? NullBrowserLog.Instance;
|
_log = log ?? NullBrowserLog.Instance;
|
||||||
Screencast = new ScreencastSession(cdp, _log);
|
Screencast = new ScreencastSession(cdp, _log, clock);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ScreencastSession Screencast { get; }
|
public ScreencastSession Screencast { get; }
|
||||||
|
|||||||
@@ -39,10 +39,22 @@ public sealed record ScreencastFrame(
|
|||||||
/// <c>Page.screencastFrameAck</c>, stopped with <c>Page.stopScreencast</c>. Frames are delivered through
|
/// <c>Page.screencastFrameAck</c>, stopped with <c>Page.stopScreencast</c>. Frames are delivered through
|
||||||
/// <see cref="ReadFramesAsync"/> (only the latest ones are kept when the reader is slow) and <see cref="FrameReceived"/>.
|
/// <see cref="ReadFramesAsync"/> (only the latest ones are kept when the reader is slow) and <see cref="FrameReceived"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// 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 <see cref="InactivePageBudget"/>. Any other failure is reported at once.
|
||||||
|
/// </remarks>
|
||||||
public sealed class ScreencastSession : IAsyncDisposable
|
public sealed class ScreencastSession : IAsyncDisposable
|
||||||
{
|
{
|
||||||
|
/// <summary>How long a start refused because the page is between two documents is tried again.</summary>
|
||||||
|
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 ICdpSession _cdp;
|
||||||
private readonly IBrowserLog _log;
|
private readonly IBrowserLog _log;
|
||||||
|
private readonly IClock _clock;
|
||||||
private readonly Channel<ScreencastFrame> _frames = Channel.CreateBounded<ScreencastFrame>(
|
private readonly Channel<ScreencastFrame> _frames = Channel.CreateBounded<ScreencastFrame>(
|
||||||
new BoundedChannelOptions(2) { FullMode = BoundedChannelFullMode.DropOldest, SingleReader = true });
|
new BoundedChannelOptions(2) { FullMode = BoundedChannelFullMode.DropOldest, SingleReader = true });
|
||||||
|
|
||||||
@@ -54,10 +66,14 @@ public sealed class ScreencastSession : IAsyncDisposable
|
|||||||
private int _stopped;
|
private int _stopped;
|
||||||
private long _framesReceived;
|
private long _framesReceived;
|
||||||
|
|
||||||
public ScreencastSession(ICdpSession cdp, IBrowserLog? log = null)
|
/// <param name="cdp">The page session.</param>
|
||||||
|
/// <param name="log">Where the start and stop are noted (never parameters or frames).</param>
|
||||||
|
/// <param name="clock">Wall-clock time for the pauses between start attempts; null: the system clock.</param>
|
||||||
|
public ScreencastSession(ICdpSession cdp, IBrowserLog? log = null, IClock? clock = null)
|
||||||
{
|
{
|
||||||
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
|
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
|
||||||
_log = log ?? NullBrowserLog.Instance;
|
_log = log ?? NullBrowserLog.Instance;
|
||||||
|
_clock = clock ?? new SystemClock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Raised on the browser event thread for every frame: handlers must be quick.</summary>
|
/// <summary>Raised on the browser event thread for every frame: handlers must be quick.</summary>
|
||||||
@@ -144,20 +160,47 @@ public sealed class ScreencastSession : IAsyncDisposable
|
|||||||
_lifetime.Dispose();
|
_lifetime.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task SendStartAsync(CancellationToken ct)
|
private async Task SendStartAsync(CancellationToken ct)
|
||||||
{
|
{
|
||||||
var o = _options ?? new ScreencastOptions();
|
var o = _options ?? new ScreencastOptions();
|
||||||
return _cdp.SendAsync(
|
var deadline = _clock.Now + InactivePageBudget;
|
||||||
"Page.startScreencast",
|
var pause = FirstPause;
|
||||||
new JsonObject
|
for (var attempt = 1; ; attempt++)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
["format"] = "jpeg",
|
await _cdp.SendAsync(
|
||||||
["quality"] = Math.Clamp(o.Quality, 0, 100),
|
"Page.startScreencast",
|
||||||
["maxWidth"] = o.MaxWidth,
|
new JsonObject
|
||||||
["maxHeight"] = o.MaxHeight,
|
{
|
||||||
["everyNthFrame"] = Math.Max(1, o.EveryNthFrame),
|
["format"] = "jpeg",
|
||||||
},
|
["quality"] = Math.Clamp(o.Quality, 0, 100),
|
||||||
ct);
|
["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) =>
|
private void OnReconnected(object? sender, EventArgs e) =>
|
||||||
|
|||||||
Reference in new issue
Block a user