Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions dotnet/src/webdriver/BiDi/BiDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ namespace OpenQA.Selenium.BiDi;

public sealed class BiDi : IAsyncDisposable
{
private readonly Broker _broker;
private readonly JsonSerializerOptions _jsonOptions;
internal Broker Broker { get; }
internal JsonSerializerOptions JsonOptions { get; }
private readonly BiDiJsonSerializerContext _jsonContext;

private BiDi(string url)
public JsonSerializerOptions DefaultBiDiOptions()
{
var uri = new Uri(url);

_jsonOptions = new JsonSerializerOptions
return new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Expand All @@ -61,20 +59,27 @@ private BiDi(string url)
new WebExtensionConverter(this),
}
};
}

private BiDi(string url)
{
var uri = new Uri(url);

_jsonContext = new BiDiJsonSerializerContext(_jsonOptions);

_broker = new Broker(this, uri, _jsonOptions);
SessionModule = Module.Create<Session.SessionModule>(this, _broker, _jsonOptions, _jsonContext);
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, _broker, _jsonOptions, _jsonContext);
Browser = Module.Create<Browser.BrowserModule>(this, _broker, _jsonOptions, _jsonContext);
Network = Module.Create<Network.NetworkModule>(this, _broker, _jsonOptions, _jsonContext);
InputModule = Module.Create<Input.InputModule>(this, _broker, _jsonOptions, _jsonContext);
Script = Module.Create<Script.ScriptModule>(this, _broker, _jsonOptions, _jsonContext);
Log = Module.Create<Log.LogModule>(this, _broker, _jsonOptions, _jsonContext);
Storage = Module.Create<Storage.StorageModule>(this, _broker, _jsonOptions, _jsonContext);
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, _broker, _jsonOptions, _jsonContext);
Emulation = Module.Create<Emulation.EmulationModule>(this, _broker, _jsonOptions, _jsonContext);
JsonOptions = DefaultBiDiOptions();

_jsonContext = new BiDiJsonSerializerContext(JsonOptions);

Broker = new Broker(this, uri, JsonOptions);
SessionModule = Module.Create<Session.SessionModule>(this, JsonOptions, _jsonContext);
BrowsingContext = Module.Create<BrowsingContext.BrowsingContextModule>(this, JsonOptions, _jsonContext);
Browser = Module.Create<Browser.BrowserModule>(this, JsonOptions, _jsonContext);
Network = Module.Create<Network.NetworkModule>(this, JsonOptions, _jsonContext);
InputModule = Module.Create<Input.InputModule>(this, JsonOptions, _jsonContext);
Script = Module.Create<Script.ScriptModule>(this, JsonOptions, _jsonContext);
Log = Module.Create<Log.LogModule>(this, JsonOptions, _jsonContext);
Storage = Module.Create<Storage.StorageModule>(this, JsonOptions, _jsonContext);
WebExtension = Module.Create<WebExtension.WebExtensionModule>(this, JsonOptions, _jsonContext);
Emulation = Module.Create<Emulation.EmulationModule>(this, JsonOptions, _jsonContext);
}

internal Session.SessionModule SessionModule { get; }
Expand Down Expand Up @@ -106,7 +111,7 @@ public static async Task<BiDi> ConnectAsync(string url, BiDiOptions? options = n
{
var bidi = new BiDi(url);

await bidi._broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);
await bidi.Broker.ConnectAsync(CancellationToken.None).ConfigureAwait(false);

return bidi;
}
Expand All @@ -118,7 +123,7 @@ public Task EndAsync(Session.EndOptions? options = null)

public async ValueTask DisposeAsync()
{
await _broker.DisposeAsync().ConfigureAwait(false);
await Broker.DisposeAsync().ConfigureAwait(false);
GC.SuppressFinalize(this);
}
}
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Browser/BrowserModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OpenQA.Selenium.BiDi.Browser;

public sealed class BrowserModule : Module
public sealed class BrowserModule : CoreModule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why CoreModule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me remind: we made Module class ctor parameterless, especially for external modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have 2 requirements:

  1. Each module should maintain its own serialization
  2. We want the first-party modules to share the same serialization

For that reason, they need to have some shared logic.

