Skip to content

Commit efcb5e7

Browse files
[dotnet] Implement third-party Permissions module (#16414)
Co-authored-by: Nikolay Borisenko <[email protected]>
1 parent b446e16 commit efcb5e7

File tree

20 files changed

+365
-34
lines changed

20 files changed

+365
-34
lines changed

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,13 @@ namespace OpenQA.Selenium.BiDi;
2929

3030
public sealed class BiDi : IAsyncDisposable
3131
{
32-
private readonly Broker _broker;
33-
private readonly JsonSerializerOptions _jsonOptions;
32+
internal Broker Broker { get; }
33+
internal JsonSerializerOptions JsonOptions { get; }
3434
private readonly BiDiJsonSerializerContext _jsonContext;
3535

36-
private BiDi(string url)
36+
public JsonSerializerOptions DefaultBiDiOptions()
3737
{
38-
var uri = new Uri(url);
39-
40-
_jsonOptions = new JsonSerializerOptions
38+
return new JsonSerializerOptions
4139
{
4240
PropertyNameCaseInsensitive = true,
4341
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
@@ -60,20 +58,27 @@ private BiDi(string url)
6058
new WebExtensionConverter(this),
6159
}
6260
};
61+
}
62+
63+
private BiDi(string url)
64+
{
65+
var uri = new Uri(url);
6366

64-
_jsonContext = new BiDiJsonSerializerContext(_jsonOptions);
65-
66-
_broker = new Broker(this, uri, _jsonOptions);
67-
SessionModule = Module.Create<Session.SessionModule>(this, _broker, _jsonOptions, _jsonContext);
68-
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, _broker, _jsonOptions, _jsonContext);
69-
Browser = Module.Create<Browser.BrowserModule>(this, _broker, _jsonOptions, _jsonContext);
70-
Network = Module.Create<Network.NetworkModule>(this, _broker, _jsonOptions, _jsonContext);
71-
InputModule = Module.Create<Input.InputModule>(this, _broker, _jsonOptions, _jsonContext);
72-
Script = Module.Create<Script.ScriptModule>(this, _broker, _jsonOptions, _jsonContext);
73-
Log = Module.Create<Log.LogModule>(this, _broker, _jsonOptions, _jsonContext);
74-
Storage = Module.Create<Storage.StorageModule>(this, _broker, _jsonOptions, _jsonContext);
75-
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, _broker, _jsonOptions, _jsonContext);
76-
Emulation = Module.Create<Emulation.EmulationModule>(this, _broker, _jsonOptions, _jsonContext);
67+
JsonOptions = DefaultBiDiOptions();
68+
69+
_jsonContext = new BiDiJsonSerializerContext(JsonOptions);
70+
71+
Broker = new Broker(this, uri, JsonOptions);
72+
SessionModule = Module.Create<Session.SessionModule>(this, JsonOptions, _jsonContext);
73+
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, JsonOptions, _jsonContext);
74+
Browser = Module.Create<Browser.BrowserModule>(this, JsonOptions, _jsonContext);
75+
Network = Module.Create<Network.NetworkModule>(this, JsonOptions, _jsonContext);
76+
InputModule = Module.Create<Input.InputModule>(this, JsonOptions, _jsonContext);
77+
Script = Module.Create<Script.ScriptModule>(this, JsonOptions, _jsonContext);
78+
Log = Module.Create<Log.LogModule>(this, JsonOptions, _jsonContext);
79+
Storage = Module.Create<Storage.StorageModule>(this, JsonOptions, _jsonContext);
80+
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, JsonOptions, _jsonContext);
81+
Emulation = Module.Create<Emulation.EmulationModule>(this, JsonOptions, _jsonContext);
7782
}
7883

7984
internal Session.SessionModule SessionModule { get; }
@@ -105,7 +110,7 @@ public static async Task<BiDi> ConnectAsync(string url, BiDiOptions? options = n
105110
{
106111
var bidi = new BiDi(url);
107112

108-
await bidi._broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
113+
await bidi.Broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
109114

110115
return bidi;
111116
}
@@ -117,7 +122,7 @@ public Task EndAsync(Session.EndOptions? options = null)
117122

118123
public async ValueTask DisposeAsync()
119124
{
120-
await _broker.DisposeAsync().ConfigureAwait(false);
125+
await Broker.DisposeAsync().ConfigureAwait(false);
121126
GC.SuppressFinalize(this);
122127
}
123128
}

dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
21+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2023
using System.Threading.Tasks;
2124

2225
namespace OpenQA.Selenium.BiDi.Browser;
2326

2427
public sealed class BrowserModule : Module
2528
{
29+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
30+
2631
public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
2732
{
2833
return await Broker.ExecuteCommandAsync(new CloseCommand(), options, JsonContext.Browser_CloseCommand, JsonContext.Browser_CloseResult).ConfigureAwait(false);
@@ -73,4 +78,8 @@ public async Task<SetDownloadBehaviorResult> SetDownloadBehaviorDeniedAsync(SetD
7378

7479
return await Broker.ExecuteCommandAsync(new SetDownloadBehaviorCommand(@params), options, JsonContext.SetDownloadBehaviorCommand, JsonContext.SetDownloadBehaviorResult).ConfigureAwait(false);
7580
}
81+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
82+
{
83+
return new BiDiJsonSerializerContext(options);
84+
}
7685
}

dotnet/src/webdriver/BiDi/Browser/UserContext.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
namespace OpenQA.Selenium.BiDi.Browser;
2424

25-
public sealed class UserContext : IAsyncDisposable
25+
public sealed class UserContext : IEquatable<UserContext>, IAsyncDisposable
2626
{
2727
private readonly BiDi _bidi;
2828

@@ -44,15 +44,19 @@ public async ValueTask DisposeAsync()
4444
await RemoveAsync().ConfigureAwait(false);
4545
}
4646

47-
public override bool Equals(object? obj)
47+
public bool Equals(UserContext? other)
4848
{
49-
if (obj is UserContext userContextObj) return userContextObj.Id == Id;
49+
return other is not null && string.Equals(Id, other.Id, StringComparison.Ordinal);
50+
}
51+
5052

51-
return false;
53+
public override bool Equals(object? obj)
54+
{
55+
return Equals(obj as UserContext);
5256
}
5357

5458
public override int GetHashCode()
5559
{
56-
return Id.GetHashCode();
60+
return StringComparer.Ordinal.GetHashCode(Id);
5761
}
5862
}

dotnet/src/webdriver/BiDi/BrowsingContext/BrowsingContextModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
2021
using System;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
2124
using System.Threading.Tasks;
2225

2326
namespace OpenQA.Selenium.BiDi.BrowsingContext;
2427

