Skip to content

Commit ef67b61

Browse files
[dotnet] Annotate nullability on Target protocol (#15240)
* [dotnet] Annotate nullability on `Target` protocol * Handle nullability of adjacent `TargetInfo`
1 parent 572b087 commit ef67b61

File tree

6 files changed

+162
-130
lines changed

6 files changed

+162
-130
lines changed

dotnet/src/webdriver/DevTools/Target.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.ObjectModel;
2222
using System.Threading.Tasks;
2323

24+
#nullable enable
25+
2426
namespace OpenQA.Selenium.DevTools
2527
{
2628
/// <summary>
@@ -31,12 +33,12 @@ public abstract class Target
3133
/// <summary>
3234
/// Occurs when a target is detached.
3335
/// </summary>
34-
public event EventHandler<TargetDetachedEventArgs> TargetDetached;
36+
public event EventHandler<TargetDetachedEventArgs>? TargetDetached;
3537

3638
/// <summary>
3739
/// Occurs when a target is attached.
3840
/// </summary>
39-
public event EventHandler<TargetAttachedEventArgs> TargetAttached;
41+
public event EventHandler<TargetAttachedEventArgs>? TargetAttached;
4042

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

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

7173
/// <summary>
7274
/// Asynchronously sets the DevTools Protocol connection to automatically attach to new targets.

dotnet/src/webdriver/DevTools/TargetInfo.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,69 @@
1717
// under the License.
1818
// </copyright>
1919

20+
#nullable enable
21+
2022
namespace OpenQA.Selenium.DevTools
2123
{
2224
/// <summary>
2325
/// Represents information about the target of a DevTools Protocol command
2426
/// </summary>
2527
public class TargetInfo
2628
{
29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="TargetInfo"/> type.
31+
/// </summary>
32+
/// <param name="targetId">The ID of the target.</param>
33+
/// <param name="type">The type of target.</param>
34+
/// <param name="title">The title of the target.</param>
35+
/// <param name="url">The URL of the target.</param>
36+
/// <param name="isAttached">Whether the protocol is attached to the target.</param>
37+
/// <param name="openerId">The ID of the opener of the target.</param>
38+
/// <param name="browserContextId">The browser context ID.</param>
39+
public TargetInfo(string targetId, string type, string title, string url, bool isAttached, string? openerId, string? browserContextId)
40+
{
41+
this.TargetId = targetId;
42+
this.Type = type;
43+
this.Title = title;
44+
this.Url = url;
45+
this.IsAttached = isAttached;
46+
this.OpenerId = openerId;
47+
this.BrowserContextId = browserContextId;
48+
}
49+
2750
/// <summary>
2851
/// Gets the ID of the target.
2952
/// </summary>
30-
public string TargetId { get; internal set; }
53+
public string TargetId { get; }
3154

3255
/// <summary>
3356
/// Gets the type of target.
3457
/// </summary>
35-
public string Type { get; internal set; }
58+
public string Type { get; }
3659

3760
/// <summary>
3861
/// Gets the title of the target.
3962
/// </summary>
40-
public string Title { get; internal set; }
63+
public string Title { get; }
4164

4265
/// <summary>
4366
/// Gets the URL of the target.
4467
/// </summary>
45-
public string Url { get; internal set; }
68+
public string Url { get; }
4669

4770
/// <summary>
4871
/// Gets a value indicating if the protocol is attached to the target.
4972
/// </summary>
50-
public bool IsAttached { get; internal set; }
73+
public bool IsAttached { get; }
5174

5275
/// <summary>
5376
/// Gets the ID of the opener of the target.
5477
/// </summary>
55-
public string OpenerId { get; internal set; }
78+
public string? OpenerId { get; }
5679

5780
/// <summary>
5881
/// Gets the browser context ID.
5982
/// </summary>
60-
public string BrowserContextId { get; internal set; }
83+
public string? BrowserContextId { get; }
6184
}
6285
}

dotnet/src/webdriver/DevTools/v130/V130Target.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
using System.Collections.ObjectModel;
2424
using System.Threading.Tasks;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium.DevTools.V130
2729
{
2830
/// <summary>
2931
/// Class providing functionality for manipulating targets for version 130 of the DevTools Protocol
3032
/// </summary>
3133
public class V130Target : DevTools.Target
3234
{
33-
private TargetAdapter adapter;
35+
private readonly TargetAdapter adapter;
3436

3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="V130Target"/> class.
3739
/// </summary>
3840
/// <param name="adapter">The adapter for the Target domain.</param>
41+
/// <exception cref="ArgumentNullException">If <paramref name="adapter"/> is <see langword="null"/>.</exception>
3942
public V130Target(TargetAdapter adapter)
4043
{
41-
this.adapter = adapter;
44+
this.adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
4245
adapter.DetachedFromTarget += OnDetachedFromTarget;
4346
adapter.AttachedToTarget += OnAttachedToTarget;
4447
}
@@ -51,28 +54,26 @@ public V130Target(TargetAdapter adapter)
5154
/// contains the list of <see cref="TargetInfo"/> objects describing the
5255
/// targets available for this session.
5356
/// </returns>
54-
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(Object settings = null)
55-
57+
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(object? settings = null)
5658
{
57-
List<TargetInfo> targets = new List<TargetInfo>();
58-
if (settings == null)
59-
{
60-
settings = new GetTargetsCommandSettings();
61-
}
59+
settings ??= new GetTargetsCommandSettings();
60+
6261
var response = await adapter.GetTargets((GetTargetsCommandSettings)settings).ConfigureAwait(false);
62+
63+
List<TargetInfo> targets = new List<TargetInfo>(response.TargetInfos.Length);
6364
for (int i = 0; i < response.TargetInfos.Length; i++)
6465
{
6566
var targetInfo = response.TargetInfos[i];
66-
var mapped = new TargetInfo()
67-
{
68-
TargetId = targetInfo.TargetId,
69-
Title = targetInfo.Title,
70-
Type = targetInfo.Type,
71-
Url = targetInfo.Url,
72-
OpenerId = targetInfo.OpenerId,
73-
BrowserContextId = targetInfo.BrowserContextId,
74-
IsAttached = targetInfo.Attached
75-
};
67+
var mapped = new TargetInfo
68+
(
69+
targetId: targetInfo.TargetId,
70+
title: targetInfo.Title,
71+
type: targetInfo.Type,
72+
url: targetInfo.Url,
73+
openerId: targetInfo.OpenerId,
74+
browserContextId: targetInfo.BrowserContextId,
75+
isAttached: targetInfo.Attached
76+
);
7677
targets.Add(mapped);
7778
}
7879

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

120-
private void OnDetachedFromTarget(object sender, DetachedFromTargetEventArgs e)
121+
private void OnDetachedFromTarget(object? sender, DetachedFromTargetEventArgs e)
121122
{
122123
this.OnTargetDetached(new TargetDetachedEventArgs(e.SessionId, e.TargetId));
123124
}
124125

125-
private void OnAttachedToTarget(object sender, AttachedToTargetEventArgs e)
126+
private void OnAttachedToTarget(object? sender, AttachedToTargetEventArgs e)
126127
{
127128
var targetInfo = e.TargetInfo == null ? null : new TargetInfo
128-
{
129-
BrowserContextId = e.TargetInfo.BrowserContextId,
130-
IsAttached = e.TargetInfo.Attached,
131-
OpenerId = e.TargetInfo.OpenerId,
132-
TargetId = e.TargetInfo.TargetId,
133-
Title = e.TargetInfo.Title,
134-
Type = e.TargetInfo.Type,
135-
Url = e.TargetInfo.Url
136-
};
129+
(
130+
browserContextId: e.TargetInfo.BrowserContextId,
131+
isAttached: e.TargetInfo.Attached,
132+
openerId: e.TargetInfo.OpenerId,
133+
targetId: e.TargetInfo.TargetId,
134+
title: e.TargetInfo.Title,
135+
type: e.TargetInfo.Type,
136+
url: e.TargetInfo.Url
137+
);
137138

138139
this.OnTargetAttached(new TargetAttachedEventArgs
139140
(

dotnet/src/webdriver/DevTools/v131/V131Target.cs

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,25 @@
2323
using System.Collections.ObjectModel;
2424
using System.Threading.Tasks;
2525

26+
#nullable enable
27+
2628
namespace OpenQA.Selenium.DevTools.V131
2729
{
2830
/// <summary>
2931
/// Class providing functionality for manipulating targets for version 131 of the DevTools Protocol
3032
/// </summary>
3133
public class V131Target : DevTools.Target
3234
{
33-
private TargetAdapter adapter;
35+
private readonly TargetAdapter adapter;
3436

3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="V131Target"/> class.
3739
/// </summary>
3840
/// <param name="adapter">The adapter for the Target domain.</param>
41+
/// <exception cref="ArgumentNullException">If <paramref name="adapter"/> is <see langword="null"/>.</exception>
3942
public V131Target(TargetAdapter adapter)
4043
{
41-
this.adapter = adapter;
44+
this.adapter = adapter ?? throw new ArgumentNullException(nameof(adapter));
4245
adapter.DetachedFromTarget += OnDetachedFromTarget;
4346
adapter.AttachedToTarget += OnAttachedToTarget;
4447
}
@@ -51,28 +54,26 @@ public V131Target(TargetAdapter adapter)
5154
/// contains the list of <see cref="TargetInfo"/> objects describing the
5255
/// targets available for this session.
5356
/// </returns>
54-
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(Object settings = null)
55-
57+
public override async Task<ReadOnlyCollection<TargetInfo>> GetTargets(object? settings = null)
5658
{
57-
List<TargetInfo> targets = new List<TargetInfo>();
58-
if (settings == null)
59-
{
60-
settings = new GetTargetsCommandSettings();
61-
}
59+
settings ??= new GetTargetsCommandSettings();
60+
6261
var response = await adapter.GetTargets((GetTargetsCommandSettings)settings).ConfigureAwait(false);
62+
63+
List<TargetInfo> targets = new List<TargetInfo>(response.TargetInfos.Length);
6364
for (int i = 0; i < response.TargetInfos.Length; i++)
6465
{
6566
var targetInfo = response.TargetInfos[i];
66-
var mapped = new TargetInfo()
67-
{
68-
TargetId = targetInfo.TargetId,
69-
Title = targetInfo.Title,
70-
Type = targetInfo.Type,
71-
Url = targetInfo.Url,
72-
OpenerId = targetInfo.OpenerId,
73-
BrowserContextId = targetInfo.BrowserContextId,
74-
IsAttached = targetInfo.Attached
75-
};
67+
var mapped = new TargetInfo
68+
(
69+
targetId: targetInfo.TargetId,
70+
title: targetInfo.Title,
71+
type: targetInfo.Type,
72+
url: targetInfo.Url,
73+
openerId: targetInfo.OpenerId,
74+
browserContextId: targetInfo.BrowserContextId,
75+
isAttached: targetInfo.Attached
76+
);
7677
targets.Add(mapped);
7778
}
7879

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

120-
private void OnDetachedFromTarget(object sender, DetachedFromTargetEventArgs e)
121+
private void OnDetachedFromTarget(object? sender, DetachedFromTargetEventArgs e)
121122
{
122123
this.OnTargetDetached(new TargetDetachedEventArgs(e.SessionId, e.TargetId));
123124
}
124125

125-
private void OnAttachedToTarget(object sender, AttachedToTargetEventArgs e)
126+
private void OnAttachedToTarget(object? sender, AttachedToTargetEventArgs e)
126127
{
127128
var targetInfo = e.TargetInfo == null ? null : new TargetInfo
128-
{
129-
BrowserContextId = e.TargetInfo.BrowserContextId,
130-
IsAttached = e.TargetInfo.Attached,
131-
OpenerId = e.TargetInfo.OpenerId,
132-
TargetId = e.TargetInfo.TargetId,
133-
Title = e.TargetInfo.Title,
134-
Type = e.TargetInfo.Type,
135-
Url = e.TargetInfo.Url
136-
};
129+
(
130+
browserContextId: e.TargetInfo.BrowserContextId,
131+
isAttached: e.TargetInfo.Attached,
132+
openerId: e.TargetInfo.OpenerId,
133+
targetId: e.TargetInfo.TargetId,
134+
title: e.TargetInfo.Title,
135+
type: e.TargetInfo.Type,
136+
url: e.TargetInfo.Url
137+
);
137138

138139
this.OnTargetAttached(new TargetAttachedEventArgs
139140
(

0 commit comments

Comments
 (0)