{
public async Task<CloseResult> CloseAsync(CloseOptions? options = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace OpenQA.Selenium.BiDi.BrowsingContext;

public sealed class BrowsingContextModule : Module
public sealed class BrowsingContextModule : CoreModule
{
public async Task<CreateResult> CreateAsync(ContextType type, CreateOptions? options = null)
{
Expand Down
34 changes: 34 additions & 0 deletions dotnet/src/webdriver/BiDi/CoreModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <copyright file="CoreModule.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi;

public abstract class CoreModule : Module
{
internal new BiDiJsonSerializerContext JsonContext => (BiDiJsonSerializerContext)base.JsonContext;

protected override JsonSerializerContext Initialize(JsonSerializerOptions options)
{
return new BiDiJsonSerializerContext(options);
}
}
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Emulation/EmulationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OpenQA.Selenium.BiDi.Emulation;

public sealed class EmulationModule : Module
public sealed class EmulationModule : CoreModule
{
public async Task<SetTimezoneOverrideResult> SetTimezoneOverrideAsync(string? timezone, SetTimezoneOverrideOptions? options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Input/InputModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace OpenQA.Selenium.BiDi.Input;

public sealed class InputModule : Module
public sealed class InputModule : CoreModule
{
public async Task<PerformActionsResult> PerformActionsAsync(BrowsingContext.BrowsingContext context, IEnumerable<SourceActions> actions, PerformActionsOptions? options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Log/LogModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace OpenQA.Selenium.BiDi.Log;

public sealed class LogModule : Module
public sealed class LogModule : CoreModule
{
public async Task<Subscription> OnEntryAddedAsync(Func<LogEntry, Task> handler, SubscriptionOptions? options = null)
{
Expand Down
13 changes: 6 additions & 7 deletions dotnet/src/webdriver/BiDi/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@
// </copyright>

using OpenQA.Selenium.BiDi.Communication;
using OpenQA.Selenium.BiDi.Communication.Json;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.BiDi;

public abstract class Module
{
protected Broker Broker { get; private set; }

internal BiDiJsonSerializerContext JsonContext { get; private set; }
internal JsonSerializerContext JsonContext { get; private set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not ready to generalize it in this PR. Still not clear how to move on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an example in this PR’s description, of how to move forward with these changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though making Module.Create(...) to public would be enough, please give me time to review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Observed it is theoretical changes. Let be practicable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, except for the shared JSON context introduced in #16402

It is not theoretical changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came to the following:

  1. BiDi has new method AsModule<T>()
  2. PermissionsModule creates its own context based on options (and keep it as private field)

Then changes looks simple.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel introducing new AsModule<T>() method will be good for us:

  • we may introduce cached modules
  • it negotiates use of internal properties

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, AsModule<T> is required. Built-in modules are cached, third-party modules also want to be cached per bidi instance. Let's do it (here or in separate PR).


protected virtual void Initialize(JsonSerializerOptions options) { }
protected abstract JsonSerializerContext Initialize(JsonSerializerOptions options);
Copy link
Member

@nvborisenko nvborisenko Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should choose between void Initialize(...) and JsonSerializerContext ConfigureJsonContext(...). I vote for void, letting each module to decide whatever it wants. Input parameters might be changed in future (breaking change), but I think it will be another story.


internal static TModule Create<TModule>(BiDi bidi, Broker broker, JsonSerializerOptions jsonOptions, BiDiJsonSerializerContext context)
public static TModule Create<TModule>(BiDi bidi, JsonSerializerOptions jsonOptions, JsonSerializerContext? cachedContext = null)
where TModule : Module, new()
{
TModule module = new()
{
Broker = broker,
JsonContext = context
Broker = bidi.Broker,
};

module.Initialize(jsonOptions);
module.JsonContext = cachedContext ?? module.Initialize(jsonOptions);

return module;
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Network/NetworkModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OpenQA.Selenium.BiDi.Network;

public sealed partial class NetworkModule : Module
public sealed partial class NetworkModule : CoreModule
{
public async Task<Collector> AddDataCollectorAsync(IEnumerable<DataType> DataTypes, int MaxEncodedDataSize, AddDataCollectorOptions? options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Script/ScriptModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OpenQA.Selenium.BiDi.Script;

public sealed class ScriptModule : Module
public sealed class ScriptModule : CoreModule
{
public async Task<EvaluateResult> EvaluateAsync(string expression, bool awaitPromise, Target target, EvaluateOptions? options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Session/SessionModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace OpenQA.Selenium.BiDi.Session;

internal sealed class SessionModule : Module
internal sealed class SessionModule : CoreModule
{
public async Task<StatusResult> StatusAsync(StatusOptions? options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Storage/StorageModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OpenQA.Selenium.BiDi.Storage;

public sealed class StorageModule : Module
public sealed class StorageModule : CoreModule
{
public async Task<GetCookiesResult> GetCookiesAsync(GetCookiesOptions? options = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace OpenQA.Selenium.BiDi.WebExtension;

public sealed class WebExtensionModule : Module
public sealed class WebExtensionModule : CoreModule
{
public async Task<InstallResult> InstallAsync(ExtensionData extensionData, InstallOptions? options = null)
{
Expand Down