addon-sdk/source/lib/sdk/addon/installer.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 module.metadata = {
michael@0 6 "stability": "experimental"
michael@0 7 };
michael@0 8
michael@0 9 const { Cc, Ci, Cu } = require("chrome");
michael@0 10 const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm");
michael@0 11 const { defer } = require("../core/promise");
michael@0 12 const { setTimeout } = require("../timers");
michael@0 13
michael@0 14 /**
michael@0 15 * `install` method error codes:
michael@0 16 *
michael@0 17 * https://developer.mozilla.org/en/Addons/Add-on_Manager/AddonManager#AddonInstall_errors
michael@0 18 */
michael@0 19 exports.ERROR_NETWORK_FAILURE = AddonManager.ERROR_NETWORK_FAILURE;
michael@0 20 exports.ERROR_INCORRECT_HASH = AddonManager.ERROR_INCORRECT_HASH;
michael@0 21 exports.ERROR_CORRUPT_FILE = AddonManager.ERROR_CORRUPT_FILE;
michael@0 22 exports.ERROR_FILE_ACCESS = AddonManager.ERROR_FILE_ACCESS;
michael@0 23
michael@0 24 /**
michael@0 25 * Immediatly install an addon.
michael@0 26 *
michael@0 27 * @param {String} xpiPath
michael@0 28 * file path to an xpi file to install
michael@0 29 * @return {Promise}
michael@0 30 * A promise resolved when the addon is finally installed.
michael@0 31 * Resolved with addon id as value or rejected with an error code.
michael@0 32 */
michael@0 33 exports.install = function install(xpiPath) {
michael@0 34 let { promise, resolve, reject } = defer();
michael@0 35
michael@0 36 // Create nsIFile for the xpi file
michael@0 37 let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
michael@0 38 try {
michael@0 39 file.initWithPath(xpiPath);
michael@0 40 }
michael@0 41 catch(e) {
michael@0 42 reject(exports.ERROR_FILE_ACCESS);
michael@0 43 return promise;
michael@0 44 }
michael@0 45
michael@0 46 // Listen for installation end
michael@0 47 let listener = {
michael@0 48 onInstallEnded: function(aInstall, aAddon) {
michael@0 49 aInstall.removeListener(listener);
michael@0 50 // Bug 749745: on FF14+, onInstallEnded is called just before `startup()`
michael@0 51 // is called, but we expect to resolve the promise only after it.
michael@0 52 // As startup is called synchronously just after onInstallEnded,
michael@0 53 // a simple setTimeout(0) is enough
michael@0 54 setTimeout(resolve, 0, aAddon.id);
michael@0 55 },
michael@0 56 onInstallFailed: function (aInstall) {
michael@0 57 console.log("failed");
michael@0 58 aInstall.removeListener(listener);
michael@0 59 reject(aInstall.error);
michael@0 60 },
michael@0 61 onDownloadFailed: function(aInstall) {
michael@0 62 this.onInstallFailed(aInstall);
michael@0 63 }
michael@0 64 };
michael@0 65
michael@0 66 // Order AddonManager to install the addon
michael@0 67 AddonManager.getInstallForFile(file, function(install) {
michael@0 68 if (install.error != null) {
michael@0 69 install.addListener(listener);
michael@0 70 install.install();
michael@0 71 } else {
michael@0 72 reject(install.error);
michael@0 73 }
michael@0 74 });
michael@0 75
michael@0 76 return promise;
michael@0 77 };
michael@0 78
michael@0 79 exports.uninstall = function uninstall(addonId) {
michael@0 80 let { promise, resolve, reject } = defer();
michael@0 81
michael@0 82 // Listen for uninstallation end
michael@0 83 let listener = {
michael@0 84 onUninstalled: function onUninstalled(aAddon) {
michael@0 85 if (aAddon.id != addonId)
michael@0 86 return;
michael@0 87 AddonManager.removeAddonListener(listener);
michael@0 88 resolve();
michael@0 89 }
michael@0 90 };
michael@0 91 AddonManager.addAddonListener(listener);
michael@0 92
michael@0 93 // Order Addonmanager to uninstall the addon
michael@0 94 getAddon(addonId).then(addon => addon.uninstall(), reject);
michael@0 95
michael@0 96 return promise;
michael@0 97 };
michael@0 98
michael@0 99 exports.disable = function disable(addonId) {
michael@0 100 return getAddon(addonId).then(addon => {
michael@0 101 addon.userDisabled = true;
michael@0 102 return addonId;
michael@0 103 });
michael@0 104 };
michael@0 105
michael@0 106 exports.enable = function enabled(addonId) {
michael@0 107 return getAddon(addonId).then(addon => {
michael@0 108 addon.userDisabled = false;
michael@0 109 return addonId;
michael@0 110 });
michael@0 111 };
michael@0 112
michael@0 113 exports.isActive = function isActive(addonId) {
michael@0 114 return getAddon(addonId).then(addon => addon.isActive && !addon.appDisabled);
michael@0 115 };
michael@0 116
michael@0 117 function getAddon (id) {
michael@0 118 let { promise, resolve, reject } = defer();
michael@0 119 AddonManager.getAddonByID(id, addon => addon ? resolve(addon) : reject());
michael@0 120 return promise;
michael@0 121 }

mercurial