2528
public sealed class BrowsingContextModule : Module
2629
{
30+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
31+
2732
public async Task<CreateResult> CreateAsync(ContextType type, CreateOptions? options = null)
2833
{
2934
var @params = new CreateParameters(type, options?.ReferenceContext, options?.Background, options?.UserContext);
@@ -247,4 +252,8 @@ public async Task<Subscription> OnUserPromptClosedAsync(Action<UserPromptClosedE
247252
{
248253
return await Broker.SubscribeAsync("browsingContext.userPromptClosed", handler, options, JsonContext.UserPromptClosedEventArgs).ConfigureAwait(false);
249254
}
255+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
256+
{
257+
return new BiDiJsonSerializerContext(options);
258+
}
250259
}

dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
21+
using System.Text.Json;
22+
using System.Text.Json.Serialization;
2023
using System.Threading.Tasks;
2124

2225
namespace OpenQA.Selenium.BiDi.Emulation;
2326

2427
public sealed class EmulationModule : Module
2528
{
29+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
30+
2631
public async Task<SetTimezoneOverrideResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
2732
{
2833
var @params = new SetTimezoneOverrideParameters(timezone, options?.Contexts, options?.UserContexts);
@@ -87,4 +92,8 @@ public async Task<SetGeolocationOverrideResult> SetGeolocationPositionErrorOverr
8792

8893
return await Broker.ExecuteCommandAsync(new SetGeolocationOverrideCommand(@params), options, JsonContext.SetGeolocationOverrideCommand, JsonContext.SetGeolocationOverrideResult).ConfigureAwait(false);
8994
}
95+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
96+
{
97+
return new BiDiJsonSerializerContext(options);
98+
}
9099
}

dotnet/src/webdriver/BiDi/Input/InputModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
2021
using System.Collections.Generic;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
2124
using System.Threading.Tasks;
2225

2326
namespace OpenQA.Selenium.BiDi.Input;
2427

2528
public sealed class InputModule : Module
2629
{
30+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
31+
2732
public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
2833
{
2934
var @params = new PerformActionsParameters(context, actions);
@@ -44,4 +49,8 @@ public async Task<SetFilesResult> SetFilesAsync(BrowsingContext.BrowsingContext
4449

4550
return await Broker.ExecuteCommandAsync(new SetFilesCommand(@params), options, JsonContext.SetFilesCommand, JsonContext.SetFilesResult).ConfigureAwait(false);
4651
}
52+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
53+
{
54+
return new BiDiJsonSerializerContext(options);
55+
}
4756
}

dotnet/src/webdriver/BiDi/Log/LogModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
2021
using System;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
2124
using System.Threading.Tasks;
2225

2326
namespace OpenQA.Selenium.BiDi.Log;
2427

2528
public sealed class LogModule : Module
2629
{
30+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
31+
2732
public async Task<Subscription> OnEntryAddedAsync(Func<LogEntry, Task> handler, SubscriptionOptions? options = null)
2833
{
2934
return await Broker.SubscribeAsync("log.entryAdded", handler, options, JsonContext.LogEntry).ConfigureAwait(false);
@@ -33,4 +38,8 @@ public async Task<Subscription> OnEntryAddedAsync(Action<LogEntry> handler, Subs
3338
{
3439
return await Broker.SubscribeAsync("log.entryAdded", handler, options, JsonContext.LogEntry).ConfigureAwait(false);
3540
}
41+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
42+
{
43+
return new BiDiJsonSerializerContext(options);
44+
}
3645
}

dotnet/src/webdriver/BiDi/Module.cs

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

20-
using OpenQA.Selenium.BiDi.Json;
2120
using System.Text.Json;
21+
using System.Text.Json.Serialization;
2222

2323
namespace OpenQA.Selenium.BiDi;
2424

2525
public abstract class Module
2626
{
2727
protected Broker Broker { get; private set; }
2828

29-
internal BiDiJsonSerializerContext JsonContext { get; private set; }
29+
internal JsonSerializerContext JsonContext { get; private set; }
3030

31-
protected virtual void Initialize(JsonSerializerOptions options) { }
31+
protected abstract JsonSerializerContext CreateJsonContext(JsonSerializerOptions options);
3232

33-
internal static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions, BiDiJsonSerializerContext context)
33+
public static TModule Create<TModule>(BiDi bidi, JsonSerializerOptions jsonOptions, JsonSerializerContext? cachedContext = null)
3434
where TModule : Module, new()
3535
{
3636
TModule module = new()
3737
{
38-
Broker = broker,
39-
JsonContext = context
38+
Broker = bidi.Broker,
4039
};
4140

42-
module.Initialize(jsonOptions);
41+
module.JsonContext = cachedContext ?? module.CreateJsonContext(jsonOptions);
4342

4443
return module;
4544
}

dotnet/src/webdriver/BiDi/Network/NetworkModule.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
// under the License.
1818
// </copyright>
1919

20+
using OpenQA.Selenium.BiDi.Json;
2021
using System;
2122
using System.Collections.Generic;
23+
using System.Text.Json;
24+
using System.Text.Json.Serialization;
2225
using System.Threading.Tasks;
2326

2427
namespace OpenQA.Selenium.BiDi.Network;
2528

2629
public sealed partial class NetworkModule : Module
2730
{
31+
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;
32+
2833
public async Task<Collector> AddDataCollectorAsync(IEnumerable<DataType> DataTypes, int MaxEncodedDataSize, AddDataCollectorOptions? options = null)
2934
{
3035
var @params = new AddDataCollectorParameters(DataTypes, MaxEncodedDataSize, options?.CollectorType, options?.Contexts, options?.UserContexts);
@@ -172,4 +177,8 @@ public async Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs
172177
{
173178
return await Broker.SubscribeAsync("network.authRequired", handler, options, JsonContext.AuthRequiredEventArgs).ConfigureAwait(false);
174179
}
180+
protected override JsonSerializerContext CreateJsonContext(JsonSerializerOptions options)
181+
{
182+
return new BiDiJsonSerializerContext(options);
183+
}
175184
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <copyright file="PermissionDescriptor.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
namespace OpenQA.Selenium.BiDi.Permissions;
21+
22+
public record PermissionDescriptor(string Name);

0 commit comments

Comments
 (0)