Strato browser (M0/M2): client CDP con elenco dei comandi ammessi, ChromiumProcessHost e Xvfb sorvegliati, gesti umani, mappa dei bersagli, cattura delle risposte, segnali di arresto, scaricamento dal browser, vista remota, distribuzioni del ritmo; prove unitarie e d'insieme con Chromium su Xvfb; esito locale di S5 e S6

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-13 15:51:02 +00:00
1 parent c803bd98b5
commit 925141bb37
44 files changed
+8396

No files matched your search

@@ -0,0 +1,253 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using Febe.Browser.Cdp;
using Febe.Browser.Diagnostics;
using Febe.Browser.Gestures;
using Febe.Browser.Hosting;
using Febe.Core.Abstractions;
namespace Febe.Browser.RemoteView;
public enum RemoteMouseAction
{
Move,
Down,
Up,
Wheel,
}
public enum RemoteMouseButton
{
None,
Left,
Middle,
Right,
}
/// <summary>A mouse event from the interface's remote view.</summary>
/// <param name="Action">Move, press, release or wheel.</param>
/// <param name="X">Horizontal position: 0–1 of the frame when <paramref name="Normalized"/>, CSS pixels otherwise.</param>
/// <param name="Y">Vertical position: 0–1 of the frame when <paramref name="Normalized"/>, CSS pixels otherwise.</param>
/// <param name="Button">Button pressed or released.</param>
/// <param name="ClickCount">1 for a click, 2 for a double click.</param>
/// <param name="DeltaX">Horizontal wheel delta in CSS pixels.</param>
/// <param name="DeltaY">Vertical wheel delta in CSS pixels (positive scrolls down).</param>
/// <param name="Modifiers">Modifier keys held.</param>
/// <param name="Normalized">Whether X and Y are fractions of the frame.</param>
public sealed record RemoteMouseEvent(
RemoteMouseAction Action,
double X,
double Y,
RemoteMouseButton Button = RemoteMouseButton.None,
int ClickCount = 1,
double DeltaX = 0,
double DeltaY = 0,
KeyModifiers Modifiers = KeyModifiers.None,
bool Normalized = true);
/// <summary>A key event from the interface's remote view. Never logged or stored (spec §13.2).</summary>
public sealed record RemoteKeyEvent(bool Down, KeyStroke Key)
{
public override string ToString() => "RemoteKeyEvent(…)";
}
/// <summary>
/// The remote view used by Alberto from the interface (spec §4.1, §11, mockup 10): the screencast goes to the page,
/// mouse and keys come back to the browser through <c>Input.*</c>. While the session is active the
/// <see cref="UserDrivingGate"/> holds a lease, so the engine stays still. Keys are forwarded, never logged.
/// </summary>
public sealed class RemoteViewSession : IAsyncDisposable
{
private readonly ICdpSession _cdp;
private readonly UserDrivingGate _gate;
private readonly IBrowserLog _log;
private IDisposable? _lease;
private int _buttons;
public RemoteViewSession(ICdpSession cdp, UserDrivingGate gate, IBrowserLog? log = null)
{
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
_gate = gate ?? throw new ArgumentNullException(nameof(gate));
_log = log ?? NullBrowserLog.Instance;
Screencast = new ScreencastSession(cdp, _log);
}
public ScreencastSession Screencast { get; }
public bool IsActive => _lease is not null;
/// <summary>Takes the «Alberto is driving» lease and starts the frames.</summary>
public async Task StartAsync(ScreencastOptions? options = null, CancellationToken ct = default)
{
if (_lease is not null)
{
throw new InvalidOperationException("La vista remota è già attiva.");
}
_lease = _gate.Begin();
try
{
await Screencast.StartAsync(options, ct).ConfigureAwait(false);
}
catch
{
_lease.Dispose();
_lease = null;
throw;
}
_log.Info("Vista remota aperta: il motore resta fermo finché Alberto usa il browser.");
}
public async Task MouseAsync(RemoteMouseEvent e, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(e);
EnsureActive();
var (x, y) = ToPage(e);
var button = e.Button switch
{
RemoteMouseButton.Left => "left",
RemoteMouseButton.Middle => "middle",
RemoteMouseButton.Right => "right",
_ => "none",
};
var bit = e.Button switch
{
RemoteMouseButton.Left => 1,
RemoteMouseButton.Right => 2,
RemoteMouseButton.Middle => 4,
_ => 0,
};
var parameters = new JsonObject
{
["x"] = x,
["y"] = y,
["modifiers"] = (int)e.Modifiers,
["pointerType"] = "mouse",
};
switch (e.Action)
{
case RemoteMouseAction.Move:
parameters["type"] = "mouseMoved";
parameters["button"] = "none";
parameters["buttons"] = _buttons;
break;
case RemoteMouseAction.Down:
_buttons |= bit;
parameters["type"] = "mousePressed";
parameters["button"] = button;
parameters["buttons"] = _buttons;
parameters["clickCount"] = Math.Max(1, e.ClickCount);
break;
case RemoteMouseAction.Up:
_buttons &= ~bit;
parameters["type"] = "mouseReleased";
parameters["button"] = button;
parameters["buttons"] = _buttons;
parameters["clickCount"] = Math.Max(1, e.ClickCount);
break;
case RemoteMouseAction.Wheel:
parameters["type"] = "mouseWheel";
parameters["deltaX"] = e.DeltaX;
parameters["deltaY"] = e.DeltaY;
break;
default:
throw new ArgumentOutOfRangeException(nameof(e), e.Action, null);
}
await _cdp.SendAsync("Input.dispatchMouseEvent", parameters, ct).ConfigureAwait(false);
}
public Task KeyAsync(RemoteKeyEvent e, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(e);
EnsureActive();
return e.Down ? Keyboard.KeyDownAsync(_cdp, e.Key, ct) : Keyboard.KeyUpAsync(_cdp, e.Key, ct);
}
/// <summary>Opens an address chosen by Alberto (e.g. the login page from the connection page).</summary>
public Task<NavigationResult> NavigateAsync(string url, CancellationToken ct = default)
{
EnsureActive();
return _cdp.NavigateAsync(url, ct);
}
/// <summary>Stops the frames and releases the lease: the engine may act again.</summary>
public async Task StopAsync(CancellationToken ct = default)
{
try
{
await Screencast.StopAsync(ct).ConfigureAwait(false);
}
finally
{
Interlocked.Exchange(ref _lease, null)?.Dispose();
_buttons = 0;
}
_log.Info("Vista remota chiusa.");
}
public async ValueTask DisposeAsync()
{
if (_lease is not null)
{
await StopAsync().ConfigureAwait(false);
}
await Screencast.DisposeAsync().ConfigureAwait(false);
}
private (double X, double Y) ToPage(RemoteMouseEvent e)
{
if (!e.Normalized)
{
return (e.X, e.Y);
}
var frame = Screencast.LastFrame;
if (frame is null || frame.DeviceWidth <= 0 || frame.DeviceHeight <= 0)
{
throw new InvalidOperationException("Nessun fotogramma ricevuto: posizione del puntatore non calcolabile.");
}
var scale = frame.PageScaleFactor > 0 ? frame.PageScaleFactor : 1;
var x = Math.Clamp(e.X, 0, 1) * frame.DeviceWidth / scale;
var y = Math.Clamp(e.Y, 0, 1) * frame.DeviceHeight / scale;
return (Math.Round(x, 1), Math.Round(y, 1));
}
private void EnsureActive()
{
if (_lease is null)
{
throw new InvalidOperationException("La vista remota non è attiva.");
}
}
}
/// <summary>The still preview of the Activity page (mockup 06): one frame every few seconds, not a video.</summary>
public static class PagePreview
{
/// <summary>Captures the visible page as JPEG through <c>Page.captureScreenshot</c>.</summary>
public static async Task<byte[]> CaptureAsync(ICdpSession cdp, int quality = 60, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(cdp);
var result = await cdp.SendAsync(
"Page.captureScreenshot",
new JsonObject
{
["format"] = "jpeg",
["quality"] = Math.Clamp(quality, 0, 100),
["fromSurface"] = true,
["captureBeyondViewport"] = false,
},
ct).ConfigureAwait(false);
return result.TryGetProperty("data", out var data) && data.ValueKind == JsonValueKind.String
? data.GetBytesFromBase64()
: [];
}
}
@@ -0,0 +1,225 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Channels;
using Febe.Browser.Cdp;
using Febe.Browser.Diagnostics;
using Febe.Core.Abstractions;
namespace Febe.Browser.RemoteView;
/// <summary>Settings of a screencast (<c>Page.startScreencast</c>).</summary>
/// <param name="Quality">JPEG quality, 0–100.</param>
/// <param name="MaxWidth">Largest frame width in pixels.</param>
/// <param name="MaxHeight">Largest frame height in pixels.</param>
/// <param name="EveryNthFrame">Send one frame out of this many.</param>
public sealed record ScreencastOptions(int Quality = 70, int MaxWidth = 1280, int MaxHeight = 900, int EveryNthFrame = 1);
/// <summary>One frame of the remote view.</summary>
/// <param name="Jpeg">The image.</param>
/// <param name="DeviceWidth">Page width in CSS pixels the frame represents.</param>
/// <param name="DeviceHeight">Page height in CSS pixels the frame represents.</param>
/// <param name="PageScaleFactor">Page zoom.</param>
/// <param name="OffsetTop">Top offset in CSS pixels (browser controls).</param>
/// <param name="ScrollOffsetX">Horizontal scroll position.</param>
/// <param name="ScrollOffsetY">Vertical scroll position.</param>
/// <param name="Timestamp">Browser timestamp (seconds), when given.</param>
public sealed record ScreencastFrame(
byte[] Jpeg,
double DeviceWidth,
double DeviceHeight,
double PageScaleFactor,
double OffsetTop,
double ScrollOffsetX,
double ScrollOffsetY,
double? Timestamp);
/// <summary>
/// The remote view's video (spec §4.1, §11): <c>Page.startScreencast</c> in JPEG, every frame acknowledged with
/// <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"/>.
/// </summary>
public sealed class ScreencastSession : IAsyncDisposable
{
private readonly ICdpSession _cdp;
private readonly IBrowserLog _log;
private readonly Channel<ScreencastFrame> _frames = Channel.CreateBounded<ScreencastFrame>(
new BoundedChannelOptions(2) { FullMode = BoundedChannelFullMode.DropOldest, SingleReader = true });
private readonly Channel<int> _acks = Channel.CreateUnbounded<int>(new UnboundedChannelOptions { SingleReader = true });
private readonly CancellationTokenSource _lifetime = new();
private IDisposable? _subscription;
private Task? _ackPump;
private ScreencastOptions? _options;
private int _stopped;
private long _framesReceived;
public ScreencastSession(ICdpSession cdp, IBrowserLog? log = null)
{
_cdp = cdp ?? throw new ArgumentNullException(nameof(cdp));
_log = log ?? NullBrowserLog.Instance;
}
/// <summary>Raised on the browser event thread for every frame: handlers must be quick.</summary>
public event EventHandler<ScreencastFrame>? FrameReceived;
/// <summary>The last frame received, for mapping input coordinates.</summary>
public ScreencastFrame? LastFrame { get; private set; }
public long FramesReceived => Interlocked.Read(ref _framesReceived);
public async Task StartAsync(ScreencastOptions? options = null, CancellationToken ct = default)
{
if (_subscription is not null)
{
throw new InvalidOperationException("La vista remota è già avviata.");
}
_options = options ?? new ScreencastOptions();
_subscription = _cdp.On("Page.screencastFrame", OnFrame);
if (_cdp is ICdpConnectionEvents events)
{
events.Reconnected += OnReconnected;
}
_ackPump = Task.Run(() => AckPumpAsync(_lifetime.Token), CancellationToken.None);
await SendStartAsync(ct).ConfigureAwait(false);
_log.Debug("Vista remota avviata.");
}
/// <summary>Frames as they arrive; the oldest are dropped if the reader falls behind. Ends when the session stops.</summary>
public async IAsyncEnumerable<ScreencastFrame> ReadFramesAsync([EnumeratorCancellation] CancellationToken ct = default)
{
await foreach (var frame in _frames.Reader.ReadAllAsync(ct).ConfigureAwait(false))
{
yield return frame;
}
}
public async Task StopAsync(CancellationToken ct = default)
{
if (Interlocked.Exchange(ref _stopped, 1) == 1)
{
return;
}
if (_cdp is ICdpConnectionEvents events)
{
events.Reconnected -= OnReconnected;
}
if (_subscription is not null)
{
_subscription.Dispose();
try
{
await _cdp.SendAsync("Page.stopScreencast", null, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is CdpDisconnectedException or BrowserUnavailableException or CdpCommandException)
{
// The browser is gone: nothing to stop.
}
}
_frames.Writer.TryComplete();
_acks.Writer.TryComplete();
await _lifetime.CancelAsync().ConfigureAwait(false);
if (_ackPump is not null)
{
try
{
await _ackPump.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
}
}
_log.Debug("Vista remota fermata.");
}
public async ValueTask DisposeAsync()
{
await StopAsync().ConfigureAwait(false);
_lifetime.Dispose();
}
private Task SendStartAsync(CancellationToken ct)
{
var o = _options ?? new ScreencastOptions();
return _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);
}
private void OnReconnected(object? sender, EventArgs e) =>
_ = Task.Run(async () =>
{
try
{
await SendStartAsync(_lifetime.Token).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_log.Warn("Vista remota: non è ripartita dopo il riavvio del browser.", ex);
}
});
private void OnFrame(JsonElement p)
{
if (!p.TryGetProperty("sessionId", out var sid) || !sid.TryGetInt32(out var sessionId))
{
return;
}
// Acknowledge first, from the pump: the browser sends the next frame only after the ack.
_acks.Writer.TryWrite(sessionId);
if (!p.TryGetProperty("data", out var data) || !data.TryGetBytesFromBase64(out var jpeg))
{
return;
}
var meta = p.TryGetProperty("metadata", out var m) ? m : default;
var frame = new ScreencastFrame(
jpeg,
Read(meta, "deviceWidth"),
Read(meta, "deviceHeight"),
Read(meta, "pageScaleFactor", 1),
Read(meta, "offsetTop"),
Read(meta, "scrollOffsetX"),
Read(meta, "scrollOffsetY"),
meta.ValueKind == JsonValueKind.Object && meta.TryGetProperty("timestamp", out var ts) && ts.TryGetDouble(out var t) ? t : null);
LastFrame = frame;
Interlocked.Increment(ref _framesReceived);
_frames.Writer.TryWrite(frame);
FrameReceived?.Invoke(this, frame);
}
private async Task AckPumpAsync(CancellationToken ct)
{
await foreach (var sessionId in _acks.Reader.ReadAllAsync(ct).ConfigureAwait(false))
{
try
{
await _cdp.SendAsync("Page.screencastFrameAck", new JsonObject { ["sessionId"] = sessionId }, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is CdpDisconnectedException or BrowserUnavailableException or CdpCommandException or TimeoutException)
{
// A lost ack only pauses frames until the next start.
}
}
}
private static double Read(JsonElement meta, string name, double fallback = 0) =>
meta.ValueKind == JsonValueKind.Object && meta.TryGetProperty(name, out var v) && v.TryGetDouble(out var d) ? d : fallback;
}