Skip to content

[dotnet] [bidi] Implement Permissions extension #15425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 19 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
70 changes: 50 additions & 20 deletions dotnet/src/webdriver/BiDi/BiDi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
// under the License.
// </copyright>

using OpenQA.Selenium.BiDi.Communication;
using OpenQA.Selenium.BiDi.Communication.Json;
using OpenQA.Selenium.BiDi.Communication.Json.Converters;
using System;
using System.Threading.Tasks;
using OpenQA.Selenium.BiDi.Communication;

namespace OpenQA.Selenium.BiDi;

public class BiDi : IAsyncDisposable
{
private readonly Broker _broker;
private readonly bool _ownsConnection;
protected BiDiConnection BiDiConnection { get; }

private readonly Lazy<Modules.Session.SessionModule> _sessionModule;
private readonly Lazy<Modules.BrowsingContext.BrowsingContextModule> _browsingContextModule;
private readonly Lazy<Modules.Browser.BrowserModule> _browserModule;
private readonly Lazy<Modules.Network.NetworkModule> _networkModule;
Expand All @@ -36,23 +38,42 @@ public class BiDi : IAsyncDisposable
private readonly Lazy<Modules.Log.LogModule> _logModule;
private readonly Lazy<Modules.Storage.StorageModule> _storageModule;

internal BiDi(string url)
protected BiDi(BiDiConnection connection, bool disposeConnection) : this()
{
_ownsConnection = disposeConnection;
BiDiConnection = connection;
AddBiDiModuleJsonInfo(connection);
}

private BiDi()
{
_browsingContextModule = new Lazy<Modules.BrowsingContext.BrowsingContextModule>(() => new Modules.BrowsingContext.BrowsingContextModule(BiDiConnection));
_browserModule = new Lazy<Modules.Browser.BrowserModule>(() => new Modules.Browser.BrowserModule(BiDiConnection));
_networkModule = new Lazy<Modules.Network.NetworkModule>(() => new Modules.Network.NetworkModule(BiDiConnection));
_inputModule = new Lazy<Modules.Input.InputModule>(() => new Modules.Input.InputModule(BiDiConnection));
_scriptModule = new Lazy<Modules.Script.ScriptModule>(() => new Modules.Script.ScriptModule(BiDiConnection));
_logModule = new Lazy<Modules.Log.LogModule>(() => new Modules.Log.LogModule(BiDiConnection));
_storageModule = new Lazy<Modules.Storage.StorageModule>(() => new Modules.Storage.StorageModule(BiDiConnection));
}

private BiDiConnection AddBiDiModuleJsonInfo(BiDiConnection connection)
{
var uri = new Uri(url);

_broker = new Broker(this, uri);

_sessionModule = new Lazy<Modules.Session.SessionModule>(() => new Modules.Session.SessionModule(_broker));
_browsingContextModule = new Lazy<Modules.BrowsingContext.BrowsingContextModule>(() => new Modules.BrowsingContext.BrowsingContextModule(_broker));
_browserModule = new Lazy<Modules.Browser.BrowserModule>(() => new Modules.Browser.BrowserModule(_broker));
_networkModule = new Lazy<Modules.Network.NetworkModule>(() => new Modules.Network.NetworkModule(_broker));
_inputModule = new Lazy<Modules.Input.InputModule>(() => new Modules.Input.InputModule(_broker));
_scriptModule = new Lazy<Modules.Script.ScriptModule>(() => new Modules.Script.ScriptModule(_broker));
_logModule = new Lazy<Modules.Log.LogModule>(() => new Modules.Log.LogModule(_broker));
_storageModule = new Lazy<Modules.Storage.StorageModule>(() => new Modules.Storage.StorageModule(_broker));
connection.AddSerializerContextAndConverters(BiDiJsonSerializerContext.Default,
[
new BrowsingContextConverter(this),
new BrowserUserContextConverter(this),
new InterceptConverter(this),
new RequestConverter(this),
new HandleConverter(this),
new InternalIdConverter(this),
new PreloadScriptConverter(this),
new RealmConverter(this),
]);

return connection;
}

internal Modules.Session.SessionModule SessionModule => _sessionModule.Value;
internal Modules.Session.SessionModule SessionModule => BiDiConnection.SessionModule;
public Modules.BrowsingContext.BrowsingContextModule BrowsingContext => _browsingContextModule.Value;
public Modules.Browser.BrowserModule Browser => _browserModule.Value;
public Modules.Network.NetworkModule Network => _networkModule.Value;
Expand All @@ -68,13 +89,19 @@ internal BiDi(string url)

public static async Task<BiDi> ConnectAsync(string url)
{
var bidi = new BiDi(url);
var connection = new BiDiConnection(new Uri(url));
var bidi = new BiDi(connection, disposeConnection: true);

await bidi._broker.ConnectAsync(default).ConfigureAwait(false);
await bidi.BiDiConnection.ConnectAsync(default).ConfigureAwait(false);

return bidi;
}

public static BiDi Attach(BiDiConnection connection, bool disposeConnection = false)
{
return new BiDi(connection, disposeConnection);
}

public Task EndAsync(Modules.Session.EndOptions? options = null)
{
return SessionModule.EndAsync(options);
Expand All @@ -88,6 +115,9 @@ public async ValueTask DisposeAsync()

protected virtual async ValueTask DisposeAsyncCore()
{
await _broker.DisposeAsync().ConfigureAwait(false);
if (_ownsConnection)
{
await BiDiConnection.DisposeAsync().ConfigureAwait(false);
}
}
}
58 changes: 58 additions & 0 deletions dotnet/src/webdriver/BiDi/Communication/BiDiBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <copyright file="BiDiBuilder.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 System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Communication;

public class BiDiBuilder
{
private readonly BiDiConnection _connection;
private readonly BiDi _coreBidi;
public List<Modules.Module> Extensions { get; } = [];

public BiDiBuilder(string url) : this(new Uri(url))
{
}

public BiDiBuilder(Uri url)
{
_connection = new BiDiConnection(url);
_coreBidi = BiDi.Attach(_connection, disposeConnection: true);
}

public BiDiBuilder AddExtension<TModule>(Func<BiDiConnection, TModule> addModuleFactory, out TModule module)
where TModule : Modules.Module
{
var newModule = addModuleFactory(_connection);
Extensions.Add(newModule);
module = newModule;

return this;
}

public async Task<BiDi> BuildAsync()
{
await _connection.ConnectAsync(default);

return _coreBidi;
}
}
Loading