Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #4 from ochameau/bug/733582-post-1.4-refactoring-f…

…ixes

Bug 733582: Post 1.4 refactoring fixes. r=@erikvold
  • Loading branch information...
commit 0c040e6f081472be35a5bbe7885fdc8d2fab4636 2 parents 7b46c8f + ccf05aa
@ochameau ochameau authored
View
76 lib/addon-install.js
@@ -1,76 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-const { Cc, Ci, Cu } = require('chrome');
-const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm");
-
-const { Base } = require("api-utils/base");
-
-/**
- * Class to manage an addon: install and uninstall it.
- */
-exports.AddonInstall = Base.extend({
- /**
- * Immediatly install an addon. Note that the given xpi file will be
- * automatically removed when `callback` is being called.
- *
- * @param {String} xpiPath
- * file path to an xpi to install
- * @param {Function} callback
- * function called when addon install finished. First argument being a
- * boolean to say if the installation was successfull
- */
- initialize: function (xpiPath, callback) {
-
- let self = this;
- let installListener = {
- onInstallEnded: function(aInstall, aAddon) {
- self._addon = aAddon;
- onInstalled(aInstall, true);
- },
- onInstallFailed: function (aInstall) {
- onInstalled(aInstall, false);
- }
- };
- function onInstalled(aInstall, success) {
- aInstall.removeListener(installListener);
- file.remove(xpiPath);
- callback(success);
- }
-
- // Create nsIFile for the xpi file
- let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
- file.initWithPath(xpiPath);
-
- // Order AddonManager to install it!
- AddonManager.getInstallForFile(file, function(aInstall) {
- aInstall.addListener(installListener);
- aInstall.install();
- });
-
- },
-
- get id() this._addon ? this._addon.id : null,
-
- get isInstalled() "_addon" in this,
-
- unload: function (callback) {
- // Order Addonmanager to uninstall our addon
- if (!"_addon" in this)
- return callback();
- let addon = this._addon;
- let self = this;
- let uninstallListener = {
- onUninstalled: function onUninstalled(aAddon) {
- if (aAddon.id != addon.id)
- return;
- AddonManager.removeAddonListener(uninstallListener);
- delete self._addon;
- callback();
- }
- };
- AddonManager.addAddonListener(uninstallListener);
- addon.uninstall();
- }
-});
View
77 lib/addons-builder-helper.js
@@ -5,11 +5,12 @@
const { Cc, Ci, Cu } = require('chrome');
const windowManager = Cc['@mozilla.org/appshell/window-mediator;1'].
getService(Ci.nsIWindowMediator);
+const windowUtils = require("api-utils/window/utils");
const { PageMod } = require("addon-kit/page-mod");
-const { AddonInstall } = require("addon-install");
-const { createTemporaryFileFromData } = require("file-utils");
+const AddonInstaller = require("api-utils/addon/installer");
+const tmpFile = require("test-harness/tmp-file");
const CONFIG_FILENAME = "addon-config.json";
const CONFIG_PREF = "extensions.addonBuilderHelper.trustedOrigins";
@@ -17,28 +18,37 @@ const CONFIG_PREF = "extensions.addonBuilderHelper.trustedOrigins";
/**
* Utility method to toggle the XUL JS Console.
- * Return `message` attribute send back to webpage.
+ * Return `message` attribute sent back to the webpage.
*/
-let consoleWindow = null;
function toggleConsoleWindow(command) {
- consoleWindow = windowManager.getMostRecentWindow('global:console');
+ let consoleWindow = windowManager.getMostRecentWindow('global:console');
switch(command) {
case 'open':
- consoleWindow = consoleWindow ? consoleWindow.focus() :
- windowManager.getMostRecentWindow(null)
- .open('chrome://global/content/console.xul',
- '_blank',
- 'chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar');
+ if (consoleWindow)
+ consoleWindow.focus();
+ else
+ windowUtils.open('chrome://global/content/console.xul', {
+ features: {
+ chrome: true,
+ extrachrome: true,
+ menubar: true,
+ resizable: true,
+ scrollbars: true,
+ status: true,
+ toolbar: true
+ }
+ });
break;
case 'close':
- consoleWindow = consoleWindow ? consoleWindow.close() : null;
+ if (consoleWindow)
+ consoleWindow.close();
break;
case 'isOpen':
return consoleWindow ? true : false;
break;
default:
- return 'An unrecognized command was passed through the "contents" ' +
- 'property of the mozFlightDeck send object. Available commands ' +
+ return 'An unrecognized command was passed for the "toggleConsole" ' +
+ 'command of the mozFlightDeck send action. Available commands ' +
'are "open", "close", and "isOpen"';
}
return null;
@@ -124,36 +134,49 @@ function onContentScriptRequest(addonManager, args, callback) {
}
function SingleAddonManager() {
- var self = this;
- var currExtension = null;
+ let currentAddonId = null;
return {
get isInstalled() {
- return (currExtension != null && currExtension.isInstalled);
+ return currentAddonId != null;
},
get installedID() {
- if (!currExtension)
- return null;
- return currExtension.id;
+ return currentAddonId;
},
install: function install(xpiData, callback) {
- if (currExtension) {
+ // If an addon is already installed, uninstall it first
+ if (currentAddonId) {
let self = this;
self.uninstall(function () {
self.install(xpiData, callback);
});
}
- else
- currExtension = AddonInstall.new(createTemporaryFileFromData(xpiData), callback);
+ else {
+ let xpiPath = tmpFile.createFromString(xpiData);
+ AddonInstaller.install(xpiPath).then(function success(addonId) {
+ currentAddonId = addonId;
+ if (typeof callback == "function")
+ callback(true, addonId);
+ },
+ function failure(reason) {
+ currentAddonId = null;
+ if (typeof callback == "function")
+ callback(false);
+ });
+ }
},
uninstall: function uninstall(callback) {
- if (currExtension) {
- var oldExtension = currExtension;
- currExtension = null;
- oldExtension.unload(callback);
+ if (!currentAddonId) {
+ if (typeof callback == "function")
+ callback();
+ }
+ else {
+ let addonId = currentAddonId;
+ currentAddonId = null;
+ AddonInstaller.uninstall(addonId).then(callback);
}
}
};
@@ -186,7 +209,7 @@ exports.main = function main() {
try {
config.trustedOrigins = config.trustedOrigins.concat(pref.split(","));
} catch (e) {
- console.log("Error when reading preference " + CONFIG_PREF);
+ console.error("Error when reading preference " + CONFIG_PREF);
console.exception(e);
}
}
View
37 lib/file-utils.js
@@ -1,37 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-// Create a temporary file for a given string data
-const file = require("api-utils/file");
-exports.createTemporaryFileFromData = function (data, tmpName) {
- var profileDir = require("system").pathFor("ProfD");
- var path = file.join(profileDir, tmpName ? tmpName : "tmp-file");
-
- var tmpFile = file.open(path, "wb");
- tmpFile.write(data);
- tmpFile.close();
-
- return path;
-}
-
-exports.createTemporaryFileFromURL = function (url, tmpName) {
- let data = exports.readBinaryURI(url);
- return exports.createTemporaryFileFromData(data, tmpName);
-}
-
-// Utility function that synchronously reads local resource from the given
-// `uri` and returns content string. Read in binary mode.
-const {ByteReader} = require("api-utils/byte-streams");
-const {Cc, Ci} = require("chrome");
-exports.readBinaryURI = function readBinaryURI(uri) {
- let ioservice = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- let channel = ioservice.newChannel(uri, "UTF-8", null);
- let stream = channel.open();
-
- let reader = new ByteReader(stream);
- let data = reader.read();
- stream.close();
-
- return data;
-}
View
46 tests/test-addon-install.js
@@ -1,46 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-const { Cc, Ci, Cu } = require("chrome");
-const { AddonInstall } = require("addon-install");
-const { Services } = Components.utils.import("resource://gre/modules/Services.jsm");
-
-const SELF = require("self");
-const ADDON_URL = SELF.data.url("abh-unit-test@mozilla.com.xpi");
-const ADDON_PATH = require("file-utils").createTemporaryFileFromURL(ADDON_URL);
-
-exports.testInstall = function (test) {
- test.waitUntilDone();
-
- // Save all events distpatched by bootstrap.js of the installed addon
- let events = [];
- let eventsObserver = {
- observe: function (subject, topic, data) {
- events.push(data);
- }
- };
- Services.obs.addObserver(eventsObserver, "abh-unit-test", false);
-
- // Install the test addon
- let install = AddonInstall.new(ADDON_PATH, function onInstalled(success) {
- test.assert(success, "Installed successfully");
- test.assertEqual(install.id, "abh-unit-test@mozilla.com", "`install.id` is valid");
- test.assert(install.isInstalled, "`install.isInstalled` is true on 'onInstalled' call");
-
- // Now uninstall it
- install.unload(function () {
- test.assert(!install.isInstalled, "`install.isInstalled` is false when unload callback is called");
- test.assert(!install.id, "`install.id` is empty after uninstall");
-
- // Ensure that bootstrap.js methods of the addon have been called
- // successfully and in the right order
- Services.obs.removeObserver(eventsObserver, "abh-unit-test");
- test.assertEqual(JSON.stringify(events),
- JSON.stringify(["install", "startup", "shutdown", "uninstall"]),
- "addon's bootstrap.js functions have been called");
-
- test.done();
- });
- });
-}
View
6 tests/test-addons-builder-helper.js
@@ -4,9 +4,9 @@
const { Cc, Ci, Cu } = require("chrome");
const tabs = require("addon-kit/tabs");
-const { Services } = Components.utils.import("resource://gre/modules/Services.jsm");
+const { Services } = Cu.import("resource://gre/modules/Services.jsm");
-const { readBinaryURI } = require("file-utils");
+const { getChromeURIContent } = require("api-utils/utils/data");
const TEST_ADDON_URL = require("self").data.url("abh-unit-test@mozilla.com.xpi");
@@ -189,7 +189,7 @@ exports.testInstall = createTest(
Services.obs.addObserver(eventsObserver, "abh-unit-test", false);
// We can't use self.data.load as it doesn't read in binary mode!
- let xpiData = readBinaryURI(TEST_ADDON_URL);
+ let xpiData = getChromeURIContent(TEST_ADDON_URL);
worker.port.emit("xpiData", xpiData);
worker.port.on("uninstalled", function () {
Please sign in to comment.
Something went wrong with that request. Please try again.