Skip to content
Merged
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
40 changes: 18 additions & 22 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>
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,19 @@ private static string ReadIdFromInstallRdf(string root)

private static string ReadIdFromManifestJson(string root)
{
string id = null;
string id = string.Empty;
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))
{
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)
{
throw new ArgumentNullException(nameof(extensionToInstall));
}

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

Expand Down
Loading