Skip to content

Commit e2bffeb

Browse files
authored
[dotnet] [bidi] Support WebExtension module (#15850)
1 parent ae953fc commit e2bffeb

File tree

12 files changed

+321
-0
lines changed

12 files changed

+321
-0
lines changed

common/extensions/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ filegroup(
77
"**/*",
88
]),
99
visibility = [
10+
"//dotnet/test/common:__pkg__",
1011
"//java/test/org/openqa/selenium/chrome:__pkg__",
1112
"//java/test/org/openqa/selenium/edge:__pkg__",
1213
"//java/test/org/openqa/selenium/environment:__pkg__",

dotnet/src/webdriver/BiDi/BiDi.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public sealed class BiDi : IAsyncDisposable
3636
private Script.ScriptModule? _scriptModule;
3737
private Log.LogModule? _logModule;
3838
private Storage.StorageModule? _storageModule;
39+
private WebExtension.WebExtensionModule? _webExtensionModule;
3940

4041
private readonly object _moduleLock = new();
4142

@@ -150,6 +151,19 @@ public Storage.StorageModule Storage
150151
}
151152
}
152153

154+
public WebExtension.WebExtensionModule WebExtension
155+
{
156+
get
157+
{
158+
if (_webExtensionModule is not null) return _webExtensionModule;
159+
lock (_moduleLock)
160+
{
161+
_webExtensionModule ??= new WebExtension.WebExtensionModule(_broker);
162+
}
163+
return _webExtensionModule;
164+
}
165+
}
166+
153167
public Task<Session.StatusResult> StatusAsync()
154168
{
155169
return SessionModule.StatusAsync();

dotnet/src/webdriver/BiDi/Communication/Broker.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ internal Broker(BiDi bidi, Uri url)
8686
new DateTimeOffsetConverter(),
8787
new PrintPageRangeConverter(),
8888
new InputOriginConverter(),
89+
new WebExtensionConverter(_bidi),
8990
new SubscriptionConverter(),
9091
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
9192

dotnet/src/webdriver/BiDi/Communication/Json/BiDiJsonSerializerContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,8 @@ namespace OpenQA.Selenium.BiDi.Communication.Json;
165165
[JsonSerializable(typeof(IEnumerable<Input.INoneSourceAction>))]
166166
[JsonSerializable(typeof(IEnumerable<Input.IWheelSourceAction>))]
167167

168+
[JsonSerializable(typeof(WebExtension.InstallCommand))]
169+
[JsonSerializable(typeof(WebExtension.InstallResult))]
170+
[JsonSerializable(typeof(WebExtension.UninstallCommand))]
171+
168172
internal partial class BiDiJsonSerializerContext : JsonSerializerContext;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <copyright file="WebExtensionConverter.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+
using OpenQA.Selenium.BiDi.WebExtension;
21+
using System;
22+
using System.Text.Json;
23+
using System.Text.Json.Serialization;
24+
25+
namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;
26+
27+
internal class WebExtensionConverter : JsonConverter<Extension>
28+
{
29+
private readonly BiDi _bidi;
30+
31+
public WebExtensionConverter(BiDi bidi)
32+
{
33+
_bidi = bidi;
34+
}
35+
36+
public override Extension? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
37+
{
38+
var id = reader.GetString();
39+
40+
return new Extension(_bidi, id!);
41+
}
42+
43+
public override void Write(Utf8JsonWriter writer, Extension value, JsonSerializerOptions options)
44+
{
45+
writer.WriteStringValue(value.Id);
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <copyright file="Extension.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+
using System.Threading.Tasks;
21+
22+
namespace OpenQA.Selenium.BiDi.WebExtension;
23+
24+
public sealed class Extension
25+
{
26+
private readonly BiDi _bidi;
27+
28+
public Extension(BiDi bidi, string id)
29+
{
30+
_bidi = bidi;
31+
Id = id;
32+
}
33+
34+
internal string Id { get; }
35+
36+
public Task UninstallAsync(UninstallOptions? options = null)
37+
{
38+
return _bidi.WebExtension.UninstallAsync(this, options);
39+
}
40+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// <copyright file="InstallCommand.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+
using OpenQA.Selenium.BiDi.Communication;
21+
using System.Text.Json.Serialization;
22+
23+
namespace OpenQA.Selenium.BiDi.WebExtension;
24+
25+
internal sealed class InstallCommand(InstallParameters @params)
26+
: Command<InstallParameters, InstallResult>(@params, "webExtension.install");
27+
28+
internal sealed record InstallParameters(ExtensionData ExtensionData) : Parameters;
29+
30+
[JsonPolymorphic(TypeDiscriminatorPropertyName = "type")]
31+
[JsonDerivedType(typeof(ExtensionArchivePath), "archivePath")]
32+
[JsonDerivedType(typeof(ExtensionBase64Encoded), "base64")]
33+
[JsonDerivedType(typeof(ExtensionPath), "path")]
34+
public abstract record ExtensionData;
35+
36+
public sealed record ExtensionArchivePath(string Path) : ExtensionData;
37+
38+
public sealed record ExtensionBase64Encoded(string Value) : ExtensionData;
39+
40+
public sealed record ExtensionPath(string Path) : ExtensionData;
41+
42+
public sealed class InstallOptions : CommandOptions;
43+
44+
public sealed record InstallResult(Extension Extension) : EmptyResult;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// <copyright file="UninstallCommand.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+
using OpenQA.Selenium.BiDi.Communication;
21+
22+
namespace OpenQA.Selenium.BiDi.WebExtension;
23+
24+
internal sealed class UninstallCommand(UninstallParameters @params)
25+
: Command<UninstallParameters, EmptyResult>(@params, "webExtension.uninstall");
26+
27+
internal sealed record UninstallParameters(Extension Extension) : Parameters;
28+
29+
public sealed class UninstallOptions : CommandOptions;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <copyright file="WebExtensionModule.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+
using OpenQA.Selenium.BiDi.Communication;
21+
using System.Threading.Tasks;
22+
23+
namespace OpenQA.Selenium.BiDi.WebExtension;
24+
25+
public sealed class WebExtensionModule(Broker broker) : Module(broker)
26+
{
27+
public async Task<InstallResult> InstallAsync(ExtensionData extensionData, InstallOptions? options = null)
28+
{
29+
var @params = new InstallParameters(extensionData);
30+
31+
return await Broker.ExecuteCommandAsync<InstallCommand, InstallResult>(new InstallCommand(@params), options).ConfigureAwait(false);
32+
}
33+
34+
public async Task<EmptyResult> UninstallAsync(Extension extension, UninstallOptions? options = null)
35+
{
36+
var @params = new UninstallParameters(extension);
37+
38+
return await Broker.ExecuteCommandAsync<UninstallCommand, EmptyResult>(new UninstallCommand(@params), options).ConfigureAwait(false);
39+
}
40+
}

dotnet/test/common/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ filegroup(
1717
srcs = [],
1818
data = [
1919
"appconfig.json",
20+
"//common/extensions",
2021
"//common/src/web",
2122
"//dotnet/src/webdriver:manager-linux",
2223
"//dotnet/src/webdriver:manager-macos",

0 commit comments

Comments
 (0)