Skip to content

Commit

Permalink
Add Firefox addon install using manifest.json
Browse files Browse the repository at this point in the history
Firefox addons/extensions built with WebExtensions API have a
manifest.json instead of install.rdf. When Selenium extract the
addon/extenion id, it only looks for install.rdf. This fix adds
extracting the addon/extension id from the manifest.json in addition to
install.rdf.

Fixes SeleniumHQ#4093

Signed-off-by: Jim Evans <james.h.evans.jr@gmail.com>
  • Loading branch information
vflame authored and jimevans committed Mar 12, 2018
1 parent 11cb216 commit 4756fd1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
60 changes: 57 additions & 3 deletions dotnet/src/webdriver/Firefox/FirefoxExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="FirefoxExtension.cs" company="WebDriver Committers">
// <copyright file="FirefoxExtension.cs" company="WebDriver 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
Expand All @@ -18,11 +18,15 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Xml;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium.Internal;



namespace OpenQA.Selenium.Firefox
{
/// <summary>
Expand All @@ -31,6 +35,8 @@ namespace OpenQA.Selenium.Firefox
public class FirefoxExtension
{
private const string EmNamespaceUri = "http://www.mozilla.org/2004/em-rdf#";
private const string RdfManifestFileName = "install.rdf";
private const string JsonManifestFileName = "manifest.json";

private string extensionFileName;
private string extensionResourceId;
Expand Down Expand Up @@ -95,7 +101,7 @@ public void Install(string profileDirectory)

// Then, copy the contents of the temporarly location into the
// proper location in the Firefox profile directory.
string id = ReadIdFromInstallRdf(tempFileName);
string id = GetExtensionId(tempFileName);
string extensionDirectory = Path.Combine(Path.Combine(profileDirectory, "extensions"), id);
if (Directory.Exists(extensionDirectory))
{
Expand All @@ -110,12 +116,32 @@ public void Install(string profileDirectory)
FileUtilities.DeleteDirectory(stagingDirectoryName);
}

private static string GetExtensionId(string root)
{
// Checks if manifest.json or install.rdf file exists and extracts
// the addon/extenion id from the file accordingly
string manifestJsonPath = Path.Combine(root, JsonManifestFileName);
string installRdfPath = Path.Combine(root, RdfManifestFileName);

if (File.Exists(installRdfPath))
{
return ReadIdFromInstallRdf(root);
}

if (File.Exists(manifestJsonPath))
{
return ReadIdFromManifestJson(root);
}

throw new WebDriverException("Extension should contain either install.rdf or manifest.json metadata file");
}

private static string ReadIdFromInstallRdf(string root)
{
string id = null;
string installRdf = Path.Combine(root, "install.rdf");
try
{
string installRdf = Path.Combine(root, "install.rdf");
XmlDocument rdfXmlDocument = new XmlDocument();
rdfXmlDocument.Load(installRdf);

Expand Down Expand Up @@ -152,5 +178,33 @@ private static string ReadIdFromInstallRdf(string root)

return id;
}

private static string ReadIdFromManifestJson(string root)
{
string id = null;
string manifestJsonPath = Path.Combine(root, JsonManifestFileName);
var manifestObject = JObject.Parse(File.ReadAllText(manifestJsonPath));
if (manifestObject["applications"] != null)
{
var applicationObject = manifestObject["applications"];
if (applicationObject["gecko"] != null)
{
var geckoObject = applicationObject["gecko"];
if (geckoObject["id"] != null)
{
id = geckoObject["id"].ToString().Trim();
}
}
}

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

return id;
}
}
}
1 change: 0 additions & 1 deletion go.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
@echo off

java -Xmx4096m -XX:MetaspaceSize=1024m -XX:ReservedCodeCacheSize=512m -client -jar third_party\jruby\jruby-complete.jar -X-C -S rake %*

0 comments on commit 4756fd1

Please sign in to comment.