Skip to content
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

[dotnet] Add nullability to FirefoxExtension #14964

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 19 additions & 24 deletions dotnet/src/webdriver/Firefox/FirefoxExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using System.Text.Json.Nodes;
using System.Xml;


#nullable enable

namespace OpenQA.Selenium.Firefox
{
Expand All @@ -38,8 +38,8 @@ public class FirefoxExtension
private const string RdfManifestFileName = "install.rdf";
private const string JsonManifestFileName = "manifest.json";

private string extensionFileName;
private string extensionResourceId;
private readonly string extensionFileName;
private readonly string extensionResourceId;

/// <summary>
/// Initializes a new instance of the <see cref="FirefoxExtension"/> class.
Expand All @@ -48,6 +48,7 @@ public class FirefoxExtension
/// <remarks>WebDriver attempts to resolve the <paramref name="fileName"/> parameter
/// by looking first for the specified file in the directory of the calling assembly,
/// then using the full path to the file, if a full path is provided.</remarks>
/// <exception cref="ArgumentNullException">If <paramref name="fileName"/> is <see langword="null"/>.</exception>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is already unreachable with null using normal means, but this constructor is public-facing so we need the validation.

public FirefoxExtension(string fileName)
: this(fileName, string.Empty)
{
Expand All @@ -65,16 +66,18 @@ public FirefoxExtension(string fileName)
/// not found in the file system, WebDriver attempts to locate a resource in the
/// executing assembly with the name specified by the <paramref name="resourceId"/>
/// parameter.</remarks>
/// <exception cref="ArgumentNullException">If <paramref name="fileName"/> or <paramref name="resourceId"/> are <see langword="null"/>.</exception>
internal FirefoxExtension(string fileName, string resourceId)
{
this.extensionFileName = fileName;
this.extensionResourceId = resourceId;
this.extensionFileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
this.extensionResourceId = resourceId ?? throw new ArgumentNullException(nameof(resourceId));
}

/// <summary>
/// Installs the extension into a profile directory.
/// </summary>
/// <param name="profileDirectory">The Firefox profile directory into which to install the extension.</param>
/// <exception cref="ArgumentNullException">If <paramref name="profileDirectory"/> is <see langword="null"/>.</exception>
public void Install(string profileDirectory)
{
DirectoryInfo info = new DirectoryInfo(profileDirectory);
Expand Down Expand Up @@ -132,7 +135,7 @@ private static string GetExtensionId(string root)

private static string ReadIdFromInstallRdf(string root)
{
string id = null;
string id;
string installRdf = Path.Combine(root, "install.rdf");
try
{
Expand All @@ -143,11 +146,11 @@ private static string ReadIdFromInstallRdf(string root)
rdfNamespaceManager.AddNamespace("em", EmNamespaceUri);
rdfNamespaceManager.AddNamespace("RDF", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");

XmlNode node = rdfXmlDocument.SelectSingleNode("//em:id", rdfNamespaceManager);
XmlNode? node = rdfXmlDocument.SelectSingleNode("//em:id", rdfNamespaceManager);
if (node == null)
{
XmlNode descriptionNode = rdfXmlDocument.SelectSingleNode("//RDF:Description", rdfNamespaceManager);
XmlAttribute attribute = descriptionNode.Attributes["id", EmNamespaceUri];
XmlNode? descriptionNode = rdfXmlDocument.SelectSingleNode("//RDF:Description", rdfNamespaceManager);
XmlAttribute? attribute = descriptionNode?.Attributes?["id", EmNamespaceUri];
if (attribute == null)
{
throw new WebDriverException("Cannot locate node containing extension id: " + installRdf);
Expand Down Expand Up @@ -175,26 +178,18 @@ private static string ReadIdFromInstallRdf(string root)

private static string ReadIdFromManifestJson(string root)
{
string id = null;
string id;
string manifestJsonPath = Path.Combine(root, JsonManifestFileName);

var manifestObject = JsonNode.Parse(File.ReadAllText(manifestJsonPath));
if (manifestObject["applications"] != null)
if (manifestObject!["applications"]?["gecko"]?["id"] is { } idNode)
{
var applicationObject = manifestObject["applications"];
if (applicationObject["gecko"] != null)
{
var geckoObject = applicationObject["gecko"];
if (geckoObject["id"] != null)
{
id = geckoObject["id"].ToString().Trim();
}
}
id = idNode.ToString().Trim();
}

if (string.IsNullOrEmpty(id))
else
{
string addInName = manifestObject["name"].ToString().Replace(" ", "");
string addInVersion = manifestObject["version"].ToString();
string addInName = manifestObject["name"]!.ToString().Replace(" ", "");
string addInVersion = manifestObject["version"]!.ToString();
id = string.Format(CultureInfo.InvariantCulture, "{0}@{1}", addInName, addInVersion);
}

Expand Down
6 changes: 6 additions & 0 deletions dotnet/src/webdriver/Firefox/FirefoxProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ public static FirefoxProfile FromBase64String(string base64)
/// Adds a Firefox Extension to this profile
/// </summary>
/// <param name="extensionToInstall">The path to the new extension</param>
/// <exception cref="ArgumentNullException">If <paramref name="extensionToInstall"/> is <see langword="null"/>.</exception>
public void AddExtension(string extensionToInstall)
{
if (extensionToInstall is null)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This already throws ArgumentNullException because of the Dictionary.Add just below

{
throw new ArgumentNullException(nameof(extensionToInstall));
}

this.extensions.Add(Path.GetFileNameWithoutExtension(extensionToInstall), new FirefoxExtension(extensionToInstall));
}

Expand Down