Skip to content

Commit

Permalink
[dotnet] Annotate nullability on Target protocol (#15240)
Browse files Browse the repository at this point in the history
* [dotnet] Annotate nullability on `Target` protocol

* Handle nullability of adjacent `TargetInfo`
  • Loading branch information
RenderMichael authored Feb 6, 2025
1 parent 572b087 commit ef67b61
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 130 deletions.
10 changes: 6 additions & 4 deletions dotnet/src/webdriver/DevTools/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using System.Collections.ObjectModel;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
Expand All @@ -31,12 +33,12 @@ public abstract class Target
/// <summary>
/// Occurs when a target is detached.
/// </summary>
public event EventHandler<TargetDetachedEventArgs> TargetDetached;
public event EventHandler<TargetDetachedEventArgs>? TargetDetached;

/// <summary>
/// Occurs when a target is attached.
/// </summary>
public event EventHandler<TargetAttachedEventArgs> TargetAttached;
public event EventHandler<TargetAttachedEventArgs>? TargetAttached;

/// <summary>
/// Asynchronously gets the targets available for this session.
Expand All @@ -46,7 +48,7 @@ public abstract class Target
/// contains the list of <see cref="TargetInfo"/> objects describing the
/// targets available for this session.
/// </returns>
public abstract Task<ReadOnlyCollection<TargetInfo>> GetTargets(Object settings = null);
public abstract Task<ReadOnlyCollection<TargetInfo>> GetTargets(object? settings = null);

/// <summary>
/// Asynchronously attaches to a target.
Expand All @@ -66,7 +68,7 @@ public abstract class Target
/// <returns>
/// A task representing the asynchronous detach operation.
/// </returns>
public abstract Task DetachFromTarget(string sessionId = null, string targetId = null);
public abstract Task DetachFromTarget(string? sessionId = null, string? targetId = null);

/// <summary>
/// Asynchronously sets the DevTools Protocol connection to automatically attach to new targets.
Expand Down
37 changes: 30 additions & 7 deletions dotnet/src/webdriver/DevTools/TargetInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,69 @@
// under the License.
// </copyright>

#nullable enable

namespace OpenQA.Selenium.DevTools
{
/// <summary>
/// Represents information about the target of a DevTools Protocol command
/// </summary>
public class TargetInfo
{
/// <summary>
/// Initializes a new instance of the <see cref="TargetInfo"/> type.
/// </summary>
/// <param name="targetId">The ID of the target.</param>
/// <param name="type">The type of target.</param>
/// <param name="title">The title of the target.</param>
/// <param name="url">The URL of the target.</param>
/// <param name="isAttached">Whether the protocol is attached to the target.</param>
/// <param name="openerId">The ID of the opener of the target.</param>
/// <param name="browserContextId">The browser context ID.</param>
public TargetInfo(string targetId, string type, string title, string url, bool isAttached, string? openerId, string? browserContextId)
{
this.TargetId = targetId;
this.Type = type;
this.Title = title;
this.Url = url;
this.IsAttached = isAttached;
this.OpenerId = openerId;
this.BrowserContextId = browserContextId;
}

/// <summary>
/// Gets the ID of the target.
/// </summary>
public string TargetId { get; internal set; }
public string TargetId { get; }

/// <summary>
/// Gets the type of target.
/// </summary>
public string Type { get; internal set; }
public string Type { get; }

/// <summary>
/// Gets the title of the target.
/// </summary>
public string Title { get; internal set; }
public string Title { get; }

/// <summary>
/// Gets the URL of the target.
/// </summary>
public string Url { get; internal set; }
public string Url { get; }

/// <summary>
/// Gets a value indicating if the protocol is attached to the target.
/// </summary>
public bool IsAttached { get; internal set; }
public bool IsAttached { get; }

/// <summary>
/// Gets the ID of the opener of the target.
/// </summary>
public string OpenerId { get; internal set; }
public string? OpenerId { get; }

/// <summary>
/// Gets the browser context ID.
/// </summary>
public string BrowserContextId { get; internal set; }
public string? BrowserContextId { get; }
}
}
63 changes: 32 additions & 31 deletions dotnet/src/webdriver/DevTools/v130/V130Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@
using System.Collections.ObjectModel;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium.DevTools.V130
{
/// <summary>
/// Class providing functionality for manipulating targets for version 130 of the DevTools Protocol
/// </summary>
public class V130Target : DevTools.Target
{
private TargetAdapter adapter;
private readonly TargetAdapter adapter;

/// <summary>
/// Initializes a new instance of the <see cref="V130Target"/> class.
/// </summary>
/// <param name="adapter">The adapter for the Target domain.</param>
/// <exception cref="ArgumentNullException">If <paramref name="adapter"/> is <see langword="null"/>.</exception>
public V130Target(TargetAdapter adapter)
{
this.adapter = adapter;
this.adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
adapter.DetachedFromTarget += OnDetachedFromTarget;
adapter.AttachedToTarget += OnAttachedToTarget;
}
Expand All @@ -51,28 +54,26 @@ public V130Target(TargetAdapter adapter)
/// contains the list of <see cref="TargetInfo"/> objects describing the
/// targets available for this session.
/// </returns>
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(Object settings = null)

public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(object? settings = null)
{
List<TargetInfo> targets = new List<TargetInfo>();
if (settings == null)
{
settings = new GetTargetsCommandSettings();
}
settings ??= new GetTargetsCommandSettings();

var response = await adapter.GetTargets((GetTargetsCommandSettings)settings).ConfigureAwait(false);

List<TargetInfo> targets = new List<TargetInfo>(response.TargetInfos.Length);
for (int i = 0; i < response.TargetInfos.Length; i++)
{
var targetInfo = response.TargetInfos[i];
var mapped = new TargetInfo()
{
TargetId = targetInfo.TargetId,
Title = targetInfo.Title,
Type = targetInfo.Type,
Url = targetInfo.Url,
OpenerId = targetInfo.OpenerId,
BrowserContextId = targetInfo.BrowserContextId,
IsAttached = targetInfo.Attached
};
var mapped = new TargetInfo
(
targetId: targetInfo.TargetId,
title: targetInfo.Title,
type: targetInfo.Type,
url: targetInfo.Url,
openerId: targetInfo.OpenerId,
browserContextId: targetInfo.BrowserContextId,
isAttached: targetInfo.Attached
);
targets.Add(mapped);
}

Expand All @@ -99,7 +100,7 @@ public override async Task<string> AttachToTarget(string targetId)
/// <param name="sessionId">The ID of the session of the target from which to detach.</param>
/// <param name="targetId">The ID of the target from which to detach.</param>
/// <returns>A task representing the asynchronous detach operation.</returns>
public override async Task DetachFromTarget(string sessionId = null, string targetId = null)
public override async Task DetachFromTarget(string? sessionId = null, string? targetId = null)
{
await adapter.DetachFromTarget(new DetachFromTargetCommandSettings()
{
Expand All @@ -117,23 +118,23 @@ public override async Task SetAutoAttach()
await adapter.SetAutoAttach(new SetAutoAttachCommandSettings() { AutoAttach = true, WaitForDebuggerOnStart = false, Flatten = true }).ConfigureAwait(false);
}

private void OnDetachedFromTarget(object sender, DetachedFromTargetEventArgs e)
private void OnDetachedFromTarget(object? sender, DetachedFromTargetEventArgs e)
{
this.OnTargetDetached(new TargetDetachedEventArgs(e.SessionId, e.TargetId));
}

private void OnAttachedToTarget(object sender, AttachedToTargetEventArgs e)
private void OnAttachedToTarget(object? sender, AttachedToTargetEventArgs e)
{
var targetInfo = e.TargetInfo == null ? null : new TargetInfo
{
BrowserContextId = e.TargetInfo.BrowserContextId,
IsAttached = e.TargetInfo.Attached,
OpenerId = e.TargetInfo.OpenerId,
TargetId = e.TargetInfo.TargetId,
Title = e.TargetInfo.Title,
Type = e.TargetInfo.Type,
Url = e.TargetInfo.Url
};
(
browserContextId: e.TargetInfo.BrowserContextId,
isAttached: e.TargetInfo.Attached,
openerId: e.TargetInfo.OpenerId,
targetId: e.TargetInfo.TargetId,
title: e.TargetInfo.Title,
type: e.TargetInfo.Type,
url: e.TargetInfo.Url
);

this.OnTargetAttached(new TargetAttachedEventArgs
(
Expand Down
63 changes: 32 additions & 31 deletions dotnet/src/webdriver/DevTools/v131/V131Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,25 @@
using System.Collections.ObjectModel;
using System.Threading.Tasks;

#nullable enable

namespace OpenQA.Selenium.DevTools.V131
{
/// <summary>
/// Class providing functionality for manipulating targets for version 131 of the DevTools Protocol
/// </summary>
public class V131Target : DevTools.Target
{
private TargetAdapter adapter;
private readonly TargetAdapter adapter;

/// <summary>
/// Initializes a new instance of the <see cref="V131Target"/> class.
/// </summary>
/// <param name="adapter">The adapter for the Target domain.</param>
/// <exception cref="ArgumentNullException">If <paramref name="adapter"/> is <see langword="null"/>.</exception>
public V131Target(TargetAdapter adapter)
{
this.adapter = adapter;
this.adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
adapter.DetachedFromTarget += OnDetachedFromTarget;
adapter.AttachedToTarget += OnAttachedToTarget;
}
Expand All @@ -51,28 +54,26 @@ public V131Target(TargetAdapter adapter)
/// contains the list of <see cref="TargetInfo"/> objects describing the
/// targets available for this session.
/// </returns>
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(Object settings = null)

public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(object? settings = null)
{
List<TargetInfo> targets = new List<TargetInfo>();
if (settings == null)
{
settings = new GetTargetsCommandSettings();
}
settings ??= new GetTargetsCommandSettings();

var response = await adapter.GetTargets((GetTargetsCommandSettings)settings).ConfigureAwait(false);

List<TargetInfo> targets = new List<TargetInfo>(response.TargetInfos.Length);
for (int i = 0; i < response.TargetInfos.Length; i++)
{
var targetInfo = response.TargetInfos[i];
var mapped = new TargetInfo()
{
TargetId = targetInfo.TargetId,
Title = targetInfo.Title,
Type = targetInfo.Type,
Url = targetInfo.Url,
OpenerId = targetInfo.OpenerId,
BrowserContextId = targetInfo.BrowserContextId,
IsAttached = targetInfo.Attached
};
var mapped = new TargetInfo
(
targetId: targetInfo.TargetId,
title: targetInfo.Title,
type: targetInfo.Type,
url: targetInfo.Url,
openerId: targetInfo.OpenerId,
browserContextId: targetInfo.BrowserContextId,
isAttached: targetInfo.Attached
);
targets.Add(mapped);
}

Expand All @@ -99,7 +100,7 @@ public override async Task<string> AttachToTarget(string targetId)
/// <param name="sessionId">The ID of the session of the target from which to detach.</param>
/// <param name="targetId">The ID of the target from which to detach.</param>
/// <returns>A task representing the asynchronous detach operation.</returns>
public override async Task DetachFromTarget(string sessionId = null, string targetId = null)
public override async Task DetachFromTarget(string? sessionId = null, string? targetId = null)
{
await adapter.DetachFromTarget(new DetachFromTargetCommandSettings()
{
Expand All @@ -117,23 +118,23 @@ public override async Task SetAutoAttach()
await adapter.SetAutoAttach(new SetAutoAttachCommandSettings() { AutoAttach = true, WaitForDebuggerOnStart = false, Flatten = true }).ConfigureAwait(false);
}

private void OnDetachedFromTarget(object sender, DetachedFromTargetEventArgs e)
private void OnDetachedFromTarget(object? sender, DetachedFromTargetEventArgs e)
{
this.OnTargetDetached(new TargetDetachedEventArgs(e.SessionId, e.TargetId));
}

private void OnAttachedToTarget(object sender, AttachedToTargetEventArgs e)
private void OnAttachedToTarget(object? sender, AttachedToTargetEventArgs e)
{
var targetInfo = e.TargetInfo == null ? null : new TargetInfo
{
BrowserContextId = e.TargetInfo.BrowserContextId,
IsAttached = e.TargetInfo.Attached,
OpenerId = e.TargetInfo.OpenerId,
TargetId = e.TargetInfo.TargetId,
Title = e.TargetInfo.Title,
Type = e.TargetInfo.Type,
Url = e.TargetInfo.Url
};
(
browserContextId: e.TargetInfo.BrowserContextId,
isAttached: e.TargetInfo.Attached,
openerId: e.TargetInfo.OpenerId,
targetId: e.TargetInfo.TargetId,
title: e.TargetInfo.Title,
type: e.TargetInfo.Type,
url: e.TargetInfo.Url
);

this.OnTargetAttached(new TargetAttachedEventArgs
(
Expand Down
Loading

0 comments on commit ef67b61

Please sign in to comment.